Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / core / network.h
1 #ifndef __NETWORK_H
2 #define __NETWORK_H
3
4 #ifdef HAVE_SOCKS_H
5 #include <socks.h>
6 #endif
7
8 #include <sys/types.h>
9 #ifndef WIN32
10 #  include <sys/socket.h>
11 #  include <netinet/in.h>
12 #  include <netdb.h>
13 #  include <arpa/inet.h>
14 #endif
15
16 #ifndef AF_INET6
17 #  ifdef PF_INET6
18 #    define AF_INET6 PF_INET6
19 #  else
20 #    define AF_INET6 10
21 #  endif
22 #endif
23
24 struct _IPADDR {
25         unsigned short family;
26 #ifdef HAVE_IPV6
27         struct in6_addr ip;
28 #else
29         struct in_addr ip;
30 #endif
31 };
32
33 /* maxmimum string length of IP address */
34 #ifdef HAVE_IPV6
35 #  define MAX_IP_LEN INET6_ADDRSTRLEN
36 #else
37 #  define MAX_IP_LEN 20
38 #endif
39
40 #define IPADDR_IS_V6(ip) ((ip)->family != AF_INET)
41
42 extern IPADDR ip4_any;
43
44 /* returns 1 if IPADDRs are the same */
45 int net_ip_compare(IPADDR *ip1, IPADDR *ip2);
46
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);
60
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);
65
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);
70
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);
83
84 /* Get socket address/port */
85 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port);
86
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);
91
92 /* Get socket error */
93 int net_geterror(GIOChannel *handle);
94
95 /* Get name of TCP service */
96 char *net_getservbyport(int port);
97
98 int is_ipv4_address(const char *host);
99 int is_ipv6_address(const char *host);
100
101 #endif