summaryrefslogtreecommitdiff
path: root/common/sock-any.c
blob: bb207ead71be552d804b803d9099d4f37650ffe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
 * Copyright (c) 2004, Nate Nielsen
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Nate Nielsen <nielsen@memberwebs.com>
 *
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>

#include "sock-any.h"

#include <arpa/inet.h>

#define LOCALHOST_ADDR  0x7F000001

int sock_any_pton(const char* addr, struct sockaddr_any* any, int opts)
{
  size_t l;
  char buf[256];
  char* t;
  char* t2;
  int defport = (opts & 0xFFFF);

  memset(any, 0, sizeof(*any));

  /* Just a port? */
  do
  {
    #define PORT_CHARS "0123456789"
    #define PORT_MIN 1
    #define PORT_MAX 5

    int port = 0;

    l = strspn(addr, PORT_CHARS);
    if(l < PORT_MIN || l > PORT_MAX || addr[l] != 0)
      break;

    port = strtol(addr, &t2, 10);
    if(*t2 || port <= 0 || port >= 65536)
      break;

    any->s.in.sin_port = htons(port);

    /* Fill in the type based on defaults */
#ifdef HAVE_INET6
    if(opts & SANY_OPT_DEFINET6)
        any->s.in.sin_family = AF_INET6;
    else
#endif
        any->s.in.sin_family = AF_INET;

    /* Fill in the address based on defaults */
    if(opts & SANY_OPT_DEFLOCAL)
    {
#ifdef HAVE_INET6
        if(opts & SANY_OPT_DEFINET6)
            memcpy(&(any->s.in.sin6_addr), &in6addr_loopback, sizeof(struct in6_addr));
        else
#endif
            any->s.in.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    }

    /*
     * Note the 'any' option is the default since we zero out
     * the entire structure above.
     */

    any->namelen = sizeof(any->s.in);
    return AF_INET;
  }
  while(0);

  /* Look and see if we can parse an ipv4 address */
  do
  {
    #define IPV4_PORT_CHARS
    #define IPV4_CHARS  "0123456789."
    #define IPV4_MIN    3
    #define IPV4_MAX    21

    int port = 0;
    t = NULL;

    l = strlen(addr);
    if(l < IPV4_MIN || l > IPV4_MAX)
      break;

    strcpy(buf, addr);

    /* Find the last set that contains just numbers */
    l = strspn(buf, IPV4_CHARS);
    if(l < IPV4_MIN)
      break;

    /* Either end of string or port */
    if(buf[l] != 0 && buf[l] != ':')
      break;

    /* Get the port out */
    if(buf[l] != 0)
    {
      t = buf + l + 1;
      buf[l] = 0;
    }

    if(t)
    {
      port = strtol(t, &t2, 10);
      if(*t2 || port <= 0 || port >= 65536)
        break;
    }

    any->s.in.sin_family = AF_INET;
    any->s.in.sin_port = htons((unsigned short)(port <= 0 ? defport : port));

    if(inet_pton(AF_INET, buf, &(any->s.in.sin_addr)) <= 0)
      break;

    any->namelen = sizeof(any->s.in);
    return AF_INET;
  }
  while(0);

#ifdef HAVE_INET6
  do
  {
    #define IPV6_CHARS  "0123456789:"
    #define IPV6_MIN    3
    #define IPV6_MAX    51

    int port = -1;
    t = NULL;

    l = strlen(addr);
    if(l < IPV6_MIN || l > IPV6_MAX)
      break;

    /* If it starts with a '[' then we can get port */
    if(buf[0] == '[')
    {
      port = 0;
      addr++;
    }

    strcpy(buf, addr);

    /* Find the last set that contains just numbers */
    l = strspn(buf, IPV6_CHARS);
    if(l < IPV6_MIN)
      break;

    /* Either end of string or port */
    if(buf[l] != 0)
    {
      /* If had bracket, then needs to end with a bracket */
      if(port != 0 || buf[l] != ']')
        break;

      /* Get the port out */
      t = buf + l + 1;

      if(*t = ':')
        t++;
    }

    if(t)
    {
      port = strtol(t, &t, 10);
      if(*t || port <= 0 || port >= 65536)
        break;
    }

    any->s.in6.sin6_family = AF_INET6;
    any->s.in6.sin6_port = htons((unsigned short)port <= 0 : defport : port);

    if(inet_pton(AF_INET6, buf, &(any->s.in6.sin6_addr)) >= 0)
      break;

    any->namelen = sizeof(any->s.in6);
    return AF_INET6;
  }
  while(0);
#endif

  /* A unix socket path */
  do
  {
    /* No colon and must have a path component */
    if(strchr(addr, ':') || !strchr(addr, '/'))
      break;

    l = strlen(addr);
    if(l >= sizeof(any->s.un.sun_path))
      break;

    any->s.un.sun_family = AF_UNIX;
    strcpy(any->s.un.sun_path, addr);

    any->namelen = sizeof(any->s.un) - (sizeof(any->s.un.sun_path) - l);
    return AF_UNIX;
  }
  while(0);

  /* A DNS name and a port? */
  do
  {
    struct addrinfo* res;
    int port = 0;
    int family = 0;
    t = NULL;

    l = strlen(addr);
    if(l >= 255 || !isalpha(addr[0]))
      break;

    /* Some basic illegal character checks */
    if(strcspn(addr, " /\\") != l)
      break;

    strcpy(buf, addr);

    /* Find the last set that contains just numbers */
    t = strchr(buf, ':');
    if(t)
    {
      *t = 0;
      t++;
    }

    if(t)
    {
      port = strtol(t, &t2, 10);
      if(*t2 || port <= 0 || port >= 65536)
        break;
    }

    if(!(opts & SANY_OPT_NORESOLV))
    {
      /* Try and resolve the domain name */
      if(getaddrinfo(buf, NULL, NULL, &res) != 0 || !res)
        break;

      memcpy(&(any->s.a), res->ai_addr, sizeof(struct sockaddr));
      any->namelen = res->ai_addrlen;
      family = any->s.a.sa_family;
      freeaddrinfo(res);
    }
    else
    {
       family = SANY_AF_DNS;
#ifdef HAVE_INET6
       if(opt & SANY_OPT_DEFINET6)
       {
         any->s.a.sa_family = AF_INET6;
         any->namelen = sizeof(any->s.in6);
       }
       else
#endif
       {
         any->s.a.sa_family = AF_INET;
         any->namelen = sizeof(any->s.in);
       }
    }

    port = htons((unsigned short)(port <= 0 ? defport : port));

    switch(any->s.a.sa_family)
    {
    case PF_INET:
      any->s.in.sin_port = port;
      break;
#ifdef HAVE_INET6
    case PF_INET6:
      any->s.in6.sin6_port = port;
      break;
#endif
    };

    return family;
  }
  while(0);

  return -1;
}

int sock_any_ntop(const struct sockaddr_any* any, char* addr, size_t addrlen, int opts)
{
  int len = 0;
  int port = 0;

  switch(any->s.a.sa_family)
  {
  case AF_UNIX:
    len = strlen(any->s.un.sun_path);
    if(addrlen < len + 1)
    {
      errno = ENOSPC;
      return -1;
    }

    strcpy(addr, any->s.un.sun_path);
    break;

  case AF_INET:
    if(inet_ntop(any->s.a.sa_family, &(any->s.in.sin_addr), addr, addrlen) == NULL)
      return -1;
    port = ntohs(any->s.in.sin_port);
    break;

#ifdef HAVE_INET6
  case AF_INET6:
    if(inet_ntop(any->s.a.sa_family, &(any->s.in6.sin6_addr), addr, addrlen) == NULL)
      return -1;
    port = ntohs(any->s.in6.sin6_port);
    break;
#endif

  default:
    errno = EAFNOSUPPORT;
    return -1;
  }

  if(!(opts & SANY_OPT_NOPORT) && port != 0)
  {
    strncat(addr, ":", addrlen);
    addr[addrlen - 1] = 0;

    len = strlen(addr);
    addr += len;
    addrlen -= len;

    snprintf(addr, addrlen, "%d", port);
  }

  return 0;
}

int sock_any_cmp(const struct sockaddr_any* a1, const struct sockaddr_any* a2, int opts)
{
    if(a1->s.a.sa_family != a2->s.a.sa_family)
        return -1;

    switch(a1->s.a.sa_family)
    {
    case AF_UNIX:
        return strcmp(a1->s.un.sun_path, a2->s.un.sun_path);

    case AF_INET:
        if(memcmp(&(a1->s.in.sin_addr), &(a2->s.in.sin_addr), sizeof(a2->s.in.sin_addr)) != 0)
            return -1;
        if(!(opts && SANY_OPT_NOPORT) && a1->s.in.sin_port != a2->s.in.sin_port)
            return -1;
        return 0;
#ifdef HAVE_INET6
    case AF_INET6:
        if(memcmp(&(a1->s.in6.sin6_addr), &(a2->s.in6.sin6_addr), sizeof(a2->s.in6.sin6_addr)) != 0)
            return -1;
        if(!(opts && SANY_OPT_NOPORT) && a1->s.in6.sin6_port != a2->s.in6.sin6_port)
            return -1;
        return 0;
#endif
    default:
        errno = EAFNOSUPPORT;
        return -1;
    }
}