00001
00002
00003
00004
00005 #ifndef _U_NET_H_
00006 #define _U_NET_H_
00007 #include "libu_conf.h"
00008
00009 #ifdef OS_UNIX
00010 #include <sys/types.h>
00011 #include <sys/socket.h>
00012 #include <netinet/in.h>
00013 #include <netinet/tcp.h>
00014 #ifdef HAVE_SYSUIO
00015 #include <sys/uio.h>
00016 #endif
00017 #include <arpa/inet.h>
00018 #include <netdb.h>
00019 #ifndef NO_UNIXSOCK
00020 #include <sys/un.h>
00021 #endif
00022 #endif
00023
00024 #ifdef OS_WIN
00025 #include <windows.h>
00026 #include <winsock.h>
00027 #include <winsock2.h>
00028 #include <ws2tcpip.h>
00029 #endif
00030
00031 #ifndef HAVE_IN_ADDR_T
00032 typedef unsigned long in_addr_t;
00033 #endif
00034
00035 #include <stdio.h>
00036 #include <u/os.h>
00037 #include <u/uri.h>
00038
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042
00043 #define U_NET_BACKLOG 300
00044
00045
00046
00047
00048
00049 struct u_net_addr_s
00050 {
00051 enum {
00052 U_NET_TYPE_MIN,
00053 U_NET_TCP4,
00054 U_NET_TCP6,
00055 U_NET_UDP4,
00056 U_NET_UDP6,
00057 U_NET_UNIX,
00058 U_NET_TYPE_MAX
00059 } type;
00060 union
00061 {
00062 struct sockaddr_in sin;
00063 #ifndef NO_IPV6
00064 struct sockaddr_in6 sin6;
00065 #endif
00066 #ifndef NO_UNIXSOCK
00067 struct sockaddr_un sun;
00068 #endif
00069 } sa;
00070 };
00071
00072 typedef struct u_net_addr_s u_net_addr_t;
00073
00074
00075 enum { U_NET_SSOCK, U_NET_CSOCK };
00076
00077
00078 typedef ssize_t (*iof_t) (int, void *, size_t);
00079
00080 int u_net_io (iof_t f, int sd, void *buf, size_t l, ssize_t *n, int *iseof);
00081
00088 #define u_net_write(sd, buf, nbytes, nw, iseof) \
00089 u_net_io((iof_t) write, sd, buf, nbytes, nw, iseof)
00090
00092 #define u_net_read(sd, buf, nbytes, nw, iseof) \
00093 u_net_io(read, sd, buf, nbytes, nw, iseof)
00094
00096 #define u_net_writen(sd, buf, nbytes) \
00097 u_net_io((iof_t) write, sd, buf, nbytes, 0, 0)
00098
00100 #define u_net_readn(sd, buf, nbytes) \
00101 u_net_io(read, sd, buf, nbytes, 0, 0)
00102
00107
00108 int u_net_sock (const char *uri, int mode);
00109 int u_net_sock_tcp (u_net_addr_t *addr, int mode);
00110 int u_net_sock_udp (u_net_addr_t *addr, int mode);
00111 int u_net_sock_unix (u_net_addr_t *addr, int mode);
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
00121
00122 int u_net_uri2addr (const char *uri, u_net_addr_t **pa);
00123 int u_net_uri2sin (u_uri_t *uri, struct sockaddr_in *sad);
00124 #ifndef NO_UNIXSOCK
00125 int u_net_uri2sun (u_uri_t *uri, struct sockaddr_un *sad);
00126 #endif
00127
00128
00129 int u_net_addr_new (int type, u_net_addr_t **pa);
00130 void u_net_addr_free (u_net_addr_t *addr);
00131
00132 #ifdef __cplusplus
00133 }
00134 #endif
00135
00136 #endif