*/\r
\r
/**\r
- * @file util/sock.c\r
+ * @file util/network.c\r
* @brief basic, low-level networking interface\r
* @author Nils Durner\r
*/\r
#define FD_COPY(s, d) (memcpy ((d), (s), sizeof (fd_set)))\r
#endif\r
\r
+/**\r
+ * accept a new connection on a socket\r
+ *\r
+ * @param desc bound socket\r
+ * @param address address of the connecting peer, may be NULL\r
+ * @param address_len length of address\r
+ * @return client socket\r
+ */\r
struct GNUNET_NETWORK_Handle *\r
GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc,\r
struct sockaddr *address,\r
return ret;\r
}\r
\r
+/**\r
+ * Bind to a connected socket\r
+ * @param desc socket\r
+ * @param address address to be bound\r
+ * @param address_len length of address\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_bind (struct GNUNET_NETWORK_Handle *desc,\r
const struct sockaddr *address,\r
if (SOCKET_ERROR == ret)\r
SetErrnoFromWinsockError (WSAGetLastError ());\r
#endif\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
/**\r
* Set if a socket should use blocking or non-blocking IO.\r
- *\r
+ * @param fd socket\r
+ * @param doBlock blocking mode\r
* @return GNUNET_OK on success, GNUNET_SYSERR on error\r
*/\r
int\r
#endif\r
}\r
\r
+/**\r
+ * Close a socket\r
+ * @param desc socket\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_close (struct GNUNET_NETWORK_Handle *desc)\r
{\r
}\r
#endif\r
\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Connect a socket\r
+ * @param desc socket\r
+ * @param address peer address\r
+ * @param length of address\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_connect (const struct GNUNET_NETWORK_Handle *desc,\r
const struct sockaddr *address,\r
ret = connect (desc->fd, address, address_len);\r
#ifdef MINGW\r
if (SOCKET_ERROR == ret)\r
- SetErrnoFromWinsockError (WSAGetLastError ());\r
+ {\r
+ SetErrnoFromWinsockError (WSAGetLastError ());\r
+ if (errno == EWOULDBLOCK)\r
+ errno = EINPROGRESS;\r
+ }\r
#endif\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Get socket options\r
+ * @param desc socket\r
+ * @param level protocol level of the option\r
+ * @param optname identifier of the option\r
+ * @param optval options\r
+ * @param optlen length of optval\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_getsockopt (const struct GNUNET_NETWORK_Handle *desc,\r
int level, int optname, void *optval,\r
else if (SOCKET_ERROR == ret)\r
SetErrnoFromWinsockError (WSAGetLastError ());\r
#endif\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Listen on a socket\r
+ * @param desc socket\r
+ * @param backlog length of the listen queue\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_listen (const struct GNUNET_NETWORK_Handle *desc,\r
int backlog)\r
SetErrnoFromWinsockError (WSAGetLastError ());\r
#endif\r
\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Read data from a connected socket\r
+ * @param desc socket\r
+ * @param buffer buffer\r
+ * @param length length of buffer\r
+ * @param flags type of message reception\r
+ */\r
ssize_t\r
GNUNET_NETWORK_socket_recv (const struct GNUNET_NETWORK_Handle * desc,\r
void *buffer, size_t length, int flags)\r
return ret;\r
}\r
\r
+/**\r
+ * Send data\r
+ * @param desc socket\r
+ * @param buffer data to send\r
+ * @param length size of the buffer\r
+ * @param flags type of message transmission\r
+ * @return number of bytes sent, GNUNET_SYSERR on error\r
+ */\r
ssize_t\r
GNUNET_NETWORK_socket_send (const struct GNUNET_NETWORK_Handle * desc,\r
const void *buffer, size_t length, int flags)\r
return ret;\r
}\r
\r
+/**\r
+ * Send data\r
+ * @param desc socket\r
+ * @param message data to send\r
+ * @param length size of the data\r
+ * @param flags type of message transmission\r
+ * @param dest_addr destination address\r
+ * @param dest_len length of address\r
+ * @return number of bytes sent, GNUNET_SYSERR on error\r
+ */\r
ssize_t\r
GNUNET_NETWORK_socket_sendto (const struct GNUNET_NETWORK_Handle * desc,\r
const void *message, size_t length, int flags,\r
return ret;\r
}\r
\r
+/**\r
+ * Set socket option\r
+ * @param fd socket\r
+ * @param level protocol level of the option\r
+ * @param option_name option identifier\r
+ * @param option_value value to set\r
+ * @param option_len size of option_value\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_setsockopt (struct GNUNET_NETWORK_Handle *fd,\r
int level, int option_name,\r
SetErrnoFromWinsockError (WSAGetLastError ());\r
#endif\r
\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Create a new socket\r
+ * @param domain domain of the socket\r
+ * @param type socket type\r
+ * @param protocol network protocol\r
+ * @return new socket, NULL on error\r
+ */\r
struct GNUNET_NETWORK_Handle *\r
GNUNET_NETWORK_socket_socket (int domain, int type, int protocol)\r
{\r
return ret;\r
}\r
\r
+/**\r
+ * Shut down socket operations\r
+ * @param desc socket\r
+ * @param how type of shutdown\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ */\r
int\r
GNUNET_NETWORK_socket_shutdown (struct GNUNET_NETWORK_Handle *desc,\r
int how)\r
SetErrnoFromWinsockError (WSAGetLastError ());\r
#endif\r
\r
- return ret;\r
+ return ret == 0 ? GNUNET_OK : GNUNET_SYSERR;\r
}\r
\r
+/**\r
+ * Make a non-inheritable to child processes\r
+ * @param socket\r
+ * @return GNUNET_OK on success, GNUNET_SYSERR otherwise\r
+ * @warning Not implemented on Windows\r
+ */\r
int\r
GNUNET_NETWORK_socket_set_inheritable (const struct GNUNET_NETWORK_Handle\r
*desc)\r
#endif\r
}\r
\r
+/**\r
+ * Reset FD set\r
+ * @param fds fd set\r
+ */\r
void\r
GNUNET_NETWORK_fdset_zero (struct GNUNET_NETWORK_FDSet *fds)\r
{\r
#endif\r
}\r
\r
+/**\r
+ * Add a socket to the FD set\r
+ * @param fds fd set\r
+ * @param desc socket to add\r
+ */\r
void\r
GNUNET_NETWORK_fdset_set (struct GNUNET_NETWORK_FDSet *fds,\r
const struct GNUNET_NETWORK_Handle *desc)\r
fds->nsds = desc->fd + 1;\r
}\r
\r
+/**\r
+ * Check whether a socket is part of the fd set\r
+ * @param fds fd set\r
+ * @param desc socket\r
+ */\r
int\r
GNUNET_NETWORK_fdset_isset (const struct GNUNET_NETWORK_FDSet *fds,\r
const struct GNUNET_NETWORK_Handle *desc)\r
return FD_ISSET (desc->fd, &fds->sds);\r
}\r
\r
+/**\r
+ * Add one fd set to another\r
+ * @param dst the fd set to add to\r
+ * @param src the fd set to add from\r
+ */\r
void\r
GNUNET_NETWORK_fdset_add (struct GNUNET_NETWORK_FDSet *dst,\r
const struct GNUNET_NETWORK_FDSet *src)\r
}\r
}\r
\r
+/**\r
+ * Copy one fd set to another\r
+ * @param to destination\r
+ * @param from source\r
+ */\r
void\r
GNUNET_NETWORK_fdset_copy (struct GNUNET_NETWORK_FDSet *to,\r
const struct GNUNET_NETWORK_FDSet *from)\r
#endif\r
}\r
\r
+/**\r
+ * Copy a native fd set\r
+ * @param to destination\r
+ * @param from native source set\r
+ * @param the biggest socket number in from + 1\r
+ */\r
void\r
GNUNET_NETWORK_fdset_copy_native (struct GNUNET_NETWORK_FDSet *to,\r
const fd_set * from, int nfds)\r
to->nsds = nfds;\r
}\r
\r
+/**\r
+ * Add a file handle to the fd set\r
+ * @param fds fd set\r
+ * @param h the file handle to add\r
+ */\r
void\r
GNUNET_NETWORK_fdset_handle_set (struct GNUNET_NETWORK_FDSet *fds,\r
const struct GNUNET_DISK_FileHandle *h)\r
#endif\r
}\r
\r
+/**\r
+ * Check if a file handle is part of an fd set\r
+ * @param fds fd set\r
+ * @param h file handle\r
+ * @return GNUNET_YES if the file handle is part of the set\r
+ */\r
int\r
GNUNET_NETWORK_fdset_handle_isset (const struct GNUNET_NETWORK_FDSet *fds,\r
const struct GNUNET_DISK_FileHandle *h)\r
#endif\r
}\r
\r
+/**\r
+ * Checks if two fd sets overlap\r
+ * @param fds1 first fd set\r
+ * @param fds2 second fd set\r
+ * @return GNUNET_YES if they do overlap, GNUNET_NO otherwise\r
+ */\r
int\r
GNUNET_NETWORK_fdset_overlap (const struct GNUNET_NETWORK_FDSet *fds1,\r
const struct GNUNET_NETWORK_FDSet *fds2)\r
return GNUNET_NO;\r
}\r
\r
+/**\r
+ * Creates an fd set\r
+ * @return a new fd set\r
+ */\r
struct GNUNET_NETWORK_FDSet *\r
GNUNET_NETWORK_fdset_create ()\r
{\r
return fds;\r
}\r
\r
+/**\r
+ * Releases the associated memory of an fd set\r
+ * @param fds fd set\r
+ */\r
void\r
GNUNET_NETWORK_fdset_destroy (struct GNUNET_NETWORK_FDSet *fds)\r
{\r
GNUNET_free (fds);\r
}\r
\r
+/**\r
+ * Check if sockets meet certain conditions\r
+ * @param rfds set of sockets to be checked for readability\r
+ * @param wfds set of sockets to be checked for writability\r
+ * @param efds set of sockets to be checked for exceptions\r
+ * @param timeout relative value when to return\r
+ * @return number of selected sockets, GNUNET_SYSERR on error\r
+ */\r
int\r
GNUNET_NETWORK_socket_select (struct GNUNET_NETWORK_FDSet *rfds,\r
struct GNUNET_NETWORK_FDSet *wfds,\r
else\r
FD_ZERO(&sock_except);\r
\r
- /*\r
- if (rfds)\r
- FD_COPY (&rfds->sds, &sock_read);\r
- else\r
- FD_ZERO(&sock_read);\r
-\r
- if (wfds)\r
- FD_COPY (&wfds->sds, &sock_write);\r
- else\r
- FD_ZERO(&sock_write);\r
-\r
- if (efds)\r
- FD_COPY (&efds->sds, &sock_except);\r
- else\r
- FD_ZERO(&sock_except);\r
-*/\r
-\r
/* multiplex between winsock select() and waiting on the handles */\r
\r
FD_ZERO (&aread);\r
#endif\r
}\r
\r
-/* end of network_socket.c */\r
+/* end of network.c */\r