-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifaddr.c
241 lines (191 loc) · 5.53 KB
/
ifaddr.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
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
/* @(#) interface/address conversion
*
* Copyright 2008-2011 Pavel V. Cherenkov ([email protected])
*
* This file is part of udpxy.
*
* udpxy 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 3 of the License, or
* (at your option) any later version.
*
* udpxy 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 udpxy. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <net/if.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <limits.h>
#include "osdef.h"
#include "ifaddr.h"
/* check if ifr contains info on the desired ifname
*/
static int
chkifr( const struct ifreq* ifr, const char* ifname,
const size_t addrlen, size_t* offset )
{
size_t sa_len = 0;
assert(ifr && ifname && offset);
#ifdef NO_SOCKADDR_SA_LEN
switch( ifr->ifr_addr.sa_family )
{
#ifndef NO_INET6_SUPPORT
case AF_INET6: sa_len = sizeof(struct sockaddr_in6); break;
#endif
case AF_INET: sa_len = sizeof(struct sockaddr); break;
default: sa_len = 0; break;
}
#else
sa_len = ifr->ifr_addr.sa_len;
#endif
if( sa_len > 0 ) {
if ( (ifr->ifr_addr.sa_family == AF_INET) &&
(0 == strncmp(ifname, ifr->ifr_name, sizeof(struct ifreq))) &&
(addrlen >= sa_len) ) {
*offset = sa_len;
return 0;
}
}
#if defined(__linux)
*offset = sizeof(*ifr);
#else
*offset = (sa_len + sizeof( ifr->ifr_name ));
/* the above is per R. Stevens' book and not working on 64-bit Linux */
#endif
return -1;
}
/* retrieve IPv4 address of the given network interface
*/
int
if2addr( const char* ifname,
struct sockaddr *addr, size_t addrlen )
{
int rc, sockfd;
char *buf, *rec;
size_t buflen, offset;
int last_len;
struct ifconf ifc;
struct ifreq ifr;
static size_t IFC_TABLE_SIZE;
static const size_t IFC_ENTRIES = 32;
static const size_t MAX_IFCBUF_SIZE = (1024 * 256);
IFC_TABLE_SIZE = sizeof(struct ifreq) * IFC_ENTRIES;
assert( ifname && addr && addrlen );
rc = 0;
/* acquire the list of network interfaces */
sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
if( -1 == sockfd ) return -1;
buf = NULL; buflen = IFC_TABLE_SIZE; last_len = 0;
for( ; buflen < MAX_IFCBUF_SIZE; buflen += IFC_TABLE_SIZE ) {
if( NULL == (buf = malloc( buflen )) ) {
rc = -1;
break;
}
ifc.ifc_len = buflen;
ifc.ifc_buf = buf;
if( ioctl( sockfd, SIOCGIFCONF, &ifc ) < 0 ) {
if( (EINVAL != errno) || (last_len != 0) ) {
rc = errno;
break;
}
}
else {
if( ifc.ifc_len == last_len )
break;
else
last_len = ifc.ifc_len;
}
free( buf );
buf = NULL;
} /* for */
(void) close( sockfd );
if( buflen > MAX_IFCBUF_SIZE ) rc = -1;
if( 0 != rc ) {
if( NULL != buf ) free( buf );
return rc;
}
assert( ifc.ifc_buf );
/* look for ifname in the list */
for( rec = ifc.ifc_buf; rec < (ifc.ifc_buf + ifc.ifc_len); ) {
(void) memcpy( &ifr, rec, sizeof(struct ifreq) );
offset = 0;
rc = chkifr( &ifr, ifname, addrlen, &offset );
if ( 0 == rc ) {
(void) memcpy( addr, &(ifr.ifr_addr), offset );
break;
}
if( 0 == offset ) break;
rec += offset;
} /* for */
if( rec >= (buf + ifc.ifc_len) ) {
rc = -1;
}
free( buf );
return rc;
}
/* convert input parameter into an IPv4-address string
*/
int
get_ipv4_address( const char* s, char* buf, size_t len )
{
struct sockaddr_in saddr;
int rc = 0;
assert( s && buf && len );
if( 1 == inet_aton(s, &(saddr.sin_addr)) ) {
(void) strncpy( buf, s, len );
}
else {
rc = if2addr( s, (struct sockaddr*)&saddr, sizeof(saddr) );
if( 0 != rc ) return rc;
(void) strncpy( buf, inet_ntoa(saddr.sin_addr), len );
}
buf[ len - 1 ] = 0;
return rc;
}
/*jivco*/
/* split input string into IP address and port
*/
int
get_addrport( const char* s, char* addr, size_t len, int* port )
{
struct sockaddr_in saddr;
size_t i = 0;
int iport = 0;
static const int ERR_NOPORT = -1;
static const int ERR_BADADDR = -2;
static const int ERR_BADPORT = -3;
static const int ERR_OVERFLOW = -4;
assert( s && addr && len && port );
for( i = 0; (i < len) && s[i] && (':' != s[i]); ++i )
addr[ i ] = s[ i ];
if( i >= len )
return ERR_OVERFLOW;
else
addr[i] = '\0';
/* IP address is not followed by port */
if( ':' != s[ i ] ) return ERR_NOPORT;
if( 1 != inet_aton( addr, &(saddr.sin_addr)) )
return ERR_BADADDR;
++i;
if( i >= len || !s[i] ) return ERR_NOPORT;
errno = 0;
iport = atoi( s + i );
if( errno || (iport <= 0) || (iport > (int)USHRT_MAX) )
return ERR_BADPORT;
*port = iport;
return 0;
}
/* __EOF__ */