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 */
48 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip);
49 /* Disconnect socket */
50 void net_disconnect(GIOChannel *handle);
51 /* Try to let the other side close the connection, if it still isn't
52 disconnected after certain amount of time, close it ourself */
53 void net_disconnect_later(GIOChannel *handle);
55 /* Listen for connections on a socket */
56 GIOChannel *net_listen(IPADDR *my_ip, int *port);
57 /* Accept a connection on a socket */
58 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port);
60 /* Read data from socket, return number of bytes read, -1 = error */
61 int net_receive(GIOChannel *handle, char *buf, int len);
62 /* Transmit data, return number of bytes sent, -1 = error */
63 int net_transmit(GIOChannel *handle, const char *data, int len);
65 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
66 If ip->family is 0, the address wasn't found.
67 Returns 0 = ok, others = error code for net_gethosterror() */
68 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6);
69 /* Get name for host, *name should be g_free()'d unless it's NULL.
70 Return values are the same as with net_gethostbyname() */
71 int net_gethostbyaddr(IPADDR *ip, char **name);
72 /* get error of net_gethostname() */
73 const char *net_gethosterror(int error);
74 /* return TRUE if host lookup failed because it didn't exist (ie. not
75 some error with name server) */
76 int net_hosterror_notfound(int error);
78 /* Get socket address/port */
79 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port);
81 /* IPADDR -> char* translation. `host' must be at least MAX_IP_LEN bytes */
82 int net_ip2host(IPADDR *ip, char *host);
83 /* char* -> IPADDR translation. */
84 int net_host2ip(const char *host, IPADDR *ip);
86 /* Get socket error */
87 int net_geterror(GIOChannel *handle);
89 /* Get name of TCP service */
90 char *net_getservbyport(int port);
92 int is_ipv4_address(const char *host);
93 int is_ipv6_address(const char *host);