-
Notifications
You must be signed in to change notification settings - Fork 2
/
parse_addr.c
170 lines (139 loc) · 4.2 KB
/
parse_addr.c
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
#define _GNU_SOURCE
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include "parse_addr.h"
/*************************************************************************/
int to_addr_port(
sockaddr__u *restrict sockaddr,
const char *restrict addrport,
twdip__t atype
)
{
struct addrinfo hints;
struct addrinfo *results;
size_t len = strlen(addrport);
char ipaddr[len + 1];
char *taddr;
char *tport;
int rc;
assert(sockaddr != NULL);
assert(addrport != NULL);
assert((atype == TWDIP_v4) || (atype == TWDIP_v6) || (atype == TWDIP_any));
memcpy(ipaddr,addrport,len + 1);
/*--------------------------------------------------------------------
; We search backwards through the string, instead of forwards, because
; IPv6 addresses has embedded colons (but are embedded in brackets, with
; the port outside the brackets. It will look like:
;
; [fc00::1]:2222
;
;-----------------------------------------------------------------------*/
tport = memrchr(ipaddr,':',len);
if (tport != NULL)
*tport++ = '\0';
else
return EINVAL; /* when we get an IANA port, we can specify it here */
/*--------------------------------------------------------------------
; if the first character of the addrport is a bracket, this is an IPv6
; address. Using a hack, if the two characters previous to the port text
; is NOT a closing bracket, we return an error, otherwise, we NUL out the
; closing bracket, and skip the addrport pointer past the opening bracket,
; thus leaving us with a straight IPv6 address.
;-------------------------------------------------------------------------*/
if (ipaddr[0] == '[')
{
if (tport[-2] != ']')
return EINVAL;
tport[-2] = '\0';
taddr = &ipaddr[1];
}
else
taddr = ipaddr;
memset(&hints,0,sizeof(hints));
results = NULL;
hints.ai_family = atype;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
rc = getaddrinfo(taddr,tport,&hints,&results);
/*-----------------------------------------------------------------------
; Woot! We finally have an address! Now grovel through the results for
; something we can use.
;------------------------------------------------------------------------*/
if (rc == 0)
memcpy(&sockaddr->sa,results->ai_addr,results->ai_addrlen);
else
rc = EADDRNOTAVAIL;
freeaddrinfo(results);
return rc;
}
/*************************************************************************/
#ifdef TEST
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
twdip__t atype;
int ret;
int i;
if (argc == 1)
{
fprintf(stderr,"usage: %s [a46] hosts...\n",argv[0]);
return EXIT_FAILURE;
}
switch(argv[1][0])
{
case '4': atype = TWDIP_v4; break;
case '6': atype = TWDIP_v6; break;
case 'a': atype = TWDIP_any; break;
default:
fprintf(stderr,"usage: %s [a46] hosts...\n",argv[0]);
return EXIT_FAILURE;
}
for (i = 2 , ret = EXIT_SUCCESS ; i < argc ; i++)
{
sockaddr__u addr;
char tipv4[INET_ADDRSTRLEN];
char tipv6[INET6_ADDRSTRLEN];
int rc;
rc = to_addr_port(&addr,argv[i],atype);
if (rc != 0)
{
printf("NONE %s: %s\n",argv[i],strerror(rc));
ret = EXIT_FAILURE;
continue;
}
switch(addr.sa.sa_family)
{
case AF_INET:
printf(
"IPv4 %s %s:%d\n",
argv[i],
inet_ntop(AF_INET,&addr.sin.sin_addr.s_addr,tipv4,sizeof(tipv4)),
ntohs(addr.sin.sin_port)
);
break;
case AF_INET6:
printf(
"IPv6: %s [%s]:%d\n",
argv[i],
inet_ntop(AF_INET6,&addr.sin6.sin6_addr.s6_addr,tipv6,sizeof(tipv6)),
ntohs(addr.sin6.sin6_port)
);
break;
default:
fprintf(stderr,"this should not happen\n");
ret = EXIT_FAILURE;
break;
}
}
return ret;
}
#endif