00001
00002
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
00048
00049
00050
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
00071 } sa;
00072 };
00073
00074 typedef struct u_net_addr_s u_net_addr_t;
00075
00076
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
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
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
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
00131
00132
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