c6b08f9fe48d6cb830a5089c27de217c245b19c7
[silc.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 /* returns 1 if IPADDRs are the same */
43 int net_ip_compare(IPADDR *ip1, IPADDR *ip2);
44
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);
58
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);
63
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);
68
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);
81
82 /* Get socket address/port */
83 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port);
84
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);
89
90 /* Get socket error */
91 int net_geterror(GIOChannel *handle);
92
93 /* Get name of TCP service */
94 char *net_getservbyport(int port);
95
96 int is_ipv4_address(const char *host);
97 int is_ipv6_address(const char *host);
98
99 #endif