10 # include <sys/socket.h>
11 # include <netinet/in.h>
13 # include <arpa/inet.h>
18 # define AF_INET6 PF_INET6
25 unsigned short family;
33 /* maxmimum string length of IP address */
35 # define MAX_IP_LEN INET6_ADDRSTRLEN
37 # define MAX_IP_LEN 20
40 #define IPADDR_IS_V6(ip) ((ip)->family != AF_INET)
42 /* returns 1 if IPADDRs are the same */
43 int net_ip_compare(IPADDR *ip1, IPADDR *ip2);
45 /* Connect to socket */
46 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip);
47 /* Connect to socket with ip address and SSL*/
48 GIOChannel *net_connect_ip_ssl(IPADDR *ip, int port, IPADDR *my_ip);
49 /* Connect to socket with ip address */
50 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip);
51 /* Connect to named UNIX socket */
52 GIOChannel *net_connect_unix(const char *path);
53 /* Disconnect socket */
54 void net_disconnect(GIOChannel *handle);
55 /* Try to let the other side close the connection, if it still isn't
56 disconnected after certain amount of time, close it ourself */
57 void net_disconnect_later(GIOChannel *handle);
59 /* Listen for connections on a socket */
60 GIOChannel *net_listen(IPADDR *my_ip, int *port);
61 /* Accept a connection on a socket */
62 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port);
64 /* Read data from socket, return number of bytes read, -1 = error */
65 int net_receive(GIOChannel *handle, char *buf, int len);
66 /* Transmit data, return number of bytes sent, -1 = error */
67 int net_transmit(GIOChannel *handle, const char *data, int len);
69 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
70 If ip->family is 0, the address wasn't found.
71 Returns 0 = ok, others = error code for net_gethosterror() */
72 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6);
73 /* Get name for host, *name should be g_free()'d unless it's NULL.
74 Return values are the same as with net_gethostbyname() */
75 int net_gethostbyaddr(IPADDR *ip, char **name);
76 /* get error of net_gethostname() */
77 const char *net_gethosterror(int error);
78 /* return TRUE if host lookup failed because it didn't exist (ie. not
79 some error with name server) */
80 int net_hosterror_notfound(int error);
82 /* Get socket address/port */
83 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port);
85 /* IPADDR -> char* translation. `host' must be at least MAX_IP_LEN bytes */
86 int net_ip2host(IPADDR *ip, char *host);
87 /* char* -> IPADDR translation. */
88 int net_host2ip(const char *host, IPADDR *ip);
90 /* Get socket error */
91 int net_geterror(GIOChannel *handle);
93 /* Get name of TCP service */
94 char *net_getservbyport(int port);
96 int is_ipv4_address(const char *host);
97 int is_ipv6_address(const char *host);