2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
17 * Netatalk utility functions
22 #endif /* HAVE_CONFIG_H */
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
36 #include <atalk/logger.h>
38 static char ipv4mapprefix[] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
41 * @brief set or unset non-blocking IO on a fd
43 * @param fd (r) File descriptor
44 * @param cmd (r) 0: disable non-blocking IO, ie block\n
45 * <>0: enable non-blocking IO
47 * @returns 0 on success, -1 on failure
49 int setnonblock(int fd, int cmd)
54 if ((fdflags = ofdflags = fcntl(fd, F_GETFL, 0)) == -1)
58 fdflags |= O_NONBLOCK;
60 fdflags &= ~O_NONBLOCK;
62 if (fdflags != ofdflags)
63 if (fcntl(fd, F_SETFL, fdflags) == -1)
70 * non-blocking drop-in replacement for read with timeout using select
72 * @param socket (r) socket, if in blocking mode, pass "setnonblocking" arg as 1
73 * @param data (rw) buffer for the read data
74 * @param lenght (r) how many bytes to read
75 * @param setnonblocking (r) when non-zero this func will enable and disable non blocking
76 * io mode for the socket
77 * @param timeout (r) number of seconds to try reading
79 * @returns number of bytes actually read or -1 on fatal error
81 ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout)
85 struct timeval now, end, tv;
92 if (setnonblock(socket, 1) != 0)
96 /* Calculate end time */
97 (void)gettimeofday(&now, NULL);
99 end.tv_sec += timeout;
101 while (stored < length) {
102 len = read(socket, (char *) data + stored, length - stored);
109 FD_SET(socket, &rfds);
113 while ((ret = select(socket + 1, &rfds, NULL, NULL, &tv)) < 1) {
116 LOG(log_warning, logtype_afpd, "select timeout %d s", timeout);
120 if (errno == EINTR) {
121 (void)gettimeofday(&now, NULL);
122 if (now.tv_sec >= end.tv_sec && now.tv_usec >= end.tv_usec) {
123 LOG(log_warning, logtype_afpd, "select timeout %d s", timeout);
126 if (now.tv_usec > end.tv_usec) {
127 tv.tv_usec = 1000000 + end.tv_usec - now.tv_usec;
128 tv.tv_sec = end.tv_sec - now.tv_sec - 1;
130 tv.tv_usec = end.tv_usec - now.tv_usec;
131 tv.tv_sec = end.tv_sec - now.tv_sec;
134 FD_SET(socket, &rfds);
137 LOG(log_error, logtype_afpd, "select: %s", strerror(errno));
141 } /* while (select) */
143 } /* switch (errno) */
144 LOG(log_error, logtype_afpd, "read: %s", strerror(errno));
152 } /* while (stored < length) */
155 if (setnonblocking) {
156 if (setnonblock(socket, 0) != 0)
160 if (len == -1 && stored == 0)
161 /* last read or select got an error and we haven't got yet anything => return -1*/
167 * @brief convert an IPv4 or IPv6 address to a static string using inet_ntop
169 * IPv6 mapped IPv4 addresses are returned as IPv4 addreses eg
170 * ::ffff:10.0.0.0 is returned as "10.0.0.0".
172 * @param sa (r) pointer to an struct sockaddr
174 * @returns pointer to a static string cotaining the converted address as string.\n
175 * On error pointers to "0.0.0.0" or "::0" are returned.
177 const char *getip_string(const struct sockaddr *sa)
179 static char ip4[INET_ADDRSTRLEN];
180 static char ip6[INET6_ADDRSTRLEN];
182 switch (sa->sa_family) {
185 const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
186 if ((inet_ntop(AF_INET, &(sai4->sin_addr), ip4, INET_ADDRSTRLEN)) == NULL)
191 const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
192 if ((inet_ntop(AF_INET6, &(sai6->sin6_addr), ip6, INET6_ADDRSTRLEN)) == NULL)
195 /* Deal with IPv6 mapped IPv4 addresses*/
196 if ((memcmp(sai6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0)
197 return (strrchr(ip6, ':') + 1);
201 return "getip_string ERROR";
204 /* We never get here */
208 * @brief return port number from struct sockaddr
210 * @param sa (r) pointer to an struct sockaddr
212 * @returns port as unsigned int
214 unsigned int getip_port(const struct sockaddr *sa)
216 if (sa->sa_family == AF_INET) { /* IPv4 */
217 const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
218 return ntohs(sai4->sin_port);
220 const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
221 return ntohs(sai6->sin6_port);
224 /* We never get here */
228 * @brief apply netmask to IP (v4 or v6)
230 * Modifies IP address in sa->sin[6]_addr-s[6]_addr. The caller is responsible
231 * for passing a value for mask that is sensible to the passed address,
232 * eg 0 <= mask <= 32 for IPv4 or 0<= mask <= 128 for IPv6. mask > 32 for
233 * IPv4 is treated as mask = 32, mask > 128 is set to 128 for IPv6.
235 * @param ai (rw) pointer to an struct sockaddr
236 * @parma mask (r) number of maskbits
238 void apply_ip_mask(struct sockaddr *sa, uint32_t mask)
241 switch (sa->sa_family) {
246 struct sockaddr_in *si = (struct sockaddr_in *)sa;
247 uint32_t nmask = mask ? ~((1 << (32 - mask)) - 1) : 0;
248 si->sin_addr.s_addr &= htonl(nmask);
255 int i, maskbytes, maskbits;
256 struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)sa;
258 /* Deal with IPv6 mapped IPv4 addresses*/
259 if ((memcmp(si6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0) {
265 maskbytes = (128 - mask) / 8; /* maskbytes really are those that will be 0'ed */
268 for (i = maskbytes - 1; i >= 0; i--)
269 si6->sin6_addr.s6_addr[15 - i] = 0;
271 si6->sin6_addr.s6_addr[15 - maskbytes] &= ~((1 << (8 - maskbits)) - 1);
280 * @brief compare IP addresses for equality
282 * @param sa1 (r) pointer to an struct sockaddr
283 * @param sa2 (r) pointer to an struct sockaddr
285 * @returns Addresses are converted to strings and compared with strcmp and
286 * the result of strcmp is returned.
288 * @note IPv6 mapped IPv4 addresses are treated as IPv4 addresses.
290 int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
296 ip1 = strdup(getip_string(sa1));
297 ip2 = getip_string(sa2);
299 ret = strcmp(ip1, ip2);