~ubuntu-branches/ubuntu/vivid/keepalived/vivid

« back to all changes in this revision

Viewing changes to lib/utils.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2011-10-25 16:10:58 UTC
  • mfrom: (3.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20111025161058-bgqn5elt0xo1tq0a
Tags: 1:1.2.2-1ubuntu1
* Resynchronise with Debian.  Remaining changes:
  - debian/rules: DEB_UPDATE_RCD_PARAMS := explicit init start/stop
    parameters (don't stop at 0 and 6)
  - debian/init.d: init script header adapted to stop rule
  - debian/keepalived.postinst: Remove shutdown and reboot links
* Build with libnl3, thanks to a patch from Marc - A. Dahlhaus.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 *              as published by the Free Software Foundation; either version
18
18
 *              2 of the License, or (at your option) any later version.
19
19
 *
20
 
 * Copyright (C) 2001-2010 Alexandre Cassen, <acassen@freebox.fr>
 
20
 * Copyright (C) 2001-2011 Alexandre Cassen, <acassen@linux-vs.org>
21
21
 */
22
22
 
23
23
#include "utils.h"
159
159
        return range;
160
160
}
161
161
 
 
162
/* IP string to sockaddr_storage */
 
163
int
 
164
inet_stosockaddr(char *ip, char *port, struct sockaddr_storage *addr)
 
165
{
 
166
        void *addr_ip;
 
167
        char *cp = ip;
 
168
 
 
169
        addr->ss_family = (strchr(ip, ':')) ? AF_INET6 : AF_INET;
 
170
 
 
171
        /* remove range and mask stuff */
 
172
        if (strstr(ip, "-")) {
 
173
                while (*cp != '-' && *cp != '\0')
 
174
                        cp++;
 
175
                if (*cp == '-')
 
176
                        *cp = 0;
 
177
        } else if (strstr(ip, "/")) {
 
178
                while (*cp != '/' && *cp != '\0')
 
179
                        cp++;
 
180
                if (*cp == '/')
 
181
                        *cp = 0;
 
182
        }
 
183
 
 
184
        if (addr->ss_family == AF_INET6) {
 
185
                struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
 
186
                if (port)
 
187
                        addr6->sin6_port = htons(atoi(port));
 
188
                addr_ip = &addr6->sin6_addr;
 
189
        } else {
 
190
                struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
 
191
                if (port)
 
192
                        addr4->sin_port = htons(atoi(port));
 
193
                addr_ip = &addr4->sin_addr;
 
194
        }
 
195
 
 
196
        if (!inet_pton(addr->ss_family, ip, addr_ip))
 
197
                return -1;
 
198
 
 
199
        return 0;
 
200
}
 
201
 
 
202
/* IP network to string representation */
 
203
char *
 
204
inet_sockaddrtos2(struct sockaddr_storage *addr, char *addr_str)
 
205
{
 
206
        void *addr_ip;
 
207
 
 
208
        if (addr->ss_family == AF_INET6) {
 
209
                struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
 
210
                addr_ip = &addr6->sin6_addr;
 
211
        } else {
 
212
                struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
 
213
                addr_ip = &addr4->sin_addr;
 
214
        }
 
215
 
 
216
        if (!inet_ntop(addr->ss_family, addr_ip, addr_str, INET6_ADDRSTRLEN))
 
217
                return NULL;
 
218
 
 
219
        return addr_str;
 
220
}
 
221
 
 
222
char *
 
223
inet_sockaddrtos(struct sockaddr_storage *addr)
 
224
{
 
225
        static char addr_str[INET6_ADDRSTRLEN];
 
226
        inet_sockaddrtos2(addr, addr_str);
 
227
        return addr_str;
 
228
}
 
229
 
 
230
uint16_t
 
231
inet_sockaddrport(struct sockaddr_storage *addr)
 
232
{
 
233
        uint16_t port;
 
234
 
 
235
        if (addr->ss_family == AF_INET6) {
 
236
                struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
 
237
                port = addr6->sin6_port;
 
238
        } else {
 
239
                struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
 
240
                port = addr4->sin_port;
 
241
        }
 
242
        
 
243
        return port;
 
244
}
 
245
 
 
246
uint32_t
 
247
inet_sockaddrip4(struct sockaddr_storage *addr)
 
248
{
 
249
        if (addr->ss_family != AF_INET)
 
250
                return -1;
 
251
        
 
252
        return ((struct sockaddr_in *) addr)->sin_addr.s_addr;
 
253
}
 
254
 
 
255
int
 
256
inet_sockaddrip6(struct sockaddr_storage *addr, struct in6_addr *ip6)
 
257
{
 
258
        if (addr->ss_family != AF_INET6)
 
259
                return -1;
 
260
        
 
261
        *ip6 = ((struct sockaddr_in6 *) addr)->sin6_addr;
 
262
        return 0;
 
263
}
 
264
 
 
265
 
162
266
/*
163
267
 * IP string to network representation
164
268
 * Highly inspired from Paul Vixie code.