]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/socket.c
More doxygen style API documentation
[netatalk.git] / libatalk / util / socket.c
1 /*
2    $Id: socket.c,v 1.6 2010-01-05 19:05:52 franklahm Exp $
3    Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9  
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 */
15
16 /*!
17  * @file
18  * Netatalk utility functions
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif /* HAVE_CONFIG_H */
24
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
30 #include <netinet/in.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 static char ipv4mapprefix[] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
35
36 /*!
37  * @brief set or unset non-blocking IO on a fd
38  *
39  * @param     fd         (r) File descriptor
40  * @param     cmd        (r) 0: disable non-blocking IO, ie block\n
41  *                           <>0: enable non-blocking IO
42  *
43  * @returns   0 on success, -1 on failure
44  */
45 int setnonblock(int fd, int cmd)
46 {
47     int ofdflags;
48     int fdflags;
49
50     if ((fdflags = ofdflags = fcntl(fd, F_GETFL, 0)) == -1)
51         return -1;
52
53     if (cmd)
54         fdflags |= O_NONBLOCK;
55     else
56         fdflags &= ~O_NONBLOCK;
57
58     if (fdflags != ofdflags)
59         if (fcntl(fd, F_SETFL, fdflags) == -1)
60             return -1;
61
62     return 0;
63 }
64
65 /*!
66  * @brief convert an IPv4 or IPv6 address to a static string using inet_ntop
67  *
68  * IPv6 mapped IPv4 addresses are returned as IPv4 addreses eg
69  * ::ffff:10.0.0.0 is returned as "10.0.0.0".
70  *
71  * @param  sa        (r) pointer to an struct sockaddr
72  *
73  * @returns pointer to a static string cotaining the converted address as string.\n
74  *          On error pointers to "0.0.0.0" or "::0" are returned.
75  */
76 const char *getip_string(const struct sockaddr *sa)
77 {
78     static char ip4[INET_ADDRSTRLEN];
79     static char ip6[INET6_ADDRSTRLEN];
80
81     switch (sa->sa_family) {
82
83     case AF_INET: {
84         const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
85         if ((inet_ntop(AF_INET, &(sai4->sin_addr), ip4, INET_ADDRSTRLEN)) == NULL)
86             return "0.0.0.0";
87         return ip4;
88     }
89     case AF_INET6: {
90         const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
91         if ((inet_ntop(AF_INET6, &(sai6->sin6_addr), ip6, INET6_ADDRSTRLEN)) == NULL)
92             return "::0";
93
94         /* Deal with IPv6 mapped IPv4 addresses*/
95         if ((memcmp(sai6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0)
96             return (strrchr(ip6, ':') + 1);
97         return ip6;
98     }
99     default:
100         return "getip_string ERROR";
101     }
102
103     /* We never get here */
104 }
105
106 /*!
107  * @brief return port number from struct sockaddr
108  *
109  * @param  sa        (r) pointer to an struct sockaddr
110  *
111  * @returns port as unsigned int
112  */
113 unsigned int getip_port(const struct sockaddr  *sa)
114 {
115     if (sa->sa_family == AF_INET) { /* IPv4 */
116         const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
117         return ntohs(sai4->sin_port);
118     } else {                       /* IPv6 */
119         const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
120         return ntohs(sai6->sin6_port);
121     }
122
123     /* We never get here */
124 }
125
126 /*!
127  * @brief apply netmask to IP (v4 or v6)
128  *
129  * Modifies IP address in sa->sin[6]_addr-s[6]_addr. The caller is responsible
130  * for passing a value for mask that is sensible to the passed address,
131  * eg 0 <= mask <= 32 for IPv4 or 0<= mask <= 128 for IPv6. mask > 32 for
132  * IPv4 is treated as mask = 32, mask > 128 is set to 128 for IPv6.
133  *
134  * @param  ai        (rw) pointer to an struct sockaddr
135  * @parma  mask      (r) number of maskbits
136  */
137 void apply_ip_mask(struct sockaddr *sa, uint32_t mask)
138 {
139
140     switch (sa->sa_family) {
141     case AF_INET: {
142         if (mask >= 32)
143             return;
144
145         struct sockaddr_in *si = (struct sockaddr_in *)sa;
146         uint32_t nmask = mask ? ~((1 << (32 - mask)) - 1) : 0;
147         si->sin_addr.s_addr &= htonl(nmask);
148         break;
149     }
150     case AF_INET6: {
151         if (mask >= 128)
152             return;
153
154         int i, maskbytes, maskbits;
155         struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)sa;
156
157         /* Deal with IPv6 mapped IPv4 addresses*/
158         if ((memcmp(si6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0) {
159             mask += 96;
160             if (mask >= 128)
161                 return;
162         }
163
164         maskbytes = (128 - mask) / 8; /* maskbytes really are those that will be 0'ed */
165         maskbits = mask % 8;
166
167         for (i = maskbytes - 1; i >= 0; i--)
168             si6->sin6_addr.s6_addr[15 - i] = 0;
169         if (maskbits)
170             si6->sin6_addr.s6_addr[15 - maskbytes] &= ~((1 << (8 - maskbits)) - 1);
171         break;
172     }
173     default:
174         break;
175     }
176 }
177
178 /*!
179  * @brief compare IP addresses for equality
180  *
181  * @param  sa1       (r) pointer to an struct sockaddr
182  * @param  sa2       (r) pointer to an struct sockaddr
183  *
184  * @returns Addresses are converted to strings and compared with strcmp and
185  *          the result of strcmp is returned.
186  *
187  * @note IPv6 mapped IPv4 addresses are treated as IPv4 addresses.
188  */
189 int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
190 {
191     int ret;
192     char *ip1;
193     const char *ip2;
194
195     ip1 = strdup(getip_string(sa1));
196     ip2 = getip_string(sa2);
197
198     ret = strcmp(ip1, ip2);
199
200     free(ip1);
201
202     return ret;
203 }