]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/socket.c
IPv6 support for afpd and cnid_metad
[netatalk.git] / libatalk / util / socket.c
1 /*
2    $Id: socket.c,v 1.2 2009-11-05 14:38:08 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 #ifdef HAVE_CONFIG_H
17 #include "config.h"
18 #endif /* HAVE_CONFIG_H */
19
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <string.h>
26
27 #include <atalk/logger.h>
28
29 static char ipv4mapprefix[] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff};
30
31 int setnonblock(int fd, int cmd)
32 {
33     int ofdflags;
34     int fdflags;
35
36     if ((fdflags = ofdflags = fcntl(fd, F_GETFL, 0)) == -1)
37         return -1;
38
39     if (cmd)
40         fdflags |= O_NONBLOCK;
41     else
42         fdflags &= ~O_NONBLOCK;
43
44     if (fdflags != ofdflags)
45         if (fcntl(fd, F_SETFL, fdflags) == -1)
46             return -1;
47
48     return 0;
49 }
50
51 const char *getip_string(const struct sockaddr *sa)
52 {
53     static char ip4[INET_ADDRSTRLEN];
54     static char ip6[INET6_ADDRSTRLEN];
55
56     switch (sa->sa_family) {
57
58     case AF_INET: {
59         const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
60         if ((inet_ntop(AF_INET, &(sai4->sin_addr), ip4, INET_ADDRSTRLEN)) == NULL)
61             return "0.0.0.0";
62         return ip4;
63     }
64     case AF_INET6: {
65         const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
66         if ((inet_ntop(AF_INET6, &(sai6->sin6_addr), ip6, INET6_ADDRSTRLEN)) == NULL)
67             return "::0";
68
69         /* Deal with IPv6 mapped IPv4 addresses*/
70         if ((memcmp(sai6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0)
71             return (strrchr(ip6, ':') + 1);
72         return ip6;
73     }
74     default:
75         return "getip_string ERROR";
76     }
77
78     /* We never get here */
79 }
80
81 unsigned int getip_port(const struct sockaddr  *sa)
82 {
83     if (sa->sa_family == AF_INET) { /* IPv4 */
84         const struct sockaddr_in *sai4 = (const struct sockaddr_in *)sa;
85         return ntohs(sai4->sin_port);
86     } else {                       /* IPv6 */
87         const struct sockaddr_in6 *sai6 = (const struct sockaddr_in6 *)sa;
88         return ntohs(sai6->sin6_port);
89     }
90
91     /* We never get here */
92 }
93
94 void apply_ip_mask(struct sockaddr *sa, uint32_t mask)
95 {
96     if (mask < 0)
97         return;
98
99     switch (sa->sa_family) {
100     case AF_INET: {
101         if (mask >= 32)
102             return;
103
104         struct sockaddr_in *si = (struct sockaddr_in *)sa;
105         uint32_t nmask = mask ? ~((1 << (32 - mask)) - 1) : 0;
106         si->sin_addr.s_addr &= htonl(nmask);
107         break;
108     }
109     case AF_INET6: {
110         if (mask >= 128)
111             return;
112
113         int i, maskbytes, maskbits;
114         struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)sa;
115
116         /* Deal with IPv6 mapped IPv4 addresses*/
117         if ((memcmp(si6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0) {
118             mask += 96;
119             if (mask >= 128)
120                 return;
121         }
122
123         maskbytes = (128 - mask) / 8; /* maskbytes really are those that will be 0'ed */
124         maskbits = mask % 8;
125
126         for (i = maskbytes - 1; i >= 0; i--)
127             si6->sin6_addr.s6_addr[15 - i] = 0;
128         if (maskbits)
129             si6->sin6_addr.s6_addr[15 - maskbytes] &= ~((1 << (8 - maskbits)) - 1);
130         break;
131     }
132     default:
133         break;
134     }
135 }
136
137 int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
138 {
139     int ret;
140     const char *ip1;
141     const char *ip2;
142
143     ip1 = strdup(getip_string(sa1));
144     ip2 = getip_string(sa2);
145
146     ret = strcmp(ip1, ip2);
147
148     free(ip1);
149
150     return ret;
151 }