net.h

00001 /*
00002  * Copyright (c) 2005-2012 by KoanLogic s.r.l. - All rights reserved.
00003  */
00004 
00005 #ifndef _U_NET_H_
00006 #define _U_NET_H_
00007 
00008 #include <u/libu_conf.h>
00009 
00010 #ifdef OS_UNIX
00011   #include <sys/types.h>
00012   #ifdef HAVE_SYS_SOCKET
00013     #include <sys/socket.h>
00014   #endif
00015   #ifdef HAVE_NETINET_IN
00016     #include <netinet/in.h>
00017   #endif
00018   #ifdef HAVE_NETINET_TCP
00019     #include <netinet/tcp.h>
00020   #endif
00021   #ifdef HAVE_NETINET_SCTP
00022     #include <netinet/sctp.h>
00023   #endif
00024   #ifdef HAVE_SYSUIO
00025     #include <sys/uio.h>
00026   #endif
00027   #include <arpa/inet.h>
00028   #include <netdb.h>
00029   #ifndef NO_UNIXSOCK
00030     #include <sys/un.h>
00031   #endif
00032 #endif  /* OS_UNIX */
00033 
00034 #ifdef HAVE_FCNTL
00035   #include <fcntl.h>
00036 #endif  /* HAVE_FCNTL */
00037 
00038 #ifdef OS_WIN
00039   #include <windows.h>
00040   /* #include <winsock.h> not compatible with ws2tcpip.h */
00041   #include <winsock2.h>
00042   #include <ws2tcpip.h>
00043 
00044   #define EINPROGRESS   WSAEWOULDBLOCK
00045   #define ETIMEDOUT     WSAETIMEDOUT
00046   #define EAFNOSUPPORT  WSAEAFNOSUPPORT
00047 #endif  /* OS_WIN */
00048 
00049 #ifndef HAVE_IN_ADDR_T
00050   typedef unsigned long in_addr_t;
00051 #endif
00052 
00053 #ifndef HAVE_SOCKLEN_T
00054   typedef int u_socklen_t;
00055 #else   /* HAVE_SOCKLEN_T */
00056   typedef socklen_t u_socklen_t;
00057 #endif  /* !HAVE_SOCKLEN_T */
00058 
00059 #ifndef HAVE_INET_ADDRSTRLEN
00060 #define U_INET_ADDRSTRLEN 16
00061 #else   /* HAVE_INET_ADDRSTRLEN */
00062 #define U_INET_ADDRSTRLEN   INET_ADDRSTRLEN
00063 #endif  /* !HAVE_INET_ADDRSTRLEN */
00064 
00065 #ifdef __cplusplus
00066 extern "C" {
00067 #endif
00068 
00075 #define U_NET_BACKLOG 300
00076 
00077 /* forward decl */
00078 struct u_net_addr_s;
00079 
00083 typedef struct u_net_addr_s u_net_addr_t;
00084 
00087 typedef enum { 
00088     U_NET_SSOCK = 0,    
00089     U_NET_CSOCK = 1     
00090 } u_net_mode_t;
00091 #define U_NET_IS_MODE(m) (m == U_NET_SSOCK || m == U_NET_CSOCK)
00092 
00093 
00097 typedef enum {
00098     U_NET_OPT_DONT_REUSE_ADDR = (1 << 0),
00101     U_NET_OPT_DONT_CONNECT = (1 << 1),  
00104     U_NET_OPT_SCTP_ONE_TO_MANY = (1 << 2),  
00107     U_NET_OPT_SCTP_DATA_IO_EVENT = (1 << 3),
00111     U_NET_OPT_SCTP_ASSOCIATION_EVENT = (1 << 4),
00115     U_NET_OPT_SCTP_ADDRESS_EVENT = (1 << 5),
00120     U_NET_OPT_SCTP_SEND_FAILURE_EVENT = (1 << 6),
00123     U_NET_OPT_SCTP_PEER_ERROR_EVENT = (1 << 7),
00126     U_NET_OPT_SCTP_SHUTDOWN_EVENT = (1 << 8),
00130     U_NET_OPT_SCTP_PARTIAL_DELIVERY_EVENT = (1 << 9),
00134     U_NET_OPT_SCTP_ADAPTATION_LAYER_EVENT = (1 << 10),
00137     U_NET_OPT_SCTP_AUTHENTICATION_EVENT = (1 << 11),
00140     U_NET_OPT_DGRAM_BROADCAST = (1 << 20)
00144 } u_net_opts_t;
00145 
00146 #ifdef HAVE_GETADDRINFO
00147 typedef struct addrinfo u_addrinfo_t;
00148 #else
00149 struct u_addrinfo_s;
00150 typedef struct u_addrinfo_s u_addrinfo_t;
00151 #endif  /* HAVE_GETADDRINFO */
00152 
00154 #define u_net_write(sd, buf, nbytes, nw, iseof) \
00155     u_io((iof_t) write, sd, buf, nbytes, nw, iseof)
00156 
00158 #define u_net_read(sd, buf, nbytes, nr, iseof) \
00159     u_io(read, sd, buf, nbytes, nr, iseof)
00160 
00162 #define u_net_writen(sd, buf, nbytes) \
00163     u_io((iof_t) write, sd, buf, nbytes, NULL, NULL)
00164 
00166 #define u_net_readn(sd, buf, nbytes) \
00167     u_io(read, sd, buf, nbytes, NULL, NULL)
00168 
00173 /* hi-level socket creation */
00174 int u_net_sd_ex (const char *uri, u_net_mode_t mode, int opts, 
00175         struct timeval *timeout);
00176 int u_net_sd (const char *uri, u_net_mode_t mode, int opts);
00177 int u_net_sd_by_addr_ex (u_net_addr_t *a, struct timeval *timeout);
00178 int u_net_sd_by_addr (u_net_addr_t *a);
00179 
00180 /* address ctor/dtor & co. */
00181 int u_net_uri2addr (const char *uri, u_net_mode_t mode, u_net_addr_t **pa);
00182 void u_net_addr_free (u_net_addr_t *a);
00183 int u_net_addr_can_accept (u_net_addr_t *a);
00184 
00185 /* address opts manipulation (applies to socket creation) */
00186 void u_net_addr_set_opts (u_net_addr_t *a, int opts);
00187 void u_net_addr_add_opts (u_net_addr_t *a, int opts);
00188 
00189 /* misc */
00190 int u_net_nagle_off (int sd);
00191 int u_net_set_nonblocking (int sd);
00192 int u_net_unset_nonblocking (int sd);
00193 
00194 /* networking syscall wrappers */
00195 int u_socket (int domain, int type, int protocol);
00196 int u_connect_ex (int sd, const struct sockaddr *addr, u_socklen_t addrlen,
00197         struct timeval *timeout);
00198 int u_connect (int sd, const struct sockaddr *addr, u_socklen_t addrlen);
00199 int u_listen (int sd, int backlog);
00200 int u_accept(int ld, struct sockaddr *addr, u_socklen_t *addrlen);
00201 int u_bind (int sd, const struct sockaddr *addr, u_socklen_t addrlen);
00202 int u_setsockopt (int sd, int lev, int name, const void *val, u_socklen_t len);
00203 int u_getsockopt (int sd, int lev, int name, void *val, u_socklen_t *len);
00204 
00205 /* addressing conversion */
00206 const char *u_sa_ntop (const struct sockaddr *sa, char *dst, size_t dst_len);
00207 const char *u_inet_ntop (int af, const void *src, char *dst, u_socklen_t len);
00208 
00209 #ifdef __cplusplus
00210 }
00211 #endif
00212 
00213 #endif /* !_U_NET_H_ */

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved