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