]> arthur.barton.de Git - netatalk.git/blob - libatalk/util/socket.c
770c93fe2f03b69ca442a80dd4a923b774ad0f30
[netatalk.git] / libatalk / util / socket.c
1 /*
2    $Id: socket.c,v 1.5 2009-11-23 19:04:15 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 <netinet/in.h>
26 #include <stdlib.h>
27 #include <string.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
97     switch (sa->sa_family) {
98     case AF_INET: {
99         if (mask >= 32)
100             return;
101
102         struct sockaddr_in *si = (struct sockaddr_in *)sa;
103         uint32_t nmask = mask ? ~((1 << (32 - mask)) - 1) : 0;
104         si->sin_addr.s_addr &= htonl(nmask);
105         break;
106     }
107     case AF_INET6: {
108         if (mask >= 128)
109             return;
110
111         int i, maskbytes, maskbits;
112         struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)sa;
113
114         /* Deal with IPv6 mapped IPv4 addresses*/
115         if ((memcmp(si6->sin6_addr.s6_addr, ipv4mapprefix, sizeof(ipv4mapprefix))) == 0) {
116             mask += 96;
117             if (mask >= 128)
118                 return;
119         }
120
121         maskbytes = (128 - mask) / 8; /* maskbytes really are those that will be 0'ed */
122         maskbits = mask % 8;
123
124         for (i = maskbytes - 1; i >= 0; i--)
125             si6->sin6_addr.s6_addr[15 - i] = 0;
126         if (maskbits)
127             si6->sin6_addr.s6_addr[15 - maskbytes] &= ~((1 << (8 - maskbits)) - 1);
128         break;
129     }
130     default:
131         break;
132     }
133 }
134
135 int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
136 {
137     int ret;
138     char *ip1;
139     const char *ip2;
140
141     ip1 = strdup(getip_string(sa1));
142     ip2 = getip_string(sa2);
143
144     ret = strcmp(ip1, ip2);
145
146     free(ip1);
147
148     return ret;
149 }