summaryrefslogtreecommitdiff
path: root/imap/src/osdep/unix/ip6_unix.c
blob: 2727adb2ee9a59526823128f98b270925f44401b (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
/* ========================================================================
 * Copyright 2008-2011 Mark Crispin
 * ========================================================================
 */

/*
 * Program:	UNIX IPv6 routines
 *
 * Author:	Mark Crispin
 *
 * Date:	18 December 2003
 * Last Edited:	30 July 2011
 *
 * Previous versions of this file were
 *
 * Copyright 1988-2006 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 */


/*
 * There is some amazingly bad design in IPv6 sockets.
 *
 * Supposedly, the new getnameinfo() and getaddrinfo() functions create an
 * abstraction that is not dependent upon IPv4 or IPv6.  However, the
 * definition of getnameinfo() requires that the caller pass the length of
 * the sockaddr instead of deriving it from sa_family.  The man page says
 * that there's an sa_len member in the sockaddr, but actually there isn't.
 * This means that any caller to getnameinfo() and getaddrinfo() has to know
 * the size for the protocol family used by that sockaddr.
 *
 * The new sockaddr_in6 is bigger than the generic sockaddr (which is what
 * connect(), accept(), bind(), getpeername(), getsockname(), etc. expect).
 * Rather than increase the size of sockaddr, there's a new sockaddr_storage
 * which is only usable for allocating space.
 */

#define SADRLEN sizeof (struct sockaddr_storage)

#define SADR4(sadr) ((struct sockaddr_in *) sadr)
#define SADR4LEN sizeof (struct sockaddr_in)
#define SADR4ADR(sadr) SADR4 (sadr)->sin_addr
#define ADR4LEN sizeof (struct in_addr)
#define SADR4PORT(sadr) SADR4 (sadr)->sin_port

#define SADR6(sadr) ((struct sockaddr_in6 *) sadr)
#define SADR6LEN sizeof (struct sockaddr_in6)
#define SADR6ADR(sadr) SADR6 (sadr)->sin6_addr
#define ADR6LEN sizeof (struct in6_addr)
#define SADR6PORT(sadr) SADR6 (sadr)->sin6_port


/* IP abstraction layer */

char *ip_sockaddrtostring (struct sockaddr *sadr,char buf[NI_MAXHOST]);
long ip_sockaddrtoport (struct sockaddr *sadr);
void *ip_stringtoaddr (char *text,size_t *len,int *family);
struct sockaddr *ip_newsockaddr (size_t *len);
struct sockaddr *ip_sockaddr (int family,void *adr,size_t adrlen,
			      unsigned short port,size_t *len);
char *ip_sockaddrtoname (struct sockaddr *sadr,char buf[NI_MAXHOST]);
void *ip_nametoaddr (char *name,size_t *len,int *family,char **canonical,
		     void **next,void **cleanup);

/* Return IP address string from socket address
 * Accepts: socket address
 *	    buffer
 * Returns: IP address as name string
 */

char *ip_sockaddrtostring (struct sockaddr *sadr,char buf[NI_MAXHOST])
{
  switch (sadr->sa_family) {
  case PF_INET:			/* IPv4 */
    if (!getnameinfo (sadr,SADR4LEN,buf,NI_MAXHOST,NIL,NIL,NI_NUMERICHOST))
      return buf;
    break;
  case PF_INET6:		/* IPv6 */
    if (!getnameinfo (sadr,SADR6LEN,buf,NI_MAXHOST,NIL,NIL,NI_NUMERICHOST))
      return buf;
    break;
  }
  return "NON-IP";
}


/* Return port from socket address
 * Accepts: socket address
 * Returns: port number or -1 if can't determine it
 */

long ip_sockaddrtoport (struct sockaddr *sadr)
{
  switch (sadr->sa_family) {
  case PF_INET:
    return ntohs (SADR4PORT (sadr));
  case PF_INET6:
    return ntohs (SADR6PORT (sadr));
  }
  return -1;
}

/* Return IP address from string
 * Accepts: name string
 *	    pointer to returned length
 *	    pointer to returned address family
 * Returns: address if valid, length and family updated, or NIL
 */

void *ip_stringtoaddr (char *text,size_t *len,int *family)

{
  char tmp[MAILTMPLEN];
  struct addrinfo hints;
  struct addrinfo *ai;
  void *adr = NIL;
				/* initialize hints */
  memset (&hints,NIL,sizeof (hints));
  hints.ai_family = AF_UNSPEC;/* allow any address family */
  hints.ai_socktype = SOCK_STREAM;
				/* numeric name only */
  hints.ai_flags = AI_NUMERICHOST;
				/* case-independent lookup */
  if (text && (strlen (text) < MAILTMPLEN) &&
      (!getaddrinfo (lcase (strcpy (tmp,text)),NIL,&hints,&ai))) {
    switch (*family = ai->ai_family) {
    case AF_INET:		/* IPv4 */
      adr = fs_get (*len = ADR4LEN);
      memcpy (adr,(void *) &SADR4ADR (ai->ai_addr),*len);
      break;
    case AF_INET6:		/* IPv6 */
      adr = fs_get (*len = ADR6LEN);
      memcpy (adr,(void *) &SADR6ADR (ai->ai_addr),*len);
      break;
    }
    freeaddrinfo (ai);		/* free addrinfo */
  }
  return adr;
}

/* Create a maximum-size socket address
 * Accepts: pointer to return maximum socket address length
 * Returns: new, empty socket address of maximum size
 */

struct sockaddr *ip_newsockaddr (size_t *len)
{
  return (struct sockaddr *) memset (fs_get (SADRLEN),0,*len = SADRLEN);
}


/* Stuff a socket address
 * Accepts: address family
 *	    IPv4 address
 *	    length of address
 *	    port number
 *	    pointer to return socket address length
 * Returns: socket address
 */

struct sockaddr *ip_sockaddr (int family,void *adr,size_t adrlen,
			      unsigned short port,size_t *len)
{
  struct sockaddr *sadr = ip_newsockaddr (len);
  switch (family) {		/* build socket address based upon family */
  case AF_INET:			/* IPv4 */
    sadr->sa_family = PF_INET;
				/* copy host address */
    memcpy (&SADR4ADR (sadr),adr,adrlen);
				/* copy port number in network format */
    SADR4PORT (sadr) = htons (port);
    *len = SADR4LEN;
    break;
  case AF_INET6:		/* IPv6 */
    sadr->sa_family = PF_INET6;
				/* copy host address */
    memcpy (&SADR6ADR (sadr),adr,adrlen);
				/* copy port number in network format */
    SADR6PORT (sadr) = htons (port);
    *len = SADR6LEN;
    break;
  default:			/* non-IP?? */
    sadr->sa_family = PF_UNSPEC;
    break;
  }
  return sadr;
}

/* Return name from socket address
 * Accepts: socket address
 *	    buffer
 * Returns: canonical name for that address or NIL if none
 */

char *ip_sockaddrtoname (struct sockaddr *sadr,char buf[NI_MAXHOST])
{
  switch (sadr->sa_family) {
  case PF_INET:			/* IPv4 */
    if (!getnameinfo (sadr,SADR4LEN,buf,NI_MAXHOST,NIL,NIL,NI_NAMEREQD))
      return buf;
    break;
  case PF_INET6:		/* IPv6 */
    if (!getnameinfo (sadr,SADR6LEN,buf,NI_MAXHOST,NIL,NIL,NI_NAMEREQD))
      return buf;
    break;
  }
  return NIL;
}

/* Return address from name
 * Accepts: name or NIL to return next address
 *	    pointer to previous/returned length
 *	    pointer to previous/returned address family
 *	    pointer to previous/returned canonical name
 *	    pointer to previous/return state for next-address calls
 *	    pointer to cleanup (or NIL to get canonical name only)
 * Returns: address with length/family/canonical updated if needed, or NIL
 */

void *ip_nametoaddr (char *name,size_t *len,int *family,char **canonical,
		     void **next,void **cleanup)
{
  char tmp[MAILTMPLEN];
  struct addrinfo *cur = NIL;
  struct addrinfo hints;
  void *ret = NIL;
				/* initialize hints */
  memset (&hints,NIL,sizeof (hints));
				/* allow any address family */
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
				/* need canonical name */
  hints.ai_flags = AI_CANONNAME;
  if (name) {			/* name supplied? */
    struct addrinfo *aitmp = NIL;
    if (!cleanup) cleanup = (void **) &aitmp;
    else if (*cleanup) {
      freeaddrinfo (*cleanup);	/* free old addrinfo */
      *cleanup = NIL;
    }
				/* case-independent lookup */
    if ((strlen (name) < MAILTMPLEN) &&
	(!getaddrinfo (lcase (strcpy (tmp,name)),NIL,&hints,
		       (struct addrinfo **) cleanup))) {
      cur = *cleanup;		/* current block */
      if (canonical)		/* set canonical name */
	*canonical = cpystr (cur->ai_canonname ? cur->ai_canonname : tmp);
				/* remember as next block */
      if (next) *next = (void *) cur;
    }
    else {			/* error */
      cur = NIL;
      if (len) *len = 0;
      if (family) *family = 0;
      if (canonical) *canonical = NIL;
      if (next) *next = NIL;
    }
    if (aitmp) {		/* special call to get canonical name */
      freeaddrinfo (aitmp);
      if (len) *len = 0;
      if (family) *family = 0;
      if (next) *next = NIL;
      return VOIDT;		/* return only needs to be non-NIL */
    }
  }

				/* return next in series */
  else if (next && (cur = ((struct addrinfo *) *next)->ai_next)) {
    *next = cur;		/* set as last address */
				/* set canonical in case changed */
    if (canonical && cur->ai_canonname) {
      if (*canonical) fs_give ((void **) canonical);
      *canonical = cpystr (cur->ai_canonname);
    }
  }
  else if (*cleanup) {
    freeaddrinfo (*cleanup);	/* free old addrinfo */
    *cleanup = NIL;
  }
  if (cur) {			/* got data? */
    if (family) *family = cur->ai_family;
    switch (cur->ai_family) {
    case AF_INET:
      if (len) *len = ADR4LEN;
      ret = (void *) &SADR4ADR (cur->ai_addr);
      break;
    case AF_INET6:
      if (len) *len = ADR6LEN;
      ret = (void *) &SADR6ADR (cur->ai_addr);
      break;
    default:
      if (len) *len = 0;	/* error return */
    }
  }
  return ret;
}