From b75dda23d7759aa7f8d7b2098376b5fc480cbf76 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 10 Nov 2016 19:31:55 -0300 Subject: [PATCH 1/5] Added net directory to initial implementation of c-libp2p-network. --- net/socket.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++ net/socket.h | 11 ++++++ 2 files changed, 120 insertions(+) create mode 100644 net/socket.c create mode 100644 net/socket.h diff --git a/net/socket.c b/net/socket.c new file mode 100644 index 0000000..d9ca445 --- /dev/null +++ b/net/socket.c @@ -0,0 +1,109 @@ +#include +#include +#include +#include +#include +#include +#include "socket.h" + +/* Create a TCP socket. + */ +int socket_tcp4(void) +{ + int s; + + s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) return -1; + return s; +} + +/* associate an IP address with an port to a socket. + * first param is the socket file description + * second is an array of four bytes IP address + * in binary format, this function return 0 on sucess + * or -1 on error setting errno apropriated. + */ +int socket_bind4(int s, uint32_t ip, uint16_t port) +{ + struct sockaddr_in sa; + + bzero(&sa, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = ip; + + return bind(s, (struct sockaddr *) &sa, sizeof sa); +} + +/* Same as socket_bind4(), but set SO_REUSEADDR before + */ +int socket_bind4_reuse(int s, uint32_t ip, uint16_t port) +{ + int opt = 1; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof opt); + return socket_bind4(s, ip, port); +} + +/* Accept a connection in a socket and return ip and port of + * remote connection at pointers passed as parameters. + */ +int socket_accept4(int s, uint32_t *ip, uint16_t *port) +{ + struct sockaddr_in sa; + socklen_t dummy = sizeof sa; + int fd; + + fd = accept(s, (struct sockaddr *) &sa, &dummy); + if (fd == -1) return -1; + + *ip = sa.sin_addr.s_addr; + *port = ntohs(sa.sin_port); + + return fd; +} + +/* retrieve local ip and port information from a socket. + */ +int socket_local4(int s, uint32_t *ip, uint16_t *port) +{ + struct sockaddr_in sa; + socklen_t dummy = sizeof sa; + + if (getsockname(s, (struct sockaddr *) &sa, &dummy) == -1) return -1; + *ip = sa.sin_addr.s_addr; + *port = ntohs(sa.sin_port); + return 0; +} + +/* start a client connection. + */ +int socket_connect4(int s, uint32_t ip, uint16_t port) +{ + struct sockaddr_in sa; + + memset(&sa, 0, sizeof sa); + sa.sin_family = AF_INET; + sa.sin_port = htons(port); + sa.sin_addr.s_addr = ip; + + return connect(s, (struct sockaddr *) &sa, sizeof sa); +} + +/* bind and listen to a socket. + */ +int socket_listen(int s, uint32_t *localip, uint16_t *localport) +{ + if (socket_bind4_reuse(s, *localip, *localport) == -1) { + close(s); + return -1; + } + if (socket_local4(s, localip, localport) == -1) { + close(s); + return -1; + } + if (listen(s, 1) == -1) { + close(s); + return -1; + } + return s; +} diff --git a/net/socket.h b/net/socket.h new file mode 100644 index 0000000..b0bdfb6 --- /dev/null +++ b/net/socket.h @@ -0,0 +1,11 @@ +#ifndef SOCKET_H + #define SOCKET_H + + int socket_tcp4(void); + int socket_bind4(int s, uint32_t ip, uint16_t port); + int socket_bind4_reuse(int s, uint32_t ip, uint16_t port); + int socket_accept4(int s, uint32_t *ip, uint16_t *port); + int socket_local4(int s, uint32_t *ip, uint16_t *port); + int socket_connect4(int s, uint32_t ip, uint16_t port); + int socket_listen(int s, uint32_t *localip, uint16_t *localport); +#endif From 7a053b3b6991b45167ada7876d61dcb99c3fcc02 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 10 Nov 2016 19:36:42 -0300 Subject: [PATCH 2/5] Split code and renamed socket.h to p2pnet.h --- net/{socket.h => p2pnet.h} | 9 +++++---- net/socket.c | 13 +------------ net/tcp.c | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 16 deletions(-) rename net/{socket.h => p2pnet.h} (87%) create mode 100644 net/tcp.c diff --git a/net/socket.h b/net/p2pnet.h similarity index 87% rename from net/socket.h rename to net/p2pnet.h index b0bdfb6..5f88635 100644 --- a/net/socket.h +++ b/net/p2pnet.h @@ -1,11 +1,12 @@ -#ifndef SOCKET_H - #define SOCKET_H +#ifndef P2PNET_H + #define P2PNET_H - int socket_tcp4(void); int socket_bind4(int s, uint32_t ip, uint16_t port); int socket_bind4_reuse(int s, uint32_t ip, uint16_t port); int socket_accept4(int s, uint32_t *ip, uint16_t *port); int socket_local4(int s, uint32_t *ip, uint16_t *port); int socket_connect4(int s, uint32_t ip, uint16_t port); int socket_listen(int s, uint32_t *localip, uint16_t *localport); -#endif + + int socket_tcp4(void); +#endif // P2PNET_H diff --git a/net/socket.c b/net/socket.c index d9ca445..3c1867f 100644 --- a/net/socket.c +++ b/net/socket.c @@ -4,18 +4,7 @@ #include #include #include -#include "socket.h" - -/* Create a TCP socket. - */ -int socket_tcp4(void) -{ - int s; - - s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); - if (s == -1) return -1; - return s; -} +#include "p2pnet.h" /* associate an IP address with an port to a socket. * first param is the socket file description diff --git a/net/tcp.c b/net/tcp.c new file mode 100644 index 0000000..7ab51a9 --- /dev/null +++ b/net/tcp.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include "p2pnet.h" + +/* Create a TCP socket. + */ +int socket_tcp4(void) +{ + int s; + + s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (s == -1) return -1; + return s; +} From 648982793c45c4fda4ab6f07145453e7c9c4461e Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 10 Nov 2016 19:40:27 -0300 Subject: [PATCH 3/5] Implemented SCTP stream socket. --- net/p2pnet.h | 4 ++++ net/sctp.c | 15 +++++++++++++++ net/socket.c | 16 ++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 net/sctp.c diff --git a/net/p2pnet.h b/net/p2pnet.h index 5f88635..1de67b5 100644 --- a/net/p2pnet.h +++ b/net/p2pnet.h @@ -7,6 +7,10 @@ int socket_local4(int s, uint32_t *ip, uint16_t *port); int socket_connect4(int s, uint32_t ip, uint16_t port); int socket_listen(int s, uint32_t *localip, uint16_t *localport); + ssize_t socket_read(int s, char *buf, size_t len, int flags); + ssize_t socket_write(int s, char *buf, size_t len, int flags); int socket_tcp4(void); + + int socket_stream_sctp4(void); #endif // P2PNET_H diff --git a/net/sctp.c b/net/sctp.c new file mode 100644 index 0000000..0ebbe7a --- /dev/null +++ b/net/sctp.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include "p2pnet.h" + +/* Create a SCTP socket. + */ +int socket_stream_sctp4(void) +{ + int s; + + s = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP); + if (s == -1) return -1; + return s; +} diff --git a/net/socket.c b/net/socket.c index 3c1867f..7c7f476 100644 --- a/net/socket.c +++ b/net/socket.c @@ -96,3 +96,19 @@ int socket_listen(int s, uint32_t *localip, uint16_t *localport) } return s; } + +/* Reads data from a socket, used instead of recv so if a protocol needs + * to use something else before or after it can be done here instead of + * outside the lib. + */ +ssize_t socket_read(int s, char *buf, size_t len, int flags) +{ + return recv(s, buf, len, flags); +} + +/* Same reason as socket_read, but to send data instead of receive. + */ +ssize_t socket_write(int s, char *buf, size_t len, int flags) +{ + return send(s, buf, len, flags); +} From 2b55ebfeb49ef97855f59d28f065ba4f4589e401 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 10 Nov 2016 19:43:31 -0300 Subject: [PATCH 4/5] Implemented initial UDP socket. --- net/p2pnet.h | 2 ++ net/udp.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 net/udp.c diff --git a/net/p2pnet.h b/net/p2pnet.h index 1de67b5..a11e025 100644 --- a/net/p2pnet.h +++ b/net/p2pnet.h @@ -13,4 +13,6 @@ int socket_tcp4(void); int socket_stream_sctp4(void); + + int socket_udp4(void); #endif // P2PNET_H diff --git a/net/udp.c b/net/udp.c new file mode 100644 index 0000000..08dab8d --- /dev/null +++ b/net/udp.c @@ -0,0 +1,16 @@ +#include +#include +#include +#include "p2pnet.h" + +/* Create a UDP socket. + */ +int socket_udp4(void) +{ + int s; + + s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); + + if (s == -1) return -1; + return s; +} From a43b8ec73628a2b356f3ece7e4966a6a38ddc6cb Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 10 Nov 2016 22:39:11 -0300 Subject: [PATCH 5/5] Added libp2p-net TODO. --- net/TODO | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 net/TODO diff --git a/net/TODO b/net/TODO new file mode 100644 index 0000000..6623ac4 --- /dev/null +++ b/net/TODO @@ -0,0 +1,12 @@ +- SCTP datagram and sequenced packets implementations, just stream for now. + +- UDP implementation, incomplete. + +- Implement some kind of network events such as libutp does, so libutp can + be added and a more robust implementation for already implemented protocols, + it may also be easier to switch protocols or use multiples at once from + high-level calls. + +- Implement UDT + +- IPv6.