~ubuntu-branches/ubuntu/wily/dovecot/wily

« back to all changes in this revision

Viewing changes to src/lib/net.h

  • Committer: Package Import Robot
  • Author(s): Jaldhar H. Vyas
  • Date: 2013-09-09 00:57:32 UTC
  • mfrom: (1.13.11)
  • mto: (4.8.5 experimental) (1.16.1)
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: package-import@ubuntu.com-20130909005732-dn1eell8srqbhh0e
Tags: upstream-2.2.5
ImportĀ upstreamĀ versionĀ 2.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef NET_H
 
2
#define NET_H
 
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
 
 
23
struct ip_addr {
 
24
        unsigned short family;
 
25
        union {
 
26
#ifdef HAVE_IPV6
 
27
                struct in6_addr ip6;
 
28
#endif
 
29
                struct in_addr ip4;
 
30
        } u;
 
31
};
 
32
ARRAY_DEFINE_TYPE(ip_addr, struct ip_addr);
 
33
 
 
34
struct net_unix_cred {
 
35
        uid_t uid;
 
36
        gid_t gid;
 
37
        pid_t pid;
 
38
};
 
39
 
 
40
/* maxmimum string length of IP address */
 
41
#ifdef HAVE_IPV6
 
42
#  define MAX_IP_LEN INET6_ADDRSTRLEN
 
43
#else
 
44
#  define MAX_IP_LEN 20
 
45
#endif
 
46
 
 
47
#ifndef HAVE_IPV6
 
48
#  undef EAI_NONAME
 
49
#  define EAI_NONAME NO_ADDRESS
 
50
#  undef EAI_FAIL
 
51
#  define EAI_FAIL NO_RECOVERY
 
52
#endif
 
53
 
 
54
#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
 
55
#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
 
56
#define IPADDR_BITS(ip) (IPADDR_IS_V4(ip) ? 32 : 128)
 
57
 
 
58
/* Returns TRUE if IPs are the same */
 
59
bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
 
60
/* Returns 0 if IPs are the same, -1 or 1 otherwise. */
 
61
int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
 
62
unsigned int net_ip_hash(const struct ip_addr *ip);
 
63
 
 
64
/* Connect to socket with ip address. The socket and connect() is
 
65
   non-blocking. */
 
66
int net_connect_ip(const struct ip_addr *ip, unsigned int port,
 
67
                   const struct ip_addr *my_ip) ATTR_NULL(3);
 
68
/* Like net_connect_ip(), but do a blocking connect(). */
 
69
int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
 
70
                            const struct ip_addr *my_ip) ATTR_NULL(3);
 
71
/* Returns 0 if we can bind() as given IP, -1 if not. */
 
72
int net_try_bind(const struct ip_addr *ip);
 
73
/* Connect to named UNIX socket */
 
74
int net_connect_unix(const char *path);
 
75
/* Try to connect to UNIX socket for give number of seconds when connect()
 
76
   returns EAGAIN or ECONNREFUSED. */
 
77
int net_connect_unix_with_retries(const char *path, unsigned int msecs);
 
78
/* Disconnect socket */
 
79
void net_disconnect(int fd);
 
80
 
 
81
/* Set socket blocking/nonblocking */
 
82
void net_set_nonblock(int fd, bool nonblock);
 
83
/* Set TCP_CORK if supported, ie. don't send out partial frames.
 
84
   Returns 0 if ok, -1 if failed. */
 
85
int net_set_cork(int fd, bool cork) ATTR_NOWARN_UNUSED_RESULT;
 
86
 
 
87
/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may
 
88
   include IPv4 depending on the system (Linux yes, BSD no). */
 
89
void net_get_ip_any4(struct ip_addr *ip);
 
90
void net_get_ip_any6(struct ip_addr *ip);
 
91
 
 
92
/* Listen for connections on a socket */
 
93
int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
 
94
/* Listen for connections on an UNIX socket */
 
95
int net_listen_unix(const char *path, int backlog);
 
96
/* Like net_listen_unix(), but if socket already exists, try to connect to it.
 
97
   If it fails with ECONNREFUSED, unlink the socket and try creating it
 
98
   again. */
 
99
int net_listen_unix_unlink_stale(const char *path, int backlog);
 
100
/* Accept a connection on a socket. Returns -1 if the connection got closed,
 
101
   -2 for other failures. For UNIX sockets addr_r->family=port=0. */
 
102
int net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r)
 
103
        ATTR_NULL(2, 3);
 
104
 
 
105
/* Read data from socket, return number of bytes read,
 
106
   -1 = error, -2 = disconnected */
 
107
ssize_t net_receive(int fd, void *buf, size_t len);
 
108
/* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */
 
109
ssize_t net_transmit(int fd, const void *data, size_t len);
 
110
 
 
111
/* Get IP addresses for host. ips contains ips_count of IPs, they don't need
 
112
   to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
 
113
int net_gethostbyname(const char *addr, struct ip_addr **ips,
 
114
                      unsigned int *ips_count);
 
115
/* Return host for the IP address. Returns 0 = ok, others = error code for
 
116
   net_gethosterror(). */
 
117
int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r);
 
118
/* get error of net_gethostname() */
 
119
const char *net_gethosterror(int error) ATTR_CONST;
 
120
/* return TRUE if host lookup failed because it didn't exist (ie. not
 
121
   some error with name server) */
 
122
int net_hosterror_notfound(int error) ATTR_CONST;
 
123
 
 
124
/* Get socket local address/port. For UNIX sockets addr->family=port=0. */
 
125
int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
 
126
        ATTR_NULL(2, 3);
 
127
/* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
 
128
int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
 
129
        ATTR_NULL(2, 3);
 
130
/* Get UNIX socket name. */
 
131
int net_getunixname(int fd, const char **name_r);
 
132
/* Get UNIX socket peer process's credentials. The pid may be (pid_t)-1 if
 
133
   unavailable. */
 
134
int net_getunixcred(int fd, struct net_unix_cred *cred_r);
 
135
 
 
136
/* Returns ip_addr as string, or NULL if ip is invalid. */
 
137
const char *net_ip2addr(const struct ip_addr *ip);
 
138
/* char* -> struct ip_addr translation. */
 
139
int net_addr2ip(const char *addr, struct ip_addr *ip);
 
140
/* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
 
141
   successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
 
142
int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
 
143
                                 struct ip_addr *dest);
 
144
 
 
145
/* Get socket error */
 
146
int net_geterror(int fd);
 
147
 
 
148
/* Get name of TCP service */
 
149
const char *net_getservbyport(unsigned short port) ATTR_CONST;
 
150
 
 
151
bool is_ipv4_address(const char *addr) ATTR_PURE;
 
152
bool is_ipv6_address(const char *addr) ATTR_PURE;
 
153
 
 
154
/* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */
 
155
int net_parse_range(const char *network, struct ip_addr *ip_r,
 
156
                    unsigned int *bits_r);
 
157
/* Returns TRUE if ip is in net_ip/bits network. IPv6 mapped IPv4 addresses
 
158
   are converted to plain IPv4 addresses before matching. Invalid IPs
 
159
   (family=0) never match anything. */
 
160
bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,
 
161
                       unsigned int bits) ATTR_PURE;
 
162
 
 
163
#endif