├── LICENSE
├── Makefile
├── Makefile.console
├── Makefile.console.win32
├── Makefile.macOS
├── Makefile.native.win32
├── Makefile.win32
├── README.md
├── api
└── example.c
├── appify.sh
├── bwallet.png
├── edwork.icns
├── edwork.ico
├── edwork.sh
├── resource.rc
├── smartcard
├── edworkSignature.jcproj
└── src
│ └── edwork
│ ├── SecP256r1.java
│ └── edwork.java
└── src
├── avl.c
├── avl.h
├── base32.c
├── base32.h
├── base64.c
├── base64.h
├── blockchain.c
├── blockchain.h
├── chacha.c
├── chacha.h
├── curve25519.c
├── curve25519.h
├── defuse.c
├── defuse.h
├── doops.h
├── duk_config.h
├── duktape.c
├── duktape.h
├── edd25519.c
├── edd25519.h
├── edfs.h
├── edfs_console.c
├── edfs_core.c
├── edfs_core.h
├── edfs_fuse.c
├── edfs_js.c
├── edfs_js.h
├── edfs_js_mustache.h
├── edfs_key_data.c
├── edfs_key_data.h
├── edfs_types.h
├── edwork.c
├── edwork.h
├── edwork_smartcard.c
├── edwork_smartcard.h
├── edwork_smartcard_plugin.c
├── edwork_smartcard_plugin.h
├── khash.h
├── libedfs.c
├── log.c
├── log.h
├── miniz.c
├── parson.c
├── parson.h
├── poa.c
├── poa.h
├── sha256.c
├── sha256.h
├── sha3.c
├── sha3.h
├── smartcard.c
├── smartcard.h
├── sort.c
├── sort.h
├── store.c
├── store.h
├── thread.h
├── tinydir.h
├── ui
├── edwork_settings_form.h
├── htmlwindow.h
├── macOS
│ ├── htmlwindow.c
│ └── resource.m
└── win32
│ ├── htmlwindow.c
│ └── resource.h
├── usrsctp
├── netinet
│ ├── sctp.h
│ ├── sctp_asconf.c
│ ├── sctp_asconf.h
│ ├── sctp_auth.c
│ ├── sctp_auth.h
│ ├── sctp_bsd_addr.c
│ ├── sctp_bsd_addr.h
│ ├── sctp_callout.c
│ ├── sctp_callout.h
│ ├── sctp_cc_functions.c
│ ├── sctp_constants.h
│ ├── sctp_crc32.c
│ ├── sctp_crc32.h
│ ├── sctp_header.h
│ ├── sctp_indata.c
│ ├── sctp_indata.h
│ ├── sctp_input.c
│ ├── sctp_input.h
│ ├── sctp_lock_userspace.h
│ ├── sctp_os.h
│ ├── sctp_os_userspace.h
│ ├── sctp_output.c
│ ├── sctp_output.h
│ ├── sctp_pcb.c
│ ├── sctp_pcb.h
│ ├── sctp_peeloff.c
│ ├── sctp_peeloff.h
│ ├── sctp_process_lock.h
│ ├── sctp_sha1.c
│ ├── sctp_sha1.h
│ ├── sctp_ss_functions.c
│ ├── sctp_structs.h
│ ├── sctp_sysctl.c
│ ├── sctp_sysctl.h
│ ├── sctp_timer.c
│ ├── sctp_timer.h
│ ├── sctp_uio.h
│ ├── sctp_userspace.c
│ ├── sctp_usrreq.c
│ ├── sctp_var.h
│ ├── sctputil.c
│ └── sctputil.h
├── netinet6
│ ├── sctp6_usrreq.c
│ └── sctp6_var.h
├── user_atomic.h
├── user_environment.c
├── user_environment.h
├── user_inpcb.h
├── user_ip6_var.h
├── user_ip_icmp.h
├── user_malloc.h
├── user_mbuf.c
├── user_mbuf.h
├── user_queue.h
├── user_recv_thread.c
├── user_recv_thread.h
├── user_route.h
├── user_socket.c
├── user_socketvar.h
├── user_uma.h
└── usrsctp.h
├── xxhash.c
└── xxhash.h
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Eduard Suica
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | USRSCTP_CFLAGS = -DSCTP_SIMPLE_ALLOCATOR -DSCTP_PROCESS_LEVEL_LOCKS -D__Userspace__ -D__Userspace_os_Linux -DINET -D_LIB -Isrc/usrsctp
4 | CFLAGS = -pthread -O3 -D_FILE_OFFSET_BITS=64 -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DWITH_SCTP -DWITH_USRSCTP $(USRSCTP_CFLAGS)
5 | LIBS = -lfuse -lm
6 | BUILDFLAGS= -o edfs_mount
7 |
8 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
9 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
10 | SRC = $(USRSCTP_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edfs_key_data.c src/edwork.c src/edfs_core.c src/edfs_fuse.c
11 | OBJS = $(SRC: .c=.o)
12 |
13 | edfs: ${OBJS}
14 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS}
15 |
16 | %.o:
17 | ${CC} ${CFLAGS} -c $<
18 |
19 | .PHONY: clean
20 | clean:
21 | @echo all cleaned up!
22 |
--------------------------------------------------------------------------------
/Makefile.console:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | USRSCTP_CFLAGS = -DSCTP_SIMPLE_ALLOCATOR -DSCTP_PROCESS_LEVEL_LOCKS -D__Userspace__ -D__Userspace_os_Linux -DINET -D_LIB -DEDFS_CONSOLE -Isrc/usrsctp
4 | CFLAGS = -pthread -O3 -D_FILE_OFFSET_BITS=64 -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DWITH_SCTP -DWITH_USRSCTP $(USRSCTP_CFLAGS)
5 | LIBS = -lm
6 | BUILDFLAGS = -o edfs_console
7 |
8 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
9 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
10 | SRC = $(USRSCTP_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edfs_key_data.c src/edwork.c src/edfs_core.c src/edfs_console.c
11 | OBJS = $(SRC: .c=.o)
12 |
13 | edfs: ${OBJS}
14 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS}
15 |
16 | %.o:
17 | ${CC} ${CFLAGS} -c $<
18 |
19 | .PHONY: clean
20 | clean:
21 | @echo all cleaned up!
22 |
--------------------------------------------------------------------------------
/Makefile.console.win32:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | USRSCTP_CFLAGS = -DSCTP_SIMPLE_ALLOCATOR -DSCTP_PROCESS_LEVEL_LOCKS -D__Userspace__ -D__Userspace_os_Windows -DINET -DINET6 -D_LIB -DEDFS_CONSOLE -Isrc/usrsctp
4 | CFLAGS = -O2 -D_FILE_OFFSET_BITS=64 -DUSE_STAT_PATCH -DHAVE_TIMESPEC -DHAVE_TIMEZONE -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DEDFS_MULTITHREADED -DWITH_SCTP -DWITH_USRSCTP -DWITH_SMARTCARD -Isrc/usrsctp $(USRSCTP_CFLAGS)
5 | LIBS = -lws2_32 -liphlpapi -lwinmm -lole32 -loleaut32 -luuid -lshlwapi -lshlwapi -lwinscard -lcredui
6 | BUILDFLAGS= -o edfs_console
7 |
8 | SMARTCARD_SRC = src/smartcard.c src/edwork_smartcard.c src/edwork_smartcard_plugin.c
9 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
10 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
11 | SRC = $(USRSCTP_SRC) $(DUKTAPE_SRC) $(SMARTCARD_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edwork.c src/edfs_core.c src/edfs_console.c
12 | OBJS = $(SRC: .c=.o)
13 |
14 | edfs: ${OBJS}
15 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS}
16 |
17 | %.o:
18 | ${CC} ${CFLAGS} -c $<
19 |
20 | .PHONY: clean
21 | clean:
22 | @echo all cleaned up!
23 |
--------------------------------------------------------------------------------
/Makefile.macOS:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | USRSCTP_CFLAGS = -DHAVE_NETINET_IP_ICMP_H -DHAVE_SA_LEN -DHAVE_SCONN_LEN -DHAVE_SIN6_LEN -DHAVE_SIN_LEN -DHAVE_STDATOMIC_H -DHAVE_SYS_QUEUE_H -DINET -DINET6 -DSCTP_PROCESS_LEVEL_LOCKS -DSCTP_SIMPLE_ALLOCATOR -D__APPLE_USE_RFC_2292 -D__Userspace__ -D__Userspace_os_Darwin -Isrc/usrsctp -Wno-deprecated-declarations
4 | CFLAGS = -O3 -I/usr/local/include/osxfuse -D_FILE_OFFSET_BITS=64 -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DEDFS_MULTITHREADED -DWITH_SCTP -DWITH_USRSCTP $(USRSCTP_CFLAGS)
5 | LIBS = -L/usr/local/lib -losxfuse -framework Cocoa -framework WebKit -framework PCSC
6 | BUILDFLAGS= -o edfs_mount
7 |
8 | UI_SRC = src/ui/macOS/htmlwindow.c src/ui/macOS/resource.m
9 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
10 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
11 | SRC = $(UI_SRC) $(USRSCTP_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edfs_key_data.c src/edwork.c src/edfs_core.c src/edfs_fuse.c src/smartcard.c src/edwork_smartcard_plugin.c src/edwork_smartcard.c
12 | OBJS = $(SRC: .c=.o)
13 |
14 | edfs: ${OBJS}
15 | rm -rf edwork.app
16 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS} && ./appify.sh --script edwork.sh --icons edwork.icns && rm -f edfs_mount
17 |
18 | %.o:
19 | ${CC} ${CFLAGS} -c $<
20 |
21 | .PHONY: clean
22 | clean:
23 | rm -rf ./edwork.app
24 | @echo all cleaned up!
25 |
26 |
--------------------------------------------------------------------------------
/Makefile.native.win32:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | RESC = windres
4 | USRSCTP_CFLAGS = -DSCTP_SIMPLE_ALLOCATOR -DSCTP_PROCESS_LEVEL_LOCKS -D__Userspace__ -D__Userspace_os_Windows -DINET -DINET6 -D_LIB -Isrc/usrsctp
5 | CFLAGS = -mwindows -O2 -D_FILE_OFFSET_BITS=64 -DHAVE_TIMESPEC -DHAVE_TIMEZONE -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DEDFS_MULTITHREADED -DWITH_SCTP -DWITH_USRSCTP -DWITH_PJFS -Isrc/usrsctp $(USRSCTP_CFLAGS)
6 | LIBS = -lws2_32 -liphlpapi -lwinmm -lole32 -loleaut32 -luuid -lshlwapi -lcredui -lProjectedFSLib
7 | BUILDFLAGS= -o edfs
8 |
9 | UI_SRC = src/ui/win32/htmlwindow.c
10 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
11 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
12 | SRC = $(UI_SRC) $(USRSCTP_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edfs_key_data.c src/edwork.c src/edfs_core.c src/edfs_fuse.c src/gyro_fuse.c
13 |
14 | OBJS = $(SRC: .c=.o) resource.o
15 |
16 | edfs: ${OBJS}
17 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS}
18 |
19 | %.o:
20 | ${CC} ${CFLAGS} -c $<
21 |
22 | resource.o: resource.rc
23 | ${RESC} resource.rc -o coff -o resource.o
24 |
25 | .PHONY: clean
26 | clean:
27 | @echo all cleaned up!
28 |
--------------------------------------------------------------------------------
/Makefile.win32:
--------------------------------------------------------------------------------
1 | CC = gcc
2 | RM = rm
3 | RESC = windres
4 | USRSCTP_CFLAGS = -DSCTP_SIMPLE_ALLOCATOR -DSCTP_PROCESS_LEVEL_LOCKS -D__Userspace__ -D__Userspace_os_Windows -DINET -DINET6 -D_LIB -Isrc/usrsctp
5 | CFLAGS = -mwindows -O2 -D_FILE_OFFSET_BITS=64 -DUSE_STAT_PATCH -DHAVE_TIMESPEC -DHAVE_TIMEZONE -DEDFS_DEFAULT_HOST=\"discovery.gyrogears.com:4848\" -DEDFS_MULTITHREADED -DWITH_SCTP -DWITH_USRSCTP -DEDFS_MULTITHREADED -Isrc/usrsctp $(USRSCTP_CFLAGS)
6 | LIBS = -lws2_32 -liphlpapi -lwinmm -lole32 -loleaut32 -luuid -lshlwapi -lcredui -ldokanfuse1
7 | BUILDFLAGS= -o edfs_dokan
8 |
9 | UI_SRC = src/ui/win32/htmlwindow.c
10 | USRSCTP_SRC = src/usrsctp/user_environment.c src/usrsctp/user_mbuf.c src/usrsctp/user_recv_thread.c src/usrsctp/user_socket.c src/usrsctp/netinet/sctputil.c src/usrsctp/netinet/sctp_asconf.c src/usrsctp/netinet/sctp_auth.c src/usrsctp/netinet/sctp_bsd_addr.c src/usrsctp/netinet/sctp_callout.c src/usrsctp/netinet/sctp_cc_functions.c src/usrsctp/netinet/sctp_crc32.c src/usrsctp/netinet/sctp_indata.c src/usrsctp/netinet/sctp_input.c src/usrsctp/netinet/sctp_output.c src/usrsctp/netinet/sctp_pcb.c src/usrsctp/netinet/sctp_peeloff.c src/usrsctp/netinet/sctp_sha1.c src/usrsctp/netinet/sctp_ss_functions.c src/usrsctp/netinet/sctp_sysctl.c src/usrsctp/netinet/sctp_timer.c src/usrsctp/netinet/sctp_userspace.c src/usrsctp/netinet/sctp_usrreq.c src/usrsctp/netinet6/sctp6_usrreq.c
11 | DUKTAPE_SRC = src/duktape.c src/edfs_js.c
12 | SRC = $(UI_SRC) $(USRSCTP_SRC) $(DUKTAPE_SRC) src/sha256.c src/xxhash.c src/base64.c src/base32.c src/parson.c src/edd25519.c src/avl.c src/chacha.c src/log.c src/sha3.c src/curve25519.c src/sort.c src/blockchain.c src/edfs_key_data.c src/edwork.c src/edfs_core.c src/edfs_fuse.c
13 |
14 | OBJS = $(SRC: .c=.o) resource.o
15 |
16 | edfs: ${OBJS}
17 | ${CC} ${BUILDFLAGS} ${CFLAGS} ${OBJS} ${LIBS}
18 |
19 | %.o:
20 | ${CC} ${CFLAGS} -c $<
21 |
22 | resource.o: resource.rc
23 | ${RESC} resource.rc -o coff -o resource.o
24 |
25 | .PHONY: clean
26 | clean:
27 | @echo all cleaned up!
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # edwork
2 | You may donate Bitcoin for this project at 14LqvMzFfaJ82C7wY5iavvTf9HPELYWsax
3 |
4 | 
5 |
6 | edwork is a lightweight decentralized, distributed read-write filesystem. It uses UDP and SCTP/UDP protocol for node sync. For now, it should not be used in production.
7 |
8 | How it works
9 | ------------
10 |
11 | After compiling you will get either an `edfs_mount`(if built with fuse or dokany) or `edfs_console`. This is a node. Every node is the same (no master). The difference is that some nodes may have a full key pair (public-private) and some have only the public key, that can be only used for checking signatures.
12 |
13 | The nodes discover each other by using 3 strategies:
14 | 1. using the -use parameter to explicitly set the first node (eg.: `./edfs_mount -use someserver:4848`)
15 | 2. hardcoding a node (by defining EDFS_DEFAULT_HOST at compile time)
16 | 3. exchanging lists of addresses between them
17 |
18 | All data traffic is encrypted using chacha20.
19 |
20 | Every file has an unique 64-bit id, computed as:
21 | ```
22 | file_id = xxhash(sha256(parent_id || filename))
23 | ```
24 |
25 | There is some collision probability on deployments with lots of files. This could be mediated by using larger file ids. 64bit id's were used for compatibility with fuse's `ino_t` type.
26 |
27 | After the file id is computed, the file content is split into 57k chunks (in order to fit in a UDP datagram). Each chunk is optionally compressed (compression is enabled by default).
28 | Every user will have the full inode list, but not every file chunk. The inode list is sent when receiving a `ROOT` request.
29 |
30 | File chunks are sent "on-needed" by broadcasting a ``WANT`` request. In order to avoid flooding the edwork nodes, a cost function (proof of work) function was added. It is closely related with hashcash original specification, but instead of sha-1, it uses sha3-256. For every chunk (57k or less) a node must compute a hash begining with 12 zero bits. A `ROOT` request needs a 14 zero bit proof of work.
31 |
32 | The filesystem supports most of the I/O operations except rename. This is because the `file_id` is dependent of its name.
33 |
34 | Compiling
35 | ------------
36 |
37 | edwork may be compiled stand-alone (with no dependencies), with fuse or with dokany-fuse compatibility layer.
38 |
39 | On Linux (fuse):
40 | ```
41 | $ make
42 | ```
43 |
44 | Linux, FreeBSD, OS X, without fuse:
45 | ```
46 | $ make -f Makefile.console
47 | ```
48 |
49 | Windows (dokany):
50 | ```
51 | make -f Makefile.win32
52 | ```
53 |
54 | Windows (console):
55 | ```
56 | make -f Makefile.console.win32
57 | ```
58 |
59 | Work in progress
60 | ------------
61 | This is work in progress, protocol and API may change. For now, is not suitable for production use.
62 |
--------------------------------------------------------------------------------
/api/example.c:
--------------------------------------------------------------------------------
1 | #include "src/edfs.h"
2 |
3 | int main() {
4 | // initialize edwork
5 | struct edfs *edfs_context = edfs_create_context(NULL);
6 | edfs_edwork_init(edfs_context, 4848);
7 | edfs_edwork_wait_initialization(edfs_context, 3000);
8 |
9 | // example for read file
10 | EDFS_FILE *f = ed_fopen(edfs_context, "sintaxa.txt", "r");
11 | if (f) {
12 | char buf[8192];
13 | int size = ed_fread(buf, 1, sizeof(buf), f);
14 | if (size > 0)
15 | fwrite(buf, size, 1, stdout);
16 | ed_fclose(f);
17 | } else
18 | perror("ed_fopen");
19 |
20 | // example for read dir & stat
21 | EDFS_DIR *dir = ed_opendir(edfs_context, "/");
22 | if (dir) {
23 | struct dirent *pdir;
24 | while ((pdir = ed_readdir(dir)) != NULL) {
25 | edfs_stat stbuf;
26 | ed_stat(edfs_context, pdir->d_name, &stbuf);
27 | if (stbuf.st_mode & S_IFDIR)
28 | fprintf(stdout, "[%s]\n", pdir->d_name);
29 | else
30 | fprintf(stdout, "%s\n", pdir->d_name);
31 | }
32 |
33 | ed_closedir(dir);
34 | } else
35 | perror("ed_opendir");
36 |
37 | // standard deinitialization //
38 | edfs_edwork_done(edfs_context);
39 | edfs_destroy_context(edfs_context);
40 |
41 | return 0;
42 | }
43 |
--------------------------------------------------------------------------------
/appify.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | VERSION=4.0.1
4 | SCRIPT=`basename "$0"`
5 | APPNAME="edwork"
6 | APPICONS="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericApplicationIcon.icns"
7 | OSX_VERSION=`sw_vers -productVersion`
8 | PWD=`pwd`
9 |
10 | function usage {
11 | cat <
44 | Modified by Mathias Bynens
45 | Modified by Andrew Dvorak
46 | Rewritten by Duncan McGreggor
47 |
48 | EOF
49 | exit 1
50 | }
51 |
52 | function version {
53 | echo "v${VERSION}"
54 | exit 1
55 | }
56 |
57 | function error {
58 | echo
59 | echo "ERROR: $1"
60 | echo
61 | usage
62 | }
63 |
64 | while :; do
65 | case $1 in
66 | -h | --help ) usage;;
67 | -s | --script ) APPSCRIPT="$2"; shift ;;
68 | -n | --name ) APPNAME="$2"; shift ;;
69 | -i | --icons ) APPICONS="$2"; shift ;;
70 | -v | --version ) version;;
71 | -- ) shift; break ;;
72 | * ) break ;;
73 | esac
74 | shift
75 | done
76 |
77 | if [ -z ${APPSCRIPT+nil} ]; then
78 | error "the script to appify must be provided!"
79 | fi
80 |
81 | if [ ! -f "$APPSCRIPT" ]; then
82 | error "the can't find the script '$APPSCRIPT'"
83 | fi
84 |
85 | if [ -a "$APPNAME.app" ]; then
86 | error "the bundle '$PWD/$APPNAME.app' already exists"
87 | fi
88 |
89 | APPDIR="$APPNAME.app/Contents"
90 |
91 | mkdir -vp "$APPDIR"/{MacOS,Resources}
92 | cp -v "$APPICONS" "$APPDIR/Resources/$APPNAME.icns"
93 | cp -v "$APPSCRIPT" "$APPDIR/MacOS/$APPNAME"
94 | cp -v "edfs_mount" "$APPDIR/MacOS/edfs_mount"
95 | chmod +x "$APPDIR/MacOS/$APPNAME"
96 |
97 | cat < "$APPDIR/Info.plist"
98 |
99 |
100 |
101 |
102 | CFBundleExecutable
103 | $APPNAME
104 | CFBundleGetInfoString
105 | $APPNAME
106 | CFBundleIconFile
107 | $APPNAME
108 | CFBundleName
109 | $APPNAME
110 | CFBundlePackageType
111 | APPL
112 | CFBundleSignature
113 | 4242
114 |
115 |
116 | EOF
117 |
118 | echo "Application bundle created at '$PWD/$APPNAME.app'"
119 | echo
120 |
121 |
--------------------------------------------------------------------------------
/bwallet.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eduardsui/edwork/957b3eec4391e9b23f770313ae2757d6bfabab15/bwallet.png
--------------------------------------------------------------------------------
/edwork.icns:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eduardsui/edwork/957b3eec4391e9b23f770313ae2757d6bfabab15/edwork.icns
--------------------------------------------------------------------------------
/edwork.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eduardsui/edwork/957b3eec4391e9b23f770313ae2757d6bfabab15/edwork.ico
--------------------------------------------------------------------------------
/edwork.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | DIR="$(cd "$(dirname "$0")" && pwd)"
4 | $DIR/edfs_mount -dir "$HOME/edfs" -app -gui
5 |
--------------------------------------------------------------------------------
/resource.rc:
--------------------------------------------------------------------------------
1 | #include "src/ui/win32/resource.h"
2 |
3 | 1 VERSIONINFO
4 | FILEVERSION 1,0,0,1
5 | PRODUCTVERSION 1,0,0,1
6 | BEGIN
7 | BLOCK "StringFileInfo"
8 | BEGIN
9 | BLOCK "040904b0"
10 | BEGIN
11 | VALUE "Comments", "This program is in the public domain"
12 | VALUE "CompanyName", "Eduard Suica"
13 | VALUE "FileDescription", "edwork client"
14 | VALUE "FileVersion", "1, 0, 0, 1"
15 | VALUE "InternalName", "edwork"
16 | VALUE "LegalCopyright", "put in the public domain in 2018 by Eduard Suica"
17 | VALUE "OriginalFilename", "edwork_mount.exe"
18 | VALUE "ProductName", "edwork client"
19 | VALUE "ProductVersion", "1, 0, 0, 1"
20 | VALUE "SpecialBuild", ""
21 | END
22 | END
23 | BLOCK "VarFileInfo"
24 | BEGIN
25 | VALUE "Translation", 0x409, 1200
26 | END
27 | END
28 |
29 | IDI_ICON1 ICON "edwork.ico"
30 |
--------------------------------------------------------------------------------
/smartcard/edworkSignature.jcproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 | EXP JCA CAP
11 | <_SupportInt>1
12 | <_verbos>0
13 | <_nowarn>0
14 | <_debug>1
15 | <_nobanner>0
16 | <_noverify>0
17 | <_mask>0
18 | <_Adddition/>
19 |
20 |
21 | <_Source>1.5
22 | <_Target>1.5
23 | <_Encoding>windows-1252
24 | <_GenAllDegugInfo>1
25 | <_GenNoWarnings>0
26 | <_OutputAboutCompilerDoing>0
27 | <_OutputWhereDeprecatedApisAre>1
28 | <_Adddition/>
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/smartcard/src/edwork/SecP256r1.java:
--------------------------------------------------------------------------------
1 | package edwork;
2 |
3 | import javacard.security.ECPrivateKey;
4 | import javacard.security.ECPublicKey;
5 | import javacard.security.KeyPair;
6 |
7 | public class SecP256r1 {
8 |
9 | private final static byte[] p = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00,
10 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
11 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
12 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff };
13 |
14 | private final static byte[] a = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00,
15 | 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff,
16 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
17 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xfc
18 | };
19 |
20 | private final static byte[] b = { 0x5a, (byte) 0xc6, 0x35, (byte) 0xd8, (byte) 0xaa, 0x3a,
21 | (byte) 0x93, (byte) 0xe7, (byte) 0xb3, (byte) 0xeb, (byte) 0xbd, 0x55, 0x76, (byte) 0x98,
22 | (byte) 0x86, (byte) 0xbc, 0x65, 0x1d, 0x06, (byte) 0xb0, (byte) 0xcc, 0x53, (byte) 0xb0,
23 | (byte) 0xf6, 0x3b, (byte) 0xce, 0x3c, 0x3e, 0x27, (byte) 0xd2, 0x60, 0x4b };
24 |
25 | private final static byte[] G = { 0x04, 0x6b, 0x17, (byte) 0xd1, (byte) 0xf2, (byte) 0xe1, 0x2c,
26 | 0x42, 0x47, (byte) 0xf8, (byte) 0xbc, (byte) 0xe6, (byte) 0xe5, 0x63, (byte) 0xa4, 0x40,
27 | (byte) 0xf2, 0x77, 0x03, 0x7d, (byte) 0x81, 0x2d, (byte) 0xeb, 0x33, (byte) 0xa0, (byte) 0xf4,
28 | (byte) 0xa1, 0x39, 0x45, (byte) 0xd8, (byte) 0x98, (byte) 0xc2, (byte) 0x96, 0x4f, (byte) 0xe3,
29 | 0x42, (byte) 0xe2, (byte) 0xfe, 0x1a, 0x7f, (byte) 0x9b, (byte) 0x8e, (byte) 0xe7, (byte) 0xeb,
30 | 0x4a, 0x7c, 0x0f, (byte) 0x9e, 0x16, 0x2b, (byte) 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e,
31 | (byte) 0xce, (byte) 0xcb, (byte) 0xb6, 0x40, 0x68, 0x37, (byte) 0xbf, 0x51, (byte) 0xf5 };
32 |
33 | private final static byte[] r = { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x00, 0x00,
34 | 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
35 | (byte) 0xff, (byte) 0xbc, (byte) 0xe6, (byte) 0xfa, (byte) 0xad, (byte) 0xa7, 0x17, (byte) 0x9e,
36 | (byte) 0x84, (byte) 0xf3, (byte) 0xb9, (byte) 0xca, (byte) 0xc2, (byte) 0xfc, 0x63, 0x25, 0x51 };
37 |
38 | static public KeyPair newKeyPair() {
39 | KeyPair key = new KeyPair(KeyPair.ALG_EC_FP, (short) 256);
40 |
41 | ECPrivateKey privKey = (ECPrivateKey) key.getPrivate();
42 | ECPublicKey pubKey = (ECPublicKey) key.getPublic();
43 |
44 | privKey.setFieldFP(p, (short) 0, (short) p.length);
45 | privKey.setA(a, (short) 0, (short) a.length);
46 | privKey.setB(b, (short) 0, (short) b.length);
47 | privKey.setG(G, (short) 0, (short) G.length);
48 | privKey.setR(r, (short) 0, (short) r.length);
49 |
50 | pubKey.setFieldFP(p, (short) 0, (short) p.length);
51 | pubKey.setA(a, (short) 0, (short) a.length);
52 | pubKey.setB(b, (short) 0, (short) b.length);
53 | pubKey.setG(G, (short) 0, (short) G.length);
54 | pubKey.setR(r, (short) 0, (short) r.length);
55 |
56 | return key;
57 | }
58 | }
--------------------------------------------------------------------------------
/smartcard/src/edwork/edwork.java:
--------------------------------------------------------------------------------
1 | package edwork;
2 |
3 | import javacard.framework.*;
4 | import javacard.security.ECPublicKey;
5 | import javacard.security.*;
6 |
7 | public class edwork extends Applet {
8 | private byte[] hello;
9 | private byte[] password = {0x48, 0x48, 0x48, 0x48, 0x48, 0x48};
10 | private static final byte INS_HELLO = (byte)0x01;
11 | private static final byte INS_ECC_GEN_KEYPAIR = (byte)0x41;
12 | private static final byte INS_ECC_GENW = (byte)0x45;
13 | private static final byte INS_ECC_SIGN = (byte)0x48;
14 | private static final byte INS_ECC_VERIFY = (byte)0x49;
15 |
16 | private KeyPair eccKey;
17 | private Signature ecdsa;
18 | short wrong_pin = 0;
19 |
20 | private byte[] flags;
21 |
22 | public static void install(byte[] bArray, short bOffset, byte bLength) {
23 | edwork ed = new edwork();
24 | if (bLength > 0) {
25 | ed.hello = new byte[bLength];
26 | Util.arrayCopy(bArray, bOffset, ed.hello, (short)0, (byte)bLength);
27 | }
28 | ed.register();
29 | }
30 |
31 | public edwork() {
32 | eccKey = SecP256r1.newKeyPair();
33 | eccKey.genKeyPair();
34 | ecdsa = Signature.getInstance(Signature.ALG_ECDSA_SHA, false);
35 | flags = JCSystem.makeTransientByteArray((short)16, JCSystem.CLEAR_ON_DESELECT);
36 | }
37 |
38 | public void process(APDU apdu) {
39 | if (selectingApplet())
40 | return;
41 |
42 | if (wrong_pin > 10)
43 | ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
44 |
45 | byte[] buf = apdu.getBuffer();
46 | short len = apdu.setIncomingAndReceive();
47 | if ((buf[ISO7816.OFFSET_CLA] == (byte)0x10) && (isContactless())) {
48 | switch (buf[ISO7816.OFFSET_INS]) {
49 | case INS_HELLO:
50 | if ((hello != null) && (hello.length > 0)) {
51 | Util.arrayCopy(hello, (byte)0, buf, ISO7816.OFFSET_CDATA, (byte)hello.length);
52 | apdu.setOutgoingAndSend(ISO7816.OFFSET_CDATA, (byte)hello.length);
53 | }
54 | break;
55 | // DEBUG ONLY!
56 | // case INS_ECC_GEN_KEYPAIR:
57 | // GenEccKeyPair(apdu, len);
58 | // break;
59 | case INS_ECC_SIGN:
60 | Ecc_Sign(apdu, len);
61 | break;
62 | case INS_ECC_VERIFY:
63 | Ecc_Verify(apdu, len);
64 | break;
65 | case INS_ECC_GENW:
66 | {
67 | ECPublicKey pubKey = (ECPublicKey) eccKey.getPublic();
68 | short sendlen = pubKey.getW(apdu.getBuffer(), (short)0);
69 | apdu.setOutgoingAndSend((short)0, sendlen);
70 | }
71 | break;
72 | default:
73 | ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
74 | }
75 | } else
76 | if (buf[ISO7816.OFFSET_CLA] == (byte)0x00) {
77 | switch (buf[ISO7816.OFFSET_INS]) {
78 | case (byte)0x2A:
79 | if (flags[0] != 1)
80 | ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
81 | if ((buf[ISO7816.OFFSET_P1] == (byte)0x9E) && (buf[ISO7816.OFFSET_P2] == (byte)0x9A))
82 | Ecc_Sign(apdu, len);
83 | else
84 | ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
85 | break;
86 | case (byte)0x20:
87 | if ((buf[ISO7816.OFFSET_P1] == (byte)0x00) && ((buf[ISO7816.OFFSET_P2] == (byte)0x81) || (buf[ISO7816.OFFSET_P2] == (byte)0x82))) {
88 | if (len >= 6) {
89 | if ((password.length != len) || (Util.arrayCompare(buf, (byte)ISO7816.OFFSET_CDATA, password, (byte)0, (byte)password.length) != 0)) {
90 | ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
91 | wrong_pin ++;
92 | } else {
93 | flags[0] = 1;
94 | if (wrong_pin > 0)
95 | wrong_pin = 0;
96 | }
97 | } else {
98 | ISOException.throwIt(ISO7816.SW_CORRECT_LENGTH_00);
99 | wrong_pin ++;
100 | }
101 | } else
102 | ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
103 | break;
104 | case (byte)0x24:
105 | if ((buf[ISO7816.OFFSET_P1] == (byte)0x00) && (buf[ISO7816.OFFSET_P2] == (byte)0x81)) {
106 | if ((len >= (byte)password.length + (byte)6) && ((byte)buf[ISO7816.OFFSET_LC] >= (byte)password.length + (byte)6)) {
107 | if (Util.arrayCompare(buf, (byte)ISO7816.OFFSET_CDATA, password, (byte)0, (byte)password.length) != 0) {
108 | ISOException.throwIt(ISO7816.SW_SECURITY_STATUS_NOT_SATISFIED);
109 | wrong_pin ++;
110 | } else {
111 | byte new_len = (byte)(buf[ISO7816.OFFSET_LC] - (byte)password.length);
112 | byte offset = (byte)(ISO7816.OFFSET_CDATA + (byte)password.length);
113 | flags[0] = 0;
114 | if (wrong_pin > 0)
115 | wrong_pin = 0;
116 | password = new byte[new_len];
117 | Util.arrayCopy(buf, offset, password, (byte)0, (byte)new_len);
118 | }
119 | } else
120 | ISOException.throwIt(ISO7816.SW_CORRECT_LENGTH_00);
121 | } else
122 | ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
123 | break;
124 | default:
125 | ISOException.throwIt(ISO7816.SW_INS_NOT_SUPPORTED);
126 | }
127 | } else
128 | ISOException.throwIt(ISO7816.SW_CLA_NOT_SUPPORTED);
129 | }
130 |
131 | protected static boolean isContactless() {
132 | return ((APDU.getProtocol() & APDU.PROTOCOL_MEDIA_MASK) == APDU.PROTOCOL_MEDIA_CONTACTLESS_TYPE_A);
133 | }
134 |
135 | private void GenEccKeyPair(APDU apdu, short len){
136 | byte[] buffer = apdu.getBuffer();
137 | short keyLen = (short)0;
138 | switch (buffer[ISO7816.OFFSET_P1]) {
139 | case (byte)0x01:
140 | //Constructs a KeyPair instance for the specified algorithm and keylength;
141 | eccKey = SecP256r1.newKeyPair();
142 | keyLen = (short)32;
143 | break;
144 | default:
145 | ISOException.throwIt(ISO7816.SW_INCORRECT_P1P2);
146 | break;
147 | }
148 | eccKey.genKeyPair();
149 |
150 | ECPublicKey pubKey = (ECPublicKey) eccKey.getPublic();
151 |
152 | short sendlen = pubKey.getW(apdu.getBuffer(), (short)0);
153 | apdu.setOutgoingAndSend((short)0, sendlen);
154 | }
155 |
156 | private void Ecc_Sign(APDU apdu, short len) {
157 | byte[] buffer = apdu.getBuffer();
158 | ecdsa.init(eccKey.getPrivate(), Signature.MODE_SIGN);
159 | short lenTmp = ecdsa.sign(buffer, ISO7816.OFFSET_CDATA, len, buffer, (short)0);
160 | apdu.setOutgoingAndSend((short)0, lenTmp);
161 | }
162 |
163 | private void Ecc_Verify(APDU apdu, short len) {
164 | byte[] buffer = apdu.getBuffer();
165 | short signLen = buffer[ISO7816.OFFSET_P1];
166 | short plainLen = (short)(len - signLen);
167 | short tmpOff = (short)(ISO7816.OFFSET_CDATA + signLen);
168 | ecdsa.init(eccKey.getPublic(), Signature.MODE_VERIFY);
169 | boolean ret = ecdsa.verify(buffer, (short)tmpOff, plainLen, buffer, ISO7816.OFFSET_CDATA, signLen);
170 | buffer[(short)0] = ret ? (byte)1 : (byte)0;
171 | apdu.setOutgoingAndSend((short)0, (short)1);
172 | }
173 | }
174 |
--------------------------------------------------------------------------------
/src/avl.h:
--------------------------------------------------------------------------------
1 | /*
2 | AVL tree implementation, header file.
3 |
4 | This implementation was written by Kent "ethereal" Williams-King and is
5 | hereby released into the public domain. Do what you wish with it.
6 |
7 | No guarantees as to the correctness of the implementation are provided.
8 | */
9 |
10 | #ifndef AVL_H
11 | #define AVL_H
12 |
13 | /* modify this macro to change the prefix */
14 | #define AVL_NAME(name) avl_ ## name
15 | /* memory allocation macros, change as necessary */
16 | #define AVL_ALLOC(variable, type) variable = (type *)malloc(sizeof(type))
17 | #define AVL_FREE(variable) free(variable)
18 | #include /* for malloc() */
19 |
20 | typedef int (*AVL_NAME(comparator_t))(void *key1, void *key2);
21 | typedef void (*AVL_NAME(key_destructor_t))(void *key);
22 | typedef void (*AVL_NAME(node_visitor_t))(void *key, void *data);
23 |
24 | typedef struct AVL_NAME(tree_node_t) {
25 | struct AVL_NAME(tree_node_t) *left, *right;
26 | int depth;
27 |
28 | void *key;
29 | void *data;
30 | } AVL_NAME(tree_node_t);
31 |
32 | typedef struct {
33 | AVL_NAME(tree_node_t) *root;
34 | AVL_NAME(comparator_t) comparator;
35 | AVL_NAME(key_destructor_t) destructor;
36 | } AVL_NAME(tree_t);
37 |
38 | void AVL_NAME(initialize)(AVL_NAME(tree_t) *tree,
39 | AVL_NAME(comparator_t) comparator, AVL_NAME(key_destructor_t) destructor);
40 | void AVL_NAME(destroy)(AVL_NAME(tree_t) *tree,
41 | AVL_NAME(node_visitor_t) visitor);
42 |
43 | void *AVL_NAME(search)(AVL_NAME(tree_t) *tree, void *key);
44 | void *AVL_NAME(insert)(AVL_NAME(tree_t) *tree, void *key, void *data);
45 | void *AVL_NAME(remove)(AVL_NAME(tree_t) *tree, void *key);
46 |
47 | int AVL_NAME(tree_depth)(AVL_NAME(tree_t) *tree);
48 |
49 | int AVL_NAME(ptrcmp)(void *key1, void *key2);
50 | int AVL_NAME(intcmp)(void *key1, void *key2);
51 | int AVL_NAME(ulongcmp)(void *key1, void *key2);
52 |
53 | void AVL_NAME(free_data)(void *key, void *data);
54 |
55 | #endif
56 |
--------------------------------------------------------------------------------
/src/base32.c:
--------------------------------------------------------------------------------
1 | // Base32 implementation
2 | //
3 | // Copyright 2010 Google Inc.
4 | // Author: Markus Gutschke
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 |
18 | #include
19 |
20 | #include "base32.h"
21 |
22 | int base32_decode(const uint8_t *encoded, uint8_t *result, int bufSize) {
23 | int buffer = 0;
24 | int bitsLeft = 0;
25 | int count = 0;
26 | const uint8_t *ptr;
27 | for (ptr = encoded; count < bufSize && *ptr; ++ptr) {
28 | uint8_t ch = *ptr;
29 | if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '-') {
30 | continue;
31 | }
32 | buffer <<= 5;
33 |
34 | // Deal with commonly mistyped characters
35 | if (ch == '0') {
36 | ch = 'O';
37 | } else if (ch == '1') {
38 | ch = 'L';
39 | } else if (ch == '8') {
40 | ch = 'B';
41 | }
42 |
43 | // Look up one base32 digit
44 | if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
45 | ch = (ch & 0x1F) - 1;
46 | } else if (ch >= '2' && ch <= '7') {
47 | ch -= '2' - 26;
48 | } else {
49 | return -1;
50 | }
51 |
52 | buffer |= ch;
53 | bitsLeft += 5;
54 | if (bitsLeft >= 8) {
55 | result[count++] = buffer >> (bitsLeft - 8);
56 | bitsLeft -= 8;
57 | }
58 | }
59 | if (count < bufSize) {
60 | result[count] = '\000';
61 | }
62 | return count;
63 | }
64 |
65 | int base32_encode(const uint8_t *data, int length, uint8_t *result,
66 | int bufSize) {
67 | if (length < 0 || length > (1 << 28)) {
68 | return -1;
69 | }
70 | int count = 0;
71 | if (length > 0) {
72 | int buffer = data[0];
73 | int next = 1;
74 | int bitsLeft = 8;
75 | while (count < bufSize && (bitsLeft > 0 || next < length)) {
76 | if (bitsLeft < 5) {
77 | if (next < length) {
78 | buffer <<= 8;
79 | buffer |= data[next++] & 0xFF;
80 | bitsLeft += 8;
81 | } else {
82 | int pad = 5 - bitsLeft;
83 | buffer <<= pad;
84 | bitsLeft += pad;
85 | }
86 | }
87 | int index = 0x1F & (buffer >> (bitsLeft - 5));
88 | bitsLeft -= 5;
89 | result[count++] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"[index];
90 | }
91 | }
92 | if (count < bufSize) {
93 | result[count] = '\000';
94 | }
95 | return count;
96 | }
97 |
--------------------------------------------------------------------------------
/src/base32.h:
--------------------------------------------------------------------------------
1 | // Base32 implementation
2 | //
3 | // Copyright 2010 Google Inc.
4 | // Author: Markus Gutschke
5 | //
6 | // Licensed under the Apache License, Version 2.0 (the "License");
7 | // you may not use this file except in compliance with the License.
8 | // You may obtain a copy of the License at
9 | //
10 | // http://www.apache.org/licenses/LICENSE-2.0
11 | //
12 | // Unless required by applicable law or agreed to in writing, software
13 | // distributed under the License is distributed on an "AS IS" BASIS,
14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | // See the License for the specific language governing permissions and
16 | // limitations under the License.
17 | //
18 | // Encode and decode from base32 encoding using the following alphabet:
19 | // ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
20 | // This alphabet is documented in RFC 4648/3548
21 | //
22 | // We allow white-space and hyphens, but all other characters are considered
23 | // invalid.
24 | //
25 | // All functions return the number of output bytes or -1 on error. If the
26 | // output buffer is too small, the result will silently be truncated.
27 |
28 | #ifndef _BASE32_H_
29 | #define _BASE32_H_
30 |
31 | #include
32 |
33 | int base32_decode(const uint8_t *encoded, uint8_t *result, int bufSize);
34 | int base32_encode(const uint8_t *data, int length, uint8_t *result, int bufSize);
35 |
36 | #endif /* _BASE32_H_ */
37 |
--------------------------------------------------------------------------------
/src/base64.c:
--------------------------------------------------------------------------------
1 | /* base64.c : base-64 / MIME encode/decode */
2 | /* PUBLIC DOMAIN - Jon Mayo - November 13, 2003 */
3 | /* $Id: base64.c 156 2007-07-12 23:29:10Z orange $ */
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include "base64.h"
9 |
10 | static const uint8_t base64enc_tab[]= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
11 |
12 | static const uint8_t base64dec_tab[256]= {
13 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
14 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
15 | 255,255,255,255,255,255,255,255,255,255,255,255,255, 62,255,255,
16 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
17 | 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
18 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,255,255,255,255, 63,
19 | 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
20 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,255,255,255,255,255,
21 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
22 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
23 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
24 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
25 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
26 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
27 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
28 | 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
29 | };
30 |
31 | /* decode a base64 string in one shot */
32 | int base64_decode(size_t in_len, const char *in, size_t out_len, unsigned char *out) {
33 | unsigned ii, io;
34 | uint_least32_t v;
35 | unsigned rem;
36 |
37 | for (io = 0, ii = 0,v = 0, rem = 0; ii < in_len; ii ++) {
38 | unsigned char ch;
39 | if (isspace(in[ii]))
40 | continue;
41 | if ((in[ii]=='=') || (!in[ii]))
42 | break; /* stop at = or null character*/
43 | ch = base64dec_tab[(unsigned char)in[ii]];
44 | if (ch == 255)
45 | break; /* stop at a parse error */
46 | v = (v<<6) | ch;
47 | rem += 6;
48 | if (rem >= 8) {
49 | rem -= 8;
50 | if (io >= out_len)
51 | return -1; /* truncation is failure */
52 | out[io ++] = (v >> rem) & 255;
53 | }
54 | }
55 | if (rem >= 8) {
56 | rem -= 8;
57 | if (io >= out_len)
58 | return -1; /* truncation is failure */
59 | out[io ++] = (v >> rem) & 255;
60 | }
61 | return io;
62 | }
63 |
64 | int base64_encode(size_t in_len, const unsigned char *in, size_t out_len, char *out) {
65 | unsigned ii, io;
66 | uint_least32_t v;
67 | unsigned rem;
68 |
69 | for(io = 0, ii = 0, v = 0, rem = 0; ii < in_len; ii ++) {
70 | unsigned char ch;
71 | ch = in[ii];
72 | v = (v << 8) | ch;
73 | rem += 8;
74 | while (rem >= 6) {
75 | rem -= 6;
76 | if (io >= out_len)
77 | return -1; /* truncation is failure */
78 | out[io ++] = base64enc_tab[(v >> rem) & 63];
79 | }
80 | }
81 | if (rem) {
82 | v <<= (6 - rem);
83 | if (io >= out_len)
84 | return -1; /* truncation is failure */
85 | out[io ++] = base64enc_tab[v & 63];
86 | }
87 | if (io < out_len)
88 | out[io] = 0;
89 |
90 | return io;
91 | }
92 |
--------------------------------------------------------------------------------
/src/base64.h:
--------------------------------------------------------------------------------
1 | /* base64.h : base-64 / MIME encode/decode */
2 | /* PUBLIC DOMAIN - Jon Mayo - November 13, 2003 */
3 | /* $Id: base64.h 128 2007-04-20 08:20:40Z orange $ */
4 | #ifndef BASE64_H
5 | #define BASE64_H
6 | #include
7 |
8 |
9 | int base64_encode(size_t in_len, const unsigned char *in, size_t out_len, char *out);
10 | int base64_decode(size_t in_len, const char *in, size_t out_len, unsigned char *out);
11 |
12 | #endif /* BASE64_H */
13 |
--------------------------------------------------------------------------------
/src/blockchain.h:
--------------------------------------------------------------------------------
1 | #ifndef _BLOCKCHAIN_H
2 | #define _BLOCKCHAIN_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | struct block {
11 | uint64_t index;
12 | uint64_t timestamp;
13 | unsigned char *data;
14 | uint64_t nonce;
15 | unsigned int data_len;
16 | void *previous_block;
17 | unsigned char hash[32];
18 | };
19 |
20 | struct block *block_new(struct block *previous_block, const unsigned char *data, unsigned int data_len);
21 | void block_free(struct block *block);
22 | int block_mine_with_copy(struct block *newblock, int zero_bits, unsigned char previous_hash[32], int *loop_condition);
23 | int block_mine(struct block *newblock, int zero_bits);
24 | int block_verify(struct block *newblock, int zero_bits);
25 | int blockchain_verify(struct block *newblock, int zero_bits);
26 | int block_save(struct block *newblock, const char *path);
27 | struct block *block_load(const char *path, uint64_t index);
28 | struct block *blockchain_load(const char *path);
29 | unsigned char *block_save_buffer(struct block *newblock, int *size);
30 | struct block *block_load_buffer(const unsigned char *buffer, int size);
31 | void blockchain_free(struct block *block);
32 |
33 | #endif
34 |
35 |
--------------------------------------------------------------------------------
/src/chacha.c:
--------------------------------------------------------------------------------
1 | /*
2 | chacha-merged.c version 20080118
3 | D. J. Bernstein
4 | Public domain.
5 | */
6 |
7 | #include "chacha.h"
8 |
9 | #define U8C(v) (v##U)
10 | #define U32C(v) (v##U)
11 |
12 | #define U8V(v) ((unsigned char)(v) & U8C(0xFF))
13 | #define U32V(v) ((uint32_t)(v) & U32C(0xFFFFFFFF))
14 |
15 | #define ROTL32(v, n) \
16 | (U32V((v) << (n)) | ((v) >> (32 - (n))))
17 |
18 | #if (USE_UNALIGNED == 1)
19 | #define U8TO32_LITTLE(p) \
20 | (*((uint32_t *)(p)))
21 | #define U32TO8_LITTLE(p, v) \
22 | do { \
23 | *((uint32_t *)(p)) = v; \
24 | } while (0)
25 | #else
26 | #define U8TO32_LITTLE(p) \
27 | (((uint32_t)((p)[0]) ) | \
28 | ((uint32_t)((p)[1]) << 8) | \
29 | ((uint32_t)((p)[2]) << 16) | \
30 | ((uint32_t)((p)[3]) << 24))
31 | #define U32TO8_LITTLE(p, v) \
32 | do { \
33 | (p)[0] = U8V((v) ); \
34 | (p)[1] = U8V((v) >> 8); \
35 | (p)[2] = U8V((v) >> 16); \
36 | (p)[3] = U8V((v) >> 24); \
37 | } while (0)
38 | #endif
39 |
40 | #define ROTATE(v,c) (ROTL32(v,c))
41 | #define XOR(v,w) ((v) ^ (w))
42 | #define PLUS(v,w) (U32V((v) + (w)))
43 | #define PLUSONE(v) (PLUS((v),1))
44 |
45 | #define QUARTERROUND(a,b,c,d) \
46 | a = PLUS(a,b); d = ROTATE(XOR(d,a),16); \
47 | c = PLUS(c,d); b = ROTATE(XOR(b,c),12); \
48 | a = PLUS(a,b); d = ROTATE(XOR(d,a), 8); \
49 | c = PLUS(c,d); b = ROTATE(XOR(b,c), 7);
50 |
51 | static const char sigma[] = "expand 32-byte k";
52 | static const char tau[] = "expand 16-byte k";
53 |
54 | void
55 | chacha_keysetup(struct chacha_ctx *x,const unsigned char *k,uint32_t kbits)
56 | {
57 | const char *constants;
58 |
59 | x->input[4] = U8TO32_LITTLE(k + 0);
60 | x->input[5] = U8TO32_LITTLE(k + 4);
61 | x->input[6] = U8TO32_LITTLE(k + 8);
62 | x->input[7] = U8TO32_LITTLE(k + 12);
63 | if (kbits == 256) { /* recommended */
64 | k += 16;
65 | constants = sigma;
66 | } else { /* kbits == 128 */
67 | constants = tau;
68 | }
69 | x->input[8] = U8TO32_LITTLE(k + 0);
70 | x->input[9] = U8TO32_LITTLE(k + 4);
71 | x->input[10] = U8TO32_LITTLE(k + 8);
72 | x->input[11] = U8TO32_LITTLE(k + 12);
73 | x->input[0] = U8TO32_LITTLE(constants + 0);
74 | x->input[1] = U8TO32_LITTLE(constants + 4);
75 | x->input[2] = U8TO32_LITTLE(constants + 8);
76 | x->input[3] = U8TO32_LITTLE(constants + 12);
77 | }
78 |
79 | void
80 | chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *counter)
81 | {
82 | x->input[12] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 0);
83 | //x->input[13] = counter == NULL ? 0 : U8TO32_LITTLE(counter + 4);
84 | x->input[13] = U8TO32_LITTLE(iv + 0);
85 | x->input[14] = U8TO32_LITTLE(iv + 4);
86 | x->input[15] = U8TO32_LITTLE(iv + 8);
87 | }
88 |
89 | void
90 | chacha_encrypt_bytes(struct chacha_ctx *x,const unsigned char *m,unsigned char *c,uint32_t bytes)
91 | {
92 | uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
93 | uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
94 | unsigned char *ctarget = NULL;
95 | unsigned char tmp[64];
96 | uint32_t i;
97 |
98 | if (!bytes) return;
99 |
100 | j0 = x->input[0];
101 | j1 = x->input[1];
102 | j2 = x->input[2];
103 | j3 = x->input[3];
104 | j4 = x->input[4];
105 | j5 = x->input[5];
106 | j6 = x->input[6];
107 | j7 = x->input[7];
108 | j8 = x->input[8];
109 | j9 = x->input[9];
110 | j10 = x->input[10];
111 | j11 = x->input[11];
112 | j12 = x->input[12];
113 | j13 = x->input[13];
114 | j14 = x->input[14];
115 | j15 = x->input[15];
116 |
117 | for (;;) {
118 | if (bytes < 64) {
119 | #if (USE_MEMCPY == 1)
120 | memcpy(tmp, m, bytes);
121 | #else
122 | for (i = 0;i < bytes;++i) tmp[i] = m[i];
123 | #endif
124 | m = tmp;
125 | ctarget = c;
126 | c = tmp;
127 | }
128 | x0 = j0;
129 | x1 = j1;
130 | x2 = j2;
131 | x3 = j3;
132 | x4 = j4;
133 | x5 = j5;
134 | x6 = j6;
135 | x7 = j7;
136 | x8 = j8;
137 | x9 = j9;
138 | x10 = j10;
139 | x11 = j11;
140 | x12 = j12;
141 | x13 = j13;
142 | x14 = j14;
143 | x15 = j15;
144 | for (i = 20;i > 0;i -= 2) {
145 | QUARTERROUND( x0, x4, x8,x12)
146 | QUARTERROUND( x1, x5, x9,x13)
147 | QUARTERROUND( x2, x6,x10,x14)
148 | QUARTERROUND( x3, x7,x11,x15)
149 | QUARTERROUND( x0, x5,x10,x15)
150 | QUARTERROUND( x1, x6,x11,x12)
151 | QUARTERROUND( x2, x7, x8,x13)
152 | QUARTERROUND( x3, x4, x9,x14)
153 | }
154 | x0 = PLUS(x0,j0);
155 | x1 = PLUS(x1,j1);
156 | x2 = PLUS(x2,j2);
157 | x3 = PLUS(x3,j3);
158 | x4 = PLUS(x4,j4);
159 | x5 = PLUS(x5,j5);
160 | x6 = PLUS(x6,j6);
161 | x7 = PLUS(x7,j7);
162 | x8 = PLUS(x8,j8);
163 | x9 = PLUS(x9,j9);
164 | x10 = PLUS(x10,j10);
165 | x11 = PLUS(x11,j11);
166 | x12 = PLUS(x12,j12);
167 | x13 = PLUS(x13,j13);
168 | x14 = PLUS(x14,j14);
169 | x15 = PLUS(x15,j15);
170 |
171 | x0 = XOR(x0,U8TO32_LITTLE(m + 0));
172 | x1 = XOR(x1,U8TO32_LITTLE(m + 4));
173 | x2 = XOR(x2,U8TO32_LITTLE(m + 8));
174 | x3 = XOR(x3,U8TO32_LITTLE(m + 12));
175 | x4 = XOR(x4,U8TO32_LITTLE(m + 16));
176 | x5 = XOR(x5,U8TO32_LITTLE(m + 20));
177 | x6 = XOR(x6,U8TO32_LITTLE(m + 24));
178 | x7 = XOR(x7,U8TO32_LITTLE(m + 28));
179 | x8 = XOR(x8,U8TO32_LITTLE(m + 32));
180 | x9 = XOR(x9,U8TO32_LITTLE(m + 36));
181 | x10 = XOR(x10,U8TO32_LITTLE(m + 40));
182 | x11 = XOR(x11,U8TO32_LITTLE(m + 44));
183 | x12 = XOR(x12,U8TO32_LITTLE(m + 48));
184 | x13 = XOR(x13,U8TO32_LITTLE(m + 52));
185 | x14 = XOR(x14,U8TO32_LITTLE(m + 56));
186 | x15 = XOR(x15,U8TO32_LITTLE(m + 60));
187 |
188 | j12 = PLUSONE(j12);
189 | if (!j12) {
190 | j13 = PLUSONE(j13);
191 | /* stopping at 2^70 bytes per nonce is user's responsibility */
192 | }
193 |
194 | U32TO8_LITTLE(c + 0,x0);
195 | U32TO8_LITTLE(c + 4,x1);
196 | U32TO8_LITTLE(c + 8,x2);
197 | U32TO8_LITTLE(c + 12,x3);
198 | U32TO8_LITTLE(c + 16,x4);
199 | U32TO8_LITTLE(c + 20,x5);
200 | U32TO8_LITTLE(c + 24,x6);
201 | U32TO8_LITTLE(c + 28,x7);
202 | U32TO8_LITTLE(c + 32,x8);
203 | U32TO8_LITTLE(c + 36,x9);
204 | U32TO8_LITTLE(c + 40,x10);
205 | U32TO8_LITTLE(c + 44,x11);
206 | U32TO8_LITTLE(c + 48,x12);
207 | U32TO8_LITTLE(c + 52,x13);
208 | U32TO8_LITTLE(c + 56,x14);
209 | U32TO8_LITTLE(c + 60,x15);
210 |
211 | if (bytes <= 64) {
212 | if (bytes < 64) {
213 | #if (USE_MEMCPY == 1)
214 | memcpy(ctarget, c, bytes);
215 | #else
216 | for (i = 0;i < bytes;++i) ctarget[i] = c[i];
217 | #endif
218 | }
219 | x->input[12] = j12;
220 | x->input[13] = j13;
221 | return;
222 | }
223 | bytes -= 64;
224 | c += 64;
225 | m += 64;
226 | }
227 | }
228 |
--------------------------------------------------------------------------------
/src/chacha.h:
--------------------------------------------------------------------------------
1 | /*
2 | chacha-merged.c version 20080118
3 | D. J. Bernstein
4 | Public domain.
5 | */
6 |
7 | #ifndef CHACHA_H
8 | #define CHACHA_H
9 |
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | #define CHACHA_MINKEYLEN 16
16 | #define CHACHA_NONCELEN 8
17 | #define CHACHA_CTRLEN 8
18 | #define CHACHA_STATELEN (CHACHA_NONCELEN+CHACHA_CTRLEN)
19 | #define CHACHA_BLOCKLEN 64
20 |
21 | /* use memcpy() to copy blocks of memory (typically faster) */
22 | #define USE_MEMCPY 1
23 | /* use unaligned little-endian load/store (can be faster) */
24 | #define USE_UNALIGNED 0
25 |
26 | struct chacha_ctx {
27 | uint32_t input[16];
28 | };
29 |
30 | void chacha_keysetup(struct chacha_ctx *x, const unsigned char *k,
31 | uint32_t kbits);
32 | void chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv,
33 | const unsigned char *ctr);
34 | void chacha_encrypt_bytes(struct chacha_ctx *x, const unsigned char *m,
35 | unsigned char *c, uint32_t bytes);
36 |
37 | #endif /* CHACHA_H */
38 |
39 |
--------------------------------------------------------------------------------
/src/curve25519.c:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/eduardsui/edwork/957b3eec4391e9b23f770313ae2757d6bfabab15/src/curve25519.c
--------------------------------------------------------------------------------
/src/curve25519.h:
--------------------------------------------------------------------------------
1 | #ifndef __CURVE25519_H
2 | #define __CURVE25519_H
3 |
4 | #include
5 |
6 | void curve25519(uint8_t *mypublic, const uint8_t *secret, const uint8_t *basepoint);
7 |
8 | #endif
9 |
--------------------------------------------------------------------------------
/src/defuse.h:
--------------------------------------------------------------------------------
1 | #ifndef __defuse_h
2 | #define __defuse_h
3 |
4 | #ifdef __cplusplus
5 | extern "C" {
6 | #endif
7 |
8 | #include
9 | #include
10 |
11 | #ifndef mode_t
12 | #define mode_t int
13 | #endif
14 |
15 | #ifndef uid_t
16 | #define uid_t int
17 | #endif
18 |
19 | #ifndef gid_t
20 | #define gid_t int
21 | #endif
22 |
23 | #ifndef pid_t
24 | #define pid_t int
25 | #endif
26 |
27 | struct fuse;
28 | struct fuse_chan;
29 |
30 | struct flock {
31 | off_t l_start;
32 | off_t l_len;
33 | pid_t l_pid;
34 | short l_type;
35 | short l_whence;
36 | };
37 |
38 | struct fuse_conn_info {
39 | unsigned proto_major;
40 | unsigned proto_minor;
41 | unsigned max_write;
42 | unsigned max_read;
43 | unsigned max_readahead;
44 | unsigned capable;
45 | unsigned want;
46 | unsigned max_background;
47 | unsigned congestion_threshold;
48 | unsigned time_gran;
49 | unsigned reserved[22];
50 | };
51 |
52 | struct fuse_file_info {
53 | int flags;
54 | uint64_t fh;
55 | off_t session_offset;
56 | off_t offset;
57 | void *data;
58 | unsigned int data_len;
59 | unsigned int data_allocated;
60 | unsigned char failed_buffer;
61 | unsigned char needs_sync;
62 | };
63 |
64 | enum fuse_readdir_flags {
65 | FUSE_READDIR_PLUS = (1 << 0)
66 | };
67 |
68 | enum fuse_fill_dir_flags {
69 | FUSE_FILL_DIR_PLUS = (1 << 1)
70 | };
71 |
72 | struct fuse_config {
73 | int set_gid;
74 | unsigned int gid;
75 | int set_uid;
76 | unsigned int uid;
77 | int set_mode;
78 | unsigned int umask;
79 | double entry_timeout;
80 | double negative_timeout;
81 | double attr_timeout;
82 | int intr;
83 | int intr_signal;
84 | int remember;
85 | int hard_remove;
86 | int use_ino;
87 | int readdir_ino;
88 | int direct_io;
89 | int kernel_cache;
90 | int auto_cache;
91 | int no_rofd_flush;
92 | int ac_attr_timeout_set;
93 | double ac_attr_timeout;
94 | int nullpath_ok;
95 | int show_help;
96 | char * modules;
97 | int debug;
98 | };
99 |
100 | struct statvfs {
101 | unsigned long f_bsize;
102 | unsigned long f_frsize;
103 | unsigned long f_blocks;
104 | unsigned long f_bfree;
105 | unsigned long f_bavail;
106 | unsigned long f_files;
107 | unsigned long f_ffree;
108 | unsigned long f_favail;
109 | unsigned long f_fsid;
110 | unsigned long f_flag;
111 | unsigned long f_namemax;
112 | };
113 |
114 | typedef int (*fuse_fill_dir_t) (void * buf, const char * name, const struct stat* stbuf, off_t off);
115 |
116 | struct fuse_operations {
117 | int (*getattr) (const char *, struct stat*);
118 | int (*readlink) (const char *, char *, size_t);
119 | int (*mknod) (const char *, mode_t, dev_t);
120 | int (*mkdir) (const char *, mode_t);
121 | int (*unlink) (const char *);
122 | int (*rmdir) (const char *);
123 | int (*symlink) (const char *, const char *);
124 | int (*rename) (const char *, const char *, unsigned int flags);
125 | int (*link) (const char *, const char *);
126 | int (*chmod) (const char *, mode_t);
127 | int (*chown) (const char *, uid_t, gid_t);
128 | int (*truncate) (const char *, off_t);
129 | int (*open) (const char *, struct fuse_file_info *);
130 | int (*read) (const char *, char *, size_t, off_t, struct fuse_file_info *);
131 | int (*write) (const char *, const char *, size_t, off_t, struct fuse_file_info *);
132 | int (*statfs) (const char *, struct statvfs*);
133 | int (*flush) (const char *, struct fuse_file_info *);
134 | int (*release) (const char *, struct fuse_file_info *);
135 | int (*fsync) (const char *, int, struct fuse_file_info *);
136 | int (*setxattr) (const char *, const char *, const char *, size_t, int);
137 | int (*getxattr) (const char *, const char *, char *, size_t);
138 | int (*listxattr) (const char *, char *, size_t);
139 | int (*removexattr) (const char *, const char *);
140 | int (*opendir) (const char *, struct fuse_file_info *);
141 | int (*readdir) (const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *);
142 | int (*releasedir) (const char *, struct fuse_file_info *);
143 | int (*fsyncdir) (const char *, int, struct fuse_file_info *);
144 | void * (*init) (struct fuse_conn_info * conn, struct fuse_config* cfg);
145 | void (*destroy) (void * private_data);
146 | int (*access) (const char *, int);
147 | int (*create) (const char *, mode_t, struct fuse_file_info *);
148 | int (*lock) (const char *, struct fuse_file_info *, int cmd, struct flock *);
149 | int (*utimens) (const char *, const struct timespec tv[2]);
150 | int (*bmap) (const char *, size_t blocksize, uint64_t* idx);
151 | int (*ioctl) (const char *, unsigned int cmd, void * arg, struct fuse_file_info *, unsigned int flags, void *data);
152 | int (*flock) (const char *, struct fuse_file_info *, int op);
153 | int (*fallocate) (const char *, int, off_t, off_t, struct fuse_file_info *);
154 | off_t (*lseek) (const char *, off_t off, int whence, struct fuse_file_info *);
155 | // defuse specific
156 | int (*history) (const char *, uint64_t timestamp_limit, unsigned char **hash, uint64_t *generation, uint64_t *timestamp, int history_limit);
157 | char * (*signature)(const char *, int signature_index);
158 | };
159 |
160 | struct fuse_context {
161 | struct fuse *fuse;
162 | void * private_data;
163 | mode_t umask;
164 | };
165 |
166 | // not implemented
167 | #define fuse_opt_free_args(x)
168 |
169 | struct fuse *fuse_new(struct fuse_chan * ch, void * args, const struct fuse_operations* op, size_t op_size, void * private_data);
170 |
171 | struct fuse_chan *fuse_mount(const char *dir, void *args);
172 | void fuse_unmount(const char *dir, struct fuse_chan *ch);
173 | int fuse_reload(struct fuse *f);
174 |
175 | int fuse_set_signal_handlers(struct fuse *se);
176 | struct fuse *fuse_get_session(struct fuse *f);
177 | void fuse_remove_signal_handlers(struct fuse *se);
178 |
179 | int fuse_loop(struct fuse *f);
180 | int fuse_loop_mt(struct fuse *f);
181 | void fuse_exit(struct fuse *f);
182 | void fuse_destroy(struct fuse *f);
183 |
184 | // defuse specific
185 | void fuse_notify_delete(struct fuse *f, const char *path);
186 | void fuse_notify_refresh(struct fuse *f, const char *path);
187 | void fuse_notify_refresh_tree(struct fuse *f);
188 |
189 | #ifdef __cplusplus
190 | }
191 | #endif
192 |
193 | #endif // __gyro_fuse_h
194 |
--------------------------------------------------------------------------------
/src/edfs.h:
--------------------------------------------------------------------------------
1 | #ifndef __EDFS_H
2 | #define __EDFS_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | #include "edwork.h"
12 | #include "edfs_core.h"
13 |
14 | #define EDFS_DIR_BUFFER 128
15 |
16 | typedef struct _EDFS_FILE {
17 | struct filewritebuf *buf;
18 | struct edfs *edfs_context;
19 | edfs_ino_t ino;
20 | int64_t offset;
21 | } EDFS_FILE;
22 |
23 | typedef struct _EDFS_DIR {
24 | struct dirbuf *buf;
25 | struct edfs *edfs_context;
26 | edfs_ino_t ino;
27 | struct dirent dir_buffer[EDFS_DIR_BUFFER];
28 | int dir_buffer_offset;
29 | int dir_buffer_size;
30 | int64_t offset;
31 | } EDFS_DIR;
32 |
33 | EDFS_FILE *ed_fopen(struct edfs *edfs_context, const char *filename, const char *mode);
34 | size_t ed_fread(void *ptr, size_t size, size_t nmemb, EDFS_FILE *f);
35 | size_t ed_fwrite(const void *ptr, size_t size, size_t nmemb, EDFS_FILE *f);
36 | int ed_flush(EDFS_FILE *f);
37 | int ed_fseek(EDFS_FILE *f, int64_t offset, int64_t whence);
38 | int64_t ed_ftell(EDFS_FILE *f);
39 | int ed_fclose(EDFS_FILE *f);
40 |
41 | EDFS_DIR *ed_opendir(struct edfs *edfs_context, const char *path);
42 | struct dirent *ed_readdir(EDFS_DIR *dir);
43 | int ed_closedir(EDFS_DIR *dir);
44 |
45 | int ed_mkdir(struct edfs *edfs_context, const char *path, int mode);
46 | int ed_rmdir(struct edfs *edfs_context, const char *path);
47 | int ed_unlink(struct edfs *edfs_context, const char *path);
48 |
49 | int ed_stat(struct edfs *edfs_context, const char *path, edfs_stat *buf);
50 | int ed_chmod(struct edfs *edfs_context, const char *pathname, int mode);
51 | int ed_utime(struct edfs *edfs_context, const char *filename, const struct utimbuf *times);
52 |
53 | #endif // __EDFS_H
54 |
--------------------------------------------------------------------------------
/src/edfs_js.h:
--------------------------------------------------------------------------------
1 | #ifndef EDFS_JS_H
2 | #define EDFS_JS_H
3 |
4 | #include "duktape.h"
5 |
6 | int edfs_js_register_all(duk_context *js);
7 | int edfs_js_error(duk_context *js, const char *msg);
8 |
9 | #endif
10 |
--------------------------------------------------------------------------------
/src/edfs_key_data.h:
--------------------------------------------------------------------------------
1 | #ifndef EDFS_KEY_DATA
2 | #define EDFS_KEY_DATA
3 |
4 | #include
5 | #include
6 | #include "thread.h"
7 | #include "avl.h"
8 | #include "blockchain.h"
9 |
10 | #ifndef EDFS_NO_JS
11 | #include "duktape.h"
12 | #include "edfs_js.h"
13 | #endif
14 |
15 | #define MAX_KEY_SIZE 8192
16 | #define MAX_PROOF_INODES 300
17 |
18 | struct edfs_key_vote_item {
19 | unsigned char vote_key[0xFF];
20 | unsigned int count;
21 | };
22 |
23 | struct edfs_key_vote_data {
24 | unsigned char subject[0xFF];
25 | struct edfs_key_vote_item *votes;
26 | int vote_size;
27 | int timeout;
28 | };
29 |
30 | struct edfs_key_data {
31 | unsigned char pubkey[MAX_KEY_SIZE];
32 | unsigned char sigkey[MAX_KEY_SIZE];
33 |
34 | unsigned char key_id[32];
35 | uint64_t key_id_xxh64_be;
36 |
37 | char *working_directory;
38 | char *cache_directory;
39 | char *signature;
40 | char *blockchain_directory;
41 |
42 | avl_tree_t ino_cache;
43 | thread_mutex_t ino_cache_lock;
44 |
45 | avl_tree_t ino_checksum_mismatch;
46 | avl_tree_t ino_sync_file;
47 | avl_tree_t vote_list;
48 |
49 | avl_tree_t notify_write;
50 | avl_tree_t allow_data;
51 | thread_mutex_t notify_write_lock;
52 |
53 | unsigned char proof_of_time[40];
54 | uint64_t proof_inodes[MAX_PROOF_INODES];
55 | int proof_inodes_len;
56 | struct block *chain;
57 |
58 | size_t pub_len;
59 | int pubkey_size;
60 | int key_type;
61 | int sign_key_type;
62 | int signature_size;
63 | size_t sig_len;
64 |
65 | uint64_t top_broadcast_timestamp;
66 | uint64_t client_top_broadcast_timestamp;
67 |
68 | uint32_t chain_errors;
69 |
70 | int block_timestamp;
71 |
72 | unsigned char hblk_scheduled;
73 |
74 | unsigned char key_loaded;
75 | unsigned char pub_loaded;
76 |
77 | unsigned char read_only;
78 |
79 | int opened_files;
80 |
81 | int mining_flag;
82 |
83 | #ifndef EDFS_NO_JS
84 | duk_context *js;
85 | char *js_last_error;
86 | char *js_working_directory;
87 |
88 | thread_mutex_t js_lock;
89 |
90 | uint64_t app_version;
91 | void *edfs_context;
92 | void *js_window;
93 |
94 | thread_id_t js_thread_lock;
95 | int js_dry_locks;
96 | unsigned char js_exit;
97 | unsigned char reload_js;
98 | #endif
99 |
100 | struct edfs_key_vote_data *votes;
101 | int vote_size;
102 |
103 | void *next_key;
104 | };
105 |
106 |
107 | int edfs_key_data_init(struct edfs_key_data *key_data, const char *use_working_directory);
108 | int edfs_key_data_vote(struct edfs_key_data *key_data, const unsigned char *subject, int subject_size, const unsigned char *vote, int vote_size, int timeout);
109 | int edfs_key_data_vote_winner(struct edfs_key_data *key_data, const unsigned char *subject, int subject_size, unsigned char *vote, int vote_size);
110 | void edfs_key_data_deinit(struct edfs_key_data *key_data);
111 | #ifndef EDFS_NO_JS
112 | void edfs_key_data_js_lock(struct edfs_key_data *key_data, int lock);
113 | struct edfs_key_data *edfs_key_data_get_from_js(duk_context *js);
114 | int edfs_key_data_load_js(struct edfs_key_data *key_data, const char *js_data);
115 | void edfs_key_data_reset_js(struct edfs_key_data *key_data);
116 | void edfs_key_data_js_loop(struct edfs_key_data *key_data);
117 | int edfs_key_js_call(struct edfs_key_data *key_data, const char *jscall, ... );
118 | int edfs_key_js_call_args(struct edfs_key_data *key_data, const char *jscall, const char *fmt, ... );
119 | const char *edfs_key_data_js_error(struct edfs_key_data *key_data);
120 | #endif
121 |
122 | #endif
123 |
--------------------------------------------------------------------------------
/src/edfs_types.h:
--------------------------------------------------------------------------------
1 | #ifndef EDFS_TYPES_H
2 | #define EDFS_TYPES_H
3 |
4 | typedef unsigned char BYTE; // 8-bit byte
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/src/edwork.h:
--------------------------------------------------------------------------------
1 | #ifndef __EDWORK_H
2 | #define __EDWORK_H
3 |
4 | #include
5 | #include
6 |
7 | #include "edfs_key_data.h"
8 |
9 | // 1 second ttl
10 | #define EDWORK_SCTP_TTL 1000
11 | #define SCTP_UDP_ENCAPSULATION
12 | #define EDWORK_SCTP_UDP_TUNNELING_PORT 4884
13 | #define EDWORK_PEER_DISCOVERY_SERVICE
14 | #define EDWORK_LAST_SEEN_TIMEOUT 120
15 |
16 | struct edwork_data;
17 |
18 | typedef void (*edwork_dispatch_callback)(struct edwork_data *edwork, uint64_t sequence, uint64_t timestamp, const char *type, const unsigned char *payload, unsigned int payload_size, struct edfs_key_data *key_data, void *clientaddr, int clientaddrlen, const unsigned char *who_am_i, const unsigned char *blockhash, void *userdata, int is_sctp, int is_listen_socket);
19 | typedef struct edfs_key_data *(*edwork_find_key_callback)(uint64_t key, void *userdata);
20 |
21 | #ifdef _WIN32
22 | void usleep(uint64_t usec);
23 | #endif
24 |
25 | void edwork_init();
26 |
27 | uint64_t edwork_random();
28 | int edwork_random_bytes(unsigned char *destination, int len);
29 |
30 | struct edwork_data *edwork_create(int port, edwork_find_key_callback key_callback);
31 | void edwork_confirm_seq(struct edwork_data *data, struct edfs_key_data *key, uint64_t sequence, int acks);
32 | void edwork_add_node(struct edwork_data *data, const char *node, int port, int is_listen_socket, int sctp, unsigned short encapsulation_port, time_t timestamp);
33 | int edworks_data_pending(struct edwork_data* data, int timeout_ms);
34 | int edwork_dispatch(struct edwork_data* data, edwork_dispatch_callback callback, int timeout_ms, void *userdata);
35 | int edwork_dispatch_data(struct edwork_data* data, edwork_dispatch_callback callback, unsigned char *buffer, int n, void *clientaddr, int clientaddrlen, void *userdata, int is_sctp, int is_listen_socket);
36 | int edwork_send_to_peer(struct edwork_data *data, struct edfs_key_data *key, const char type[4], const unsigned char *buf, int len, void *clientaddr, int clientaddrlen, int is_sctp, int is_listen_socket, int ttl);
37 | int edwork_broadcast(struct edwork_data *data, struct edfs_key_data *key, const char type[4], const unsigned char *buf, int len, int confirmed_acks, int max_nodes, uint64_t ino, int force_udp, time_t threshold);
38 | int edwork_broadcast_client(struct edwork_data *data, struct edfs_key_data *key, const char type[4], const unsigned char *buf, int len, int confirmed_acks, int max_nodes, uint64_t ino, const void *clientaddr, int clientaddr_len);
39 | int edwork_broadcast_except(struct edwork_data *data, struct edfs_key_data *key, const char type[4], const unsigned char *buf, int len, int confirmed_acks, int max_nodes, const void *except, int except_len, uint64_t force_timestamp, uint64_t ino);
40 | unsigned int edwork_rebroadcast(struct edwork_data *data, struct edfs_key_data *key, unsigned int max_count, unsigned int offset);
41 | int edwork_get_node_list(struct edwork_data *data, unsigned char *buf, int *buf_size, unsigned int offset, time_t threshold, int with_timestamp);
42 | int edwork_debug_node_list(struct edwork_data *data, char *buf, int buf_size, unsigned int offset, time_t threshold, int html);
43 | int edwork_add_node_list(struct edwork_data *data, const unsigned char *buf, int buf_size);
44 | void *edwork_ensure_node_in_list(struct edwork_data *data, void *clientaddr, int clientaddrlen, int is_sctp, int is_listen_socket);
45 | int edwork_get_info(void *clientinfo, uint64_t *last_ino, uint64_t *last_chunk, uint64_t *last_msg_timestamp);
46 | int edwork_set_info(void *clientinfo, uint64_t last_ino, uint64_t last_chunk, uint64_t last_msg_timestamp);
47 | unsigned int edwork_magnitude(struct edwork_data *data);
48 | const unsigned char *edwork_who_i_am(struct edwork_data *data);
49 | int edwork_try_spend(struct edwork_data *data, const unsigned char *proof_of_work, int proof_of_work_size);
50 | int edwork_unspend(struct edwork_data *data, const unsigned char *proof_of_work, int proof_of_work_size);
51 | int edwork_udp_socket(struct edwork_data *data);
52 | #ifdef WITH_SCTP
53 | int edwork_is_sctp(struct edwork_data *data, const void *clientaddr_ptr);
54 | void edwork_force_sctp(struct edwork_data *data, int force_sctp);
55 | int edwork_reconnect(struct edwork_data *data, int seconds);
56 | #endif
57 | const char *edwork_addr_ipv4(const void *clientaddr_ptr);
58 | void edwork_close(struct edwork_data *data);
59 | void edwork_destroy(struct edwork_data *data);
60 | void edwork_callback_lock(struct edwork_data *data, int lock);
61 | void edwork_reset_id(struct edwork_data *data);
62 |
63 | void edwork_done();
64 |
65 | #endif // __EDWORK_H
66 |
--------------------------------------------------------------------------------
/src/edwork_smartcard.h:
--------------------------------------------------------------------------------
1 | #ifndef __EDWORK_SMARTCARD_H
2 | #define __EDWORK_SMARTCARD_H
3 |
4 | #include
5 | #include "smartcard.h"
6 | #include "thread.h"
7 |
8 | struct edwork_smartcard_context;
9 |
10 | typedef void (*edwork_smartcard_ui_callback)(struct edwork_smartcard_context *context);
11 | typedef int (*edwork_smartcard_read_pin_callback)(struct edwork_smartcard_context *context, const char *reader, char *pin, int *max_len);
12 |
13 | struct edwork_smartcard_context {
14 | SCARDCONTEXT hContext;
15 | char *reader;
16 |
17 | SCARDHANDLE hCard;
18 | DWORD protocol;
19 |
20 | char buf_name[0x100];
21 | unsigned char public_key[1024];
22 | int public_key_len;
23 |
24 | time_t timestamp;
25 | int status;
26 |
27 | thread_mutex_t lock;
28 |
29 | edwork_smartcard_ui_callback status_changed;
30 | edwork_smartcard_read_pin_callback read_pin;
31 | };
32 |
33 | void edwork_smartcard_init(struct edwork_smartcard_context *context);
34 | int edwork_smartcard_iterate(struct edwork_smartcard_context *context);
35 | int edwork_smartcard_sign(struct edwork_smartcard_context *context, const unsigned char *buffer, int buf_len, unsigned char *signature, int sig_len);
36 | int edwork_smartcard_verify(struct edwork_smartcard_context *context, const unsigned char *buffer, int buf_len, const unsigned char *signature, int sig_len);
37 | int edwork_smartcard_valid(struct edwork_smartcard_context *context);
38 | void edwork_smartcard_done(struct edwork_smartcard_context *context);
39 |
40 | #endif
41 |
--------------------------------------------------------------------------------
/src/edwork_smartcard_plugin.c:
--------------------------------------------------------------------------------
1 | #include "edwork_smartcard_plugin.h"
2 | #include "log.h"
3 |
4 | // ============================ plugin helper macros ============================
5 | #define TRY_APDU_DATA_RESPONSE(apdu, data, data_len, out, out_len) if (!helper_do_expect_success(hCard, protocol, apdu, sizeof(apdu), (const unsigned char *)data, (int)data_len, out, out_len)) return 0;
6 | #define TRY_APDU_RESPONSE(apdu, out, out_len) TRY_APDU_DATA_RESPONSE(apdu, NULL, 0, out, out_len)
7 | #define TRY_APDU_DATA(apdu, data, data_len) TRY_APDU_DATA_RESPONSE(apdu, data, data_len, NULL, NULL)
8 | #define TRY_APDU(apdu) TRY_APDU_DATA(apdu, NULL, 0)
9 | // =========================== /plugin helper macros ============================
10 |
11 | // smartcard specific APDU (European ID Card)
12 | static const unsigned char SELECT_ROOT[] = {0x00, 0xA4, 0x04, 0x04, 0x0B, 0xE8, 0x28, 0xBD, 0x08, 0x0F, 0xD6, 0x42, 0x00, 0x00, 0x01, 0x01, 0x00};
13 | static const unsigned char SELECT_CERT[] = {0x00, 0xA4, 0x02, 0x0C, 0x02, 0xC0, 0x01, 0x00};
14 | static const unsigned char SELECT_DATA[] = {0x00, 0xA4, 0x02, 0x0C, 0x02, 0x01, 0x01, 0x00};
15 | static const unsigned char READ_FILE[] = {0x00, 0xB0, 0x00, 0x00, 0x00};
16 | static const unsigned char VERIFY_PIN[] = {0x00, 0x20, 0x00, 0x81};
17 | static const unsigned char MSU_RESTORE[] = {0x00, 0x22, 0xF3, 0x01, 0x00};
18 | static const unsigned char SET_HASH[] = {0x00, 0x2A, 0x90, 0xA0, 0x00, 0x90, 0x20};
19 | static const unsigned char DO_SIGNATURE[]= {0x00, 0x2A, 0x9E, 0x9A, 0x00};
20 |
21 | // ============================ plugin helper functions =========================
22 | static void debug_buffer(const char *tag, LPBYTE data, int len) {
23 | fprintf(stderr, "%s: ", tag);
24 | for (int i = 0; i < len; i++)
25 | fprintf(stderr, "%02X ", (int)data[i]);
26 | fprintf(stderr, "\n");
27 | }
28 |
29 | static int helper_do_expect_success(SCARDHANDLE hCard, DWORD protocol, const unsigned char *use_apdu, int apdu_len, const unsigned char *data, int data_len, unsigned char *output, int *out_len) {
30 | BYTE baResponseApdu[300];
31 | DWORD lResponseApduLen = sizeof(baResponseApdu);
32 | LPBYTE apdu;
33 |
34 | if ((!use_apdu) || (apdu_len <= 0) || (data_len < 0) || ((data_len) && (!data)))
35 | return 0;
36 |
37 | int buf_len = apdu_len;
38 | if ((data) && (data_len)) {
39 | int apdu_len2 = apdu_len < 5 ? 5 : apdu_len;
40 | buf_len = apdu_len2 + data_len + 1;
41 | apdu = (LPBYTE)malloc(buf_len);
42 | if (!apdu)
43 | return 0;
44 |
45 | memcpy(apdu, use_apdu, apdu_len);
46 | memcpy(apdu + apdu_len2, data, data_len);
47 | apdu[4] = (BYTE)data_len;
48 | if (apdu_len > 5)
49 | apdu[4] += (BYTE)(apdu_len - 5);
50 | apdu[buf_len - 1] = 0;
51 | } else
52 | apdu = (LPBYTE)use_apdu;
53 |
54 | // debug_buffer("sent", apdu, buf_len);
55 | if (SC_Exchange(hCard, protocol, apdu, buf_len, baResponseApdu, &lResponseApduLen)) {
56 | if (apdu != use_apdu)
57 | free(apdu);
58 | // debug_buffer("received", baResponseApdu, lResponseApduLen);
59 | if ((lResponseApduLen >= 2) && (baResponseApdu[lResponseApduLen - 2] == 0x90) && (baResponseApdu[lResponseApduLen - 1] == 0x00)) {
60 | if ((out_len) && (*out_len > 0) && (output)) {
61 | int len = lResponseApduLen - 2;
62 | if (*out_len > len)
63 | *out_len = len;
64 | memcpy(output, baResponseApdu, *out_len);
65 | }
66 | return 1;
67 | }
68 | } else {
69 | if (apdu != use_apdu)
70 | free(apdu);
71 | log_error("smartcard error %x: %s", (int)SC_errno, SC_GetErrorString(SC_errno));
72 | }
73 | return 0;
74 | }
75 | // ============================ /plugin helper functions ========================
76 |
77 | int edwork_plugin_init_smartcard(SCARDHANDLE hCard, DWORD protocol) {
78 | TRY_APDU(SELECT_ROOT);
79 | return 1;
80 | }
81 |
82 | int edwork_plugin_deinit_smartcard(SCARDHANDLE hCard, DWORD protocol) {
83 | // nothing special to do
84 | return 1;
85 | }
86 |
87 | int edwork_plugin_verify_smartcard(SCARDHANDLE hCard, DWORD protocol, const char *pin, int len) {
88 | // avoid query for invalid PIN numbers
89 | if ((!pin) || (len < 4))
90 | return 0;
91 | TRY_APDU_DATA(VERIFY_PIN, pin, len);
92 | return 1;
93 | }
94 |
95 | int edwork_plugin_get_id_data(SCARDHANDLE hCard, DWORD protocol, char *name, int *name_len, unsigned char *public_key, int *pub_len) {
96 | unsigned char buf[0x100];
97 | int data_size = sizeof(buf);
98 | TRY_APDU(SELECT_DATA);
99 | if (name) {
100 | *name = 0;
101 | TRY_APDU_RESPONSE(READ_FILE, buf, &data_size);
102 | int limit = data_size - 4;
103 | int str_size;
104 | int name_count = 0;
105 | int i = 2;
106 | while (i < limit) {
107 | if (!buf[i + 1])
108 | break;
109 |
110 | switch (buf[i]) {
111 | case 0xA1:
112 | case 0xA2:
113 | // name
114 | if (buf[i + 2] == 0x0C) {
115 | // string
116 | str_size = buf[i + 3];
117 | if (i + str_size < limit) {
118 | if (name_count) {
119 | *name = ' ';
120 | name ++;
121 | }
122 | memcpy(name, &buf[i + 4], str_size);
123 | name += str_size;
124 | *name = 0;
125 | name_count ++;
126 | }
127 | }
128 | i += buf[i + 1] + 1;
129 | break;
130 | default:
131 | i += buf[i + 1] + 1;
132 | break;
133 | }
134 | i ++;
135 | }
136 | }
137 |
138 | TRY_APDU(SELECT_CERT);
139 | if ((public_key) && (pub_len) && (*pub_len)) {
140 | TRY_APDU_RESPONSE(READ_FILE, public_key, pub_len);
141 | }
142 | return 1;
143 | }
144 |
145 | int edwork_plugin_sign_data(SCARDHANDLE hCard, DWORD protocol, const unsigned char *hash_data, int len, unsigned char *sig_data, int *sig_len) {
146 | TRY_APDU(SELECT_CERT);
147 | TRY_APDU(MSU_RESTORE);
148 | TRY_APDU_DATA(SET_HASH, hash_data, len);
149 | TRY_APDU_RESPONSE(DO_SIGNATURE, sig_data, sig_len);
150 | return 1;
151 | }
152 |
153 | int edwork_plugin_verify_data(SCARDHANDLE hCard, DWORD protocol, const unsigned char *hash_data, int len, const unsigned char *sig_data, int sig_len) {
154 | TRY_APDU(SELECT_CERT);
155 | TRY_APDU(MSU_RESTORE);
156 | // not implemented
157 | return 0;
158 | }
159 |
--------------------------------------------------------------------------------
/src/edwork_smartcard_plugin.h:
--------------------------------------------------------------------------------
1 | #ifndef __EDWORK_SMARTCARD_PLUGIN_H
2 | #define __EDWORK_SMARTCARD_PLUGIN_H
3 |
4 | #include "smartcard.h"
5 |
6 | int edwork_plugin_init_smartcard(SCARDHANDLE hCard, DWORD protocol);
7 | int edwork_plugin_deinit_smartcard(SCARDHANDLE hCard, DWORD protocol);
8 | int edwork_plugin_verify_smartcard(SCARDHANDLE hCard, DWORD protocol, const char *pin, int len);
9 | int edwork_plugin_get_id_data(SCARDHANDLE hCard, DWORD protocol, char *name, int *name_len, unsigned char *public_key, int *pub_len);
10 | int edwork_plugin_sign_data(SCARDHANDLE hCard, DWORD protocol, const unsigned char *hash_data, int len, unsigned char *sig_data, int *sig_len);
11 | int edwork_plugin_verify_data(SCARDHANDLE hCard, DWORD protocol, const unsigned char *hash_data, int len, const unsigned char *sig_data, int sig_len);
12 |
13 | #endif
14 |
--------------------------------------------------------------------------------
/src/log.c:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2017 rxi
3 | *
4 | * Permission is hereby granted, free of charge, to any person obtaining a copy
5 | * of this software and associated documentation files (the "Software"), to
6 | * deal in the Software without restriction, including without limitation the
7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 | * sell copies of the Software, and to permit persons to whom the Software is
9 | * furnished to do so, subject to the following conditions:
10 | *
11 | * The above copyright notice and this permission notice shall be included in
12 | * all copies or substantial portions of the Software.
13 | *
14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 | * IN THE SOFTWARE.
21 | */
22 |
23 | #include
24 | #include
25 | #include
26 | #include
27 | #include
28 | #include
29 |
30 | #include "log.h"
31 |
32 | #define LOG_USE_COLOR
33 |
34 | static struct {
35 | void *udata;
36 | log_LockFn lock;
37 | FILE *fp;
38 | int level;
39 | int quiet;
40 | #ifdef LOG_USE_COLOR
41 | int colors;
42 | #endif
43 | } L;
44 |
45 | uint64_t microseconds();
46 |
47 | static const char *level_names[] = {
48 | "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
49 | };
50 |
51 | #ifdef LOG_USE_COLOR
52 | static const char *level_colors[] = {
53 | "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
54 | };
55 | #endif
56 |
57 |
58 | static void lock(void) {
59 | if (L.lock) {
60 | L.lock(L.udata, 1);
61 | }
62 | }
63 |
64 |
65 | static void unlock(void) {
66 | if (L.lock) {
67 | L.lock(L.udata, 0);
68 | }
69 | }
70 |
71 |
72 | void log_set_udata(void *udata) {
73 | L.udata = udata;
74 | }
75 |
76 |
77 | void log_set_lock(log_LockFn fn) {
78 | L.lock = fn;
79 | }
80 |
81 |
82 | void log_set_fp(FILE *fp) {
83 | L.fp = fp;
84 | }
85 |
86 |
87 | void log_set_level(int level) {
88 | L.level = level;
89 | }
90 |
91 |
92 | void log_set_quiet(int enable) {
93 | L.quiet = enable ? 1 : 0;
94 | }
95 |
96 | void log_set_colors(int enable) {
97 | #ifdef LOG_USE_COLOR
98 | L.colors = enable ? 1 : 0;
99 | #endif
100 | }
101 |
102 | int log_get_level() {
103 | return L.level;
104 | }
105 |
106 | void log_log(int level, const char *file, int line, const char *fmt, ...) {
107 | if (level < L.level) {
108 | return;
109 | }
110 |
111 | /* Acquire lock */
112 | lock();
113 |
114 | /* Get current time */
115 | uint64_t now = microseconds()/1000;
116 | time_t t = (time_t)(now/1000);
117 | struct tm *lt = localtime(&t);
118 |
119 | /* Log to stderr */
120 | if (!L.quiet) {
121 | va_list args;
122 | char buf[16];
123 | buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0';
124 | #ifdef LOG_USE_COLOR
125 | if (L.colors)
126 | fprintf(stderr, "%s.%03d %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ", buf, (int)(now % 1000), level_colors[level], level_names[level], file, line);
127 | else
128 | #endif
129 | fprintf(stderr, "%s.%03d %-5s %s:%d: ", buf, (int)(now % 1000), level_names[level], file, line);
130 | va_start(args, fmt);
131 | vfprintf(stderr, fmt, args);
132 | va_end(args);
133 | fprintf(stderr, "\n");
134 | fflush(stderr);
135 | }
136 |
137 | /* Log to file */
138 | if (L.fp) {
139 | va_list args;
140 | char buf[32];
141 | buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0';
142 | fprintf(L.fp, "%s.%03d %-5s %s:%d: ", buf, (int)(now % 1000), level_names[level], file, line);
143 | va_start(args, fmt);
144 | vfprintf(L.fp, fmt, args);
145 | va_end(args);
146 | fprintf(L.fp, "\n");
147 | fflush(L.fp);
148 | }
149 |
150 | /* Release lock */
151 | unlock();
152 | }
153 |
--------------------------------------------------------------------------------
/src/log.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Copyright (c) 2017 rxi
3 | *
4 | * This library is free software; you can redistribute it and/or modify it
5 | * under the terms of the MIT license. See `log.c` for details.
6 | */
7 |
8 | #ifndef LOG_H
9 | #define LOG_H
10 |
11 | #include
12 | #include
13 |
14 | #define LOG_VERSION "0.1.0"
15 |
16 | typedef void (*log_LockFn)(void *udata, int lock);
17 |
18 | enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL };
19 |
20 | #define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__)
21 | #define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__)
22 | #define log_info(...) log_log(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__)
23 | #define log_warn(...) log_log(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__)
24 | #define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__)
25 | #define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__)
26 |
27 | void log_set_udata(void *udata);
28 | void log_set_lock(log_LockFn fn);
29 | void log_set_fp(FILE *fp);
30 | void log_set_level(int level);
31 | void log_set_quiet(int enable);
32 | void log_set_colors(int enable);
33 | int log_get_level();
34 |
35 | void log_log(int level, const char *file, int line, const char *fmt, ...);
36 |
37 | #endif
38 |
--------------------------------------------------------------------------------
/src/poa.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include "sha3.h"
4 | #include "poa.h"
5 | #ifdef POA_DEBUG
6 | #include
7 | #endif
8 |
9 | int poa_init(struct poa_context *poa) {
10 | if (!poa)
11 | return -1;
12 |
13 | memset(poa, 0, sizeof(struct poa_context));
14 | return 0;
15 | }
16 |
17 | void poa_done(struct poa_context *poa) {
18 | if (!poa)
19 | return;
20 |
21 | free(poa->hashes);
22 | }
23 |
24 | int poa_add(struct poa_context *poa, const unsigned char *hash, poa_verify verify_hash, void *userdata) {
25 | if ((!poa) || (!hash))
26 | return -1;
27 |
28 | if ((verify_hash) && (verify_hash(hash, userdata) != 1))
29 | return -1;
30 |
31 | int write_at_pos = poa->hash_len;
32 | int start = 0;
33 | int end = poa->hash_len - 1;
34 | int middle = end / 2;
35 |
36 | int order = 0;
37 | int i = 0;
38 | while (start <= end) {
39 | i ++;
40 | order = memcmp(poa->hashes[middle], hash, POA_HASH_SIZE) * -1;
41 | write_at_pos = middle;
42 | if (order > 0)
43 | start = middle + 1;
44 | else
45 | if (!order)
46 | return -1;
47 | else
48 | end = middle - 1;
49 | middle = (start + end) / 2;
50 | }
51 | if (order > 0)
52 | write_at_pos ++;
53 |
54 | if (poa->hash_len >= poa->hash_allocated_size - 1) {
55 | poa->hash_allocated_size += POA_HASH_INCREMENT;
56 | poa->hashes = (poa_hash *)realloc(poa->hashes, poa->hash_allocated_size * sizeof(poa_hash));
57 | if (!poa->hashes) {
58 | poa->hash_allocated_size = 0;
59 | poa->hash_len = 0;
60 | return -1;
61 | }
62 | }
63 |
64 | if (write_at_pos != poa->hash_len)
65 | memmove(&poa->hashes[write_at_pos + 1], poa->hashes[write_at_pos], (poa->hash_len - write_at_pos + 1) * sizeof(poa_hash));
66 |
67 | poa->hash_len ++;
68 | memcpy(poa->hashes[write_at_pos], hash, POA_HASH_SIZE);
69 |
70 | return 0;
71 | }
72 |
73 | int poa_compute(struct poa_context *poa, unsigned int winner_bytes) {
74 | if ((!poa) || (poa->hash_len <= 0) || (!poa->hashes))
75 | return -1;
76 |
77 | sha3_context ctx;
78 | int i;
79 |
80 | sha3_Init256(&ctx);
81 | for (i = 0; i < poa->hash_len; i ++)
82 | sha3_Update(&ctx, poa->hashes[i], POA_HASH_SIZE);
83 |
84 | const unsigned char *data = (const unsigned char *)sha3_Finalize(&ctx);
85 |
86 | int byte_1 = 0;
87 | int byte_2 = 7;
88 | int byte_3 = 15;
89 | int byte_4 = 31;
90 |
91 | if (winner_bytes) {
92 | unsigned long use_bytes = (unsigned long)winner_bytes;
93 | byte_1 = use_bytes / 0x1000000UL % POA_HASH_SIZE;
94 | use_bytes %= 0x1000000UL;
95 |
96 | byte_2 = use_bytes / 0x10000UL % POA_HASH_SIZE;
97 | use_bytes %= 0x10000UL;
98 |
99 | byte_3 = use_bytes / 0x100UL % POA_HASH_SIZE;
100 | use_bytes %= 0x100UL;
101 |
102 | byte_4 = winner_bytes % POA_HASH_SIZE;
103 | }
104 |
105 | unsigned long modulo = (unsigned long)poa->hash_len;
106 | unsigned int winner = (data[byte_1] * 0x1000000UL + data[byte_2] * 0x10000UL + data[byte_3] * 0x100UL + data[byte_4]) % modulo;
107 | poa->hash_len = 0;
108 | poa->hash_allocated_size = 0;
109 | free(poa->hashes);
110 | poa->hashes = NULL;
111 |
112 | return winner;
113 | }
114 |
115 | #ifdef POA_DEBUG
116 | void poa_debug(struct poa_context *poa) {
117 | if (!poa)
118 | return;
119 |
120 | int i;
121 | int j;
122 | for (i = 0; i < poa->hash_len; i ++) {
123 | fprintf(stderr, "hash[%06i]: ", i);
124 | for (j = 0; j < POA_HASH_SIZE; j ++) {
125 | fprintf(stderr, "%02x", (int)poa->hashes[i][j]);
126 | }
127 | fprintf(stderr, "\n");
128 | }
129 | }
130 | #endif
131 |
--------------------------------------------------------------------------------
/src/poa.h:
--------------------------------------------------------------------------------
1 | #ifndef __POA_H
2 | #define __POA_H
3 |
4 | #define POA_HASH_INCREMENT 1024
5 | #define POA_HASH_SIZE 32
6 |
7 | typedef unsigned char poa_hash[POA_HASH_SIZE];
8 | typedef int (*poa_verify)(const unsigned char *hash, void *userdata);
9 |
10 | struct poa_context {
11 | poa_hash *hashes;
12 | int hash_allocated_size;
13 | int hash_len;
14 | };
15 |
16 |
17 | int poa_init(struct poa_context *poa);
18 | void poa_done(struct poa_context *poa);
19 | int poa_add(struct poa_context *poa, const unsigned char *hash, poa_verify verify_hash, void *userdata);
20 | int poa_compute(struct poa_context *poa, unsigned int winner_bytes);
21 | #ifdef POA_DEBUG
22 | void poa_debug(struct poa_context *poa)
23 | #endif
24 |
25 | #endif
26 |
--------------------------------------------------------------------------------
/src/sha256.c:
--------------------------------------------------------------------------------
1 | /*********************************************************************
2 | * Based on sha256.c by Brad Conte (brad AT bradconte.com)
3 | *********************************************************************/
4 | /*************************** HEADER FILES ***************************/
5 | #include
6 | #include
7 | #include "sha256.h"
8 |
9 | /****************************** MACROS ******************************/
10 | #define ROTLEFT(a,b) (((a) << (b)) | ((a) >> (32-(b))))
11 | #define ROTRIGHT(a,b) (((a) >> (b)) | ((a) << (32-(b))))
12 |
13 | #define CH(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
14 | #define MAJ(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
15 | #define EP0(x) (ROTRIGHT(x,2) ^ ROTRIGHT(x,13) ^ ROTRIGHT(x,22))
16 | #define EP1(x) (ROTRIGHT(x,6) ^ ROTRIGHT(x,11) ^ ROTRIGHT(x,25))
17 | #define SIG0(x) (ROTRIGHT(x,7) ^ ROTRIGHT(x,18) ^ ((x) >> 3))
18 | #define SIG1(x) (ROTRIGHT(x,17) ^ ROTRIGHT(x,19) ^ ((x) >> 10))
19 |
20 | /**************************** VARIABLES *****************************/
21 | static const SHA_WORD k[64] = {
22 | 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5,
23 | 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174,
24 | 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da,
25 | 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967,
26 | 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85,
27 | 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070,
28 | 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3,
29 | 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
30 | };
31 |
32 | /*********************** FUNCTION DEFINITIONS ***********************/
33 | void sha256_transform(SHA256_CTX *ctx, const BYTE data[]) {
34 | SHA_WORD a, b, c, d, e, f, g, h, i, j, t1, t2, m[64];
35 |
36 | for (i = 0, j = 0; i < 16; ++i, j += 4)
37 | m[i] = (data[j] << 24) | (data[j + 1] << 16) | (data[j + 2] << 8) | (data[j + 3]);
38 | for ( ; i < 64; ++i)
39 | m[i] = SIG1(m[i - 2]) + m[i - 7] + SIG0(m[i - 15]) + m[i - 16];
40 |
41 | a = ctx->state[0];
42 | b = ctx->state[1];
43 | c = ctx->state[2];
44 | d = ctx->state[3];
45 | e = ctx->state[4];
46 | f = ctx->state[5];
47 | g = ctx->state[6];
48 | h = ctx->state[7];
49 |
50 | for (i = 0; i < 64; ++i) {
51 | t1 = h + EP1(e) + CH(e,f,g) + k[i] + m[i];
52 | t2 = EP0(a) + MAJ(a,b,c);
53 | h = g;
54 | g = f;
55 | f = e;
56 | e = d + t1;
57 | d = c;
58 | c = b;
59 | b = a;
60 | a = t1 + t2;
61 | }
62 |
63 | ctx->state[0] += a;
64 | ctx->state[1] += b;
65 | ctx->state[2] += c;
66 | ctx->state[3] += d;
67 | ctx->state[4] += e;
68 | ctx->state[5] += f;
69 | ctx->state[6] += g;
70 | ctx->state[7] += h;
71 | }
72 |
73 | void sha256_init(SHA256_CTX *ctx) {
74 | ctx->datalen = 0;
75 | ctx->bitlen = 0;
76 | ctx->state[0] = 0x6a09e667;
77 | ctx->state[1] = 0xbb67ae85;
78 | ctx->state[2] = 0x3c6ef372;
79 | ctx->state[3] = 0xa54ff53a;
80 | ctx->state[4] = 0x510e527f;
81 | ctx->state[5] = 0x9b05688c;
82 | ctx->state[6] = 0x1f83d9ab;
83 | ctx->state[7] = 0x5be0cd19;
84 | }
85 |
86 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len)
87 | {
88 | SHA_WORD i;
89 |
90 | for (i = 0; i < len; ++i) {
91 | ctx->data[ctx->datalen] = data[i];
92 | ctx->datalen++;
93 | if (ctx->datalen == 64) {
94 | sha256_transform(ctx, ctx->data);
95 | ctx->bitlen += 512;
96 | ctx->datalen = 0;
97 | }
98 | }
99 | }
100 |
101 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]) {
102 | SHA_WORD i;
103 |
104 | i = ctx->datalen;
105 |
106 | // Pad whatever data is left in the buffer.
107 | if (ctx->datalen < 56) {
108 | ctx->data[i++] = 0x80;
109 | while (i < 56)
110 | ctx->data[i++] = 0x00;
111 | }
112 | else {
113 | ctx->data[i++] = 0x80;
114 | while (i < 64)
115 | ctx->data[i++] = 0x00;
116 | sha256_transform(ctx, ctx->data);
117 | memset(ctx->data, 0, 56);
118 | }
119 |
120 | // Append to the padding the total message's length in bits and transform.
121 | ctx->bitlen += ctx->datalen * 8;
122 | ctx->data[63] = ctx->bitlen;
123 | ctx->data[62] = ctx->bitlen >> 8;
124 | ctx->data[61] = ctx->bitlen >> 16;
125 | ctx->data[60] = ctx->bitlen >> 24;
126 | ctx->data[59] = ctx->bitlen >> 32;
127 | ctx->data[58] = ctx->bitlen >> 40;
128 | ctx->data[57] = ctx->bitlen >> 48;
129 | ctx->data[56] = ctx->bitlen >> 56;
130 | sha256_transform(ctx, ctx->data);
131 |
132 | // Since this implementation uses little endian byte ordering and SHA uses big endian,
133 | // reverse all the bytes when copying the final state to the output hash.
134 | for (i = 0; i < 4; ++i) {
135 | hash[i] = (ctx->state[0] >> (24 - i * 8)) & 0x000000ff;
136 | hash[i + 4] = (ctx->state[1] >> (24 - i * 8)) & 0x000000ff;
137 | hash[i + 8] = (ctx->state[2] >> (24 - i * 8)) & 0x000000ff;
138 | hash[i + 12] = (ctx->state[3] >> (24 - i * 8)) & 0x000000ff;
139 | hash[i + 16] = (ctx->state[4] >> (24 - i * 8)) & 0x000000ff;
140 | hash[i + 20] = (ctx->state[5] >> (24 - i * 8)) & 0x000000ff;
141 | hash[i + 24] = (ctx->state[6] >> (24 - i * 8)) & 0x000000ff;
142 | hash[i + 28] = (ctx->state[7] >> (24 - i * 8)) & 0x000000ff;
143 | }
144 | }
145 |
146 | void sha256(const BYTE in[], size_t len, BYTE hash[]) {
147 | SHA256_CTX ctx;
148 | sha256_init(&ctx);
149 | sha256_update(&ctx, in, len);
150 | sha256_final(&ctx, hash);
151 | }
152 |
153 | void hmac_sha256(const BYTE key_gc[], size_t key_len, const BYTE data[], size_t data_len, const BYTE data2[], size_t data2_len, BYTE hash[]) {
154 | SHA256_CTX sha;
155 | BYTE key[64];
156 | BYTE ikey[64];
157 | BYTE okey[64];
158 | BYTE temp_hash[32];
159 | SHA_WORD i;
160 |
161 | if (key_len > 64)
162 | sha256(key_gc, key_len, key);
163 | else
164 | if (key_len < 64) {
165 | memcpy(key, key_gc, key_len);
166 | memset(key + key_len, 0, 64 - key_len);
167 | }
168 | for (i = 0; i < 64; i++) {
169 | okey[i] = key[i] ^ 0x5C;
170 | ikey[i] = key[i] ^ 0x36;
171 | }
172 |
173 | sha256_init(&sha);
174 | sha256_update(&sha, ikey, 64);
175 | sha256_update(&sha, data, data_len);
176 | if ((data2) && (data2_len))
177 | sha256_update(&sha, data2, data2_len);
178 | sha256_final(&sha, temp_hash);
179 |
180 | sha256_init(&sha);
181 | sha256_update(&sha, okey, 64);
182 | sha256_update(&sha, temp_hash, 32);
183 | sha256_final(&sha, hash);
184 | }
185 |
--------------------------------------------------------------------------------
/src/sha256.h:
--------------------------------------------------------------------------------
1 | /*********************************************************************
2 | * Filename: sha256.h
3 | * Author: Brad Conte (brad AT bradconte.com)
4 | * Copyright:
5 | * Disclaimer: This code is presented "as is" without any guarantees.
6 | * Details: Defines the API for the corresponding SHA1 implementation.
7 | *********************************************************************/
8 |
9 | #ifndef SHA256_H
10 | #define SHA256_H
11 |
12 | /*************************** HEADER FILES ***************************/
13 | #include
14 | #include "edfs_types.h"
15 | /****************************** MACROS ******************************/
16 | #define SHA256_BLOCK_SIZE 32 // SHA256 outputs a 32 byte digest
17 |
18 | /**************************** DATA TYPES ****************************/
19 | typedef unsigned int SHA_WORD; // 32-bit word, change to "long" for 16-bit machines
20 |
21 | typedef struct {
22 | BYTE data[64];
23 | SHA_WORD datalen;
24 | unsigned long long bitlen;
25 | SHA_WORD state[8];
26 | } SHA256_CTX;
27 |
28 | /*********************** FUNCTION DECLARATIONS **********************/
29 | void sha256_init(SHA256_CTX *ctx);
30 | void sha256_update(SHA256_CTX *ctx, const BYTE data[], size_t len);
31 | void sha256_final(SHA256_CTX *ctx, BYTE hash[]);
32 |
33 | void sha256(const BYTE in[], size_t len, BYTE hash[]);
34 | void hmac_sha256(const BYTE key_gc[], size_t key_len, const BYTE data[], size_t data_len, const BYTE data2[], size_t data2_len, BYTE hash[]);
35 |
36 | #endif // SHA256_H
37 |
--------------------------------------------------------------------------------
/src/sha3.h:
--------------------------------------------------------------------------------
1 | #ifndef SHA3_H
2 | #define SHA3_H
3 | /* -------------------------------------------------------------------------
4 | * Works when compiled for either 32-bit or 64-bit targets, optimized for
5 | * 64 bit.
6 | *
7 | * Canonical implementation of Init/Update/Finalize for SHA-3 byte input.
8 | *
9 | * SHA3-256, SHA3-384, SHA-512 are implemented. SHA-224 can easily be added.
10 | *
11 | * Based on code from http://keccak.noekeon.org/ .
12 | *
13 | * I place the code that I wrote into public domain, free to use.
14 | *
15 | * I would appreciate if you give credits to this work if you used it to
16 | * write or test * your code.
17 | *
18 | * Aug 2015. Andrey Jivsov. crypto@brainhub.org
19 | * ---------------------------------------------------------------------- */
20 | #include
21 | #include
22 | /* 'Words' here refers to uint64_t */
23 | #define SHA3_KECCAK_SPONGE_WORDS \
24 | (((1600)/8/*bits to byte*/)/sizeof(uint64_t))
25 | typedef struct sha3_context_ {
26 | uint64_t saved; /* the portion of the input message that we
27 | * didn't consume yet */
28 | union { /* Keccak's state */
29 | uint64_t s[SHA3_KECCAK_SPONGE_WORDS];
30 | uint8_t sb[SHA3_KECCAK_SPONGE_WORDS * 8];
31 | };
32 | unsigned byteIndex; /* 0..7--the next byte after the set one
33 | * (starts from 0; 0--none are buffered) */
34 | unsigned wordIndex; /* 0..24--the next word to integrate input
35 | * (starts from 0) */
36 | unsigned capacityWords; /* the double size of the hash output in
37 | * words (e.g. 16 for Keccak 512) */
38 | } sha3_context;
39 |
40 |
41 | /* For Init or Reset call these: */
42 | void sha3_Init256(void *priv);
43 | void sha3_Init384(void *priv);
44 | void sha3_Init512(void *priv);
45 |
46 | void sha3_Update(void *priv, void const *bufIn, size_t len);
47 |
48 | void const *sha3_Finalize(void *priv);
49 |
50 | void sha3_256(const void *in, size_t len, void *hash);
51 | void hmac_sha3_256(const void *key_gc, size_t key_len, const void *data, size_t data_len, void *hash);
52 |
53 | #endif
54 |
--------------------------------------------------------------------------------
/src/smartcard.h:
--------------------------------------------------------------------------------
1 | #ifndef __SMARTCARD_H
2 | #define __SMARTCARD_H
3 |
4 | #include
5 | #include
6 | #ifdef _WIN32
7 | #undef UNICODE
8 | #include
9 | #include
10 | #else
11 | #include
12 | #include
13 |
14 | #define SCARD_AUTOALLOCATE (DWORD)(-1)
15 | #define SCARD_ATTR_VALUE(Class, Tag) ((((ULONG)(Class)) << 16) | ((ULONG)(Tag)))
16 | #define SCARD_PROTOCOL_Tx (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1)
17 | #define SCARD_ATTR_ATR_STRING SCARD_ATTR_VALUE(9, 0x0303)
18 | #endif
19 |
20 | extern LONG SC_errno;
21 | extern const BYTE SC_GET_DATA_APDU[5];
22 | extern const BYTE SC_GET_JAVA_CARD_ID_APDU[5];
23 |
24 | const char *SC_GetErrorString(LONG lRetValue);
25 | char **SC_ListReaders(SCARDCONTEXT hContext);
26 | void SC_FreeReaders(char **readers);
27 | SCARDCONTEXT SC_Connect();
28 | int SC_Disconnect(SCARDCONTEXT hContext);
29 | int SC_WaitForCard(SCARDCONTEXT hContext, char *szSelectedReader, int max_time);
30 | int SC_WaitForCardRemoval(SCARDCONTEXT hContext, char *szSelectedReader, int max_time);
31 | SCARDHANDLE SC_ActivateCard(SCARDCONTEXT hContext, char *szSelectedReader, DWORD *protocol);
32 | int SC_DisconnectCard(SCARDHANDLE hCard);
33 | int SC_ResetCard(SCARDHANDLE hCard);
34 | int SC_EjectCard(SCARDHANDLE hCard);
35 | int SC_GetAttributeType(SCARDHANDLE hCard, DWORD dwAttrId, char *pbAttr, DWORD *len);
36 | int SC_GetAttribute(SCARDHANDLE hCard, char *pbAttr, DWORD *len);
37 | int SC_GetAttributeAuto(SCARDHANDLE hCard, char **pbAttr, DWORD *len);
38 | int SC_Exchange(SCARDHANDLE hCard, DWORD m_dwActiveProtocol, LPCBYTE pbSendBuffer, DWORD cbSendLength, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength);
39 | int SC_Control(SCARDHANDLE hCard, DWORD dwControlCode, LPCBYTE pbSendBuffer, DWORD cbSendLength, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength);
40 | int SC_SelectApplet(SCARDHANDLE hCard, DWORD protocol, unsigned char *applet_id, int len_applet_id);
41 | int SC_Features(SCARDHANDLE hCard, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength);
42 |
43 | #endif
44 |
--------------------------------------------------------------------------------
/src/sort.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | static void insertion_sort(uint64_t* data, int count) {
4 | int i;
5 | for (i = 1; i < count; ++i) {
6 | int j = i;
7 |
8 | while (j > 0) {
9 | if (data[j - 1] > data[j]) {
10 | data[j - 1] ^= data[j];
11 | data[j] ^= data[j - 1];
12 | data[j - 1] ^= data[j];
13 | --j;
14 | } else
15 | break;
16 | }
17 | }
18 | }
19 |
20 | static void max_heapify(uint64_t *data, int heapSize, int index) {
21 | int left = (index + 1) * 2 - 1;
22 | int right = (index + 1) * 2;
23 | int largest = 0;
24 |
25 | if (left < heapSize && data[left] > data[index])
26 | largest = left;
27 | else
28 | largest = index;
29 |
30 | if (right < heapSize && data[right] > data[largest])
31 | largest = right;
32 |
33 | if (largest != index) {
34 | uint64_t temp = data[index];
35 | data[index] = data[largest];
36 | data[largest] = temp;
37 |
38 | max_heapify(data, heapSize, largest);
39 | }
40 | }
41 |
42 | static void heap_sort(uint64_t* data, int count) {
43 | int heapSize = count;
44 | int p;
45 | int i;
46 |
47 | for (p = (heapSize - 1) / 2; p >= 0; --p)
48 | max_heapify(data, heapSize, p);
49 |
50 | for (i = count - 1; i > 0; --i) {
51 | uint64_t temp = data[i];
52 | data[i] = data[0];
53 | data[0] = temp;
54 |
55 | --heapSize;
56 | max_heapify(data, heapSize, 0);
57 | }
58 | }
59 |
60 | static int partition(uint64_t* data, int left, int right) {
61 | uint64_t pivot = data[right];
62 | uint64_t temp;
63 | int i = left;
64 | int j;
65 |
66 | for (j = left; j < right; ++j) {
67 | if (data[j] <= pivot) {
68 | temp = data[j];
69 | data[j] = data[i];
70 | data[i] = temp;
71 | i++;
72 | }
73 | }
74 |
75 | data[right] = data[i];
76 | data[i] = pivot;
77 |
78 | return i;
79 | }
80 |
81 | static void quick_sort(uint64_t* data, int left, int right) {
82 | if (left < right) {
83 | int q = partition(data, left, right);
84 | quick_sort(data, left, q - 1);
85 | quick_sort(data, q + 1, right);
86 | }
87 | }
88 |
89 | static unsigned int logn(unsigned int n, unsigned int r) {
90 | return (n > r - 1) ? 1 + logn(n / r, r) : 0;
91 | }
92 |
93 | void edfs_sort(uint64_t* data, int count) {
94 | int partitionSize = partition(data, 0, count - 1);
95 |
96 | if (partitionSize < 16)
97 | insertion_sort(data, count);
98 | else
99 | if (partitionSize >(2 * logn((unsigned int)count, 2)))
100 | heap_sort(data, count);
101 | else
102 | quick_sort(data, 0, count - 1);
103 | }
104 |
--------------------------------------------------------------------------------
/src/sort.h:
--------------------------------------------------------------------------------
1 | #ifndef _SORT_H
2 | #define _SORT_H
3 |
4 | void edfs_sort(uint64_t* data, int count);
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/src/store.h:
--------------------------------------------------------------------------------
1 | #ifndef __STORE_H
2 | #define __STORE_H
3 |
4 | #include
5 | #include
6 |
7 | #define STORE_MAX_CHUNK_SIZE 0x10000
8 | #define STORE_CHUNKS_PER_FILE 20
9 |
10 | struct store_data;
11 |
12 | struct store_data *store_open(const char *path, int mode, unsigned int chunk);
13 | FILE *store_handle(struct store_data *f);
14 | int store_close(struct store_data *f);
15 | int store_write(const void *buf, int size, struct store_data *f);
16 | int store_read(void *buf, int size, struct store_data *f);
17 | int store_seek(struct store_data *f, int offset);
18 | int store_exists(const char *path, int chunk);
19 | int store_size(const char *path, int chunk);
20 | int store_unlink(const char *path, int chunk);
21 | int store_stat(const char *path, int chunk, struct stat *attrib);
22 |
23 | #endif
24 |
25 |
--------------------------------------------------------------------------------
/src/ui/htmlwindow.h:
--------------------------------------------------------------------------------
1 | #ifndef __HTMLWINDOW_H
2 | #define __HTMLWINDOW_H
3 |
4 | typedef void (*ui_trigger_event)(void *window);
5 | typedef void (*ui_idle_event)(void *userdata);
6 | typedef void (*ui_tray_event)(void *window);
7 | typedef void (*ui_event)(void *event_data, void *userdata);
8 |
9 | #define UI_EVENT_WINDOW_CREATE 0
10 | #define UI_EVENT_WINDOW_CLOSE 1
11 | #define UI_EVENT_LOOP_EXIT 2
12 | #define UI_EVENTS 3
13 |
14 | #define UI_SCHEDULER_SIZE 100
15 |
16 | int ui_app_init(ui_trigger_event event_handler);
17 | void ui_app_tray_icon(const char *tooltip, const char *notification_title, const char *notification_text, ui_tray_event event_tray);
18 | void ui_app_tray_remove();
19 | void ui_app_run_with_notify(ui_idle_event event_idle, void *userdata);
20 | void ui_app_run_schedule_once(ui_idle_event scheduled, void *userdata);
21 | void ui_app_run();
22 | int ui_app_done();
23 | void ui_app_quit();
24 | void ui_set_event(int eid, ui_event callback, void *event_userdata);
25 |
26 | void ui_message(const char *title, const char *body, int level);
27 | int ui_question(const char *title, const char *body, int level);
28 | int ui_input(const char *title, const char *body, char *val, int val_len, int masked);
29 | void ui_js(void *wnd, const char *js);
30 | char *ui_call(void *wnd, const char *function, const char *arguments[]);
31 | void ui_free_string(void *ptr);
32 | void *ui_window(const char *title, const char *body);
33 | void ui_window_maximize(void *wnd);
34 | void ui_window_minimize(void *wnd);
35 | void ui_window_restore(void *wnd);
36 | void ui_window_top(void *wnd);
37 | void ui_window_close(void *wnd);
38 | void ui_window_set_content(void *wnd, const char *body);
39 | int ui_window_count();
40 |
41 | void ui_lock();
42 | void ui_unlock();
43 |
44 | #endif
45 |
--------------------------------------------------------------------------------
/src/ui/macOS/resource.m:
--------------------------------------------------------------------------------
1 | #import
2 | #import
3 |
4 | Protocol *_reference_all() {
5 | Protocol *dummy = @protocol(WKNavigationDelegate);
6 | Protocol *dummy2 = @protocol(NSWindowDelegate);
7 | Protocol *dummy3 = @protocol(NSApplicationDelegate);
8 | return dummy;
9 | }
10 |
--------------------------------------------------------------------------------
/src/ui/win32/resource.h:
--------------------------------------------------------------------------------
1 | #ifndef __RESOURCE_H
2 | #define __RESOURCE_H
3 |
4 | #define IDI_ICON1 1
5 |
6 | #endif
7 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_asconf.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_ASCONF_H_
41 | #define _NETINET_SCTP_ASCONF_H_
42 |
43 | #if defined(_KERNEL) || defined(__Userspace__)
44 |
45 | /*
46 | * function prototypes
47 | */
48 | extern void sctp_asconf_cleanup(struct sctp_tcb *);
49 |
50 | extern struct mbuf *sctp_compose_asconf(struct sctp_tcb *, int *, int);
51 |
52 | extern void
53 | sctp_handle_asconf(struct mbuf *, unsigned int, struct sockaddr *,
54 | struct sctp_asconf_chunk *, struct sctp_tcb *, int);
55 |
56 | extern void
57 | sctp_handle_asconf_ack(struct mbuf *, int, struct sctp_asconf_ack_chunk *,
58 | struct sctp_tcb *, struct sctp_nets *, int *);
59 |
60 | extern uint32_t
61 | sctp_addr_mgmt_ep_sa(struct sctp_inpcb *, struct sockaddr *, uint32_t,
62 | uint32_t);
63 |
64 | extern int sctp_asconf_iterator_ep(struct sctp_inpcb *inp, void *ptr,
65 | uint32_t val);
66 | extern void sctp_asconf_iterator_stcb(struct sctp_inpcb *inp,
67 | struct sctp_tcb *stcb,
68 | void *ptr, uint32_t type);
69 | extern void sctp_asconf_iterator_end(void *ptr, uint32_t val);
70 |
71 | extern int32_t
72 | sctp_set_primary_ip_address_sa(struct sctp_tcb *,
73 | struct sockaddr *);
74 |
75 | extern void
76 | sctp_check_address_list(struct sctp_tcb *, struct mbuf *, int, int,
77 | struct sockaddr *, uint16_t, uint16_t, uint16_t, uint16_t);
78 |
79 | extern void
80 | sctp_assoc_immediate_retrans(struct sctp_tcb *, struct sctp_nets *);
81 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
82 | extern void
83 | sctp_net_immediate_retrans(struct sctp_tcb *, struct sctp_nets *);
84 | #endif
85 |
86 | extern void
87 | sctp_asconf_send_nat_state_update(struct sctp_tcb *stcb,
88 | struct sctp_nets *net);
89 |
90 | extern int
91 | sctp_is_addr_pending(struct sctp_tcb *, struct sctp_ifa *);
92 | #endif /* _KERNEL */
93 |
94 | #endif /* !_NETINET_SCTP_ASCONF_H_ */
95 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_bsd_addr.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_BSD_ADDR_H_
41 | #define _NETINET_SCTP_BSD_ADDR_H_
42 |
43 | #include
44 |
45 | #if defined(_KERNEL) || defined(__Userspace__)
46 |
47 | extern struct iterator_control sctp_it_ctl;
48 | void sctp_wakeup_iterator(void);
49 |
50 | void sctp_startup_iterator(void);
51 |
52 | #ifdef INET6
53 | void sctp_gather_internal_ifa_flags(struct sctp_ifa *ifa);
54 | #endif
55 |
56 | #ifdef SCTP_PACKET_LOGGING
57 |
58 | void sctp_packet_log(struct mbuf *m);
59 | int sctp_copy_out_packet_log(uint8_t *target, int length);
60 |
61 | #endif
62 |
63 | void sctp_addr_change(struct ifaddr *ifa, int cmd);
64 | #if defined(__FreeBSD__) && !defined(__Userspace__)
65 |
66 | void sctp_addr_change_event_handler(void *, struct ifaddr *, int);
67 | #endif
68 |
69 | void sctp_add_or_del_interfaces(int (*pred)(struct ifnet *), int add);
70 |
71 | #endif
72 | #endif
73 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_callout.c:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__Userspace__)
36 | #include
37 | #if !defined(_WIN32)
38 | #include
39 | #include
40 | #include
41 | #endif
42 | #if defined(__native_client__)
43 | #include
44 | #endif
45 | #include
46 | #include
47 | #include
48 | #include
49 | #include
50 | #include
51 | #include
52 | #else
53 | #include
54 | #include
55 | #include
56 | #endif
57 | #include
58 |
59 | /*
60 | * Callout/Timer routines for OS that doesn't have them
61 | */
62 | #if defined(__APPLE__) || defined(__Userspace__)
63 | static uint32_t ticks = 0;
64 | #else
65 | extern int ticks;
66 | #endif
67 |
68 | uint32_t sctp_get_tick_count(void) {
69 | uint32_t ret;
70 |
71 | SCTP_TIMERQ_LOCK();
72 | ret = ticks;
73 | SCTP_TIMERQ_UNLOCK();
74 | return ret;
75 | }
76 |
77 | /*
78 | * SCTP_TIMERQ_LOCK protects:
79 | * - SCTP_BASE_INFO(callqueue)
80 | * - sctp_os_timer_next: next timer to check
81 | */
82 | static sctp_os_timer_t *sctp_os_timer_next = NULL;
83 |
84 | void
85 | sctp_os_timer_init(sctp_os_timer_t *c)
86 | {
87 | memset(c, 0, sizeof(*c));
88 | }
89 |
90 | int
91 | sctp_os_timer_start(sctp_os_timer_t *c, uint32_t to_ticks, void (*ftn) (void *),
92 | void *arg)
93 | {
94 | int ret = 0;
95 |
96 | /* paranoia */
97 | if ((c == NULL) || (ftn == NULL))
98 | return (ret);
99 |
100 | SCTP_TIMERQ_LOCK();
101 | /* check to see if we're rescheduling a timer */
102 | if (c->c_flags & SCTP_CALLOUT_PENDING) {
103 | ret = 1;
104 | if (c == sctp_os_timer_next) {
105 | sctp_os_timer_next = TAILQ_NEXT(c, tqe);
106 | }
107 | TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
108 | /*
109 | * part of the normal "stop a pending callout" process
110 | * is to clear the CALLOUT_ACTIVE and CALLOUT_PENDING
111 | * flags. We don't bother since we are setting these
112 | * below and we still hold the lock.
113 | */
114 | }
115 |
116 | /*
117 | * We could unlock/splx here and lock/spl at the TAILQ_INSERT_TAIL,
118 | * but there's no point since doing this setup doesn't take much time.
119 | */
120 | if (to_ticks == 0)
121 | to_ticks = 1;
122 |
123 | c->c_arg = arg;
124 | c->c_flags = (SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
125 | c->c_func = ftn;
126 | c->c_time = ticks + to_ticks;
127 | TAILQ_INSERT_TAIL(&SCTP_BASE_INFO(callqueue), c, tqe);
128 | SCTP_TIMERQ_UNLOCK();
129 | return (ret);
130 | }
131 |
132 | int
133 | sctp_os_timer_stop(sctp_os_timer_t *c)
134 | {
135 | SCTP_TIMERQ_LOCK();
136 | /*
137 | * Don't attempt to delete a callout that's not on the queue.
138 | */
139 | if ((c->c_flags & SCTP_CALLOUT_PENDING) == 0) {
140 | c->c_flags &= ~SCTP_CALLOUT_ACTIVE;
141 | SCTP_TIMERQ_UNLOCK();
142 | return (0);
143 | }
144 | c->c_flags &= ~(SCTP_CALLOUT_ACTIVE | SCTP_CALLOUT_PENDING);
145 | if (c == sctp_os_timer_next) {
146 | sctp_os_timer_next = TAILQ_NEXT(c, tqe);
147 | }
148 | TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
149 | SCTP_TIMERQ_UNLOCK();
150 | return (1);
151 | }
152 |
153 | void
154 | sctp_handle_tick(uint32_t elapsed_ticks)
155 | {
156 | sctp_os_timer_t *c;
157 | void (*c_func)(void *);
158 | void *c_arg;
159 |
160 | SCTP_TIMERQ_LOCK();
161 | /* update our tick count */
162 | ticks += elapsed_ticks;
163 | c = TAILQ_FIRST(&SCTP_BASE_INFO(callqueue));
164 | while (c) {
165 | if (SCTP_UINT32_GE(ticks, c->c_time)) {
166 | sctp_os_timer_next = TAILQ_NEXT(c, tqe);
167 | TAILQ_REMOVE(&SCTP_BASE_INFO(callqueue), c, tqe);
168 | c_func = c->c_func;
169 | c_arg = c->c_arg;
170 | c->c_flags &= ~SCTP_CALLOUT_PENDING;
171 | SCTP_TIMERQ_UNLOCK();
172 | c_func(c_arg);
173 | SCTP_TIMERQ_LOCK();
174 | c = sctp_os_timer_next;
175 | } else {
176 | c = TAILQ_NEXT(c, tqe);
177 | }
178 | }
179 | sctp_os_timer_next = NULL;
180 | SCTP_TIMERQ_UNLOCK();
181 | }
182 |
183 | #if defined(__APPLE__) && !defined(__Userspace__)
184 | void
185 | sctp_timeout(void *arg SCTP_UNUSED)
186 | {
187 | sctp_handle_tick(SCTP_BASE_VAR(sctp_main_timer_ticks));
188 | sctp_start_main_timer();
189 | }
190 | #endif
191 |
192 | #if defined(__Userspace__)
193 | #define TIMEOUT_INTERVAL 10
194 |
195 | void *
196 | user_sctp_timer_iterate(void *arg)
197 | {
198 | sctp_userspace_set_threadname("SCTP timer");
199 | for (;;) {
200 | #if defined(_WIN32)
201 | Sleep(TIMEOUT_INTERVAL);
202 | #else
203 | struct timespec amount, remaining;
204 |
205 | remaining.tv_sec = 0;
206 | remaining.tv_nsec = TIMEOUT_INTERVAL * 1000 * 1000;
207 | do {
208 | amount = remaining;
209 | } while (nanosleep(&amount, &remaining) == -1);
210 | #endif
211 | if (atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 1, 1)) {
212 | break;
213 | }
214 | sctp_handle_tick(sctp_msecs_to_ticks(TIMEOUT_INTERVAL));
215 | }
216 | return (NULL);
217 | }
218 |
219 | void
220 | sctp_start_timer_thread(void)
221 | {
222 | /*
223 | * No need to do SCTP_TIMERQ_LOCK_INIT();
224 | * here, it is being done in sctp_pcb_init()
225 | */
226 | int rc;
227 |
228 | rc = sctp_userspace_thread_create(&SCTP_BASE_VAR(timer_thread), user_sctp_timer_iterate);
229 | if (rc) {
230 | SCTP_PRINTF("ERROR; return code from sctp_thread_create() is %d\n", rc);
231 | } else {
232 | SCTP_BASE_VAR(timer_thread_started) = 1;
233 | }
234 | }
235 |
236 | void
237 | sctp_stop_timer_thread(void)
238 | {
239 | atomic_cmpset_int(&SCTP_BASE_VAR(timer_thread_should_exit), 0, 1);
240 | if (SCTP_BASE_VAR(timer_thread_started)) {
241 | #if defined(_WIN32)
242 | WaitForSingleObject(SCTP_BASE_VAR(timer_thread), INFINITE);
243 | CloseHandle(SCTP_BASE_VAR(timer_thread));
244 | #else
245 | pthread_join(SCTP_BASE_VAR(timer_thread), NULL);
246 | #endif
247 | }
248 | }
249 | #endif
250 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_callout.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions
10 | * are met:
11 | * 1. Redistributions of source code must retain the above copyright
12 | * notice, this list of conditions and the following disclaimer.
13 | * 2. Redistributions in binary form must reproduce the above copyright
14 | * notice, this list of conditions and the following disclaimer in the
15 | * documentation and/or other materials provided with the distribution.
16 | * 3. Neither the name of the project nor the names of its contributors
17 | * may be used to endorse or promote products derived from this software
18 | * without specific prior written permission.
19 | *
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 | * SUCH DAMAGE.
31 | */
32 |
33 | #if defined(__FreeBSD__) && !defined(__Userspace__)
34 | #include
35 | __FBSDID("$FreeBSD$");
36 | #endif
37 |
38 | #ifndef _NETINET_SCTP_CALLOUT_
39 | #define _NETINET_SCTP_CALLOUT_
40 |
41 | /*
42 | * NOTE: the following MACROS are required for locking the callout
43 | * queue along with a lock/mutex in the OS specific headers and
44 | * implementation files::
45 | * - SCTP_TIMERQ_LOCK()
46 | * - SCTP_TIMERQ_UNLOCK()
47 | * - SCTP_TIMERQ_LOCK_INIT()
48 | * - SCTP_TIMERQ_LOCK_DESTROY()
49 | */
50 |
51 | #define _SCTP_NEEDS_CALLOUT_ 1
52 |
53 | #define SCTP_TICKS_PER_FASTTIMO 20 /* called about every 20ms */
54 |
55 | #if defined(__Userspace__)
56 | #if defined(_WIN32)
57 | #define SCTP_TIMERQ_LOCK() EnterCriticalSection(&SCTP_BASE_VAR(timer_mtx))
58 | #define SCTP_TIMERQ_UNLOCK() LeaveCriticalSection(&SCTP_BASE_VAR(timer_mtx))
59 | #define SCTP_TIMERQ_LOCK_INIT() InitializeCriticalSection(&SCTP_BASE_VAR(timer_mtx))
60 | #define SCTP_TIMERQ_LOCK_DESTROY() DeleteCriticalSection(&SCTP_BASE_VAR(timer_mtx))
61 | #else
62 | #ifdef INVARIANTS
63 | #define SCTP_TIMERQ_LOCK() KASSERT(pthread_mutex_lock(&SCTP_BASE_VAR(timer_mtx)) == 0, ("%s: timer_mtx already locked", __func__))
64 | #define SCTP_TIMERQ_UNLOCK() KASSERT(pthread_mutex_unlock(&SCTP_BASE_VAR(timer_mtx)) == 0, ("%s: timer_mtx not locked", __func__))
65 | #else
66 | #define SCTP_TIMERQ_LOCK() (void)pthread_mutex_lock(&SCTP_BASE_VAR(timer_mtx))
67 | #define SCTP_TIMERQ_UNLOCK() (void)pthread_mutex_unlock(&SCTP_BASE_VAR(timer_mtx))
68 | #endif
69 | #define SCTP_TIMERQ_LOCK_INIT() (void)pthread_mutex_init(&SCTP_BASE_VAR(timer_mtx), &SCTP_BASE_VAR(mtx_attr))
70 | #define SCTP_TIMERQ_LOCK_DESTROY() (void)pthread_mutex_destroy(&SCTP_BASE_VAR(timer_mtx))
71 | #endif
72 | #endif
73 |
74 | uint32_t sctp_get_tick_count(void);
75 |
76 | TAILQ_HEAD(calloutlist, sctp_callout);
77 |
78 | struct sctp_callout {
79 | TAILQ_ENTRY(sctp_callout) tqe;
80 | uint32_t c_time; /* ticks to the event */
81 | void *c_arg; /* function argument */
82 | void (*c_func)(void *); /* function to call */
83 | int c_flags; /* state of this entry */
84 | };
85 | typedef struct sctp_callout sctp_os_timer_t;
86 |
87 | #define SCTP_CALLOUT_ACTIVE 0x0002 /* callout is currently active */
88 | #define SCTP_CALLOUT_PENDING 0x0004 /* callout is waiting for timeout */
89 |
90 | void sctp_os_timer_init(sctp_os_timer_t *tmr);
91 | /* Returns 1 if pending timer was rescheduled, 0 otherwise. */
92 | int sctp_os_timer_start(sctp_os_timer_t *, uint32_t, void (*)(void *), void *);
93 | /* Returns 1 if pending timer was stopped, 0 otherwise. */
94 | int sctp_os_timer_stop(sctp_os_timer_t *);
95 | void sctp_handle_tick(uint32_t);
96 |
97 | #define SCTP_OS_TIMER_INIT sctp_os_timer_init
98 | /*
99 | * NOTE: The next two shouldn't be called directly outside of sctp_timer_start()
100 | * and sctp_timer_stop(), since they don't handle incrementing/decrementing
101 | * relevant reference counts.
102 | */
103 | #define SCTP_OS_TIMER_START sctp_os_timer_start
104 | #define SCTP_OS_TIMER_STOP sctp_os_timer_stop
105 | /* MT FIXME: Is the following correct? */
106 | #define SCTP_OS_TIMER_STOP_DRAIN SCTP_OS_TIMER_STOP
107 | #define SCTP_OS_TIMER_PENDING(tmr) ((tmr)->c_flags & SCTP_CALLOUT_PENDING)
108 | #define SCTP_OS_TIMER_ACTIVE(tmr) ((tmr)->c_flags & SCTP_CALLOUT_ACTIVE)
109 | #define SCTP_OS_TIMER_DEACTIVATE(tmr) ((tmr)->c_flags &= ~SCTP_CALLOUT_ACTIVE)
110 |
111 | #if defined(__Userspace__)
112 | void sctp_start_timer_thread(void);
113 | void sctp_stop_timer_thread(void);
114 | #endif
115 | #if defined(__APPLE__) && !defined(__Userspace__)
116 | void sctp_timeout(void *);
117 | #endif
118 |
119 | #endif
120 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_crc32.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_CRC32_H_
41 | #define _NETINET_SCTP_CRC32_H_
42 |
43 | #if defined(_KERNEL)
44 | uint32_t sctp_calculate_cksum(struct mbuf *, int32_t);
45 | #if defined(__FreeBSD__) && !defined(__Userspace__)
46 | #if defined(SCTP) || defined(SCTP_SUPPORT)
47 | void sctp_delayed_cksum(struct mbuf *, uint32_t offset);
48 | #endif
49 | #endif
50 | #endif /* _KERNEL */
51 | #if defined(__Userspace__)
52 | uint32_t calculate_crc32c(uint32_t, const unsigned char *, unsigned int);
53 | uint32_t sctp_finalize_crc32c(uint32_t);
54 | uint32_t sctp_calculate_cksum(struct mbuf *, int32_t);
55 | #endif
56 | #endif /* __crc32c_h__ */
57 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_indata.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_INDATA_H_
41 | #define _NETINET_SCTP_INDATA_H_
42 |
43 | #if defined(_KERNEL) || defined(__Userspace__)
44 |
45 | struct sctp_queued_to_read *
46 | sctp_build_readq_entry(struct sctp_tcb *stcb,
47 | struct sctp_nets *net,
48 | uint32_t tsn, uint32_t ppid,
49 | uint32_t context, uint16_t sid,
50 | uint32_t mid, uint8_t flags,
51 | struct mbuf *dm);
52 |
53 | #define sctp_build_readq_entry_mac(_ctl, in_it, context, net, tsn, ppid, sid, flags, dm, tfsn, mid) do { \
54 | if (_ctl) { \
55 | atomic_add_int(&((net)->ref_count), 1); \
56 | memset(_ctl, 0, sizeof(struct sctp_queued_to_read)); \
57 | (_ctl)->sinfo_stream = sid; \
58 | TAILQ_INIT(&_ctl->reasm); \
59 | (_ctl)->top_fsn = tfsn; \
60 | (_ctl)->mid = mid; \
61 | (_ctl)->sinfo_flags = (flags << 8); \
62 | (_ctl)->sinfo_ppid = ppid; \
63 | (_ctl)->sinfo_context = context; \
64 | (_ctl)->fsn_included = 0xffffffff; \
65 | (_ctl)->sinfo_tsn = tsn; \
66 | (_ctl)->sinfo_cumtsn = tsn; \
67 | (_ctl)->sinfo_assoc_id = sctp_get_associd((in_it)); \
68 | (_ctl)->whoFrom = net; \
69 | (_ctl)->data = dm; \
70 | (_ctl)->stcb = (in_it); \
71 | (_ctl)->port_from = (in_it)->rport; \
72 | if ((in_it)->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) { \
73 | (_ctl)->do_not_ref_stcb = 1; \
74 | }\
75 | } \
76 | } while (0)
77 |
78 | struct mbuf *
79 | sctp_build_ctl_nchunk(struct sctp_inpcb *inp,
80 | struct sctp_sndrcvinfo *sinfo);
81 |
82 | void sctp_set_rwnd(struct sctp_tcb *, struct sctp_association *);
83 |
84 | uint32_t
85 | sctp_calc_rwnd(struct sctp_tcb *stcb, struct sctp_association *asoc);
86 |
87 | void
88 | sctp_express_handle_sack(struct sctp_tcb *stcb, uint32_t cumack,
89 | uint32_t rwnd, int *abort_now, int ecne_seen);
90 |
91 | void
92 | sctp_handle_sack(struct mbuf *m, int offset_seg, int offset_dup,
93 | struct sctp_tcb *stcb,
94 | uint16_t num_seg, uint16_t num_nr_seg, uint16_t num_dup,
95 | int *abort_now, uint8_t flags,
96 | uint32_t cum_ack, uint32_t rwnd, int ecne_seen);
97 |
98 | /* draft-ietf-tsvwg-usctp */
99 | void
100 | sctp_handle_forward_tsn(struct sctp_tcb *,
101 | struct sctp_forward_tsn_chunk *, int *, struct mbuf *, int);
102 |
103 | struct sctp_tmit_chunk *
104 | sctp_try_advance_peer_ack_point(struct sctp_tcb *, struct sctp_association *);
105 |
106 | void sctp_service_queues(struct sctp_tcb *, struct sctp_association *);
107 |
108 | void
109 | sctp_update_acked(struct sctp_tcb *, struct sctp_shutdown_chunk *, int *);
110 |
111 | int
112 | sctp_process_data(struct mbuf **, int, int *, int,
113 | struct sctp_inpcb *, struct sctp_tcb *,
114 | struct sctp_nets *, uint32_t *);
115 |
116 | void sctp_slide_mapping_arrays(struct sctp_tcb *stcb);
117 |
118 | void sctp_sack_check(struct sctp_tcb *, int);
119 |
120 | #endif
121 | #endif
122 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_input.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_INPUT_H_
41 | #define _NETINET_SCTP_INPUT_H_
42 |
43 | #if defined(_KERNEL) || defined(__Userspace__)
44 | void
45 | sctp_common_input_processing(struct mbuf **, int, int, int,
46 | struct sockaddr *, struct sockaddr *,
47 | struct sctphdr *, struct sctp_chunkhdr *,
48 | uint8_t,
49 | uint8_t,
50 | #if defined(__FreeBSD__) && !defined(__Userspace__)
51 | uint8_t, uint32_t, uint16_t,
52 | #endif
53 | uint32_t, uint16_t);
54 |
55 | struct sctp_stream_reset_request *
56 | sctp_find_stream_reset(struct sctp_tcb *stcb, uint32_t seq,
57 | struct sctp_tmit_chunk **bchk);
58 |
59 | void
60 | sctp_reset_in_stream(struct sctp_tcb *stcb, uint32_t number_entries,
61 | uint16_t *list);
62 |
63 | int sctp_is_there_unsent_data(struct sctp_tcb *stcb, int so_locked);
64 |
65 | #endif
66 | #endif
67 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_lock_userspace.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | * Copyright (c) 2008-2012, by Brad Penoff. All rights reserved.
8 | *
9 | * Redistribution and use in source and binary forms, with or without
10 | * modification, are permitted provided that the following conditions are met:
11 | *
12 | * a) Redistributions of source code must retain the above copyright notice,
13 | * this list of conditions and the following disclaimer.
14 | *
15 | * b) Redistributions in binary form must reproduce the above copyright
16 | * notice, this list of conditions and the following disclaimer in
17 | * the documentation and/or other materials provided with the distribution.
18 | *
19 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
20 | * contributors may be used to endorse or promote products derived
21 | * from this software without specific prior written permission.
22 | *
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33 | * THE POSSIBILITY OF SUCH DAMAGE.
34 | */
35 |
36 | #if defined(__FreeBSD__) && !defined(__Userspace__)
37 | #include
38 | __FBSDID("$FreeBSD$");
39 | #endif
40 |
41 | #ifndef _NETINET_SCTP_LOCK_EMPTY_H_
42 | #define _NETINET_SCTP_LOCK_EMPTY_H_
43 |
44 | /*
45 | * Empty Lock declarations for all other platforms. Pre-process away to
46 | * nothing.
47 | */
48 |
49 | /* __Userspace__ putting lock macros in same order as sctp_lock_bsd.h ...*/
50 |
51 | #define SCTP_IPI_COUNT_INIT()
52 |
53 | #define SCTP_STATLOG_INIT_LOCK()
54 | #define SCTP_STATLOG_LOCK()
55 | #define SCTP_STATLOG_UNLOCK()
56 | #define SCTP_STATLOG_DESTROY()
57 |
58 | #define SCTP_INP_INFO_LOCK_DESTROY()
59 |
60 | #define SCTP_INP_INFO_LOCK_INIT()
61 | #define SCTP_INP_INFO_RLOCK()
62 | #define SCTP_INP_INFO_WLOCK()
63 | #define SCTP_INP_INFO_TRYLOCK() 1
64 | #define SCTP_INP_INFO_RUNLOCK()
65 | #define SCTP_INP_INFO_WUNLOCK()
66 | #define SCTP_INP_INFO_LOCK_ASSERT()
67 | #define SCTP_INP_INFO_RLOCK_ASSERT()
68 | #define SCTP_INP_INFO_WLOCK_ASSERT()
69 |
70 | #define SCTP_WQ_ADDR_INIT()
71 | #define SCTP_WQ_ADDR_DESTROY()
72 | #define SCTP_WQ_ADDR_LOCK()
73 | #define SCTP_WQ_ADDR_UNLOCK()
74 | #define SCTP_WQ_ADDR_LOCK_ASSERT()
75 |
76 | #define SCTP_IPI_ADDR_INIT()
77 | #define SCTP_IPI_ADDR_DESTROY()
78 | #define SCTP_IPI_ADDR_RLOCK()
79 | #define SCTP_IPI_ADDR_WLOCK()
80 | #define SCTP_IPI_ADDR_RUNLOCK()
81 | #define SCTP_IPI_ADDR_WUNLOCK()
82 | #define SCTP_IPI_ADDR_LOCK_ASSERT()
83 | #define SCTP_IPI_ADDR_WLOCK_ASSERT()
84 |
85 | #define SCTP_IPI_ITERATOR_WQ_INIT()
86 | #define SCTP_IPI_ITERATOR_WQ_DESTROY()
87 | #define SCTP_IPI_ITERATOR_WQ_LOCK()
88 | #define SCTP_IPI_ITERATOR_WQ_UNLOCK()
89 |
90 | #define SCTP_IP_PKTLOG_INIT()
91 | #define SCTP_IP_PKTLOG_LOCK()
92 | #define SCTP_IP_PKTLOG_UNLOCK()
93 | #define SCTP_IP_PKTLOG_DESTROY()
94 |
95 | #define SCTP_INP_READ_INIT(_inp)
96 | #define SCTP_INP_READ_DESTROY(_inp)
97 | #define SCTP_INP_READ_LOCK(_inp)
98 | #define SCTP_INP_READ_UNLOCK(_inp)
99 |
100 | #define SCTP_INP_LOCK_INIT(_inp)
101 | #define SCTP_ASOC_CREATE_LOCK_INIT(_inp)
102 | #define SCTP_INP_LOCK_DESTROY(_inp)
103 | #define SCTP_ASOC_CREATE_LOCK_DESTROY(_inp)
104 |
105 | #define SCTP_INP_RLOCK(_inp)
106 | #define SCTP_INP_WLOCK(_inp)
107 | #define SCTP_INP_RLOCK_ASSERT(_inp)
108 | #define SCTP_INP_WLOCK_ASSERT(_inp)
109 |
110 | #define SCTP_INP_LOCK_CONTENDED(_inp) (0) /* Don't know if this is possible */
111 |
112 | #define SCTP_INP_READ_CONTENDED(_inp) (0) /* Don't know if this is possible */
113 |
114 | #define SCTP_ASOC_CREATE_LOCK_CONTENDED(_inp) (0) /* Don't know if this is possible */
115 |
116 |
117 | #define SCTP_INP_INCR_REF(_inp)
118 | #define SCTP_INP_DECR_REF(_inp)
119 |
120 | #define SCTP_ASOC_CREATE_LOCK(_inp)
121 |
122 | #define SCTP_INP_RUNLOCK(_inp)
123 | #define SCTP_INP_WUNLOCK(_inp)
124 | #define SCTP_ASOC_CREATE_UNLOCK(_inp)
125 |
126 |
127 | #define SCTP_TCB_LOCK_INIT(_tcb)
128 | #define SCTP_TCB_LOCK_DESTROY(_tcb)
129 | #define SCTP_TCB_LOCK(_tcb)
130 | #define SCTP_TCB_TRYLOCK(_tcb) 1
131 | #define SCTP_TCB_UNLOCK(_tcb)
132 | #define SCTP_TCB_UNLOCK_IFOWNED(_tcb)
133 | #define SCTP_TCB_LOCK_ASSERT(_tcb)
134 |
135 |
136 |
137 | #define SCTP_ITERATOR_LOCK_INIT()
138 | #define SCTP_ITERATOR_LOCK()
139 | #define SCTP_ITERATOR_UNLOCK()
140 | #define SCTP_ITERATOR_LOCK_DESTROY()
141 |
142 |
143 |
144 | #define SCTP_INCR_EP_COUNT() \
145 | do { \
146 | sctppcbinfo.ipi_count_ep++; \
147 | } while (0)
148 |
149 | #define SCTP_DECR_EP_COUNT() \
150 | do { \
151 | sctppcbinfo.ipi_count_ep--; \
152 | } while (0)
153 |
154 | #define SCTP_INCR_ASOC_COUNT() \
155 | do { \
156 | sctppcbinfo.ipi_count_asoc++; \
157 | } while (0)
158 |
159 | #define SCTP_DECR_ASOC_COUNT() \
160 | do { \
161 | sctppcbinfo.ipi_count_asoc--; \
162 | } while (0)
163 |
164 | #define SCTP_INCR_LADDR_COUNT() \
165 | do { \
166 | sctppcbinfo.ipi_count_laddr++; \
167 | } while (0)
168 |
169 | #define SCTP_DECR_LADDR_COUNT() \
170 | do { \
171 | sctppcbinfo.ipi_count_laddr--; \
172 | } while (0)
173 |
174 | #define SCTP_INCR_RADDR_COUNT() \
175 | do { \
176 | sctppcbinfo.ipi_count_raddr++; \
177 | } while (0)
178 |
179 | #define SCTP_DECR_RADDR_COUNT() \
180 | do { \
181 | sctppcbinfo.ipi_count_raddr--; \
182 | } while (0)
183 |
184 | #define SCTP_INCR_CHK_COUNT() \
185 | do { \
186 | sctppcbinfo.ipi_count_chunk++; \
187 | } while (0)
188 |
189 | #define SCTP_DECR_CHK_COUNT() \
190 | do { \
191 | sctppcbinfo.ipi_count_chunk--; \
192 | } while (0)
193 |
194 | #define SCTP_INCR_READQ_COUNT() \
195 | do { \
196 | sctppcbinfo.ipi_count_readq++; \
197 | } while (0)
198 |
199 | #define SCTP_DECR_READQ_COUNT() \
200 | do { \
201 | sctppcbinfo.ipi_count_readq--; \
202 | } while (0)
203 |
204 | #define SCTP_INCR_STRMOQ_COUNT() \
205 | do { \
206 | sctppcbinfo.ipi_count_strmoq++; \
207 | } while (0)
208 |
209 | #define SCTP_DECR_STRMOQ_COUNT() \
210 | do { \
211 | sctppcbinfo.ipi_count_strmoq--; \
212 | } while (0)
213 |
214 |
215 | /* these were in sctp_lock_empty.h but aren't in sctp_lock_bsd.h ... */
216 | #if 0
217 | #define SCTP_IPI_ADDR_LOCK()
218 | #define SCTP_IPI_ADDR_UNLOCK()
219 | #endif
220 |
221 |
222 | /* These were in sctp_lock_empty.h because they were commented out within
223 | * within user_include/user_socketvar.h . If they are NOT commented out
224 | * in user_socketvar.h (because that seems the more natural place for them
225 | * to live), then change this "if" to 0. Keep the "if" as 1 if these ARE
226 | * indeed commented out in user_socketvar.h .
227 | *
228 | * This modularity is kept so this file can easily be chosen as an alternative
229 | * to SCTP_PROCESS_LEVEL_LOCKS. If one defines SCTP_PROCESS_LEVEL_LOCKS in
230 | * user_include/opt_sctp.h, then the file sctp_process_lock.h (which we didn't
231 | * implement) is used, and that declares these locks already (so using
232 | * SCTP_PROCESS_LEVEL_LOCKS *requires* that these defintions be commented out
233 | * in user_socketvar.h).
234 | */
235 | #if 1
236 | #define SOCK_LOCK(_so)
237 | #define SOCK_UNLOCK(_so)
238 | #define SOCKBUF_LOCK(_so_buf)
239 | #define SOCKBUF_UNLOCK(_so_buf)
240 | #define SOCKBUF_LOCK_ASSERT(_so_buf)
241 | #endif
242 |
243 | #endif
244 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_os.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2006-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_OS_H_
41 | #define _NETINET_SCTP_OS_H_
42 |
43 | /*
44 | * General kernel memory allocation:
45 | * SCTP_MALLOC(element, type, size, name)
46 | * SCTP_FREE(element)
47 | * Kernel memory allocation for "soname"- memory must be zeroed.
48 | * SCTP_MALLOC_SONAME(name, type, size)
49 | * SCTP_FREE_SONAME(name)
50 | */
51 |
52 | /*
53 | * Zone(pool) allocation routines: MUST be defined for each OS.
54 | * zone = zone/pool pointer.
55 | * name = string name of the zone/pool.
56 | * size = size of each zone/pool element.
57 | * number = number of elements in zone/pool.
58 | * type = structure type to allocate
59 | *
60 | * sctp_zone_t
61 | * SCTP_ZONE_INIT(zone, name, size, number)
62 | * SCTP_ZONE_GET(zone, type)
63 | * SCTP_ZONE_FREE(zone, element)
64 | * SCTP_ZONE_DESTROY(zone)
65 | */
66 |
67 | #if defined(__FreeBSD__) && !defined(__Userspace__)
68 | #include
69 | #else
70 | #define MODULE_GLOBAL(_B) (_B)
71 | #endif
72 | #if defined(__Userspace__)
73 | #include
74 | #endif
75 | #if defined(__APPLE__) && !defined(__Userspace__)
76 | #include
77 | #endif
78 | #if defined(_WIN32) && !defined(__Userspace__)
79 | #include
80 | #endif
81 |
82 | /* All os's must implement this address gatherer. If
83 | * no VRF's exist, then vrf 0 is the only one and all
84 | * addresses and ifn's live here.
85 | */
86 | #define SCTP_DEFAULT_VRF 0
87 | void sctp_init_vrf_list(int vrfid);
88 |
89 | #endif
90 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_output.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_OUTPUT_H_
41 | #define _NETINET_SCTP_OUTPUT_H_
42 |
43 | #include
44 |
45 | #if defined(_KERNEL) || defined(__Userspace__)
46 |
47 | struct mbuf *
48 | sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp,
49 | struct sctp_tcb *stcb,
50 | struct sctp_scoping *scope,
51 | struct mbuf *m_at,
52 | int cnt_inits_to,
53 | uint16_t *padding_len, uint16_t *chunk_len);
54 |
55 | int sctp_is_addr_restricted(struct sctp_tcb *, struct sctp_ifa *);
56 |
57 | int
58 | sctp_is_address_in_scope(struct sctp_ifa *ifa,
59 | struct sctp_scoping *scope,
60 | int do_update);
61 |
62 | int
63 | sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa);
64 |
65 | struct sctp_ifa *
66 | sctp_source_address_selection(struct sctp_inpcb *inp,
67 | struct sctp_tcb *stcb,
68 | sctp_route_t *ro, struct sctp_nets *net,
69 | int non_asoc_addr_ok, uint32_t vrf_id);
70 |
71 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
72 | int sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro);
73 |
74 | int sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro);
75 | #endif
76 |
77 | void sctp_send_initiate(struct sctp_inpcb *, struct sctp_tcb *, int);
78 |
79 | void
80 | sctp_send_initiate_ack(struct sctp_inpcb *, struct sctp_tcb *,
81 | struct sctp_nets *, struct mbuf *,
82 | int, int,
83 | struct sockaddr *, struct sockaddr *,
84 | struct sctphdr *, struct sctp_init_chunk *,
85 | #if defined(__FreeBSD__) && !defined(__Userspace__)
86 | uint8_t, uint32_t,
87 | #endif
88 | uint32_t, uint16_t);
89 |
90 | struct mbuf *
91 | sctp_arethere_unrecognized_parameters(struct mbuf *, int, int *,
92 | struct sctp_chunkhdr *, int *, int *);
93 | void sctp_queue_op_err(struct sctp_tcb *, struct mbuf *);
94 |
95 | int
96 | sctp_send_cookie_echo(struct mbuf *, int, int, struct sctp_tcb *,
97 | struct sctp_nets *);
98 |
99 | void sctp_send_cookie_ack(struct sctp_tcb *);
100 |
101 | void
102 | sctp_send_heartbeat_ack(struct sctp_tcb *, struct mbuf *, int, int,
103 | struct sctp_nets *);
104 |
105 | void
106 | sctp_remove_from_wheel(struct sctp_tcb *stcb,
107 | struct sctp_association *asoc,
108 | struct sctp_stream_out *strq, int holds_lock);
109 |
110 | void sctp_send_shutdown(struct sctp_tcb *, struct sctp_nets *);
111 |
112 | void sctp_send_shutdown_ack(struct sctp_tcb *, struct sctp_nets *);
113 |
114 | void sctp_send_shutdown_complete(struct sctp_tcb *, struct sctp_nets *, int);
115 |
116 | void sctp_send_shutdown_complete2(struct sockaddr *, struct sockaddr *,
117 | struct sctphdr *,
118 | #if defined(__FreeBSD__) && !defined(__Userspace__)
119 | uint8_t, uint32_t, uint16_t,
120 | #endif
121 | uint32_t, uint16_t);
122 |
123 | void sctp_send_asconf(struct sctp_tcb *, struct sctp_nets *, int addr_locked);
124 |
125 | void sctp_send_asconf_ack(struct sctp_tcb *);
126 |
127 | uint32_t sctp_get_frag_point(struct sctp_tcb *);
128 |
129 | void sctp_toss_old_cookies(struct sctp_tcb *, struct sctp_association *);
130 |
131 | void sctp_toss_old_asconf(struct sctp_tcb *);
132 |
133 | void sctp_fix_ecn_echo(struct sctp_association *);
134 |
135 | void sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net);
136 |
137 | #define SCTP_DATA_CHUNK_OVERHEAD(stcb) ((stcb)->asoc.idata_supported ? \
138 | sizeof(struct sctp_idata_chunk) : \
139 | sizeof(struct sctp_data_chunk))
140 |
141 | #if defined(__FreeBSD__) && !defined(__Userspace__)
142 | int
143 | sctp_output(struct sctp_inpcb *, struct mbuf *, struct sockaddr *,
144 | struct mbuf *, struct thread *, int);
145 | #elif defined(_WIN32) && !defined(__Userspace__)
146 | sctp_output(struct sctp_inpcb *, struct mbuf *, struct sockaddr *,
147 | struct mbuf *, PKTHREAD, int);
148 | #else
149 | #if defined(__Userspace__)
150 | /* sctp_output is called bu sctp_sendm. Not using sctp_sendm for __Userspace__ */
151 | #endif
152 | int
153 | sctp_output(struct sctp_inpcb *,
154 | struct mbuf *,
155 | struct sockaddr *,
156 | struct mbuf *,
157 | struct proc *, int);
158 | #endif
159 |
160 | void sctp_chunk_output(struct sctp_inpcb *, struct sctp_tcb *, int, int);
161 |
162 | void sctp_send_abort_tcb(struct sctp_tcb *, struct mbuf *, int);
163 |
164 | void send_forward_tsn(struct sctp_tcb *, struct sctp_association *);
165 |
166 | void sctp_send_sack(struct sctp_tcb *, int);
167 |
168 | void sctp_send_hb(struct sctp_tcb *, struct sctp_nets *, int);
169 |
170 | void sctp_send_ecn_echo(struct sctp_tcb *, struct sctp_nets *, uint32_t);
171 |
172 | void
173 | sctp_send_packet_dropped(struct sctp_tcb *, struct sctp_nets *, struct mbuf *,
174 | int, int, int);
175 |
176 | void sctp_send_cwr(struct sctp_tcb *, struct sctp_nets *, uint32_t, uint8_t);
177 |
178 | void
179 | sctp_add_stream_reset_result(struct sctp_tmit_chunk *, uint32_t, uint32_t);
180 |
181 | void
182 | sctp_send_deferred_reset_response(struct sctp_tcb *,
183 | struct sctp_stream_reset_list *,
184 | int);
185 |
186 | void
187 | sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *,
188 | uint32_t, uint32_t, uint32_t, uint32_t);
189 | int
190 | sctp_send_stream_reset_out_if_possible(struct sctp_tcb *, int);
191 |
192 | int
193 | sctp_send_str_reset_req(struct sctp_tcb *, uint16_t , uint16_t *,
194 | uint8_t, uint8_t, uint8_t, uint16_t, uint16_t, uint8_t);
195 |
196 | void
197 | sctp_send_abort(struct mbuf *, int, struct sockaddr *, struct sockaddr *,
198 | struct sctphdr *, uint32_t, struct mbuf *,
199 | #if defined(__FreeBSD__) && !defined(__Userspace__)
200 | uint8_t, uint32_t, uint16_t,
201 | #endif
202 | uint32_t, uint16_t);
203 |
204 | void
205 | sctp_send_operr_to(struct sockaddr *, struct sockaddr *,
206 | struct sctphdr *, uint32_t, struct mbuf *,
207 | #if defined(__FreeBSD__) && !defined(__Userspace__)
208 | uint8_t, uint32_t, uint16_t,
209 | #endif
210 | uint32_t, uint16_t);
211 |
212 | #endif /* _KERNEL || __Userspace__ */
213 |
214 | #if defined(_KERNEL) || defined(__Userspace__)
215 | int
216 | sctp_sosend(struct socket *so,
217 | struct sockaddr *addr,
218 | struct uio *uio,
219 | struct mbuf *top,
220 | struct mbuf *control,
221 | #if defined(__APPLE__) && !defined(__Userspace__)
222 | int flags
223 | #else
224 | int flags,
225 | #if defined(__FreeBSD__) && !defined(__Userspace__)
226 | struct thread *p
227 | #elif defined(_WIN32) && !defined(__Userspace__)
228 | PKTHREAD p
229 | #else
230 | #if defined(__Userspace__)
231 | /* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */
232 | #endif
233 | struct proc *p
234 | #endif
235 | #endif
236 | );
237 |
238 | #endif
239 | #endif
240 |
241 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_peeloff.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_PEELOFF_H_
41 | #define _NETINET_SCTP_PEELOFF_H_
42 | #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
43 | /* socket option peeloff */
44 | struct sctp_peeloff_opt {
45 | #if !(defined(_WIN32) && !defined(__Userspace__))
46 | int s;
47 | #else
48 | HANDLE s;
49 | #endif
50 | sctp_assoc_t assoc_id;
51 | #if !(defined(_WIN32) && !defined(__Userspace__))
52 | int new_sd;
53 | #else
54 | HANDLE new_sd;
55 | #endif
56 | };
57 | #endif /* HAVE_SCTP_PEELOFF_SOCKOPT */
58 | #if defined(_KERNEL)
59 | int sctp_can_peel_off(struct socket *, sctp_assoc_t);
60 | int sctp_do_peeloff(struct socket *, struct socket *, sctp_assoc_t);
61 | #if defined(HAVE_SCTP_PEELOFF_SOCKOPT)
62 | struct socket *sctp_get_peeloff(struct socket *, sctp_assoc_t, int *);
63 | int sctp_peeloff_option(struct proc *p, struct sctp_peeloff_opt *peeloff);
64 | #endif /* HAVE_SCTP_PEELOFF_SOCKOPT */
65 | #endif /* _KERNEL */
66 | #if defined(__Userspace__)
67 | int sctp_can_peel_off(struct socket *, sctp_assoc_t);
68 | int sctp_do_peeloff(struct socket *, struct socket *, sctp_assoc_t);
69 | #endif /* __Userspace__ */
70 | #endif /* _NETINET_SCTP_PEELOFF_H_ */
71 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_sha1.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 |
41 | #ifndef __NETINET_SCTP_SHA1_H__
42 | #define __NETINET_SCTP_SHA1_H__
43 |
44 | #include
45 | #if defined(SCTP_USE_NSS_SHA1)
46 | #include
47 | #elif defined(SCTP_USE_OPENSSL_SHA1)
48 | #include
49 | #elif defined(SCTP_USE_MBEDTLS_SHA1)
50 | #include
51 | #endif
52 |
53 | struct sctp_sha1_context {
54 | #if defined(SCTP_USE_NSS_SHA1)
55 | struct PK11Context *pk11_ctx;
56 | #elif defined(SCTP_USE_OPENSSL_SHA1)
57 | SHA_CTX sha_ctx;
58 | #elif defined(SCTP_USE_MBEDTLS_SHA1)
59 | mbedtls_sha1_context sha1_ctx;
60 | #else
61 | unsigned int A;
62 | unsigned int B;
63 | unsigned int C;
64 | unsigned int D;
65 | unsigned int E;
66 | unsigned int H0;
67 | unsigned int H1;
68 | unsigned int H2;
69 | unsigned int H3;
70 | unsigned int H4;
71 | unsigned int words[80];
72 | unsigned int TEMP;
73 | /* block I am collecting to process */
74 | char sha_block[64];
75 | /* collected so far */
76 | int how_many_in_block;
77 | unsigned int running_total;
78 | #endif
79 | };
80 |
81 | #if (defined(__APPLE__) && !defined(__Userspace__) && defined(KERNEL))
82 | #ifndef _KERNEL
83 | #define _KERNEL
84 | #endif
85 | #endif
86 |
87 | #if defined(_KERNEL) || defined(__Userspace__)
88 |
89 | void sctp_sha1_init(struct sctp_sha1_context *);
90 | void sctp_sha1_update(struct sctp_sha1_context *, const unsigned char *, unsigned int);
91 | void sctp_sha1_final(unsigned char *, struct sctp_sha1_context *);
92 |
93 | #endif
94 | #endif
95 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet/sctp_timer.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET_SCTP_TIMER_H_
41 | #define _NETINET_SCTP_TIMER_H_
42 |
43 | #if defined(_KERNEL) || defined(__Userspace__)
44 |
45 | #define SCTP_RTT_SHIFT 3
46 | #define SCTP_RTT_VAR_SHIFT 2
47 |
48 | struct sctp_nets *
49 | sctp_find_alternate_net(struct sctp_tcb *, struct sctp_nets *, int);
50 |
51 | int
52 | sctp_t3rxt_timer(struct sctp_inpcb *, struct sctp_tcb *,
53 | struct sctp_nets *);
54 |
55 | int
56 | sctp_t1init_timer(struct sctp_inpcb *, struct sctp_tcb *,
57 | struct sctp_nets *);
58 |
59 | int
60 | sctp_shutdown_timer(struct sctp_inpcb *, struct sctp_tcb *,
61 | struct sctp_nets *);
62 |
63 | int
64 | sctp_heartbeat_timer(struct sctp_inpcb *, struct sctp_tcb *,
65 | struct sctp_nets *);
66 |
67 | int
68 | sctp_cookie_timer(struct sctp_inpcb *, struct sctp_tcb *,
69 | struct sctp_nets *);
70 |
71 | void
72 | sctp_pathmtu_timer(struct sctp_inpcb *, struct sctp_tcb *,
73 | struct sctp_nets *);
74 |
75 | int
76 | sctp_shutdownack_timer(struct sctp_inpcb *, struct sctp_tcb *,
77 | struct sctp_nets *);
78 | int
79 | sctp_strreset_timer(struct sctp_inpcb *, struct sctp_tcb *);
80 |
81 | int
82 | sctp_asconf_timer(struct sctp_inpcb *, struct sctp_tcb *,
83 | struct sctp_nets *);
84 |
85 | void
86 | sctp_delete_prim_timer(struct sctp_inpcb *, struct sctp_tcb *);
87 |
88 | void
89 | sctp_autoclose_timer(struct sctp_inpcb *, struct sctp_tcb *);
90 |
91 | void sctp_audit_retranmission_queue(struct sctp_association *);
92 |
93 | void sctp_iterator_timer(struct sctp_iterator *it);
94 |
95 | #if defined(__APPLE__) && !defined(__Userspace__)
96 | #if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD) || defined(APPLE_LION) || defined(APPLE_MOUNTAINLION)
97 | void sctp_slowtimo(void);
98 | #else
99 | void sctp_gc(struct inpcbinfo *);
100 | #endif
101 | #endif
102 | #endif
103 | #endif
104 |
--------------------------------------------------------------------------------
/src/usrsctp/netinet6/sctp6_var.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * SPDX-License-Identifier: BSD-3-Clause
3 | *
4 | * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5 | * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 | * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 | *
8 | * Redistribution and use in source and binary forms, with or without
9 | * modification, are permitted provided that the following conditions are met:
10 | *
11 | * a) Redistributions of source code must retain the above copyright notice,
12 | * this list of conditions and the following disclaimer.
13 | *
14 | * b) Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in
16 | * the documentation and/or other materials provided with the distribution.
17 | *
18 | * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 | * contributors may be used to endorse or promote products derived
20 | * from this software without specific prior written permission.
21 | *
22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 | * THE POSSIBILITY OF SUCH DAMAGE.
33 | */
34 |
35 | #if defined(__FreeBSD__) && !defined(__Userspace__)
36 | #include
37 | __FBSDID("$FreeBSD$");
38 | #endif
39 |
40 | #ifndef _NETINET6_SCTP6_VAR_H_
41 | #define _NETINET6_SCTP6_VAR_H_
42 |
43 | #if defined(__Userspace__)
44 | #ifdef INET
45 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *);
46 | extern void in6_sin6_2_sin_in_sock(struct sockaddr *);
47 | extern void in6_sin_2_v4mapsin6(const struct sockaddr_in *, struct sockaddr_in6 *);
48 | #endif
49 | #endif
50 | #if defined(_KERNEL)
51 |
52 | #if !defined(__Userspace__)
53 | SYSCTL_DECL(_net_inet6_sctp6);
54 | extern struct pr_usrreqs sctp6_usrreqs;
55 | #else
56 | int sctp6_usrreq(struct socket *, int, struct mbuf *, struct mbuf *, struct mbuf *);
57 | #endif
58 |
59 | #if defined(__APPLE__) && !defined(__Userspace__)
60 | int sctp6_input(struct mbuf **, int *);
61 | int sctp6_input_with_port(struct mbuf **, int *, uint16_t);
62 | #else
63 | int sctp6_input(struct mbuf **, int *, int);
64 | int sctp6_input_with_port(struct mbuf **, int *, uint16_t);
65 | #endif
66 | int sctp6_output(struct sctp_inpcb *, struct mbuf *, struct sockaddr *,
67 | struct mbuf *, struct proc *);
68 | #if defined(__APPLE__) && !defined(__Userspace__) && !defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION) && !defined(APPLE_ELCAPITAN)
69 | void sctp6_ctlinput(int, struct sockaddr *, void *, struct ifnet * SCTP_UNUSED);
70 | #else
71 | void sctp6_ctlinput(int, struct sockaddr *, void *);
72 | #endif
73 | #if !((defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__))
74 | extern void in6_sin_2_v4mapsin6(struct sockaddr_in *, struct sockaddr_in6 *);
75 | extern void in6_sin6_2_sin(struct sockaddr_in *, struct sockaddr_in6 *);
76 | extern void in6_sin6_2_sin_in_sock(struct sockaddr *);
77 | #endif
78 | void sctp6_notify(struct sctp_inpcb *, struct sctp_tcb *, struct sctp_nets *,
79 | uint8_t, uint8_t, uint32_t);
80 | #endif
81 | #endif
82 |
--------------------------------------------------------------------------------
/src/usrsctp/user_environment.c:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 2009-2010 Brad Penoff
3 | * Copyright (c) 2009-2010 Humaira Kamal
4 | * Copyright (c) 2011-2012 Irene Ruengeler
5 | * Copyright (c) 2011-2012 Michael Tuexen
6 | *
7 | * All rights reserved.
8 | *
9 | * Redistribution and use in source and binary forms, with or without
10 | * modification, are permitted provided that the following conditions
11 | * are met:
12 | * 1. Redistributions of source code must retain the above copyright
13 | * notice, this list of conditions and the following disclaimer.
14 | * 2. Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in the
16 | * documentation and/or other materials provided with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 | * SUCH DAMAGE.
29 | */
30 |
31 | /* __Userspace__ */
32 |
33 | #if defined(_WIN32)
34 | #if !defined(_CRT_RAND_S) && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
35 | #define _CRT_RAND_S
36 | #endif
37 | #else
38 | #include
39 | #include
40 | #endif
41 | #ifdef INVARIANTS
42 | #include
43 | #endif
44 | #include
45 | #include
46 | /* #include defines MIN */
47 | #if !defined(MIN)
48 | #define MIN(arg1,arg2) ((arg1) < (arg2) ? (arg1) : (arg2))
49 | #endif
50 |
51 | #define uHZ 1000
52 |
53 | /* See user_include/user_environment.h for comments about these variables */
54 | int maxsockets = 25600;
55 | int hz = uHZ;
56 | int ip_defttl = 64;
57 | int ipport_firstauto = 49152, ipport_lastauto = 65535;
58 | int nmbclusters = 65536;
59 |
60 | /* Source ip_output.c. extern'd in ip_var.h */
61 | u_short ip_id = 0; /*__Userspace__ TODO Should it be initialized to zero? */
62 |
63 | /* used in user_include/user_atomic.h in order to make the operations
64 | * defined there truly atomic
65 | */
66 | userland_mutex_t atomic_mtx;
67 |
68 | /* If the entropy device is not loaded, make a token effort to
69 | * provide _some_ kind of randomness. This should only be used
70 | * inside other RNG's, like arc4random(9).
71 | */
72 | #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
73 | #include
74 |
75 | void
76 | init_random(void)
77 | {
78 | return;
79 | }
80 |
81 | void
82 | read_random(void *buf, size_t size)
83 | {
84 | memset(buf, 'A', size);
85 | return;
86 | }
87 |
88 | void
89 | finish_random(void)
90 | {
91 | return;
92 | }
93 | /* This define can be used to optionally use OpenSSL's random number utility,
94 | * which is capable of bypassing the chromium sandbox which normally would
95 | * prevent opening files, including /dev/urandom.
96 | */
97 | #elif defined(SCTP_USE_OPENSSL_RAND)
98 | #include
99 |
100 | /* Requiring BoringSSL because it guarantees that RAND_bytes will succeed. */
101 | #ifndef OPENSSL_IS_BORINGSSL
102 | #error Only BoringSSL is supported with SCTP_USE_OPENSSL_RAND.
103 | #endif
104 |
105 | void
106 | init_random(void)
107 | {
108 | return;
109 | }
110 |
111 | void
112 | read_random(void *buf, size_t size)
113 | {
114 | RAND_bytes((uint8_t *)buf, size);
115 | return;
116 | }
117 |
118 | void
119 | finish_random(void)
120 | {
121 | return;
122 | }
123 | #elif defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(__Bitrig__)
124 | #include
125 |
126 | void
127 | init_random(void)
128 | {
129 | return;
130 | }
131 |
132 | void
133 | read_random(void *buf, size_t size)
134 | {
135 | arc4random_buf(buf, size);
136 | return;
137 | }
138 |
139 | void
140 | finish_random(void)
141 | {
142 | return;
143 | }
144 | #elif defined(_WIN32)
145 | #include
146 |
147 | void
148 | init_random(void)
149 | {
150 | return;
151 | }
152 |
153 | void
154 | read_random(void *buf, size_t size)
155 | {
156 | unsigned int randval;
157 | size_t position, remaining;
158 |
159 | position = 0;
160 | while (position < size) {
161 | if (rand_s(&randval) == 0) {
162 | remaining = MIN(size - position, sizeof(unsigned int));
163 | memcpy((char *)buf + position, &randval, remaining);
164 | position += sizeof(unsigned int);
165 | }
166 | }
167 | return;
168 | }
169 |
170 | void
171 | finish_random(void)
172 | {
173 | return;
174 | }
175 | #elif (defined(__ANDROID__) && (__ANDROID_API__ < 28)) || defined(__QNX__) || defined(__EMSCRIPTEN__)
176 | #include
177 |
178 | static int fd = -1;
179 |
180 | void
181 | init_random(void)
182 | {
183 | fd = open("/dev/urandom", O_RDONLY);
184 | return;
185 | }
186 |
187 | void
188 | read_random(void *buf, size_t size)
189 | {
190 | size_t position;
191 | ssize_t n;
192 |
193 | position = 0;
194 | while (position < size) {
195 | n = read(fd, (char *)buf + position, size - position);
196 | if (n > 0) {
197 | position += n;
198 | }
199 | }
200 | return;
201 | }
202 |
203 | void
204 | finish_random(void)
205 | {
206 | close(fd);
207 | return;
208 | }
209 | #elif defined(__ANDROID__) && (__ANDROID_API__ >= 28)
210 | #include
211 |
212 | void
213 | init_random(void)
214 | {
215 | return;
216 | }
217 |
218 | void
219 | read_random(void *buf, size_t size)
220 | {
221 | size_t position;
222 | ssize_t n;
223 |
224 | position = 0;
225 | while (position < size) {
226 | n = getrandom((char *)buf + position, size - position, 0);
227 | if (n > 0) {
228 | position += n;
229 | }
230 | }
231 | return;
232 | }
233 |
234 | void
235 | finish_random(void)
236 | {
237 | return;
238 | }
239 | #elif defined(__linux__)
240 | #include
241 | #include
242 | #include
243 |
244 | #if defined(__has_feature)
245 | #if __has_feature(memory_sanitizer)
246 | void __msan_unpoison(void *, size_t);
247 | #endif
248 | #endif
249 |
250 | #ifdef __NR_getrandom
251 | #if !defined(GRND_NONBLOCK)
252 | #define GRND_NONBLOCK 1
253 | #endif
254 | static int getrandom_available = 0;
255 | #endif
256 | static int fd = -1;
257 |
258 | void
259 | init_random(void)
260 | {
261 | #ifdef __NR_getrandom
262 | char dummy;
263 | ssize_t n = syscall(__NR_getrandom, &dummy, sizeof(dummy), GRND_NONBLOCK);
264 | if (n > 0 || errno == EINTR || errno == EAGAIN) {
265 | /* Either getrandom succeeded, was interrupted or is waiting for entropy;
266 | * all of which mean the syscall is available.
267 | */
268 | getrandom_available = 1;
269 | } else {
270 | #ifdef INVARIANTS
271 | if (errno != ENOSYS) {
272 | panic("getrandom syscall returned unexpected error: %d", errno);
273 | }
274 | #endif
275 | /* If the syscall isn't available, fall back to /dev/urandom. */
276 | #endif
277 | fd = open("/dev/urandom", O_RDONLY);
278 | #ifdef __NR_getrandom
279 | }
280 | #endif
281 | return;
282 | }
283 |
284 | void
285 | read_random(void *buf, size_t size)
286 | {
287 | size_t position;
288 | ssize_t n;
289 |
290 | position = 0;
291 | while (position < size) {
292 | #ifdef __NR_getrandom
293 | if (getrandom_available) {
294 | /* Using syscall directly because getrandom isn't present in glibc < 2.25.
295 | */
296 | n = syscall(__NR_getrandom, (char *)buf + position, size - position, 0);
297 | if (n > 0) {
298 | #if defined(__has_feature)
299 | #if __has_feature(memory_sanitizer)
300 | /* Need to do this because MSan doesn't realize that syscall has
301 | * initialized the output buffer.
302 | */
303 | __msan_unpoison(buf + position, n);
304 | #endif
305 | #endif
306 | position += n;
307 | } else if (errno != EINTR && errno != EAGAIN) {
308 | #ifdef INVARIANTS
309 | panic("getrandom syscall returned unexpected error: %d", errno);
310 | #endif
311 | }
312 | } else
313 | #endif /* __NR_getrandom */
314 | {
315 | n = read(fd, (char *)buf + position, size - position);
316 | if (n > 0) {
317 | position += n;
318 | }
319 | }
320 | }
321 | return;
322 | }
323 |
324 | void
325 | finish_random(void)
326 | {
327 | if (fd != -1) {
328 | close(fd);
329 | }
330 | return;
331 | }
332 | #elif defined(__Fuchsia__)
333 | #include
334 |
335 | void
336 | init_random(void)
337 | {
338 | return;
339 | }
340 |
341 | void
342 | read_random(void *buf, size_t size)
343 | {
344 | zx_cprng_draw(buf, size);
345 | return;
346 | }
347 |
348 | void
349 | finish_random(void)
350 | {
351 | return;
352 | }
353 | #elif defined(__native_client__)
354 | #include
355 |
356 | void
357 | init_random(void)
358 | {
359 | return;
360 | }
361 |
362 | void
363 | read_random(void *buf, size_t size)
364 | {
365 | size_t position;
366 | size_t n;
367 |
368 | position = 0;
369 | while (position < size) {
370 | if (nacl_secure_random((char *)buf + position, size - position, &n) == 0)
371 | position += n;
372 | }
373 | }
374 | return;
375 | }
376 |
377 | void
378 | finish_random(void)
379 | {
380 | return;
381 | }
382 | #else
383 | #error "Unknown platform. Please provide platform specific RNG."
384 | #endif
385 |
--------------------------------------------------------------------------------
/src/usrsctp/user_environment.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 2009-2010 Brad Penoff
3 | * Copyright (c) 2009-2010 Humaira Kamal
4 | * Copyright (c) 2011-2012 Irene Ruengeler
5 | * Copyright (c) 2011-2012 Michael Tuexen
6 | *
7 | * All rights reserved.
8 | *
9 | * Redistribution and use in source and binary forms, with or without
10 | * modification, are permitted provided that the following conditions
11 | * are met:
12 | * 1. Redistributions of source code must retain the above copyright
13 | * notice, this list of conditions and the following disclaimer.
14 | * 2. Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in the
16 | * documentation and/or other materials provided with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 | * SUCH DAMAGE.
29 | */
30 |
31 | #ifndef _USER_ENVIRONMENT_H_
32 | #define _USER_ENVIRONMENT_H_
33 | /* __Userspace__ */
34 | #include
35 |
36 | #ifdef __FreeBSD__
37 | #ifndef _SYS_MUTEX_H_
38 | #include
39 | #endif
40 | #endif
41 | #if defined(_WIN32)
42 | #include "netinet/sctp_os_userspace.h"
43 | #endif
44 |
45 | /* maxsockets is used in SCTP_ZONE_INIT call. It refers to
46 | * kern.ipc.maxsockets kernel environment variable.
47 | */
48 | extern int maxsockets;
49 |
50 | /* int hz; is declared in sys/kern/subr_param.c and refers to kernel timer frequency.
51 | * See http://ivoras.sharanet.org/freebsd/vmware.html for additional info about kern.hz
52 | * hz is initialized in void init_param1(void) in that file.
53 | */
54 | extern int hz;
55 |
56 |
57 | /* The following two ints define a range of available ephemeral ports. */
58 | extern int ipport_firstauto, ipport_lastauto;
59 |
60 | /* nmbclusters is used in sctp_usrreq.c (e.g., sctp_init). In the FreeBSD kernel,
61 | * this is 1024 + maxusers * 64.
62 | */
63 | extern int nmbclusters;
64 |
65 | #if !defined(_MSC_VER) && !defined(__MINGW32__)
66 | #define min(a,b) (((a)>(b))?(b):(a))
67 | #define max(a,b) (((a)>(b))?(a):(b))
68 | #endif
69 |
70 | void init_random(void);
71 | void read_random(void *, size_t);
72 | void finish_random(void);
73 |
74 | /* errno's may differ per OS. errno.h now included in sctp_os_userspace.h */
75 | /* Source: /usr/src/sys/sys/errno.h */
76 | /* #define ENOSPC 28 */ /* No space left on device */
77 | /* #define ENOBUFS 55 */ /* No buffer space available */
78 | /* #define ENOMEM 12 */ /* Cannot allocate memory */
79 | /* #define EACCES 13 */ /* Permission denied */
80 | /* #define EFAULT 14 */ /* Bad address */
81 | /* #define EHOSTDOWN 64 */ /* Host is down */
82 | /* #define EHOSTUNREACH 65 */ /* No route to host */
83 |
84 | /* Source ip_output.c. extern'd in ip_var.h */
85 | extern u_short ip_id;
86 |
87 | #if defined(__linux__)
88 | #define IPV6_VERSION 0x60
89 | #endif
90 |
91 | #if defined(INVARIANTS)
92 | #include
93 |
94 | #if defined(_WIN32)
95 | static inline void __declspec(noreturn)
96 | #else
97 | static inline void __attribute__((__noreturn__))
98 | #endif
99 | terminate_non_graceful(void) {
100 | abort();
101 | }
102 |
103 | #define panic(...) \
104 | do { \
105 | SCTP_PRINTF("%s(): ", __func__); \
106 | SCTP_PRINTF(__VA_ARGS__); \
107 | SCTP_PRINTF("\n"); \
108 | terminate_non_graceful(); \
109 | } while (0)
110 |
111 | #define KASSERT(cond, args) \
112 | do { \
113 | if (!(cond)) { \
114 | panic args ; \
115 | } \
116 | } while (0)
117 | #else
118 | #define KASSERT(cond, args)
119 | #endif
120 |
121 | /* necessary for sctp_pcb.c */
122 | extern int ip_defttl;
123 | #endif
124 |
--------------------------------------------------------------------------------
/src/usrsctp/user_ip6_var.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 | * All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | * 1. Redistributions of source code must retain the above copyright
9 | * notice, this list of conditions and the following disclaimer.
10 | * 2. Redistributions in binary form must reproduce the above copyright
11 | * notice, this list of conditions and the following disclaimer in the
12 | * documentation and/or other materials provided with the distribution.
13 | * 3. Neither the name of the project nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software
15 | * without specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 | * SUCH DAMAGE.
28 | *
29 | */
30 | /*-
31 | * Copyright (c) 1982, 1986, 1993
32 | * The Regents of the University of California. All rights reserved.
33 | *
34 | * Redistribution and use in source and binary forms, with or without
35 | * modification, are permitted provided that the following conditions
36 | * are met:
37 | * 1. Redistributions of source code must retain the above copyright
38 | * notice, this list of conditions and the following disclaimer.
39 | * 2. Redistributions in binary form must reproduce the above copyright
40 | * notice, this list of conditions and the following disclaimer in the
41 | * documentation and/or other materials provided with the distribution.
42 | * 3. Neither the name of the University nor the names of its contributors
43 | * may be used to endorse or promote products derived from this software
44 | * without specific prior written permission.
45 | *
46 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 | * SUCH DAMAGE.
57 | *
58 | */
59 |
60 | #ifndef _USER_IP6_VAR_H_
61 | #define _USER_IP6_VAR_H_
62 |
63 | #if defined(_WIN32)
64 | struct ip6_hdr {
65 | union {
66 | struct ip6_hdrctl {
67 | uint32_t ip6_un1_flow; /* 20 bits of flow-ID */
68 | uint16_t ip6_un1_plen; /* payload length */
69 | uint8_t ip6_un1_nxt; /* next header */
70 | uint8_t ip6_un1_hlim; /* hop limit */
71 | } ip6_un1;
72 | uint8_t ip6_un2_vfc; /* 4 bits version, top 4 bits class */
73 | } ip6_ctlun;
74 | struct in6_addr ip6_src; /* source address */
75 | struct in6_addr ip6_dst; /* destination address */
76 | };
77 | #define ip6_vfc ip6_ctlun.ip6_un2_vfc
78 | #define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
79 | #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
80 | #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
81 | #define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
82 | #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim
83 |
84 | #define IPV6_VERSION 0x60
85 | #endif
86 |
87 | #if defined(_WIN32)
88 | #define s6_addr16 u.Word
89 | #endif
90 | #if !defined(_WIN32) && !defined(__linux__) && !defined(__EMSCRIPTEN__)
91 | #define s6_addr8 __u6_addr.__u6_addr8
92 | #define s6_addr16 __u6_addr.__u6_addr16
93 | #define s6_addr32 __u6_addr.__u6_addr32
94 | #endif
95 |
96 | #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
97 | struct route_in6 {
98 | struct rtentry *ro_rt;
99 | struct llentry *ro_lle;
100 | struct in6_addr *ro_ia6;
101 | int ro_flags;
102 | struct sockaddr_in6 ro_dst;
103 | };
104 | #endif
105 | #define IP6_EXTHDR_GET(val, typ, m, off, len) \
106 | do { \
107 | struct mbuf *t; \
108 | int tmp; \
109 | if ((m)->m_len >= (off) + (len)) \
110 | (val) = (typ)(mtod((m), caddr_t) + (off)); \
111 | else { \
112 | t = m_pulldown((m), (off), (len), &tmp); \
113 | if (t) { \
114 | KASSERT(t->m_len >= tmp + (len), \
115 | ("m_pulldown malfunction")); \
116 | (val) = (typ)(mtod(t, caddr_t) + tmp); \
117 | } else { \
118 | (val) = (typ)NULL; \
119 | (m) = NULL; \
120 | } \
121 | } \
122 | } while (0)
123 |
124 | #endif /* !_USER_IP6_VAR_H_ */
125 |
--------------------------------------------------------------------------------
/src/usrsctp/user_ip_icmp.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 1982, 1986, 1993
3 | * The Regents of the University of California. All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | * 1. Redistributions of source code must retain the above copyright
9 | * notice, this list of conditions and the following disclaimer.
10 | * 2. Redistributions in binary form must reproduce the above copyright
11 | * notice, this list of conditions and the following disclaimer in the
12 | * documentation and/or other materials provided with the distribution.
13 | * 3. Neither the name of the University nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software
15 | * without specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 | * SUCH DAMAGE.
28 | *
29 | */
30 |
31 | #ifndef _USER_IP_ICMP_H_
32 | #define _USER_IP_ICMP_H_
33 |
34 | /*
35 | * Interface Control Message Protocol Definitions.
36 | * Per RFC 792, September 1981.
37 | */
38 |
39 | /*
40 | * Internal of an ICMP Router Advertisement
41 | */
42 | struct icmp_ra_addr {
43 | uint32_t ira_addr;
44 | uint32_t ira_preference;
45 | };
46 |
47 | /*
48 | * Structure of an icmp header.
49 | */
50 | struct icmphdr {
51 | u_char icmp_type; /* type of message, see below */
52 | u_char icmp_code; /* type sub code */
53 | u_short icmp_cksum; /* ones complement cksum of struct */
54 | };
55 |
56 | #if defined(_WIN32)
57 | #pragma pack (push, 1)
58 | struct icmp6_hdr {
59 | uint8_t icmp6_type;
60 | uint8_t icmp6_code;
61 | uint16_t icmp6_cksum;
62 | union {
63 | uint32_t icmp6_un_data32[1];
64 | uint16_t icmp6_un_data16[2];
65 | uint8_t icmp6_un_data8[4];
66 | } icmp6_dataun;
67 | };
68 | #pragma pack(pop)
69 |
70 | #define icmp6_data32 icmp6_dataun.icmp6_un_data32
71 | #define icmp6_mtu icmp6_data32[0]
72 | #endif
73 |
74 | /*
75 | * Structure of an icmp packet.
76 | *
77 | * XXX: should start with a struct icmphdr.
78 | */
79 | struct icmp {
80 | u_char icmp_type; /* type of message, see below */
81 | u_char icmp_code; /* type sub code */
82 | u_short icmp_cksum; /* ones complement cksum of struct */
83 | union {
84 | u_char ih_pptr; /* ICMP_PARAMPROB */
85 | struct in_addr ih_gwaddr; /* ICMP_REDIRECT */
86 | struct ih_idseq {
87 | uint16_t icd_id; /* network format */
88 | uint16_t icd_seq; /* network format */
89 | } ih_idseq;
90 | int ih_void;
91 |
92 | /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */
93 | struct ih_pmtu {
94 | uint16_t ipm_void; /* network format */
95 | uint16_t ipm_nextmtu; /* network format */
96 | } ih_pmtu;
97 |
98 | struct ih_rtradv {
99 | u_char irt_num_addrs;
100 | u_char irt_wpa;
101 | uint16_t irt_lifetime;
102 | } ih_rtradv;
103 | } icmp_hun;
104 | #define icmp_pptr icmp_hun.ih_pptr
105 | #define icmp_gwaddr icmp_hun.ih_gwaddr
106 | #define icmp_id icmp_hun.ih_idseq.icd_id
107 | #define icmp_seq icmp_hun.ih_idseq.icd_seq
108 | #define icmp_void icmp_hun.ih_void
109 | #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void
110 | #define icmp_nextmtu icmp_hun.ih_pmtu.ipm_nextmtu
111 | #define icmp_num_addrs icmp_hun.ih_rtradv.irt_num_addrs
112 | #define icmp_wpa icmp_hun.ih_rtradv.irt_wpa
113 | #define icmp_lifetime icmp_hun.ih_rtradv.irt_lifetime
114 | union {
115 | struct id_ts { /* ICMP Timestamp */
116 | /*
117 | * The next 3 fields are in network format,
118 | * milliseconds since 00:00 GMT
119 | */
120 | uint32_t its_otime; /* Originate */
121 | uint32_t its_rtime; /* Receive */
122 | uint32_t its_ttime; /* Transmit */
123 | } id_ts;
124 | struct id_ip {
125 | struct ip idi_ip;
126 | /* options and then 64 bits of data */
127 | } id_ip;
128 | struct icmp_ra_addr id_radv;
129 | uint32_t id_mask;
130 | char id_data[1];
131 | } icmp_dun;
132 | #define icmp_otime icmp_dun.id_ts.its_otime
133 | #define icmp_rtime icmp_dun.id_ts.its_rtime
134 | #define icmp_ttime icmp_dun.id_ts.its_ttime
135 | #define icmp_ip icmp_dun.id_ip.idi_ip
136 | #define icmp_radv icmp_dun.id_radv
137 | #define icmp_mask icmp_dun.id_mask
138 | #define icmp_data icmp_dun.id_data
139 | };
140 |
141 | /*
142 | * Lower bounds on packet lengths for various types.
143 | * For the error advice packets must first insure that the
144 | * packet is large enough to contain the returned ip header.
145 | * Only then can we do the check to see if 64 bits of packet
146 | * data have been returned, since we need to check the returned
147 | * ip header length.
148 | */
149 | #define ICMP_MINLEN 8 /* abs minimum */
150 | #define ICMP_TSLEN (8 + 3 * sizeof (uint32_t)) /* timestamp */
151 | #define ICMP_MASKLEN 12 /* address mask */
152 | #define ICMP_ADVLENMIN (8 + sizeof (struct ip) + 8) /* min */
153 | #define ICMP_ADVLEN(p) (8 + ((p)->icmp_ip.ip_hl << 2) + 8)
154 | /* N.B.: must separately check that ip_hl >= 5 */
155 |
156 | /*
157 | * Definition of type and code field values.
158 | */
159 | #define ICMP_ECHOREPLY 0 /* echo reply */
160 | #define ICMP_UNREACH 3 /* dest unreachable, codes: */
161 | #define ICMP_UNREACH_NET 0 /* bad net */
162 | #define ICMP_UNREACH_HOST 1 /* bad host */
163 | #define ICMP_UNREACH_PROTOCOL 2 /* bad protocol */
164 | #define ICMP_UNREACH_PORT 3 /* bad port */
165 | #define ICMP_UNREACH_NEEDFRAG 4 /* IP_DF caused drop */
166 | #define ICMP_UNREACH_SRCFAIL 5 /* src route failed */
167 | #define ICMP_UNREACH_NET_UNKNOWN 6 /* unknown net */
168 | #define ICMP_UNREACH_HOST_UNKNOWN 7 /* unknown host */
169 | #define ICMP_UNREACH_ISOLATED 8 /* src host isolated */
170 | #define ICMP_UNREACH_NET_PROHIB 9 /* prohibited access */
171 | #define ICMP_UNREACH_HOST_PROHIB 10 /* ditto */
172 | #define ICMP_UNREACH_TOSNET 11 /* bad tos for net */
173 | #define ICMP_UNREACH_TOSHOST 12 /* bad tos for host */
174 | #define ICMP_UNREACH_FILTER_PROHIB 13 /* admin prohib */
175 | #define ICMP_UNREACH_HOST_PRECEDENCE 14 /* host prec vio. */
176 | #define ICMP_UNREACH_PRECEDENCE_CUTOFF 15 /* prec cutoff */
177 | #define ICMP_SOURCEQUENCH 4 /* packet lost, slow down */
178 | #define ICMP_REDIRECT 5 /* shorter route, codes: */
179 | #define ICMP_REDIRECT_NET 0 /* for network */
180 | #define ICMP_REDIRECT_HOST 1 /* for host */
181 | #define ICMP_REDIRECT_TOSNET 2 /* for tos and net */
182 | #define ICMP_REDIRECT_TOSHOST 3 /* for tos and host */
183 | #define ICMP_ALTHOSTADDR 6 /* alternate host address */
184 | #define ICMP_ECHO 8 /* echo service */
185 | #define ICMP_ROUTERADVERT 9 /* router advertisement */
186 | #define ICMP_ROUTERADVERT_NORMAL 0 /* normal advertisement */
187 | #define ICMP_ROUTERADVERT_NOROUTE_COMMON 16 /* selective routing */
188 | #define ICMP_ROUTERSOLICIT 10 /* router solicitation */
189 | #define ICMP_TIMXCEED 11 /* time exceeded, code: */
190 | #define ICMP_TIMXCEED_INTRANS 0 /* ttl==0 in transit */
191 | #define ICMP_TIMXCEED_REASS 1 /* ttl==0 in reass */
192 | #define ICMP_PARAMPROB 12 /* ip header bad */
193 | #define ICMP_PARAMPROB_ERRATPTR 0 /* error at param ptr */
194 | #define ICMP_PARAMPROB_OPTABSENT 1 /* req. opt. absent */
195 | #define ICMP_PARAMPROB_LENGTH 2 /* bad length */
196 | #define ICMP_TSTAMP 13 /* timestamp request */
197 | #define ICMP_TSTAMPREPLY 14 /* timestamp reply */
198 | #define ICMP_IREQ 15 /* information request */
199 | #define ICMP_IREQREPLY 16 /* information reply */
200 | #define ICMP_MASKREQ 17 /* address mask request */
201 | #define ICMP_MASKREPLY 18 /* address mask reply */
202 | #define ICMP_TRACEROUTE 30 /* traceroute */
203 | #define ICMP_DATACONVERR 31 /* data conversion error */
204 | #define ICMP_MOBILE_REDIRECT 32 /* mobile host redirect */
205 | #define ICMP_IPV6_WHEREAREYOU 33 /* IPv6 where-are-you */
206 | #define ICMP_IPV6_IAMHERE 34 /* IPv6 i-am-here */
207 | #define ICMP_MOBILE_REGREQUEST 35 /* mobile registration req */
208 | #define ICMP_MOBILE_REGREPLY 36 /* mobile registration reply */
209 | #define ICMP_SKIP 39 /* SKIP */
210 | #define ICMP_PHOTURIS 40 /* Photuris */
211 | #define ICMP_PHOTURIS_UNKNOWN_INDEX 1 /* unknown sec index */
212 | #define ICMP_PHOTURIS_AUTH_FAILED 2 /* auth failed */
213 | #define ICMP_PHOTURIS_DECRYPT_FAILED 3 /* decrypt failed */
214 |
215 | #define ICMP_MAXTYPE 40
216 |
217 | #define ICMP_INFOTYPE(type) \
218 | ((type) == ICMP_ECHOREPLY || (type) == ICMP_ECHO || \
219 | (type) == ICMP_ROUTERADVERT || (type) == ICMP_ROUTERSOLICIT || \
220 | (type) == ICMP_TSTAMP || (type) == ICMP_TSTAMPREPLY || \
221 | (type) == ICMP_IREQ || (type) == ICMP_IREQREPLY || \
222 | (type) == ICMP_MASKREQ || (type) == ICMP_MASKREPLY)
223 |
224 |
225 | #endif
226 |
--------------------------------------------------------------------------------
/src/usrsctp/user_malloc.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 1987, 1993
3 | * The Regents of the University of California.
4 | * Copyright (c) 2005 Robert N. M. Watson
5 | * All rights reserved.
6 | *
7 | * Redistribution and use in source and binary forms, with or without
8 | * modification, are permitted provided that the following conditions
9 | * are met:
10 | * 1. Redistributions of source code must retain the above copyright
11 | * notice, this list of conditions and the following disclaimer.
12 | * 2. Redistributions in binary form must reproduce the above copyright
13 | * notice, this list of conditions and the following disclaimer in the
14 | * documentation and/or other materials provided with the distribution.
15 | * 3. Neither the name of the University nor the names of its contributors
16 | * may be used to endorse or promote products derived from this software
17 | * without specific prior written permission.
18 | *
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 | * SUCH DAMAGE.
30 | *
31 | */
32 |
33 | /* This file has been renamed user_malloc.h for Userspace */
34 | #ifndef _USER_MALLOC_H_
35 | #define _USER_MALLOC_H_
36 |
37 | /*__Userspace__*/
38 | #include
39 | #include
40 | #if !defined(_WIN32)
41 | #include
42 | #include
43 | #else
44 | #if (defined(_MSC_VER) && _MSC_VER >= 1600) || (defined(__MSVCRT_VERSION__) && __MSVCRT_VERSION__ >= 1400)
45 | #include
46 | #elif defined(SCTP_STDINT_INCLUDE)
47 | #include SCTP_STDINT_INCLUDE
48 | #else
49 | #define uint32_t unsigned __int32
50 | #define uint64_t unsigned __int64
51 | #endif
52 | #include
53 | #endif
54 |
55 | #define MINALLOCSIZE UMA_SMALLEST_UNIT
56 |
57 | /*
58 | * flags to malloc.
59 | */
60 | #define M_NOWAIT 0x0001 /* do not block */
61 | #define M_WAITOK 0x0002 /* ok to block */
62 | #define M_ZERO 0x0100 /* bzero the allocation */
63 | #define M_NOVM 0x0200 /* don't ask VM for pages */
64 | #define M_USE_RESERVE 0x0400 /* can alloc out of reserve memory */
65 |
66 | #define M_MAGIC 877983977 /* time when first defined :-) */
67 |
68 | /*
69 | * Two malloc type structures are present: malloc_type, which is used by a
70 | * type owner to declare the type, and malloc_type_internal, which holds
71 | * malloc-owned statistics and other ABI-sensitive fields, such as the set of
72 | * malloc statistics indexed by the compile-time MAXCPU constant.
73 | * Applications should avoid introducing dependence on the allocator private
74 | * data layout and size.
75 | *
76 | * The malloc_type ks_next field is protected by malloc_mtx. Other fields in
77 | * malloc_type are static after initialization so unsynchronized.
78 | *
79 | * Statistics in malloc_type_stats are written only when holding a critical
80 | * section and running on the CPU associated with the index into the stat
81 | * array, but read lock-free resulting in possible (minor) races, which the
82 | * monitoring app should take into account.
83 | */
84 | struct malloc_type_stats {
85 | uint64_t mts_memalloced; /* Bytes allocated on CPU. */
86 | uint64_t mts_memfreed; /* Bytes freed on CPU. */
87 | uint64_t mts_numallocs; /* Number of allocates on CPU. */
88 | uint64_t mts_numfrees; /* number of frees on CPU. */
89 | uint64_t mts_size; /* Bitmask of sizes allocated on CPU. */
90 | uint64_t _mts_reserved1; /* Reserved field. */
91 | uint64_t _mts_reserved2; /* Reserved field. */
92 | uint64_t _mts_reserved3; /* Reserved field. */
93 | };
94 |
95 | #ifndef MAXCPU /* necessary on Linux */
96 | #define MAXCPU 4 /* arbitrary? */
97 | #endif
98 |
99 | struct malloc_type_internal {
100 | struct malloc_type_stats mti_stats[MAXCPU];
101 | };
102 |
103 | /*
104 | * ABI-compatible version of the old 'struct malloc_type', only all stats are
105 | * now malloc-managed in malloc-owned memory rather than in caller memory, so
106 | * as to avoid ABI issues. The ks_next pointer is reused as a pointer to the
107 | * internal data handle.
108 | */
109 | struct malloc_type {
110 | struct malloc_type *ks_next; /* Next in global chain. */
111 | u_long _ks_memuse; /* No longer used. */
112 | u_long _ks_size; /* No longer used. */
113 | u_long _ks_inuse; /* No longer used. */
114 | uint64_t _ks_calls; /* No longer used. */
115 | u_long _ks_maxused; /* No longer used. */
116 | u_long ks_magic; /* Detect programmer error. */
117 | const char *ks_shortdesc; /* Printable type name. */
118 |
119 | /*
120 | * struct malloc_type was terminated with a struct mtx, which is no
121 | * longer required. For ABI reasons, continue to flesh out the full
122 | * size of the old structure, but reuse the _lo_class field for our
123 | * internal data handle.
124 | */
125 | void *ks_handle; /* Priv. data, was lo_class. */
126 | const char *_lo_name;
127 | const char *_lo_type;
128 | u_int _lo_flags;
129 | void *_lo_list_next;
130 | struct witness *_lo_witness;
131 | uintptr_t _mtx_lock;
132 | u_int _mtx_recurse;
133 | };
134 |
135 | /*
136 | * Statistics structure headers for user space. The kern.malloc sysctl
137 | * exposes a structure stream consisting of a stream header, then a series of
138 | * malloc type headers and statistics structures (quantity maxcpus). For
139 | * convenience, the kernel will provide the current value of maxcpus at the
140 | * head of the stream.
141 | */
142 | #define MALLOC_TYPE_STREAM_VERSION 0x00000001
143 | struct malloc_type_stream_header {
144 | uint32_t mtsh_version; /* Stream format version. */
145 | uint32_t mtsh_maxcpus; /* Value of MAXCPU for stream. */
146 | uint32_t mtsh_count; /* Number of records. */
147 | uint32_t _mtsh_pad; /* Pad/reserved field. */
148 | };
149 |
150 | #define MALLOC_MAX_NAME 32
151 | struct malloc_type_header {
152 | char mth_name[MALLOC_MAX_NAME];
153 | };
154 |
155 | /* __Userspace__
156 | Notice that at places it uses ifdef _KERNEL. That line cannot be
157 | removed because it causes conflicts with malloc definition in
158 | /usr/include/malloc.h, which essentially says that malloc.h has
159 | been overridden by stdlib.h. We will need to use names like
160 | user_malloc.h for isolating kernel interface headers. using
161 | original names like malloc.h in a user_include header can be
162 | confusing, All userspace header files are being placed in ./user_include
163 | Better still to remove from user_include.h all irrelevant code such
164 | as that in the block starting with #ifdef _KERNEL. I am only leaving
165 | it in for the time being to see what functionality is in this file
166 | that kernel uses.
167 |
168 | Start copy: Copied code for __Userspace__ */
169 | #define MALLOC_DEFINE(type, shortdesc, longdesc) \
170 | struct malloc_type type[1] = { \
171 | { NULL, 0, 0, 0, 0, 0, M_MAGIC, shortdesc, NULL, NULL, \
172 | NULL, 0, NULL, NULL, 0, 0 } \
173 | }
174 |
175 | /* Removed "extern" in __Userspace__ code */
176 | /* If we need to use MALLOC_DECLARE before using MALLOC then
177 | we have to remove extern.
178 | In /usr/include/sys/malloc.h there is this definition:
179 | #define MALLOC_DECLARE(type) \
180 | extern struct malloc_type type[1]
181 | and loader is unable to find the extern malloc_type because
182 | it may be defined in one of kernel object files.
183 | It seems that MALLOC_DECLARE and MALLOC_DEFINE cannot be used at
184 | the same time for same "type" variable. Also, in Randall's architecture
185 | document, where it specifies O/S specific macros and functions, it says
186 | that the name in SCTP_MALLOC does not have to be used.
187 | */
188 | #define MALLOC_DECLARE(type) \
189 | extern struct malloc_type type[1]
190 |
191 | #define FREE(addr, type) free((addr))
192 |
193 | /* changed definitions of MALLOC and FREE */
194 | /* Using memset if flag M_ZERO is specified. Todo: M_WAITOK and M_NOWAIT */
195 | #define MALLOC(space, cast, size, type, flags) \
196 | ((space) = (cast)malloc((u_long)(size))); \
197 | do { \
198 | if (flags & M_ZERO) { \
199 | memset(space,0,size); \
200 | } \
201 | } while (0);
202 |
203 | #endif /* !_SYS_MALLOC_H_ */
204 |
--------------------------------------------------------------------------------
/src/usrsctp/user_recv_thread.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 2012 Michael Tuexen
3 | * Copyright (c) 2012 Irene Ruengeler
4 | * All rights reserved.
5 | *
6 | * Redistribution and use in source and binary forms, with or without
7 | * modification, are permitted provided that the following conditions
8 | * are met:
9 | * 1. Redistributions of source code must retain the above copyright
10 | * notice, this list of conditions and the following disclaimer.
11 | * 2. Redistributions in binary form must reproduce the above copyright
12 | * notice, this list of conditions and the following disclaimer in the
13 | * documentation and/or other materials provided with the distribution.
14 | *
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 | * SUCH DAMAGE.
26 | */
27 |
28 | #ifndef _USER_RECV_THREAD_H_
29 | #define _USER_RECV_THREAD_H_
30 |
31 | void recv_thread_init(void);
32 | void recv_thread_destroy(void);
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/src/usrsctp/user_route.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 1980, 1986, 1993
3 | * The Regents of the University of California. All rights reserved.
4 | *
5 | * Redistribution and use in source and binary forms, with or without
6 | * modification, are permitted provided that the following conditions
7 | * are met:
8 | * 1. Redistributions of source code must retain the above copyright
9 | * notice, this list of conditions and the following disclaimer.
10 | * 2. Redistributions in binary form must reproduce the above copyright
11 | * notice, this list of conditions and the following disclaimer in the
12 | * documentation and/or other materials provided with the distribution.
13 | * 3. Neither the name of the University nor the names of its contributors
14 | * may be used to endorse or promote products derived from this software
15 | * without specific prior written permission.
16 | *
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 | * SUCH DAMAGE.
28 | *
29 | */
30 |
31 | #ifndef _USER_ROUTE_H_
32 | #define _USER_ROUTE_H_
33 |
34 | /*
35 | * Kernel resident routing tables.
36 | *
37 | * The routing tables are initialized when interface addresses
38 | * are set by making entries for all directly connected interfaces.
39 | */
40 |
41 | /*
42 | * A route consists of a destination address and a reference
43 | * to a routing entry. These are often held by protocols
44 | * in their control blocks, e.g. inpcb.
45 | */
46 |
47 | struct sctp_route {
48 | struct sctp_rtentry *ro_rt;
49 | struct sockaddr ro_dst;
50 | };
51 |
52 | /*
53 | * These numbers are used by reliable protocols for determining
54 | * retransmission behavior and are included in the routing structure.
55 | */
56 | struct sctp_rt_metrics_lite {
57 | uint32_t rmx_mtu; /* MTU for this path */
58 | #if 0
59 | u_long rmx_expire; /* lifetime for route, e.g. redirect */
60 | u_long rmx_pksent; /* packets sent using this route */
61 | #endif
62 | };
63 |
64 | /*
65 | * We distinguish between routes to hosts and routes to networks,
66 | * preferring the former if available. For each route we infer
67 | * the interface to use from the gateway address supplied when
68 | * the route was entered. Routes that forward packets through
69 | * gateways are marked so that the output routines know to address the
70 | * gateway rather than the ultimate destination.
71 | */
72 | struct sctp_rtentry {
73 | #if 0
74 | struct radix_node rt_nodes[2]; /* tree glue, and other values */
75 | /*
76 | * XXX struct rtentry must begin with a struct radix_node (or two!)
77 | * because the code does some casts of a 'struct radix_node *'
78 | * to a 'struct rtentry *'
79 | */
80 | #define rt_key(r) (*((struct sockaddr **)(&(r)->rt_nodes->rn_key)))
81 | #define rt_mask(r) (*((struct sockaddr **)(&(r)->rt_nodes->rn_mask)))
82 | struct sockaddr *rt_gateway; /* value */
83 | u_long rt_flags; /* up/down?, host/net */
84 | #endif
85 | struct ifnet *rt_ifp; /* the answer: interface to use */
86 | struct ifaddr *rt_ifa; /* the answer: interface address to use */
87 | struct sctp_rt_metrics_lite rt_rmx; /* metrics used by rx'ing protocols */
88 | long rt_refcnt; /* # held references */
89 | #if 0
90 | struct sockaddr *rt_genmask; /* for generation of cloned routes */
91 | caddr_t rt_llinfo; /* pointer to link level info cache */
92 | struct rtentry *rt_gwroute; /* implied entry for gatewayed routes */
93 | struct rtentry *rt_parent; /* cloning parent of this route */
94 | #endif
95 | struct mtx rt_mtx; /* mutex for routing entry */
96 | };
97 |
98 | #define RT_LOCK_INIT(_rt) mtx_init(&(_rt)->rt_mtx, "rtentry", NULL, MTX_DEF | MTX_DUPOK)
99 | #define RT_LOCK(_rt) mtx_lock(&(_rt)->rt_mtx)
100 | #define RT_UNLOCK(_rt) mtx_unlock(&(_rt)->rt_mtx)
101 | #define RT_LOCK_DESTROY(_rt) mtx_destroy(&(_rt)->rt_mtx)
102 | #define RT_LOCK_ASSERT(_rt) mtx_assert(&(_rt)->rt_mtx, MA_OWNED)
103 |
104 | #define RT_ADDREF(_rt) do { \
105 | RT_LOCK_ASSERT(_rt); \
106 | KASSERT((_rt)->rt_refcnt >= 0, \
107 | ("negative refcnt %ld", (_rt)->rt_refcnt)); \
108 | (_rt)->rt_refcnt++; \
109 | } while (0)
110 | #define RT_REMREF(_rt) do { \
111 | RT_LOCK_ASSERT(_rt); \
112 | KASSERT((_rt)->rt_refcnt > 0, \
113 | ("bogus refcnt %ld", (_rt)->rt_refcnt)); \
114 | (_rt)->rt_refcnt--; \
115 | } while (0)
116 | #define RTFREE_LOCKED(_rt) do { \
117 | if ((_rt)->rt_refcnt <= 1) { \
118 | rtfree(_rt); \
119 | } else { \
120 | RT_REMREF(_rt); \
121 | RT_UNLOCK(_rt); \
122 | } \
123 | /* guard against invalid refs */ \
124 | _rt = NULL; \
125 | } while (0)
126 | #define RTFREE(_rt) do { \
127 | RT_LOCK(_rt); \
128 | RTFREE_LOCKED(_rt); \
129 | } while (0)
130 | #endif
131 |
--------------------------------------------------------------------------------
/src/usrsctp/user_uma.h:
--------------------------------------------------------------------------------
1 | /*-
2 | * Copyright (c) 2009-2010 Brad Penoff
3 | * Copyright (c) 2009-2010 Humaira Kamal
4 | * Copyright (c) 2011-2012 Irene Ruengeler
5 | * Copyright (c) 2011-2012 Michael Tuexen
6 | *
7 | * All rights reserved.
8 | *
9 | * Redistribution and use in source and binary forms, with or without
10 | * modification, are permitted provided that the following conditions
11 | * are met:
12 | * 1. Redistributions of source code must retain the above copyright
13 | * notice, this list of conditions and the following disclaimer.
14 | * 2. Redistributions in binary form must reproduce the above copyright
15 | * notice, this list of conditions and the following disclaimer in the
16 | * documentation and/or other materials provided with the distribution.
17 | *
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 | * SUCH DAMAGE.
29 | */
30 |
31 | #ifndef _USER_UMA_H_
32 | #define _USER_UMA_H_
33 |
34 | #define UMA_ZFLAG_FULL 0x40000000 /* Reached uz_maxpages */
35 | #define UMA_ALIGN_PTR (sizeof(void *) - 1) /* Alignment fit for ptr */
36 |
37 | /* __Userspace__ All these definitions will change for
38 | userspace Universal Memory Allocator (UMA). These are included
39 | for reference purposes and to avoid compile errors for the time being.
40 | */
41 | typedef int (*uma_ctor)(void *mem, int size, void *arg, int flags);
42 | typedef void (*uma_dtor)(void *mem, int size, void *arg);
43 | typedef int (*uma_init)(void *mem, int size, int flags);
44 | typedef void (*uma_fini)(void *mem, int size);
45 | typedef struct uma_zone * uma_zone_t;
46 | typedef struct uma_keg * uma_keg_t;
47 |
48 | struct uma_cache {
49 | int stub; /* TODO __Userspace__ */
50 | };
51 |
52 | struct uma_keg {
53 | int stub; /* TODO __Userspace__ */
54 | };
55 |
56 | struct uma_zone {
57 | char *uz_name; /* Text name of the zone */
58 | struct mtx *uz_lock; /* Lock for the zone (keg's lock) */
59 | uma_keg_t uz_keg; /* Our underlying Keg */
60 |
61 | LIST_ENTRY(uma_zone) uz_link; /* List of all zones in keg */
62 | LIST_HEAD(,uma_bucket) uz_full_bucket; /* full buckets */
63 | LIST_HEAD(,uma_bucket) uz_free_bucket; /* Buckets for frees */
64 |
65 | uma_ctor uz_ctor; /* Constructor for each allocation */
66 | uma_dtor uz_dtor; /* Destructor */
67 | uma_init uz_init; /* Initializer for each item */
68 | uma_fini uz_fini; /* Discards memory */
69 |
70 | u_int64_t uz_allocs; /* Total number of allocations */
71 | u_int64_t uz_frees; /* Total number of frees */
72 | u_int64_t uz_fails; /* Total number of alloc failures */
73 | uint16_t uz_fills; /* Outstanding bucket fills */
74 | uint16_t uz_count; /* Highest value ub_ptr can have */
75 |
76 | /*
77 | * This HAS to be the last item because we adjust the zone size
78 | * based on NCPU and then allocate the space for the zones.
79 | */
80 | struct uma_cache uz_cpu[1]; /* Per cpu caches */
81 | };
82 |
83 | /* Prototype */
84 | uma_zone_t
85 | uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
86 | uma_init uminit, uma_fini fini, int align, uint32_t flags);
87 |
88 |
89 | #define uma_zone_set_max(zone, number) /* stub TODO __Userspace__ */
90 |
91 | uma_zone_t
92 | uma_zcreate(char *name, size_t size, uma_ctor ctor, uma_dtor dtor,
93 | uma_init uminit, uma_fini fini, int align, uint32_t flags) {
94 | return NULL; /* stub TODO __Userspace__. Also place implementation in a separate .c file */
95 | }
96 | #endif
97 |
--------------------------------------------------------------------------------