X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Futil%2Fnetwork.c;h=e112d90932241884d8a54d77cf5b3a7ed3835a91;hb=76c024311489ede1bca79cf647e568dde70d8a48;hp=9c50d6fcddb59a479904247690a8257365e3af1d;hpb=ab3a3be75688c71b23570406816dbd12c67529ec;p=oweals%2Fgnunet.git diff --git a/src/util/network.c b/src/util/network.c index 9c50d6fcd..e112d9093 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -19,7 +19,7 @@ */ /** - * @file util/sock.c + * @file util/network.c * @brief basic, low-level networking interface * @author Nils Durner */ @@ -31,7 +31,7 @@ #define DEBUG_SOCK GNUNET_NO -struct GNUNET_NETWORK_Descriptor +struct GNUNET_NETWORK_Handle { int fd; }; @@ -51,14 +51,22 @@ struct GNUNET_NETWORK_FDSet #define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set))) #endif -struct GNUNET_NETWORK_Descriptor * -GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Descriptor *desc, +/** + * accept a new connection on a socket + * + * @param desc bound socket + * @param address address of the connecting peer, may be NULL + * @param address_len length of address + * @return client socket + */ +struct GNUNET_NETWORK_Handle * +GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t * address_len) { - struct GNUNET_NETWORK_Descriptor *ret; + struct GNUNET_NETWORK_Handle *ret; - ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor)); + ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); ret->fd = accept (desc->fd, address, address_len); #ifdef MINGW if (INVALID_SOCKET == ret->fd) @@ -67,8 +75,15 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Descriptor *desc, return ret; } +/** + * Bind to a connected socket + * @param desc socket + * @param address address to be bound + * @param address_len length of address + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Descriptor *desc, +GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len) { @@ -79,16 +94,17 @@ GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Descriptor *desc, if (SOCKET_ERROR == ret) SetErrnoFromWinsockError (WSAGetLastError ()); #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } /** * Set if a socket should use blocking or non-blocking IO. - * + * @param fd socket + * @param doBlock blocking mode * @return GNUNET_OK on success, GNUNET_SYSERR on error */ int -GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Descriptor *fd, +GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Handle *fd, int doBlock) { #if MINGW @@ -123,8 +139,13 @@ GNUNET_NETWORK_socket_set_blocking (struct GNUNET_NETWORK_Descriptor *fd, #endif } +/** + * Close a socket + * @param desc socket + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Descriptor *desc) +GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc) { int ret; #ifdef MINGW @@ -141,11 +162,18 @@ GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Descriptor *desc) } #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } +/** + * Connect a socket + * @param desc socket + * @param address peer address + * @param length of address + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Descriptor *desc, +GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len) { @@ -154,13 +182,26 @@ GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Descriptor *desc, ret = connect (desc->fd, address, address_len); #ifdef MINGW if (SOCKET_ERROR == ret) - SetErrnoFromWinsockError (WSAGetLastError ()); + { + SetErrnoFromWinsockError (WSAGetLastError ()); + if (errno == EWOULDBLOCK) + errno = EINPROGRESS; + } #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } +/** + * Get socket options + * @param desc socket + * @param level protocol level of the option + * @param optname identifier of the option + * @param optval options + * @param optlen length of optval + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Descriptor *desc, +GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t * optlen) { @@ -173,11 +214,17 @@ GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Descriptor *desc, else if (SOCKET_ERROR == ret) SetErrnoFromWinsockError (WSAGetLastError ()); #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } +/** + * Listen on a socket + * @param desc socket + * @param backlog length of the listen queue + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Descriptor *desc, +GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc, int backlog) { int ret; @@ -188,11 +235,18 @@ GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Descriptor *desc, SetErrnoFromWinsockError (WSAGetLastError ()); #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } +/** + * Read data from a connected socket + * @param desc socket + * @param buffer buffer + * @param length length of buffer + * @param flags type of message reception + */ ssize_t -GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Descriptor * desc, +GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc, void *buffer, size_t length, int flags) { int ret; @@ -206,8 +260,16 @@ GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Descriptor * desc, return ret; } +/** + * Send data + * @param desc socket + * @param buffer data to send + * @param length size of the buffer + * @param flags type of message transmission + * @return number of bytes sent, GNUNET_SYSERR on error + */ ssize_t -GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Descriptor * desc, +GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc, const void *buffer, size_t length, int flags) { int ret; @@ -221,8 +283,18 @@ GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Descriptor * desc, return ret; } +/** + * Send data + * @param desc socket + * @param message data to send + * @param length size of the data + * @param flags type of message transmission + * @param dest_addr destination address + * @param dest_len length of address + * @return number of bytes sent, GNUNET_SYSERR on error + */ ssize_t -GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Descriptor * desc, +GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc, const void *message, size_t length, int flags, const struct sockaddr * dest_addr, socklen_t dest_len) @@ -238,8 +310,17 @@ GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Descriptor * desc, return ret; } +/** + * Set socket option + * @param fd socket + * @param level protocol level of the option + * @param option_name option identifier + * @param option_value value to set + * @param option_len size of option_value + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Descriptor *fd, +GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len) @@ -252,15 +333,22 @@ GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Descriptor *fd, SetErrnoFromWinsockError (WSAGetLastError ()); #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } -struct GNUNET_NETWORK_Descriptor * +/** + * Create a new socket + * @param domain domain of the socket + * @param type socket type + * @param protocol network protocol + * @return new socket, NULL on error + */ +struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_socket (int domain, int type, int protocol) { - struct GNUNET_NETWORK_Descriptor *ret; + struct GNUNET_NETWORK_Handle *ret; - ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Descriptor)); + ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); ret->fd = socket (domain, type, protocol); #ifdef MINGW if (INVALID_SOCKET == ret->fd) @@ -276,8 +364,14 @@ GNUNET_NETWORK_socket_socket (int domain, int type, int protocol) return ret; } +/** + * Shut down socket operations + * @param desc socket + * @param how type of shutdown + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + */ int -GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Descriptor *desc, +GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc, int how) { int ret; @@ -288,11 +382,17 @@ GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Descriptor *desc, SetErrnoFromWinsockError (WSAGetLastError ()); #endif - return ret; + return ret == 0 ? GNUNET_OK : GNUNET_SYSERR; } +/** + * Make a non-inheritable to child processes + * @param socket + * @return GNUNET_OK on success, GNUNET_SYSERR otherwise + * @warning Not implemented on Windows + */ int -GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Descriptor +GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle *desc) { #ifdef MINGW @@ -306,6 +406,10 @@ GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Descriptor #endif } +/** + * Reset FD set + * @param fds fd set + */ void GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds) { @@ -318,9 +422,14 @@ GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds) #endif } +/** + * Add a socket to the FD set + * @param fds fd set + * @param desc socket to add + */ void GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_NETWORK_Descriptor *desc) + const struct GNUNET_NETWORK_Handle *desc) { FD_SET (desc->fd, &fds->sds); @@ -328,13 +437,23 @@ GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds, fds->nsds = desc->fd + 1; } +/** + * Check whether a socket is part of the fd set + * @param fds fd set + * @param desc socket + */ int GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds, - const struct GNUNET_NETWORK_Descriptor *desc) + const struct GNUNET_NETWORK_Handle *desc) { return FD_ISSET (desc->fd, &fds->sds); } +/** + * Add one fd set to another + * @param dst the fd set to add to + * @param src the fd set to add from + */ void GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, const struct GNUNET_NETWORK_FDSet *src) @@ -350,6 +469,11 @@ GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst, } } +/** + * Copy one fd set to another + * @param to destination + * @param from source + */ void GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, const struct GNUNET_NETWORK_FDSet *from) @@ -370,6 +494,12 @@ GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to, #endif } +/** + * Copy a native fd set + * @param to destination + * @param from native source set + * @param the biggest socket number in from + 1 + */ void GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, const fd_set * from, int nfds) @@ -378,6 +508,11 @@ GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to, to->nsds = nfds; } +/** + * Add a file handle to the fd set + * @param fds fd set + * @param h the file handle to add + */ void GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h) @@ -397,6 +532,12 @@ GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds, #endif } +/** + * Check if a file handle is part of an fd set + * @param fds fd set + * @param h file handle + * @return GNUNET_YES if the file handle is part of the set + */ int GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h) @@ -409,6 +550,12 @@ GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds, #endif } +/** + * Checks if two fd sets overlap + * @param fds1 first fd set + * @param fds2 second fd set + * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise + */ int GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2) @@ -426,6 +573,10 @@ GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1, return GNUNET_NO; } +/** + * Creates an fd set + * @return a new fd set + */ struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create () { @@ -440,6 +591,10 @@ GNUNET_NETWORK_fdset_create () return fds; } +/** + * Releases the associated memory of an fd set + * @param fds fd set + */ void GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds) { @@ -449,6 +604,14 @@ GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds) GNUNET_free (fds); } +/** + * Check if sockets meet certain conditions + * @param rfds set of sockets to be checked for readability + * @param wfds set of sockets to be checked for writability + * @param efds set of sockets to be checked for exceptions + * @param timeout relative value when to return + * @return number of selected sockets, GNUNET_SYSERR on error + */ int GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, struct GNUNET_NETWORK_FDSet *wfds, @@ -516,23 +679,6 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, else FD_ZERO(&sock_except); - /* - if (rfds) - FD_COPY (&rfds->sds, &sock_read); - else - FD_ZERO(&sock_read); - - if (wfds) - FD_COPY (&wfds->sds, &sock_write); - else - FD_ZERO(&sock_write); - - if (efds) - FD_COPY (&efds->sds, &sock_except); - else - FD_ZERO(&sock_except); -*/ - /* multiplex between winsock select() and waiting on the handles */ FD_ZERO (&aread); @@ -675,4 +821,4 @@ GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds, #endif } -/* end of network_socket.c */ +/* end of network.c */