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

« back to all changes in this revision

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