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
00040 #ifdef __cplusplus
00041 extern "C" {
00042 #endif
00043
00044 #define U_NET_BACKLOG 300
00045
00046
00047
00048
00049
00050 struct u_net_addr_s
00051 {
00052 enum {
00053 U_NET_TYPE_MIN,
00054 U_NET_TCP4,
00055 U_NET_TCP6,
00056 U_NET_UDP4,
00057 U_NET_UDP6,
00058 U_NET_UNIX,
00059 U_NET_TYPE_MAX
00060 } type;
00061 union
00062 {
00063 struct sockaddr_in sin;
00064 #ifndef NO_IPV6
00065 struct sockaddr_in6 sin6;
00066 #endif
00067 #ifndef NO_UNIXSOCK
00068 struct sockaddr_un sun;
00069 #endif
00070 } sa;
00071 };
00072
00073 typedef struct u_net_addr_s u_net_addr_t;
00074
00075
00076 enum { U_NET_SSOCK, U_NET_CSOCK };
00077
00078
00079 typedef ssize_t (*iof_t) (int, void *, size_t);
00080
00081 int u_net_io (iof_t f, int sd, void *buf, size_t l, ssize_t *n, int *iseof);
00082
00089 #define u_net_write(sd, buf, nbytes, nw, iseof) \
00090 u_net_io((iof_t) write, sd, buf, nbytes, nw, iseof)
00091
00093 #define u_net_read(sd, buf, nbytes, nw, iseof) \
00094 u_net_io(read, sd, buf, nbytes, nw, iseof)
00095
00097 #define u_net_writen(sd, buf, nbytes) \
00098 u_net_io((iof_t) write, sd, buf, nbytes, 0, 0)
00099
00101 #define u_net_readn(sd, buf, nbytes) \
00102 u_net_io(read, sd, buf, nbytes, 0, 0)
00103
00108
00109 int u_net_sock (const char *uri, int mode);
00110 int u_net_sock_tcp (u_net_addr_t *addr, int mode);
00111 int u_net_sock_udp (u_net_addr_t *addr, int mode);
00112 #ifndef NO_UNIXSOCK
00113 int u_net_sock_unix (u_net_addr_t *addr, int mode);
00114 #endif
00115
00116
00117 int u_net_tcp4_ssock (struct sockaddr_in *sad, int reuse, int backlog);
00118 int u_net_tcp4_csock (struct sockaddr_in *sad);
00119 #ifndef NO_IPV6
00120 int u_net_tcp6_ssock (struct sockaddr_in6 *sad, int reuse, int backlog);
00121 int u_net_tcp6_csock (struct sockaddr_in6 *sad);
00122 #endif
00123 #ifndef NO_UNIXSOCK
00124 int u_net_unix_ssock (struct sockaddr_un *sad, int backlog);
00125 int u_net_unix_csock (struct sockaddr_un *sad);
00126 #endif
00127
00128
00129 int u_net_uri2addr (const char *uri, u_net_addr_t **pa);
00130 int u_net_uri2sin (u_uri_t *uri, struct sockaddr_in *sad);
00131 #ifndef NO_UNIXSOCK
00132 int u_net_uri2sun (const char *uri, struct sockaddr_un *sad);
00133 #endif
00134
00135
00136 int u_net_addr_new (int type, u_net_addr_t **pa);
00137 void u_net_addr_free (u_net_addr_t *addr);
00138
00139 #ifdef __cplusplus
00140 }
00141 #endif
00142
00143 #endif