summaryrefslogtreecommitdiff
path: root/src/addrfam.c
blob: a357f9e33bc737e861676bc14c37fe208e881030 (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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * addrfam.c
 * - address-family specific code
 */
/*
 *  This file is part of adns, which is
 *    Copyright (C) 1997-2000,2003,2006  Ian Jackson
 *    Copyright (C) 1999-2000,2003,2006  Tony Finch
 *    Copyright (C) 1991 Massachusetts Institute of Technology
 *  (See the file INSTALL for full details.)
 *  
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 */

#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdbool.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>

#include "internal.h"

/*
 * General address-family operations.
 */

#define SIN(sa) ((struct sockaddr_in *)(sa))
#define CSIN(sa) ((const struct sockaddr_in *)(sa))

#define SIN6(sa) ((struct sockaddr_in6 *)(sa))
#define CSIN6(sa) ((const struct sockaddr_in6 *)(sa))

/* This gadget (thanks, Richard Kettlewell) makes sure that we handle the
 * same set of address families in each switch. */
#define AF_CASES(pre)							\
  case AF_INET: goto pre##_inet;					\
  case AF_INET6: goto pre##_inet6

static void unknown_af(int af) {
  fprintf(stderr, "ADNS INTERNAL: unknown address family %d\n", af);
  abort();
}

#define IN6_ADDR_EQUALP(a, b)						\
  (!memcmp((a).s6_addr, (b).s6_addr, sizeof((a).s6_addr)))

int adns__genaddr_equal_p(int af, const union gen_addr *a,
			  int bf, const void *b) {
  const union gen_addr *bb= b;
  if (af != bf) return 0;
  switch (af) {
  AF_CASES(af);
  af_inet: return a->v4.s_addr == bb->v4.s_addr;
  af_inet6: return IN6_ADDR_EQUALP(a->v6, bb->v6);
  default: unknown_af(af); return -1;
  }
}

int adns__sockaddr_equal_p(const struct sockaddr *sa,
			   const struct sockaddr *sb) {
  if (sa->sa_family != sb->sa_family) return 0;
  switch (sa->sa_family) {
  AF_CASES(af);
  af_inet: {
    const struct sockaddr_in *sina= CSIN(sa), *sinb= CSIN(sb);
    return (sina->sin_addr.s_addr == sinb->sin_addr.s_addr &&
	    sina->sin_port == sinb->sin_port);
  }
  af_inet6: {
    /* Don't check the flowlabel.  That's apparently useful for routing
     * performance, but doesn't affect the address in any important
     * respect. */
    const struct sockaddr_in6 *sin6a= CSIN6(sa), *sin6b= CSIN6(sb);
    return (IN6_ADDR_EQUALP(sin6a->sin6_addr, sin6b->sin6_addr) &&
	    sin6a->sin6_port == sin6b->sin6_port &&
	    sin6a->sin6_scope_id == sin6b->sin6_scope_id);
  }
  default:
    unknown_af(sa->sa_family);
    return -1;
  }
}

int adns__addr_width(int af) {
  switch (af) {
  AF_CASES(af);
  af_inet: return 32;
  af_inet6: return 128;
  default: unknown_af(af); return -1;
  }
}

void adns__prefix_mask(int af, int len, union gen_addr *mask_r) {
  switch (af) {
  AF_CASES(af);
  af_inet:
    assert(len <= 32);
    mask_r->v4.s_addr= htonl(!len ? 0 : 0xffffffff << (32-len));
    break;
  af_inet6: {
    int i= len/8, j= len%8;
    unsigned char *m= mask_r->v6.s6_addr;
    assert(len <= 128);
    memset(m, 0xff, i);
    if (j) m[i++]= (0xff << (8-j)) & 0xff;
    memset(m+i, 0, 16-i);
  } break;
  default:
    unknown_af(af);
    break;
  }
}

int adns__guess_prefix_length(int af, const union gen_addr *addr) {
  switch (af) {
  AF_CASES(af);
  af_inet: {
    unsigned a= (ntohl(addr->v4.s_addr) >> 24) & 0xff;
    if (a < 128) return 8;
    else if (a < 192) return 16;
    else if (a < 224) return 24;
    else return -1;
  } break;
  af_inet6:
    return 64;
  default:
    unknown_af(af);
    return -1;
  }
}

int adns__addr_match_p(int addraf, const union gen_addr *addr,
		       int netaf, const union gen_addr *base,
		       const union gen_addr *mask)
{
  if (addraf != netaf) return 0;
  switch (addraf) {
  AF_CASES(af);
  af_inet:
    return (addr->v4.s_addr & mask->v4.s_addr) == base->v4.s_addr;
  af_inet6: {
    int i;
    const char *a= addr->v6.s6_addr;
    const char *b= base->v6.s6_addr;
    const char *m= mask->v6.s6_addr;
    for (i = 0; i < 16; i++)
      if ((a[i] & m[i]) != b[i]) return 0;
    return 1;
  } break;
  default:
    unknown_af(addraf);
    return -1;
  }
}

void adns__sockaddr_extract(const struct sockaddr *sa,
			    union gen_addr *a_r, int *port_r) {
  switch (sa->sa_family) {
  AF_CASES(af);
  af_inet: {
    const struct sockaddr_in *sin = CSIN(sa);
    if (port_r) *port_r= ntohs(sin->sin_port);
    if (a_r) a_r->v4= sin->sin_addr;
    break;
  }
  af_inet6: {
    const struct sockaddr_in6 *sin6 = CSIN6(sa);
    if (port_r) *port_r= ntohs(sin6->sin6_port);
    if (a_r) a_r->v6= sin6->sin6_addr;
    break;
  }
  default:
    unknown_af(sa->sa_family);
  }
}

void adns__sockaddr_inject(const union gen_addr *a, int port,
			   struct sockaddr *sa) {
  switch (sa->sa_family) {
  AF_CASES(af);
  af_inet: {
    struct sockaddr_in *sin = SIN(sa);
    if (port != -1) sin->sin_port= htons(port);
    if (a) sin->sin_addr= a->v4;
    break;
  }
  af_inet6: {
    struct sockaddr_in6 *sin6 = SIN6(sa);
    if (port != -1) sin6->sin6_port= htons(port);
    if (a) sin6->sin6_addr= a->v6;
    break;
  }
  default:
    unknown_af(sa->sa_family);
  }
}

/*
 * addr2text and text2addr
 */

#define ADDRFAM_DEBUG
#ifdef ADDRFAM_DEBUG
static void af_debug_func(const char *fmt, ...) {
  int esave= errno;
  va_list al;
  va_start(al,fmt);
  vfprintf(stderr,fmt,al);
  va_end(al);
  errno= esave;
}
# define af_debug(fmt,...) \
  (af_debug_func("%s: " fmt "\n", __func__, __VA_ARGS__))
#else
# define af_debug(fmt,...) ((void)("" fmt "", __VA_ARGS__))
#endif

static bool addrtext_our_errno(int e) {
  return
    e==EAFNOSUPPORT ||
    e==EINVAL ||
    e==ENOSPC ||
    e==ENOSYS;
}

static bool addrtext_scope_use_ifname(const struct sockaddr *sa) {
  const struct in6_addr *in6= &CSIN6(sa)->sin6_addr;
  return
    IN6_IS_ADDR_LINKLOCAL(in6) ||
    IN6_IS_ADDR_MC_LINKLOCAL(in6);
}

int adns_text2addr(const char *text, uint16_t port, adns_queryflags flags,
		   struct sockaddr *sa, socklen_t *salen_io) {
  int af;
  char copybuf[INET6_ADDRSTRLEN];
  const char *parse=text;
  const char *scopestr=0;
  socklen_t needlen;
  void *dst;
  uint16_t *portp;

#define INVAL(how) do{				\
  af_debug("invalid: %s: `%s'", how, text);	\
  return EINVAL;				\
}while(0)

#define AFCORE(INETx,SINx,sinx)			\
    af= AF_##INETx;				\
    dst = &SINx(sa)->sinx##_addr;		\
    portp = &SINx(sa)->sinx##_port;		\
    needlen= sizeof(*SINx(sa));

  if (!strchr(text, ':')) { /* INET */

    AFCORE(INET,SIN,sin);

  } else { /* INET6 */

    AFCORE(INET6,SIN6,sin6);

    const char *percent= strchr(text, '%');
    if (percent) {
      ptrdiff_t lhslen = percent - text;
      if (lhslen >= INET6_ADDRSTRLEN) INVAL("scoped addr lhs too long");
      memcpy(copybuf, text, lhslen);
      copybuf[lhslen]= 0;

      parse= copybuf;
      scopestr= percent+1;

      af_debug("will parse scoped addr `%s' %% `%s'", parse, scopestr);
    }

  }

#undef AFCORE

  if (scopestr && (flags & adns_qf_addrlit_scope_forbid))
    INVAL("scoped addr but _scope_forbid");

  if (*salen_io < needlen) {
    *salen_io = needlen;
    return ENOSPC;
  }

  memset(sa, 0, needlen);

  sa->sa_family= af;
  *portp = htons(port);

  if (af == AF_INET && !(flags & adns_qf_addrlit_ipv4_quadonly)) {
    /* we have to use inet_aton to deal with non-dotted-quad literals */
    int r= inet_aton(parse,&SIN(sa)->sin_addr);
    if (!r) INVAL("inet_aton rejected");
  } else {
    int r= inet_pton(af,parse,dst);
    if (!r) INVAL("inet_pton rejected");
    assert(r>0);
  }

  if (scopestr) {
    errno=0;
    char *ep;
    unsigned long scope= strtoul(scopestr,&ep,10);
    if (errno==ERANGE) INVAL("numeric scope id too large for unsigned long");
    assert(!errno);
    if (!*ep) {
      if (scope > ~(uint32_t)0)
	INVAL("numeric scope id too large for uint32_t");
    } else { /* !!*ep */
      if (flags & adns_qf_addrlit_scope_numeric)
	INVAL("non-numeric scope but _scope_numeric");
      if (!addrtext_scope_use_ifname(sa)) {
	af_debug("cannot convert non-numeric scope"
		 " in non-link-local addr `%s'", text);
	return ENOSYS;
      }
      errno= 0;
      scope= if_nametoindex(scopestr);
      if (!scope) {
	/* RFC3493 says "No errors are defined".  It's not clear
	 * whether that is supposed to mean if_nametoindex "can't
	 * fail" (other than by the supplied name not being that of an
	 * interface) which seems unrealistic, or that it conflates
	 * all its errors together by failing to set errno, or simply
	 * that they didn't bother to document the errors.
	 *
	 * glibc, FreeBSD and OpenBSD all set errno (to ENXIO when
	 * appropriate).  See Debian bug #749349.
	 *
	 * We attempt to deal with this by clearing errno to start
	 * with, and then perhaps mapping the results. */
	af_debug("if_nametoindex rejected scope name (errno=%s)",
		 strerror(errno));
	if (errno==0) {
	  return ENXIO;
	} else if (addrtext_our_errno(errno)) {
	  /* we use these for other purposes, urgh. */
	  perror("adns: adns_text2addr: if_nametoindex"
		 " failed with unexpected error");
	  return EIO;
	} else {
	  return errno;
	}
      } else { /* ix>0 */
	if (scope > ~(uint32_t)0) {
	  fprintf(stderr,"adns: adns_text2addr: if_nametoindex"
		  " returned an interface index >=2^32 which will not fit"
		  " in sockaddr_in6.sin6_scope_id");
	  return EIO;
	}
      }
    } /* else; !!*ep */

    SIN6(sa)->sin6_scope_id= scope;
  } /* if (scopestr) */

  *salen_io = needlen;
  return 0;
}

int adns_addr2text(const struct sockaddr *sa, adns_queryflags flags,
		   char *buffer, int *buflen_io, int *port_r) {
  const void *src;
  int port;

  if (*buflen_io < ADNS_ADDR2TEXT_BUFLEN) {
    *buflen_io = ADNS_ADDR2TEXT_BUFLEN;
    return ENOSPC;
  }

  switch (sa->sa_family) {
    AF_CASES(af);
    af_inet:  src= &CSIN(sa)->sin_addr;    port= CSIN(sa)->sin_port;    break;
    af_inet6: src= &CSIN6(sa)->sin6_addr;  port= CSIN6(sa)->sin6_port;  break;
    default: return EAFNOSUPPORT;
  }

  const char *ok= inet_ntop(sa->sa_family, src, buffer, *buflen_io);
  assert(ok);

  if (sa->sa_family == AF_INET6) {
    uint32_t scope = CSIN6(sa)->sin6_scope_id;
    if (scope) {
      if (flags & adns_qf_addrlit_scope_forbid)
	return EINVAL;
      int scopeoffset = strlen(buffer);
      int remain = *buflen_io - scopeoffset;
      char *scopeptr =  buffer + scopeoffset;
      assert(remain >= IF_NAMESIZE+1/*%*/);
      *scopeptr++= '%'; remain--;
      bool parsedname = 0;
      af_debug("will print scoped addr %s %% %"PRIu32"", buffer, scope);
      if (scope <= UINT_MAX /* so we can pass it to if_indextoname */
	  && !(flags & adns_qf_addrlit_scope_numeric)
	  && addrtext_scope_use_ifname(sa)) {
	parsedname = if_indextoname(scope, scopeptr);
	if (!parsedname) {
	  af_debug("if_indextoname rejected scope (errno=%s)",
		   strerror(errno));
	  if (errno==ENXIO) {
	    /* fair enough, show it as a number then */
	  } else if (addrtext_our_errno(errno)) {
	    /* we use these for other purposes, urgh. */
	    perror("adns: adns_addr2text: if_indextoname"
		   " failed with unexpected error");
	    return EIO;
	  } else {
	    return errno;
	  }
	}
      }
      if (!parsedname) {
	int r = snprintf(scopeptr, remain,
			 "%"PRIu32"", scope);
	assert(r < *buflen_io - scopeoffset);
      }
      af_debug("printed scoped addr `%s'", buffer);
    }
  }

  if (port_r) *port_r= ntohs(port);
  return 0;
}