00001 /* 00002 This file is part of GNUnet. 00003 (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Christian Grothoff (and other contributing authors) 00004 00005 GNUnet is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation; either version 2, or (at your 00008 option) any later version. 00009 00010 GNUnet is distributed in the hope that it will be useful, but 00011 WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with GNUnet; see the file COPYING. If not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 00021 /** 00022 * @file include/gnunet_util_network.h 00023 * @brief networking interface to libgnunetutil 00024 * 00025 * @author Christian Grothoff 00026 * @author Krista Bennett 00027 * @author Gerd Knorr <kraxel@bytesex.org> 00028 * @author Ioana Patrascu 00029 * @author Tzvetan Horozov 00030 */ 00031 00032 #ifndef GNUNET_UTIL_NETWORK_H 00033 #define GNUNET_UTIL_NETWORK_H 00034 00035 #include "gnunet_util_config.h" 00036 #include "gnunet_util_string.h" 00037 #include "gnunet_util_os.h" 00038 #include "gnunet_util_threads.h" 00039 00040 #ifdef __cplusplus 00041 extern "C" 00042 { 00043 #if 0 /* keep Emacsens' auto-indent happy */ 00044 } 00045 #endif 00046 #endif 00047 00048 /** 00049 * We use an unsigned short in the protocol header, thus: 00050 */ 00051 #define GNUNET_MAX_BUFFER_SIZE 65536 00052 00053 /** 00054 * @brief Specify low-level network IO behavior 00055 */ 00056 typedef enum 00057 { 00058 00059 /** 00060 * Do not block. 00061 */ 00062 GNUNET_NC_NONBLOCKING = 0x000, 00063 00064 /** 00065 * Call may block. 00066 */ 00067 GNUNET_NC_BLOCKING = 0x001, 00068 00069 /** 00070 * Ignore interrupts (re-try if operation 00071 * was aborted due to interrupt) 00072 */ 00073 GNUNET_NC_IGNORE_INT = 0x010, 00074 00075 /** 00076 * Always try to read/write the maximum 00077 * amount of data (using possibly multiple 00078 * calls). Only return on non-interrupt 00079 * error or if completely done. 00080 */ 00081 GNUNET_NC_COMPLETE_TRANSFER = 0x111, 00082 00083 } GNUNET_NC_KIND; 00084 00085 /** 00086 * @brief 512-bit hashcode 00087 */ 00088 typedef struct 00089 { 00090 unsigned int bits[512 / 8 / sizeof (unsigned int)]; /* = 16 */ 00091 } GNUNET_HashCode; 00092 00093 /** 00094 * The identity of the host (basically the SHA-512 hashcode of 00095 * it's public key). 00096 */ 00097 typedef struct 00098 { 00099 GNUNET_HashCode hashPubKey; 00100 } GNUNET_PeerIdentity; 00101 00102 /** 00103 * Header for all Client-Server communications. 00104 */ 00105 typedef struct 00106 { 00107 00108 /** 00109 * The length of the struct (in bytes, including the length field itself) 00110 */ 00111 unsigned short size; 00112 00113 /** 00114 * The type of the message (XX_CS_PROTO_XXXX) 00115 */ 00116 unsigned short type; 00117 00118 } GNUNET_MessageHeader; 00119 00120 /** 00121 * Client-server communication: simple return value 00122 */ 00123 typedef struct 00124 { 00125 00126 /** 00127 * The CS header (values: sizeof(GNUNET_MessageReturnValue) + error-size, GNUNET_CS_PROTO_RETURN_VALUE) 00128 */ 00129 GNUNET_MessageHeader header; 00130 00131 /** 00132 * The return value (network byte order) 00133 */ 00134 int return_value; 00135 00136 } GNUNET_MessageReturnValue; 00137 00138 /** 00139 * Client-server communication: simple error message 00140 */ 00141 typedef struct 00142 { 00143 00144 /** 00145 * The CS header. 00146 */ 00147 GNUNET_MessageHeader header; 00148 00149 /** 00150 * The return value (network byte order) 00151 */ 00152 GNUNET_GE_KIND kind; 00153 00154 } GNUNET_MessageReturnErrorMessage; 00155 00156 /** 00157 * @brief IPV4 network in CIDR notation. 00158 */ 00159 struct GNUNET_IPv4NetworkSet; 00160 00161 /** 00162 * @brief IPV6 network in CIDR notation. 00163 */ 00164 struct GNUNET_IPv6NetworkSet; 00165 00166 /** 00167 * @brief handle for a system socket 00168 */ 00169 struct GNUNET_SocketHandle; 00170 00171 /** 00172 * @brief handle for a select manager 00173 */ 00174 struct GNUNET_SelectHandle; 00175 00176 /** 00177 * @brief callback for handling messages received by select 00178 * 00179 * @param sock socket on which the message was received 00180 * (should ONLY be used to queue reply using select methods) 00181 * @return GNUNET_OK if message was valid, GNUNET_SYSERR if corresponding 00182 * socket should be closed 00183 */ 00184 typedef int (*GNUNET_SelectMessageHandler) (void *mh_cls, 00185 struct GNUNET_SelectHandle * sh, 00186 struct GNUNET_SocketHandle * sock, 00187 void *sock_ctx, 00188 const GNUNET_MessageHeader * msg); 00189 00190 /** 00191 * We've accepted a connection, check that 00192 * the connection is valid and create the 00193 * corresponding sock_ctx for the new 00194 * connection. 00195 * 00196 * @param addr the address of the other side as reported by OS 00197 * @param addr_len the size of the address 00198 * @return NULL to reject connection, otherwise value of sock_ctx 00199 * for the new connection 00200 */ 00201 typedef void *(*GNUNET_SelectAcceptHandler) (void *ah_cls, 00202 struct GNUNET_SelectHandle * sh, 00203 struct GNUNET_SocketHandle * 00204 sock, const void *addr, 00205 unsigned int addr_len); 00206 00207 /** 00208 * Select has been forced to close a connection. 00209 * Free the associated context. 00210 */ 00211 typedef void (*GNUNET_SelectCloseHandler) (void *ch_cls, 00212 struct GNUNET_SelectHandle * sh, 00213 struct GNUNET_SocketHandle * sock, 00214 void *sock_ctx); 00215 00216 /* *********************** endianess conversion ************* */ 00217 00218 /** 00219 * Convert a long-long to host-byte-order. 00220 * @param n the value in network byte order 00221 * @return the same value in host byte order 00222 */ 00223 unsigned long long GNUNET_ntohll (unsigned long long n); 00224 00225 /** 00226 * Convert a long long to network-byte-order. 00227 * @param n the value in host byte order 00228 * @return the same value in network byte order 00229 */ 00230 unsigned long long GNUNET_htonll (unsigned long long n); 00231 00232 /* ***************** basic parsing **************** */ 00233 00234 /** 00235 * Parse a network specification. The argument specifies 00236 * a list of networks. The format is 00237 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated 00238 * with a semicolon). The network must be given in dotted-decimal 00239 * notation. The netmask can be given in CIDR notation (/16) or 00240 * in dotted-decimal (/255.255.0.0). 00241 * <p> 00242 * @param routeList a string specifying the forbidden networks 00243 * @return the converted list, NULL if the syntax is flawed 00244 */ 00245 struct GNUNET_IPv4NetworkSet *GNUNET_parse_ipv4_network_specification (struct 00246 GNUNET_GE_Context 00247 *ectx, 00248 const 00249 char 00250 *routeList); 00251 00252 /** 00253 * Parse a network specification. The argument specifies 00254 * a list of networks. The format is 00255 * <tt>[network/netmask;]*</tt> (no whitespace, must be terminated 00256 * with a semicolon). The network must be given in dotted-decimal 00257 * notation. The netmask can be given in CIDR notation (/16) or 00258 * in dotted-decimal (/255.255.0.0). 00259 * <p> 00260 * @param routeList a string specifying the forbidden networks 00261 * @return the converted list, NULL if the syntax is flawed 00262 */ 00263 struct GNUNET_IPv6NetworkSet *GNUNET_parse_ipv6_network_specification (struct 00264 GNUNET_GE_Context 00265 *ectx, 00266 const 00267 char 00268 *routeList); 00269 00270 /** 00271 * Actual definitions will be in system header files. 00272 */ 00273 struct sockaddr; 00274 struct in_addr; 00275 struct in6_addr; 00276 00277 00278 /** 00279 * Check if the given IP address is in the list of 00280 * IP addresses. 00281 * @param list a list of networks 00282 * @param ip the IP to check (in network byte order) 00283 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 00284 */ 00285 int GNUNET_check_ipv4_listed (const struct GNUNET_IPv4NetworkSet *list, 00286 const struct in_addr *ip); 00287 00288 /** 00289 * Check if the given IP address is in the list of 00290 * IP addresses. 00291 * @param list a list of networks 00292 * @param ip the IP to check (in network byte order) 00293 * @return GNUNET_NO if the IP is not in the list, GNUNET_YES if it it is 00294 */ 00295 int GNUNET_check_ipv6_listed (const struct GNUNET_IPv6NetworkSet *list, 00296 const struct in6_addr *ip); 00297 00298 00299 /* ********************* low-level socket operations **************** */ 00300 00301 /** 00302 * Create a socket handle by boxing an OS socket. 00303 * The OS socket should henceforth be no longer used 00304 * directly. GNUNET_socket_destroy will close it. 00305 */ 00306 struct GNUNET_SocketHandle *GNUNET_socket_create (struct GNUNET_GE_Context 00307 *ectx, 00308 struct GNUNET_LoadMonitor 00309 *mon, int osSocket); 00310 00311 /** 00312 * Close the socket (does NOT destroy it) 00313 */ 00314 void GNUNET_socket_close (struct GNUNET_SocketHandle *s); 00315 00316 /** 00317 * Destroy the socket (also closes it). 00318 */ 00319 void GNUNET_socket_destroy (struct GNUNET_SocketHandle *s); 00320 00321 /** 00322 * Depending on doBlock, enable or disable the nonblocking mode 00323 * of socket s. 00324 * 00325 * @return Upon successful completion, it returns zero. 00326 * @return Otherwise -1 is returned. 00327 */ 00328 int GNUNET_socket_set_blocking (struct GNUNET_SocketHandle *s, int doBlock); 00329 00330 /** 00331 * Check whether the socket is blocking 00332 * @param s the socket 00333 * @return GNUNET_YES if blocking, GNUNET_NO non-blocking 00334 */ 00335 int GNUNET_socket_test_blocking (struct GNUNET_SocketHandle *s); 00336 00337 /** 00338 * Do a read on the given socket. 00339 * 00340 * @brief reads at most max bytes to buf. Interrupts are IGNORED. 00341 * @param s socket 00342 * @param nc 00343 * @param buf buffer 00344 * @param max maximum number of bytes to read 00345 * @param read number of bytes actually read. 00346 * 0 is returned if no more bytes can be read 00347 * @return GNUNET_SYSERR on error, GNUNET_YES on success or GNUNET_NO if the operation 00348 * would have blocked 00349 */ 00350 int GNUNET_socket_recv (struct GNUNET_SocketHandle *s, 00351 GNUNET_NC_KIND nc, void *buf, size_t max, 00352 size_t * read); 00353 00354 int GNUNET_socket_recv_from (struct GNUNET_SocketHandle *s, 00355 GNUNET_NC_KIND nc, 00356 void *buf, 00357 size_t max, 00358 size_t * read, char *from, 00359 unsigned int *fromlen); 00360 00361 /** 00362 * Do a write on the given socket. 00363 * Write at most max bytes from buf. 00364 * 00365 * @param s socket 00366 * @param buf buffer to send 00367 * @param max maximum number of bytes to send 00368 * @param sent number of bytes actually sent 00369 * @return GNUNET_SYSERR on error, GNUNET_YES on success or 00370 * GNUNET_NO if the operation would have blocked. 00371 */ 00372 int GNUNET_socket_send (struct GNUNET_SocketHandle *s, 00373 GNUNET_NC_KIND nc, const void *buf, size_t max, 00374 size_t * sent); 00375 00376 int GNUNET_socket_send_to (struct GNUNET_SocketHandle *s, 00377 GNUNET_NC_KIND nc, 00378 const void *buf, 00379 size_t max, 00380 size_t * sent, const char *dst, 00381 unsigned int dstlen); 00382 00383 /** 00384 * Check if socket is valid 00385 * @return GNUNET_YES if valid, GNUNET_NO otherwise 00386 */ 00387 int GNUNET_socket_test_valid (struct GNUNET_SocketHandle *s); 00388 00389 00390 /* ********************* select operations **************** */ 00391 00392 00393 /** 00394 * Start a select thread that will accept connections 00395 * from the given socket and pass messages read to the 00396 * given message handler. 00397 * 00398 * @param desc for debugging (description) 00399 * @param sock the listen socket 00400 * @param max_addr_len maximum expected length of addresses for 00401 * connections accepted on the given socket 00402 * @param timeout after how long should inactive connections be 00403 * closed? Use 0 for no timeout. The specified timeout 00404 * will be the default for all new connections; 00405 * after (!) returning (!) from the accept handler, 00406 * clients can change the timeout of an individual 00407 * socket using GNUNET_select_change_timeout. 00408 * @param mon maybe NULL 00409 * @param memory_quota amount of memory available for 00410 * queueing messages (in bytes) 00411 * @param socket_quota how many connections do we 00412 * accept at most? 0 for unbounded 00413 * @return NULL on error 00414 */ 00415 struct GNUNET_SelectHandle *GNUNET_select_create (const char *desc, 00416 int is_udp, 00417 struct GNUNET_GE_Context 00418 *ectx, 00419 struct GNUNET_LoadMonitor 00420 *mon, int sock, 00421 unsigned int max_addr_len, 00422 GNUNET_CronTime timeout, 00423 GNUNET_SelectMessageHandler 00424 mh, void *mh_cls, 00425 GNUNET_SelectAcceptHandler 00426 ah, void *ah_cls, 00427 GNUNET_SelectCloseHandler 00428 ch, void *ch_cls, 00429 unsigned int memory_quota, 00430 int socket_quota); 00431 00432 /** 00433 * Terminate the select thread, close the socket and 00434 * all associated connections. 00435 */ 00436 void GNUNET_select_destroy (struct GNUNET_SelectHandle *sh); 00437 00438 /** 00439 * Queue the given message with the select thread. 00440 * 00441 * @param mayBlock if GNUNET_YES, blocks this thread until message 00442 * has been sent 00443 * @param force message is important, queue even if 00444 * there is not enough space 00445 * @return GNUNET_OK if the message was sent or queued 00446 * GNUNET_NO if there was not enough memory to queue it, 00447 * GNUNET_SYSERR if the sock does not belong with this select 00448 */ 00449 int GNUNET_select_write (struct GNUNET_SelectHandle *sh, 00450 struct GNUNET_SocketHandle *sock, 00451 const GNUNET_MessageHeader * msg, int mayBlock, 00452 int force); 00453 00454 00455 /** 00456 * Would select queue or send the given message at this time? 00457 * 00458 * @param mayBlock if GNUNET_YES, blocks this thread until message 00459 * has been sent 00460 * @param size size of the message 00461 * @param force message is important, queue even if 00462 * there is not enough space 00463 * @return GNUNET_OK if the message would be sent or queued, 00464 * GNUNET_NO if there was not enough memory to queue it, 00465 * GNUNET_SYSERR if the sock does not belong with this select 00466 */ 00467 int GNUNET_select_test_write_now (struct GNUNET_SelectHandle *sh, 00468 struct GNUNET_SocketHandle *sock, 00469 unsigned int size, int mayBlock, int force); 00470 00471 /** 00472 * Add another (already connected) socket to the set of 00473 * sockets managed by the select. 00474 */ 00475 int GNUNET_select_connect (struct GNUNET_SelectHandle *sh, 00476 struct GNUNET_SocketHandle *sock, void *sock_ctx); 00477 00478 00479 /** 00480 * Change the timeout for this socket to a custom 00481 * value. Use 0 to use the default timeout for 00482 * this select. 00483 */ 00484 int GNUNET_select_change_timeout (struct GNUNET_SelectHandle *sh, 00485 struct GNUNET_SocketHandle *sock, 00486 GNUNET_CronTime timeout); 00487 00488 /** 00489 */ 00490 int GNUNET_select_update_closure (struct GNUNET_SelectHandle *sh, 00491 struct GNUNET_SocketHandle *sock, 00492 void *old_sock_ctx, void *new_sock_ctx); 00493 00494 /** 00495 * Close the associated socket and remove it from the 00496 * set of sockets managed by select. 00497 */ 00498 int GNUNET_select_disconnect (struct GNUNET_SelectHandle *sh, 00499 struct GNUNET_SocketHandle *sock); 00500 00501 /** 00502 * Convert a string to an IP address. May block! 00503 * 00504 * @param hostname the hostname to resolve 00505 * @param domain AF_INET or AF_INET6; use AF_UNSPEC for "any" 00506 * @param *sa should be of type "struct sockaddr*" and 00507 * will be set to the IP address on success; 00508 * if *sa is NULL, sufficient space will be 00509 * allocated. 00510 * @param socklen will be set to the length of *sa. 00511 * If *sa is not NULL, socklen will be checked 00512 * to see if sufficient space is provided and 00513 * updated to the amount of space actually 00514 * required/used. 00515 * @return GNUNET_OK on success, GNUNET_SYSERR on error 00516 */ 00517 int 00518 GNUNET_get_ip_from_hostname (struct GNUNET_GE_Context *ectx, 00519 const char *hostname, 00520 int domain, 00521 struct sockaddr **sa, unsigned int *socklen); 00522 00523 /** 00524 * Get an IP address as a string (works for both IPv4 and IPv6). Note 00525 * that the resolution happens asynchronously and that the first call 00526 * may not immediately result in the FQN (but instead in a 00527 * human-readable IP address). 00528 * 00529 * @param sa should be of type "struct sockaddr*" 00530 */ 00531 char *GNUNET_get_ip_as_string (const void *sa, 00532 unsigned int salen, int do_resolve); 00533 00534 /** 00535 * Get the IP address for the local machine. 00536 * @return NULL on error, IP as string otherwise 00537 */ 00538 char *GNUNET_get_local_ip (struct GNUNET_GC_Configuration *cfg, 00539 struct GNUNET_GE_Context *ectx, 00540 struct in_addr *addr); 00541 00542 00543 /** 00544 * Change a file descriptor that refers to a pipe 00545 * to non-blocking IO. 00546 * @return GNUNET_OK on success 00547 */ 00548 int GNUNET_pipe_make_nonblocking (struct GNUNET_GE_Context *ectx, int pipe); 00549 00550 #if 0 /* keep Emacsens' auto-indent happy */ 00551 { 00552 #endif 00553 #ifdef __cplusplus 00554 } 00555 #endif 00556 00557 00558 /* ifndef GNUNET_UTIL_NETWORK_H */ 00559 #endif 00560 /* end of gnunet_util_network.h */
1.5.1