~ubuntu-branches/ubuntu/trusty/haproxy/trusty

« back to all changes in this revision

Viewing changes to src/standard.c

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2009-10-19 22:31:45 UTC
  • mfrom: (1.1.7 upstream) (2.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091019223145-259fs24dejjixo1f
New upstream bugfix release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * General purpose functions.
3
3
 *
4
 
 * Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
 
4
 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or
7
7
 * modify it under the terms of the GNU General Public License
241
241
}
242
242
 
243
243
/*
 
244
 * converts <str> to a struct sockaddr_in* which is locally allocated, and a
 
245
 * port range consisting in two integers. The low and high end are always set
 
246
 * even if the port is unspecified, in which case (0,0) is returned. The low
 
247
 * port is set in the sockaddr_in. Thus, it is enough to check the size of the
 
248
 * returned range to know if an array must be allocated or not. The format is
 
249
 * "addr[:port[-port]]", where "addr" can be a dotted IPv4 address, a host
 
250
 * name, or empty or "*" to indicate INADDR_ANY.
 
251
 */
 
252
struct sockaddr_in *str2sa_range(char *str, int *low, int *high)
 
253
{
 
254
        static struct sockaddr_in sa;
 
255
        char *c;
 
256
        int portl, porth;
 
257
 
 
258
        memset(&sa, 0, sizeof(sa));
 
259
        str = strdup(str);
 
260
        if (str == NULL)
 
261
                goto out_nofree;
 
262
 
 
263
        if ((c = strrchr(str,':')) != NULL) {
 
264
                char *sep;
 
265
                *c++ = '\0';
 
266
                sep = strchr(c, '-');
 
267
                if (sep)
 
268
                        *sep++ = '\0';
 
269
                else
 
270
                        sep = c;
 
271
                portl = atol(c);
 
272
                porth = atol(sep);
 
273
        }
 
274
        else {
 
275
                portl = 0;
 
276
                porth = 0;
 
277
        }
 
278
 
 
279
        if (*str == '*' || *str == '\0') { /* INADDR_ANY */
 
280
                sa.sin_addr.s_addr = INADDR_ANY;
 
281
        }
 
282
        else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
 
283
                struct hostent *he;
 
284
 
 
285
                if ((he = gethostbyname(str)) == NULL) {
 
286
                        Alert("Invalid server name: '%s'\n", str);
 
287
                }
 
288
                else
 
289
                        sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
 
290
        }
 
291
        sa.sin_port   = htons(portl);
 
292
        sa.sin_family = AF_INET;
 
293
 
 
294
        *low = portl;
 
295
        *high = porth;
 
296
 
 
297
        free(str);
 
298
 out_nofree:
 
299
        return &sa;
 
300
}
 
301
 
 
302
/*
244
303
 * converts <str> to two struct in_addr* which must be pre-allocated.
245
304
 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
246
305
 * is optionnal and either in the dotted or CIDR notation.
304
363
/*
305
364
 * Parse IP address found in url.
306
365
 */
307
 
static int url2ip(const char *addr, struct in_addr *dst)
 
366
int url2ip(const char *addr, struct in_addr *dst)
308
367
{
309
368
        int saw_digit, octets, ch;
310
369
        u_char tmp[4], *tp;