01ac2bf2d7a980c1834d009e618de160aeb43fa2
[ctdb.git] / common / system_common.c
1 /* 
2    ctdb system specific code to manage raw sockets on linux
3
4    Copyright (C) Ronnie Sahlberg  2007
5    Copyright (C) Andrew Tridgell  2007
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "system/network.h"
23
24 /*
25   uint16 checksum for n bytes
26  */
27 uint32_t uint16_checksum(uint16_t *data, size_t n)
28 {
29         uint32_t sum=0;
30         while (n>=2) {
31                 sum += (uint32_t)ntohs(*data);
32                 data++;
33                 n -= 2;
34         }
35         if (n == 1) {
36                 sum += (uint32_t)ntohs(*(uint8_t *)data);
37         }
38         return sum;
39 }
40
41 /*
42   see if we currently have an interface with the given IP
43
44   we try to bind to it, and if that fails then we don't have that IP
45   on an interface
46  */
47 bool ctdb_sys_have_ip(ctdb_sock_addr *_addr)
48 {
49         int s;
50         int ret;
51         ctdb_sock_addr __addr = *_addr;
52         ctdb_sock_addr *addr = &__addr;
53         socklen_t addrlen = 0;
54
55         switch (addr->sa.sa_family) {
56         case AF_INET:
57                 addr->ip.sin_port = 0;
58                 addrlen = sizeof(struct sockaddr_in);
59                 break;
60         case AF_INET6:
61                 addr->ip6.sin6_port = 0;
62                 addrlen = sizeof(struct sockaddr_in6);
63                 break;
64         }
65
66         s = socket(addr->sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
67         if (s == -1) {
68                 return false;
69         }
70
71         ret = bind(s, (struct sockaddr *)addr, addrlen);
72
73         close(s);
74         return ret == 0;
75 }
76
77
78 /* find which interface an ip address is currently assigned to */
79 char *ctdb_sys_find_ifname(ctdb_sock_addr *addr)
80 {
81         int s;
82         int size;
83         struct ifconf ifc;
84         char *ptr;
85
86         s = socket(AF_INET, SOCK_RAW, htons(IPPROTO_RAW));
87         if (s == -1) {
88                 DEBUG(DEBUG_CRIT,(__location__ " failed to open raw socket (%s)\n",
89                          strerror(errno)));
90                 return NULL;
91         }
92
93
94         size = sizeof(struct ifreq);
95         ifc.ifc_buf = NULL;
96         ifc.ifc_len = size;
97
98         while(ifc.ifc_len > (size - sizeof(struct ifreq))) {
99                 size *= 2;
100
101                 free(ifc.ifc_buf);      
102                 ifc.ifc_len = size;
103                 ifc.ifc_buf = malloc(size);
104                 memset(ifc.ifc_buf, 0, size);
105                 if (ioctl(s, SIOCGIFCONF, (caddr_t)&ifc) < 0) {
106                         DEBUG(DEBUG_CRIT,("Failed to read ifc buffer from socket\n"));
107                         free(ifc.ifc_buf);      
108                         close(s);
109                         return NULL;
110                 }
111         }
112
113         for (ptr =(char *)ifc.ifc_buf; ptr < ((char *)ifc.ifc_buf) + ifc.ifc_len; ) {
114                 char *ifname;
115                 struct ifreq *ifr;
116
117                 ifr = (struct ifreq *)ptr;
118
119 #ifdef HAVE_SOCKADDR_LEN
120                 if (ifr->ifr_addr.sa_len > sizeof(struct sockaddr)) {
121                         ptr += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
122                 } else {
123                         ptr += sizeof(ifr->ifr_name) + sizeof(struct sockaddr);
124                 }
125 #else
126                 ptr += sizeof(struct ifreq);
127 #endif
128
129                 if (ifr->ifr_addr.sa_family != addr->sa.sa_family) {
130                         continue;
131                 }
132
133                 switch (addr->sa.sa_family) {
134                 case AF_INET:
135
136
137                         if (memcmp(&addr->ip.sin_addr, &((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr, sizeof(addr->ip.sin_addr))) {
138                                 continue;
139                         }
140                         break;
141                 case AF_INET6:
142                         if (memcmp(&addr->ip6.sin6_addr, &((struct sockaddr_in6 *)&ifr->ifr_addr)->sin6_addr, sizeof(addr->ip6.sin6_addr))) {
143                                 continue;
144                         }
145                         break;
146                 }
147
148                 ifname = strdup(ifr->ifr_name);
149                 free(ifc.ifc_buf);      
150                 close(s);
151                 return ifname;
152         }
153
154
155         free(ifc.ifc_buf);      
156         close(s);
157
158         return NULL;
159 }