Main Page | Modules | Data Structures | Directories | File List | Data Fields

net.h

00001 /*
00002  * Copyright (c) 2005-2007 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 #include <sys/socket.h>
00013 #include <netinet/in.h>
00014 #include <netinet/tcp.h>
00015 #ifdef HAVE_SYSUIO
00016 #include <sys/uio.h>
00017 #endif
00018 #include <arpa/inet.h>
00019 #include <netdb.h>
00020 #ifndef NO_UNIXSOCK
00021 #include <sys/un.h>
00022 #endif
00023 #endif
00024 
00025 #ifdef OS_WIN
00026 #include <windows.h>
00027 #include <winsock.h>
00028 #include <winsock2.h>
00029 #include <ws2tcpip.h>
00030 #endif
00031 
00032 #ifndef HAVE_IN_ADDR_T
00033 typedef unsigned long in_addr_t;
00034 #endif
00035 
00036 #include <stdio.h>
00037 #include <u/missing.h>
00038 #include <u/toolbox/uri.h>
00039 #include <u/toolbox/misc.h>
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #define U_NET_BACKLOG 300
00046 
00047 /* The Net module defines the following (private) URI types: 
00048  *    {tc,ud}p[46]://<host>:<port> for TCP/UDP over IPv[46] addresses,
00049  *    unix://<abs_path> for UNIX IPC endpoints. 
00050  * Internally, they are translated into an u_net_addr_t structure. */
00051 struct u_net_addr_s
00052 {
00053     enum { 
00054         U_NET_TYPE_MIN,
00055         U_NET_TCP4,
00056         U_NET_TCP6,
00057         U_NET_UDP4,
00058         U_NET_UDP6,
00059         U_NET_UNIX,
00060         U_NET_TYPE_MAX
00061     } type;
00062     union
00063     {
00064         struct sockaddr_in  sin;
00065 #ifndef NO_IPV6
00066         struct sockaddr_in6 sin6;
00067 #endif
00068 #ifndef NO_UNIXSOCK
00069         struct sockaddr_un  sun;
00070 #endif /* OS_UNIX */
00071     } sa;
00072 };
00073 
00074 typedef struct u_net_addr_s u_net_addr_t;
00075 
00076 /* socket creation semantics: server or client (the 'mode' in u_net_sock) */
00077 enum { U_NET_SSOCK, U_NET_CSOCK };
00078 
00085 #define u_net_write(sd, buf, nbytes, nw, iseof) \
00086     u_io((iof_t) write, sd, buf, nbytes, nw, iseof)
00087 
00089 #define u_net_read(sd, buf, nbytes, nw, iseof) \
00090     u_io(read, sd, buf, nbytes, nw, iseof)
00091 
00093 #define u_net_writen(sd, buf, nbytes) \
00094     u_io((iof_t) write, sd, buf, nbytes, 0, 0)
00095 
00097 #define u_net_readn(sd, buf, nbytes) \
00098     u_io(read, sd, buf, nbytes, 0, 0)
00099 
00105 /* hi-level socket creation */
00106 int u_net_sock (const char *uri, int mode);
00107 int u_net_sock_tcp (u_net_addr_t *addr, int mode);
00108 int u_net_sock_udp (u_net_addr_t *addr, int mode);
00109 #ifndef NO_UNIXSOCK
00110 int u_net_sock_unix (u_net_addr_t *addr, int mode);
00111 #endif
00112 
00113 /* low-level socket creation */
00114 int u_net_tcp4_ssock (struct sockaddr_in *sad, int reuse, int backlog);
00115 int u_net_tcp4_csock (struct sockaddr_in *sad);
00116 #ifndef NO_IPV6
00117 int u_net_tcp6_ssock (struct sockaddr_in6 *sad, int reuse, int backlog);
00118 int u_net_tcp6_csock (struct sockaddr_in6 *sad);
00119 #endif
00120 #ifndef NO_UNIXSOCK
00121 int u_net_unix_ssock (struct sockaddr_un *sad, int backlog);
00122 int u_net_unix_csock (struct sockaddr_un *sad);
00123 #endif
00124 
00125 /* address translation: uri string -> u_uri -> u_net_addr -> struct sockaddr */
00126 int u_net_uri2addr (const char *uri, u_net_addr_t **pa);
00127 int u_net_uri2sin (u_uri_t *uri, struct sockaddr_in *sad);
00128 #ifndef NO_UNIXSOCK
00129 int u_net_uri2sun (const char *uri, struct sockaddr_un *sad);
00130 #endif /* OS_UNIX */
00131 
00132 /* u_net_addr */
00133 int u_net_addr_new (int type, u_net_addr_t **pa);
00134 void u_net_addr_free (u_net_addr_t *addr);
00135 
00136 #ifdef __cplusplus
00137 }
00138 #endif
00139 
00140 #endif /* !_U_NET_H_ */

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