Mercurial > dovecot > original-hg > dovecot-1.2
annotate src/lib/network.h @ 6410:e4eb71ae8e96 HEAD
Changed .h ifdef/defines to use <NAME>_H format.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Sun, 16 Sep 2007 11:31:27 +0300 |
parents | 74a6130211c2 |
children | e0007da64cdc |
rev | line source |
---|---|
6410
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
1 #ifndef NETWORK_H |
e4eb71ae8e96
Changed .h ifdef/defines to use <NAME>_H format.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
2 #define NETWORK_H |
0 | 3 |
4 #ifndef WIN32 | |
5 # include <sys/socket.h> | |
6 # include <netinet/in.h> | |
7 # include <netdb.h> | |
8 # include <arpa/inet.h> | |
9 #endif | |
10 | |
11 #ifdef HAVE_SOCKS_H | |
12 #include <socks.h> | |
13 #endif | |
14 | |
15 #ifndef AF_INET6 | |
16 # ifdef PF_INET6 | |
17 # define AF_INET6 PF_INET6 | |
18 # else | |
19 # define AF_INET6 10 | |
20 # endif | |
21 #endif | |
22 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
23 struct ip_addr { |
0 | 24 unsigned short family; |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
25 union { |
0 | 26 #ifdef HAVE_IPV6 |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
27 struct in6_addr ip6; |
0 | 28 #endif |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
29 struct in_addr ip4; |
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
30 } u; |
0 | 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_V4(ip) ((ip)->family == AF_INET) | |
41 #define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6) | |
42 | |
43 /* returns 1 if IPADDRs are the same */ | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
44 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2); |
5865 | 45 unsigned int net_ip_hash(const struct ip_addr *ip); |
0 | 46 |
47 /* Connect to socket with ip address */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
48 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
49 const struct ip_addr *my_ip); |
0 | 50 /* Connect to named UNIX socket */ |
51 int net_connect_unix(const char *path); | |
52 /* Disconnect socket */ | |
53 void net_disconnect(int fd); | |
54 | |
55 /* Set socket blocking/nonblocking */ | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
56 void net_set_nonblock(int fd, bool nonblock); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
57 /* Set TCP_CORK if supported, ie. don't send out partial frames. |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
58 Returns 0 if ok, -1 if failed. */ |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
59 int net_set_cork(int fd, bool cork); |
0 | 60 |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
61 /* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
62 include IPv4 depending on the system (Linux yes, BSD no). */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
63 void net_get_ip_any4(struct ip_addr *ip); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
64 void net_get_ip_any6(struct ip_addr *ip); |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
65 |
0 | 66 /* Listen for connections on a socket */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
67 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog); |
0 | 68 /* Listen for connections on an UNIX socket */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
69 int net_listen_unix(const char *path, int backlog); |
5358
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
70 /* Accept a connection on a socket. Returns -1 if the connection got closed, |
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
71 -2 for other failures */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
72 int net_accept(int fd, struct ip_addr *addr, unsigned int *port); |
0 | 73 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
74 /* Read data from socket, return number of bytes read, |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
75 -1 = error, -2 = disconnected */ |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
76 ssize_t net_receive(int fd, void *buf, size_t len); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
77 /* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */ |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
78 ssize_t net_transmit(int fd, const void *data, size_t len); |
0 | 79 |
80 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
81 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
82 int net_gethostbyname(const char *addr, struct ip_addr **ips, |
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
83 unsigned int *ips_count); |
0 | 84 /* get error of net_gethostname() */ |
85 const char *net_gethosterror(int error); | |
86 /* return TRUE if host lookup failed because it didn't exist (ie. not | |
87 some error with name server) */ | |
88 int net_hosterror_notfound(int error); | |
89 | |
2098 | 90 /* Get socket local address/port */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
91 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port); |
2098 | 92 /* Get socket remote address/port */ |
93 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port); | |
0 | 94 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
95 /* Returns ip_addr as string, or NULL if ip is invalid. */ |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
96 const char *net_ip2addr(const struct ip_addr *ip); |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
97 /* char* -> struct ip_addr translation. */ |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
98 int net_addr2ip(const char *addr, struct ip_addr *ip); |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
99 /* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
100 successful, -1 if the source address isn't IPv6 mapped IPv4 address. */ |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
101 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src, |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
102 struct ip_addr *dest); |
0 | 103 |
104 /* Get socket error */ | |
105 int net_geterror(int fd); | |
106 | |
107 /* Get name of TCP service */ | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
108 const char *net_getservbyport(unsigned short port); |
0 | 109 |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
110 bool is_ipv4_address(const char *addr); |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
111 bool is_ipv6_address(const char *addr); |
0 | 112 |
113 #endif |