| 1 |
/* |
| 2 |
* Copyright (c) 2005,2006 Christophe Fillot. |
| 3 |
* E-mail: [email protected] |
| 4 |
* |
| 5 |
* Network Utility functions. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "utils.h" |
| 9 |
#include "net.h" |
| 10 |
#include "crc.h" |
| 11 |
|
| 12 |
/* |
| 13 |
* IP mask table, which allows to find quickly a network mask |
| 14 |
* with a prefix length. |
| 15 |
*/ |
| 16 |
n_ip_addr_t ip_masks[N_IP_ADDR_BITS+1] = { |
| 17 |
0x0, |
| 18 |
0x80000000, 0xC0000000, 0xE0000000, 0xF0000000, |
| 19 |
0xF8000000, 0xFC000000, 0xFE000000, 0xFF000000, |
| 20 |
0xFF800000, 0xFFC00000, 0xFFE00000, 0xFFF00000, |
| 21 |
0xFFF80000, 0xFFFC0000, 0xFFFE0000, 0xFFFF0000, |
| 22 |
0xFFFF8000, 0xFFFFC000, 0xFFFFE000, 0xFFFFF000, |
| 23 |
0xFFFFF800, 0xFFFFFC00, 0xFFFFFE00, 0xFFFFFF00, |
| 24 |
0xFFFFFF80, 0xFFFFFFC0, 0xFFFFFFE0, 0xFFFFFFF0, |
| 25 |
0xFFFFFFF8, 0xFFFFFFFC, 0xFFFFFFFE, 0xFFFFFFFF |
| 26 |
}; |
| 27 |
|
| 28 |
/* |
| 29 |
* IPv6 mask table, which allows to find quickly a network mask |
| 30 |
* with a prefix length. Note this is a particularly ugly way |
| 31 |
* to do this, since we use statically 2 Kb. |
| 32 |
*/ |
| 33 |
n_ipv6_addr_t ipv6_masks[N_IPV6_ADDR_BITS+1]; |
| 34 |
|
| 35 |
/* Initialize IPv6 masks */ |
| 36 |
void ipv6_init_masks(void) |
| 37 |
{ |
| 38 |
int i,index; |
| 39 |
|
| 40 |
/* Set all bits to 1 */ |
| 41 |
memset(ipv6_masks,0xff,sizeof(ipv6_masks)); |
| 42 |
|
| 43 |
for(i=0;i<N_IPV6_ADDR_BITS;i++) |
| 44 |
{ |
| 45 |
index = i >> 3; /* Compute byte index (divide by 8) */ |
| 46 |
|
| 47 |
/* rotate byte */ |
| 48 |
ipv6_masks[i].ip6.u6_addr8[index++] <<= (8 - (i & 7)); |
| 49 |
|
| 50 |
/* clear following bytes */ |
| 51 |
while(index<N_IPV6_ADDR_LEN) |
| 52 |
ipv6_masks[i].ip6.u6_addr8[index++] = 0; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/* Convert an IPv4 address into a string */ |
| 57 |
char *n_ip_ntoa(char *buffer,n_ip_addr_t ip_addr) |
| 58 |
{ |
| 59 |
u_char *p = (u_char *)&ip_addr; |
| 60 |
sprintf(buffer,"%u.%u.%u.%u",p[0],p[1],p[2],p[3]); |
| 61 |
return(buffer); |
| 62 |
} |
| 63 |
|
| 64 |
#if HAS_RFC2553 |
| 65 |
/* Convert in IPv6 address into a string */ |
| 66 |
char *n_ipv6_ntoa(char *buffer,n_ipv6_addr_t *ipv6_addr) |
| 67 |
{ |
| 68 |
return((char *)inet_ntop(AF_INET6,ipv6_addr,buffer,INET6_ADDRSTRLEN)); |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
/* Convert a string containing an IP address in binary */ |
| 73 |
int n_ip_aton(n_ip_addr_t *ip_addr,char *ip_str) |
| 74 |
{ |
| 75 |
struct in_addr addr; |
| 76 |
|
| 77 |
if (inet_aton(ip_str,&addr) == 0) |
| 78 |
return(-1); |
| 79 |
|
| 80 |
*ip_addr = ntohl(addr.s_addr); |
| 81 |
return(0); |
| 82 |
} |
| 83 |
|
| 84 |
#if HAS_RFC2553 |
| 85 |
/* Convert an IPv6 address from string into binary */ |
| 86 |
int n_ipv6_aton(n_ipv6_addr_t *ipv6_addr,char *ip_str) |
| 87 |
{ |
| 88 |
return(inet_pton(AF_INET6,ip_str,ipv6_addr)); |
| 89 |
} |
| 90 |
#endif |
| 91 |
|
| 92 |
/* Parse an IPv4 CIDR prefix */ |
| 93 |
int ip_parse_cidr(char *token,n_ip_addr_t *net_addr,n_ip_addr_t *net_mask) |
| 94 |
{ |
| 95 |
char *sl,*tmp,*err; |
| 96 |
u_long mask; |
| 97 |
|
| 98 |
/* Find separator */ |
| 99 |
if ((sl = strchr(token,'/')) == NULL) |
| 100 |
return(-1); |
| 101 |
|
| 102 |
/* Get mask */ |
| 103 |
mask = strtoul(sl+1,&err,0); |
| 104 |
if (*err != 0) |
| 105 |
return(-1); |
| 106 |
|
| 107 |
/* Ensure that mask has a correct value */ |
| 108 |
if (mask > N_IP_ADDR_BITS) |
| 109 |
return(-1); |
| 110 |
|
| 111 |
if ((tmp = strdup(token)) == NULL) |
| 112 |
return(-1); |
| 113 |
|
| 114 |
sl = strchr(tmp,'/'); |
| 115 |
*sl = 0; |
| 116 |
|
| 117 |
/* Parse IP Address */ |
| 118 |
if (n_ip_aton(net_addr,tmp) == -1) { |
| 119 |
free(tmp); |
| 120 |
return(-1); |
| 121 |
} |
| 122 |
|
| 123 |
/* Set netmask */ |
| 124 |
*net_mask = ip_masks[mask]; |
| 125 |
|
| 126 |
free(tmp); |
| 127 |
return(0); |
| 128 |
} |
| 129 |
|
| 130 |
#if HAS_RFC2553 |
| 131 |
/* Parse an IPv6 CIDR prefix */ |
| 132 |
int ipv6_parse_cidr(char *token,n_ipv6_addr_t *net_addr,u_int *net_mask) |
| 133 |
{ |
| 134 |
char *sl,*tmp,*err; |
| 135 |
u_long mask; |
| 136 |
|
| 137 |
/* Find separator */ |
| 138 |
if ((sl = strchr(token,'/')) == NULL) |
| 139 |
return(-1); |
| 140 |
|
| 141 |
/* Get mask */ |
| 142 |
mask = strtoul(sl+1,&err,0); |
| 143 |
if (*err != 0) |
| 144 |
return(-1); |
| 145 |
|
| 146 |
/* Ensure that mask has a correct value */ |
| 147 |
if (mask > N_IPV6_ADDR_BITS) |
| 148 |
return(-1); |
| 149 |
|
| 150 |
if ((tmp = strdup(token)) == NULL) |
| 151 |
return(-1); |
| 152 |
|
| 153 |
sl = strchr(tmp,'/'); |
| 154 |
*sl = 0; |
| 155 |
|
| 156 |
/* Parse IP Address */ |
| 157 |
if (n_ipv6_aton(net_addr,tmp) <= 0) { |
| 158 |
free(tmp); |
| 159 |
return(-1); |
| 160 |
} |
| 161 |
|
| 162 |
/* Set netmask */ |
| 163 |
*net_mask = (u_int)mask; |
| 164 |
|
| 165 |
free(tmp); |
| 166 |
return(0); |
| 167 |
} |
| 168 |
#endif |
| 169 |
|
| 170 |
/* Parse a processor board id and return the eeprom settings in a buffer */ |
| 171 |
int parse_board_id(m_uint8_t *buf,const char *id,int encode) |
| 172 |
{ |
| 173 |
// Encode the serial board id |
| 174 |
// encode 4 maps this into 4 bytes |
| 175 |
// encode 9 maps into 9 bytes |
| 176 |
// encode 11 maps into 11 bytes |
| 177 |
// |
| 178 |
|
| 179 |
memset(buf,0,11); |
| 180 |
if (encode == 4) { |
| 181 |
int res; |
| 182 |
|
| 183 |
int v; |
| 184 |
res = sscanf(id,"%d",&v); |
| 185 |
if (res != 1) return (-1); |
| 186 |
buf[3] = (v & 0xFF); v>>=8; |
| 187 |
buf[2] = (v & 0xFF); v>>=8; |
| 188 |
buf[1] = (v & 0xFF); v>>=8; |
| 189 |
buf[0] = (v & 0xFF); v>>=8; |
| 190 |
//printf("%x %x %x %x \n",buf[0],buf[1],buf[2],buf[3]); |
| 191 |
return (0); |
| 192 |
} else if (encode == 9) { |
| 193 |
int res; |
| 194 |
|
| 195 |
res = sscanf(id,"%c%c%c%2hx%2hx%c%c%c%c", |
| 196 |
&buf[0],&buf[1], |
| 197 |
&buf[2],(unsigned short int *)&buf[3], |
| 198 |
(unsigned short int *)&buf[4],&buf[5], |
| 199 |
&buf[6],&buf[7],&buf[8] |
| 200 |
); |
| 201 |
if (res != 9) return (-1); |
| 202 |
//printf("%x %x %x %x %x %x %x %x .. %x\n", |
| 203 |
// buf[0],buf[1],buf[2],buf[3], |
| 204 |
// buf[4],buf[5],buf[6],buf[7] ,buf[8]); |
| 205 |
return (0); |
| 206 |
} else if (encode == 11) { |
| 207 |
int res; |
| 208 |
|
| 209 |
res = sscanf(id,"%c%c%c%c%c%c%c%c%c%c%c", |
| 210 |
&buf[0],&buf[1], |
| 211 |
&buf[2],&buf[3], |
| 212 |
&buf[4],&buf[5], |
| 213 |
&buf[6],&buf[7], |
| 214 |
&buf[8],&buf[9], |
| 215 |
&buf[10] |
| 216 |
); |
| 217 |
if (res != 11) return (-1); |
| 218 |
//printf("%x %x %x %x %x %x %x %x %x %x .. %x\n", |
| 219 |
// buf[0],buf[1],buf[2],buf[3],buf[4],buf[5], |
| 220 |
// buf[6],buf[7],buf[8],buf[9], buf[10]); |
| 221 |
return (0); |
| 222 |
} |
| 223 |
return (-1); |
| 224 |
} |
| 225 |
|
| 226 |
/* Parse a MAC address */ |
| 227 |
int parse_mac_addr(n_eth_addr_t *addr,char *str) |
| 228 |
{ |
| 229 |
u_int v[N_ETH_ALEN]; |
| 230 |
int i,res; |
| 231 |
|
| 232 |
/* First try, standard format (00:01:02:03:04:05) */ |
| 233 |
res = sscanf(str,"%x:%x:%x:%x:%x:%x",&v[0],&v[1],&v[2],&v[3],&v[4],&v[5]); |
| 234 |
|
| 235 |
if (res == 6) { |
| 236 |
for(i=0;i<N_ETH_ALEN;i++) |
| 237 |
addr->eth_addr_byte[i] = v[i]; |
| 238 |
return(0); |
| 239 |
} |
| 240 |
|
| 241 |
/* Second try, Cisco format (0001.0002.0003) */ |
| 242 |
res = sscanf(str,"%x.%x.%x",&v[0],&v[1],&v[2]); |
| 243 |
|
| 244 |
if (res == 3) { |
| 245 |
addr->eth_addr_byte[0] = (v[0] >> 8) & 0xFF; |
| 246 |
addr->eth_addr_byte[1] = v[0] & 0xFF; |
| 247 |
addr->eth_addr_byte[2] = (v[1] >> 8) & 0xFF; |
| 248 |
addr->eth_addr_byte[3] = v[1] & 0xFF; |
| 249 |
addr->eth_addr_byte[4] = (v[2] >> 8) & 0xFF; |
| 250 |
addr->eth_addr_byte[5] = v[2] & 0xFF; |
| 251 |
return(0); |
| 252 |
} |
| 253 |
|
| 254 |
return(-1); |
| 255 |
} |
| 256 |
|
| 257 |
/* Convert an Ethernet address into a string */ |
| 258 |
char *n_eth_ntoa(char *buffer,n_eth_addr_t *addr,int format) |
| 259 |
{ |
| 260 |
char *str_format; |
| 261 |
|
| 262 |
if (format == 0) { |
| 263 |
str_format = "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x"; |
| 264 |
} else { |
| 265 |
str_format = "%2.2x%2.2x.%2.2x%2.2x.%2.2x%2.2x"; |
| 266 |
} |
| 267 |
|
| 268 |
sprintf(buffer,str_format, |
| 269 |
addr->eth_addr_byte[0],addr->eth_addr_byte[1], |
| 270 |
addr->eth_addr_byte[2],addr->eth_addr_byte[3], |
| 271 |
addr->eth_addr_byte[4],addr->eth_addr_byte[5]); |
| 272 |
return(buffer); |
| 273 |
} |
| 274 |
|
| 275 |
#if HAS_RFC2553 |
| 276 |
/* Create a new socket to connect to specified host */ |
| 277 |
int udp_connect(int local_port,char *remote_host,int remote_port) |
| 278 |
{ |
| 279 |
struct addrinfo hints,*res,*res0; |
| 280 |
struct sockaddr_storage st; |
| 281 |
int error, sck = -1, yes = 1; |
| 282 |
char port_str[20]; |
| 283 |
|
| 284 |
memset(&hints,0,sizeof(hints)); |
| 285 |
hints.ai_family = PF_UNSPEC; |
| 286 |
hints.ai_socktype = SOCK_DGRAM; |
| 287 |
|
| 288 |
snprintf(port_str,sizeof(port_str),"%d",remote_port); |
| 289 |
|
| 290 |
if ((error = getaddrinfo(remote_host,port_str,&hints,&res0)) != 0) { |
| 291 |
fprintf(stderr,"%s\n",gai_strerror(error)); |
| 292 |
return(-1); |
| 293 |
} |
| 294 |
|
| 295 |
for(res=res0;res;res=res->ai_next) |
| 296 |
{ |
| 297 |
/* We want only IPv4 or IPv6 */ |
| 298 |
if ((res->ai_family != PF_INET) && (res->ai_family != PF_INET6)) |
| 299 |
continue; |
| 300 |
|
| 301 |
/* create new socket */ |
| 302 |
if ((sck = socket(res->ai_family,SOCK_DGRAM,res->ai_protocol)) < 0) { |
| 303 |
perror("udp_connect: socket"); |
| 304 |
continue; |
| 305 |
} |
| 306 |
|
| 307 |
/* bind to the local port */ |
| 308 |
memset(&st,0,sizeof(st)); |
| 309 |
|
| 310 |
switch(res->ai_family) { |
| 311 |
case PF_INET: { |
| 312 |
struct sockaddr_in *sin = (struct sockaddr_in *)&st; |
| 313 |
sin->sin_family = PF_INET; |
| 314 |
sin->sin_port = htons(local_port); |
| 315 |
break; |
| 316 |
} |
| 317 |
|
| 318 |
case PF_INET6: { |
| 319 |
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&st; |
| 320 |
#ifdef SIN6_LEN |
| 321 |
sin6->sin6_len = res->ai_addrlen; |
| 322 |
#endif |
| 323 |
sin6->sin6_family = PF_INET6; |
| 324 |
sin6->sin6_port = htons(local_port); |
| 325 |
break; |
| 326 |
} |
| 327 |
|
| 328 |
default: |
| 329 |
/* shouldn't happen */ |
| 330 |
close(sck); |
| 331 |
sck = -1; |
| 332 |
continue; |
| 333 |
} |
| 334 |
|
| 335 |
/* try to connect to remote host */ |
| 336 |
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); |
| 337 |
if (!bind(sck,(struct sockaddr *)&st,res->ai_addrlen) && |
| 338 |
!connect(sck,res->ai_addr,res->ai_addrlen)) |
| 339 |
break; |
| 340 |
|
| 341 |
close(sck); |
| 342 |
sck = -1; |
| 343 |
} |
| 344 |
|
| 345 |
freeaddrinfo(res0); |
| 346 |
|
| 347 |
if (sck >= 0 && m_fd_set_non_block(sck) < 0) |
| 348 |
perror("Warning: udp_connect: m_fd_set_non_block"); |
| 349 |
|
| 350 |
return(sck); |
| 351 |
} |
| 352 |
#else |
| 353 |
/* |
| 354 |
* Create a new socket to connect to specified host. |
| 355 |
* Version for old systems that do not support RFC 2553 (getaddrinfo()) |
| 356 |
* |
| 357 |
* See http://www.faqs.org/rfcs/rfc2553.html for more info. |
| 358 |
*/ |
| 359 |
int udp_connect(int local_port,char *remote_host,int remote_port) |
| 360 |
{ |
| 361 |
struct sockaddr_in sin; |
| 362 |
struct hostent *hp; |
| 363 |
int sck, yes = 1; |
| 364 |
|
| 365 |
if (!(hp = gethostbyname(remote_host))) { |
| 366 |
fprintf(stderr,"udp_connect: unable to resolve '%s'\n",remote_host); |
| 367 |
return(-1); |
| 368 |
} |
| 369 |
|
| 370 |
if ((sck = socket(AF_INET,SOCK_DGRAM,0)) < 0) { |
| 371 |
perror("udp_connect: socket"); |
| 372 |
return(-1); |
| 373 |
} |
| 374 |
|
| 375 |
/* bind local port */ |
| 376 |
memset(&sin,0,sizeof(sin)); |
| 377 |
sin.sin_family = PF_INET; |
| 378 |
sin.sin_port = htons(local_port); |
| 379 |
|
| 380 |
setsockopt(sck, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); |
| 381 |
if (bind(sck,(struct sockaddr *)&sin,sizeof(sin)) < 0) { |
| 382 |
perror("udp_connect: bind"); |
| 383 |
close(sck); |
| 384 |
return(-1); |
| 385 |
} |
| 386 |
|
| 387 |
/* try to connect to remote host */ |
| 388 |
memset(&sin,0,sizeof(sin)); |
| 389 |
memcpy(&sin.sin_addr,hp->h_addr_list[0],sizeof(struct in_addr)); |
| 390 |
sin.sin_family = PF_INET; |
| 391 |
sin.sin_port = htons(remote_port); |
| 392 |
|
| 393 |
if (connect(sck,(struct sockaddr *)&sin,sizeof(sin)) < 0) { |
| 394 |
perror("udp_connect: connect"); |
| 395 |
close(sck); |
| 396 |
return(-1); |
| 397 |
} |
| 398 |
|
| 399 |
if (m_fd_set_non_block(sck) < 0) |
| 400 |
perror("Warning: udp_connect: m_fd_set_non_block"); |
| 401 |
|
| 402 |
return(sck); |
| 403 |
} |
| 404 |
#endif /* HAS_RFC2553 */ |
| 405 |
|
| 406 |
#if HAS_RFC2553 |
| 407 |
/* Listen on the specified port */ |
| 408 |
int ip_listen(char *ip_addr,int port,int sock_type,int max_fd,int fd_array[]) |
| 409 |
{ |
| 410 |
struct addrinfo hints,*res,*res0; |
| 411 |
char port_str[20],*addr; |
| 412 |
int nsock,error,i; |
| 413 |
int reuse = 1; |
| 414 |
|
| 415 |
for(i=0;i<max_fd;i++) |
| 416 |
fd_array[i] = -1; |
| 417 |
|
| 418 |
memset(&hints,0,sizeof(hints)); |
| 419 |
hints.ai_family = PF_UNSPEC; |
| 420 |
hints.ai_socktype = sock_type; |
| 421 |
hints.ai_flags = AI_PASSIVE; |
| 422 |
|
| 423 |
snprintf(port_str,sizeof(port_str),"%d",port); |
| 424 |
addr = (ip_addr && strlen(ip_addr)) ? ip_addr : NULL; |
| 425 |
|
| 426 |
if ((error = getaddrinfo(addr,port_str,&hints,&res0)) != 0) { |
| 427 |
fprintf(stderr,"ip_listen: %s", gai_strerror(error)); |
| 428 |
return(-1); |
| 429 |
} |
| 430 |
|
| 431 |
nsock = 0; |
| 432 |
for(res=res0;(res && (nsock < max_fd));res=res->ai_next) |
| 433 |
{ |
| 434 |
if ((res->ai_family != PF_INET) && (res->ai_family != PF_INET6)) |
| 435 |
continue; |
| 436 |
|
| 437 |
fd_array[nsock] = socket(res->ai_family,res->ai_socktype, |
| 438 |
res->ai_protocol); |
| 439 |
|
| 440 |
if (fd_array[nsock] < 0) |
| 441 |
continue; |
| 442 |
|
| 443 |
setsockopt(fd_array[nsock],SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); |
| 444 |
|
| 445 |
if ((bind(fd_array[nsock],res->ai_addr,res->ai_addrlen) < 0) || |
| 446 |
((sock_type == SOCK_STREAM) && (listen(fd_array[nsock],5) < 0))) |
| 447 |
{ |
| 448 |
close(fd_array[nsock]); |
| 449 |
fd_array[nsock] = -1; |
| 450 |
continue; |
| 451 |
} |
| 452 |
|
| 453 |
nsock++; |
| 454 |
} |
| 455 |
|
| 456 |
freeaddrinfo(res0); |
| 457 |
return(nsock); |
| 458 |
} |
| 459 |
#else |
| 460 |
/* Listen on the specified port */ |
| 461 |
int ip_listen(char *ip_addr,int port,int sock_type,int max_fd,int fd_array[]) |
| 462 |
{ |
| 463 |
struct sockaddr_in sin; |
| 464 |
int i,sck,reuse=1; |
| 465 |
|
| 466 |
for(i=0;i<max_fd;i++) |
| 467 |
fd_array[i] = -1; |
| 468 |
|
| 469 |
if ((sck = socket(AF_INET,sock_type,0)) < 0) { |
| 470 |
perror("ip_listen: socket"); |
| 471 |
return(-1); |
| 472 |
} |
| 473 |
|
| 474 |
/* bind local port */ |
| 475 |
memset(&sin,0,sizeof(sin)); |
| 476 |
sin.sin_family = PF_INET; |
| 477 |
sin.sin_port = htons(port); |
| 478 |
|
| 479 |
if (ip_addr && strlen(ip_addr)) |
| 480 |
sin.sin_addr.s_addr = inet_addr(ip_addr); |
| 481 |
|
| 482 |
setsockopt(sck,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)); |
| 483 |
|
| 484 |
if (bind(sck,(struct sockaddr *)&sin,sizeof(sin)) < 0) { |
| 485 |
perror("ip_listen: bind"); |
| 486 |
goto error; |
| 487 |
} |
| 488 |
|
| 489 |
if ((sock_type == SOCK_STREAM) && (listen(sck,5) < 0)) { |
| 490 |
perror("ip_listen: listen"); |
| 491 |
goto error; |
| 492 |
} |
| 493 |
|
| 494 |
fd_array[0] = sck; |
| 495 |
return(1); |
| 496 |
|
| 497 |
error: |
| 498 |
close(sck); |
| 499 |
return(-1); |
| 500 |
} |
| 501 |
#endif |
| 502 |
|
| 503 |
#if HAS_RFC2553 |
| 504 |
/* Get port in an address info structure */ |
| 505 |
static int ip_socket_get_port(struct sockaddr *addr) |
| 506 |
{ |
| 507 |
switch(addr->sa_family) { |
| 508 |
case AF_INET: |
| 509 |
return(ntohs(((struct sockaddr_in *)addr)->sin_port)); |
| 510 |
case AF_INET6: |
| 511 |
return(ntohs(((struct sockaddr_in6 *)addr)->sin6_port)); |
| 512 |
default: |
| 513 |
fprintf(stderr,"ip_socket_get_port: unknown address family %d\n", |
| 514 |
addr->sa_family); |
| 515 |
return(-1); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
/* Set port in an address info structure */ |
| 520 |
static int ip_socket_set_port(struct sockaddr *addr,int port) |
| 521 |
{ |
| 522 |
if (!addr) |
| 523 |
return(-1); |
| 524 |
|
| 525 |
switch(addr->sa_family) { |
| 526 |
case AF_INET: |
| 527 |
((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 528 |
return(0); |
| 529 |
|
| 530 |
case AF_INET6: |
| 531 |
((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
| 532 |
return(0); |
| 533 |
|
| 534 |
default: |
| 535 |
fprintf(stderr,"ip_socket_set_port: unknown address family %d\n", |
| 536 |
addr->sa_family); |
| 537 |
return(-1); |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
/* Try to create a socket and bind to the specified address info */ |
| 542 |
static int ip_socket_bind(struct addrinfo *addr) |
| 543 |
{ |
| 544 |
int fd,off=0; |
| 545 |
|
| 546 |
if ((fd = socket(addr->ai_family,addr->ai_socktype,addr->ai_protocol)) < 0) |
| 547 |
return(-1); |
| 548 |
|
| 549 |
#ifdef IPV6_V6ONLY |
| 550 |
setsockopt(fd,IPPROTO_IPV6,IPV6_V6ONLY,&off,sizeof(off)); |
| 551 |
#endif |
| 552 |
|
| 553 |
if ( (bind(fd,addr->ai_addr,addr->ai_addrlen) < 0) || |
| 554 |
((addr->ai_socktype == SOCK_STREAM) && (listen(fd,5) < 0)) ) |
| 555 |
{ |
| 556 |
close(fd); |
| 557 |
return(-1); |
| 558 |
} |
| 559 |
|
| 560 |
return(fd); |
| 561 |
} |
| 562 |
|
| 563 |
/* Listen on a TCP/UDP port - port is choosen in the specified range */ |
| 564 |
int ip_listen_range(char *ip_addr,int port_start,int port_end,int *port, |
| 565 |
int sock_type) |
| 566 |
{ |
| 567 |
struct addrinfo hints,*res,*res0; |
| 568 |
struct sockaddr_storage st; |
| 569 |
socklen_t st_len; |
| 570 |
char port_str[20],*addr; |
| 571 |
int error,i,fd = -1; |
| 572 |
|
| 573 |
memset(&hints,0,sizeof(hints)); |
| 574 |
hints.ai_family = PF_UNSPEC; |
| 575 |
hints.ai_socktype = sock_type; |
| 576 |
hints.ai_flags = AI_PASSIVE; |
| 577 |
|
| 578 |
snprintf(port_str,sizeof(port_str),"%d",port_start); |
| 579 |
addr = (ip_addr && strlen(ip_addr)) ? ip_addr : NULL; |
| 580 |
|
| 581 |
if ((error = getaddrinfo(addr,port_str,&hints,&res0)) != 0) { |
| 582 |
fprintf(stderr,"ip_listen_range: %s", gai_strerror(error)); |
| 583 |
return(-1); |
| 584 |
} |
| 585 |
|
| 586 |
for(i=port_start;i<=port_end;i++) { |
| 587 |
for(res=res0;res!=NULL;res=res->ai_next) { |
| 588 |
ip_socket_set_port(res->ai_addr,i); |
| 589 |
|
| 590 |
if ((fd = ip_socket_bind(res)) >= 0) { |
| 591 |
st_len = sizeof(st); |
| 592 |
getsockname(fd,(struct sockaddr *)&st,&st_len); |
| 593 |
*port = ip_socket_get_port((struct sockaddr *)&st); |
| 594 |
goto done; |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
done: |
| 600 |
freeaddrinfo(res0); |
| 601 |
return(fd); |
| 602 |
} |
| 603 |
|
| 604 |
/* Connect an existing socket to connect to specified host */ |
| 605 |
int ip_connect_fd(int fd,char *remote_host,int remote_port) |
| 606 |
{ |
| 607 |
struct addrinfo hints,*res,*res0; |
| 608 |
char port_str[20]; |
| 609 |
int error; |
| 610 |
|
| 611 |
memset(&hints,0,sizeof(hints)); |
| 612 |
hints.ai_family = PF_UNSPEC; |
| 613 |
|
| 614 |
snprintf(port_str,sizeof(port_str),"%d",remote_port); |
| 615 |
|
| 616 |
if ((error = getaddrinfo(remote_host,port_str,&hints,&res0)) != 0) { |
| 617 |
fprintf(stderr,"%s\n",gai_strerror(error)); |
| 618 |
return(-1); |
| 619 |
} |
| 620 |
|
| 621 |
for(res=res0;res;res=res->ai_next) { |
| 622 |
if ((res->ai_family != PF_INET) && (res->ai_family != PF_INET6)) |
| 623 |
continue; |
| 624 |
|
| 625 |
if (!connect(fd,res->ai_addr,res->ai_addrlen)) |
| 626 |
break; |
| 627 |
} |
| 628 |
|
| 629 |
freeaddrinfo(res0); |
| 630 |
return(0); |
| 631 |
} |
| 632 |
#else |
| 633 |
/* Try to create a socket and bind to the specified address info */ |
| 634 |
static int ip_socket_bind(struct sockaddr_in *sin,int sock_type) |
| 635 |
{ |
| 636 |
int fd; |
| 637 |
|
| 638 |
if ((fd = socket(sin->sin_family,sock_type,0)) < 0) |
| 639 |
return(-1); |
| 640 |
|
| 641 |
if ( (bind(fd,(struct sockaddr *)sin,sizeof(*sin)) < 0) || |
| 642 |
((sock_type == SOCK_STREAM) && (listen(fd,5) < 0)) ) |
| 643 |
{ |
| 644 |
close(fd); |
| 645 |
return(-1); |
| 646 |
} |
| 647 |
|
| 648 |
return(fd); |
| 649 |
} |
| 650 |
|
| 651 |
/* Listen on a TCP/UDP port - port is choosen in the specified range */ |
| 652 |
int ip_listen_range(char *ip_addr,int port_start,int port_end,int *port, |
| 653 |
int sock_type) |
| 654 |
{ |
| 655 |
struct hostent *hp; |
| 656 |
struct sockaddr_in sin; |
| 657 |
socklen_t len; |
| 658 |
int i,fd; |
| 659 |
|
| 660 |
memset(&sin,0,sizeof(sin)); |
| 661 |
sin.sin_family = PF_INET; |
| 662 |
|
| 663 |
if (ip_addr && strlen(ip_addr)) { |
| 664 |
if (!(hp = gethostbyname(ip_addr))) { |
| 665 |
fprintf(stderr,"ip_listen_range: unable to resolve '%s'\n",ip_addr); |
| 666 |
return(-1); |
| 667 |
} |
| 668 |
|
| 669 |
memcpy(&sin.sin_addr,hp->h_addr_list[0],sizeof(struct in_addr)); |
| 670 |
} |
| 671 |
|
| 672 |
for(i=port_start;i<=port_end;i++) { |
| 673 |
sin.sin_port = htons(i); |
| 674 |
|
| 675 |
if ((fd = ip_socket_bind(&sin,sock_type)) >= 0) { |
| 676 |
len = sizeof(sin); |
| 677 |
getsockname(fd,(struct sockaddr *)&sin,&len); |
| 678 |
*port = ntohs(sin.sin_port); |
| 679 |
return(fd); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return(-1); |
| 684 |
} |
| 685 |
|
| 686 |
/* Connect an existing socket to connect to specified host */ |
| 687 |
int ip_connect_fd(int fd,char *remote_host,int remote_port) |
| 688 |
{ |
| 689 |
struct sockaddr_in sin; |
| 690 |
struct hostent *hp; |
| 691 |
|
| 692 |
if (!(hp = gethostbyname(remote_host))) { |
| 693 |
fprintf(stderr,"ip_connect_fd: unable to resolve '%s'\n",remote_host); |
| 694 |
return(-1); |
| 695 |
} |
| 696 |
|
| 697 |
/* try to connect to remote host */ |
| 698 |
memset(&sin,0,sizeof(sin)); |
| 699 |
memcpy(&sin.sin_addr,hp->h_addr_list[0],sizeof(struct in_addr)); |
| 700 |
sin.sin_family = PF_INET; |
| 701 |
sin.sin_port = htons(remote_port); |
| 702 |
|
| 703 |
return(connect(fd,(struct sockaddr *)&sin,sizeof(sin))); |
| 704 |
} |
| 705 |
#endif |
| 706 |
|
| 707 |
/* Create a socket UDP listening in a port of specified range */ |
| 708 |
int udp_listen_range(char *ip_addr,int port_start,int port_end,int *port) |
| 709 |
{ |
| 710 |
return(ip_listen_range(ip_addr,port_start,port_end,port,SOCK_DGRAM)); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
/* |
| 715 |
* ISL rewrite. |
| 716 |
* |
| 717 |
* See: http://www.cisco.com/en/US/tech/tk389/tk390/technologies_tech_note09186a0080094665.shtml |
| 718 |
*/ |
| 719 |
void cisco_isl_rewrite(m_uint8_t *pkt,m_uint32_t tot_len) |
| 720 |
{ |
| 721 |
static m_uint8_t isl_xaddr[N_ETH_ALEN] = { 0x01,0x00,0x0c,0x00,0x10,0x00 }; |
| 722 |
u_int real_offset,real_len; |
| 723 |
n_eth_hdr_t *hdr; |
| 724 |
m_uint32_t ifcs; |
| 725 |
|
| 726 |
hdr = (n_eth_hdr_t *)pkt; |
| 727 |
if (!memcmp(&hdr->daddr,isl_xaddr,N_ETH_ALEN)) { |
| 728 |
real_offset = N_ETH_HLEN + N_ISL_HDR_SIZE; |
| 729 |
real_len = ntohs(hdr->type); |
| 730 |
real_len -= (N_ISL_HDR_SIZE + 4); |
| 731 |
|
| 732 |
if ((real_offset+real_len) > tot_len) |
| 733 |
return; |
| 734 |
|
| 735 |
/* Rewrite the destination MAC address */ |
| 736 |
hdr->daddr.eth_addr_byte[4] = 0x00; |
| 737 |
|
| 738 |
/* Compute the internal FCS on the encapsulated packet */ |
| 739 |
ifcs = crc32_compute(0xFFFFFFFF,pkt+real_offset,real_len); |
| 740 |
pkt[tot_len-4] = ifcs & 0xff; |
| 741 |
pkt[tot_len-3] = (ifcs >> 8) & 0xff; |
| 742 |
pkt[tot_len-2] = (ifcs >> 16) & 0xff; |
| 743 |
pkt[tot_len-1] = ifcs >> 24; |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
/* Verify checksum of an IP header */ |
| 748 |
int ip_verify_cksum(n_ip_hdr_t *hdr) |
| 749 |
{ |
| 750 |
m_uint8_t *p = (m_uint8_t *)hdr; |
| 751 |
m_uint32_t sum = 0; |
| 752 |
u_int len; |
| 753 |
|
| 754 |
len = (hdr->ihl & 0x0F) << 1; |
| 755 |
while(len-- > 0) { |
| 756 |
sum += ((m_uint16_t)p[0] << 8) | p[1]; |
| 757 |
p += sizeof(m_uint16_t); |
| 758 |
} |
| 759 |
|
| 760 |
while(sum >> 16) |
| 761 |
sum = (sum & 0xFFFF) + (sum >> 16); |
| 762 |
|
| 763 |
return(sum == 0xFFFF); |
| 764 |
} |
| 765 |
|
| 766 |
/* Compute an IP checksum */ |
| 767 |
void ip_compute_cksum(n_ip_hdr_t *hdr) |
| 768 |
{ |
| 769 |
m_uint8_t *p = (m_uint8_t *)hdr; |
| 770 |
m_uint32_t sum = 0; |
| 771 |
u_int len; |
| 772 |
|
| 773 |
hdr->cksum = 0; |
| 774 |
|
| 775 |
len = (hdr->ihl & 0x0F) << 1; |
| 776 |
while(len-- > 0) { |
| 777 |
sum += ((m_uint16_t)p[0] << 8) | p[1]; |
| 778 |
p += sizeof(m_uint16_t); |
| 779 |
} |
| 780 |
|
| 781 |
while(sum >> 16) |
| 782 |
sum = (sum & 0xFFFF) + (sum >> 16); |
| 783 |
|
| 784 |
hdr->cksum = htons(~sum); |
| 785 |
} |
| 786 |
|
| 787 |
/* Partial checksum (for UDP/TCP) */ |
| 788 |
static inline m_uint32_t ip_cksum_partial(m_uint8_t *buf,int len) |
| 789 |
{ |
| 790 |
m_uint32_t sum = 0; |
| 791 |
|
| 792 |
while(len > 1) { |
| 793 |
sum += ((m_uint16_t)buf[0] << 8) | buf[1]; |
| 794 |
buf += sizeof(m_uint16_t); |
| 795 |
len -= sizeof(m_uint16_t); |
| 796 |
} |
| 797 |
|
| 798 |
if (len == 1) |
| 799 |
sum += (m_uint16_t)(*buf) << 8; |
| 800 |
|
| 801 |
return(sum); |
| 802 |
} |
| 803 |
|
| 804 |
/* Partial checksum test */ |
| 805 |
int ip_cksum_partial_test(void) |
| 806 |
{ |
| 807 |
#define N_BUF 4 |
| 808 |
m_uint8_t buffer[N_BUF][512]; |
| 809 |
m_uint16_t psum[N_BUF]; |
| 810 |
m_uint32_t tmp,sum,gsum; |
| 811 |
int i; |
| 812 |
|
| 813 |
for(i=0;i<N_BUF;i++) { |
| 814 |
m_randomize_block(buffer[i],sizeof(buffer[i])); |
| 815 |
//mem_dump(stdout,buffer[i],sizeof(buffer[i])); |
| 816 |
|
| 817 |
sum = ip_cksum_partial(buffer[i],sizeof(buffer[i])); |
| 818 |
|
| 819 |
while(sum >> 16) |
| 820 |
sum = (sum & 0xFFFF) + (sum >> 16); |
| 821 |
|
| 822 |
psum[i] = ~sum; |
| 823 |
} |
| 824 |
|
| 825 |
/* partial sums + accumulator */ |
| 826 |
for(i=0,tmp=0;i<N_BUF;i++) { |
| 827 |
//printf("psum[%d] = 0x%4.4x\n",i,psum[i]); |
| 828 |
tmp += (m_uint16_t)(~psum[i]); |
| 829 |
} |
| 830 |
|
| 831 |
/* global sum */ |
| 832 |
sum = ip_cksum_partial((m_uint8_t *)buffer,sizeof(buffer)); |
| 833 |
|
| 834 |
while(sum >> 16) |
| 835 |
sum = (sum & 0xFFFF) + (sum >> 16); |
| 836 |
|
| 837 |
gsum = sum; |
| 838 |
|
| 839 |
/* accumulator */ |
| 840 |
while(tmp >> 16) |
| 841 |
tmp = (tmp & 0xFFFF) + (tmp >> 16); |
| 842 |
|
| 843 |
//printf("gsum = 0x%4.4x, tmp = 0x%4.4x : %s\n", |
| 844 |
// gsum,tmp,(gsum == tmp) ? "OK" : "FAILURE"); |
| 845 |
|
| 846 |
return(tmp == gsum); |
| 847 |
#undef N_BUF |
| 848 |
} |
| 849 |
|
| 850 |
/* Compute TCP/UDP checksum */ |
| 851 |
m_uint16_t pkt_ctx_tcp_cksum(n_pkt_ctx_t *ctx,int ph) |
| 852 |
{ |
| 853 |
m_uint32_t sum; |
| 854 |
m_uint16_t old_cksum = 0; |
| 855 |
u_int len; |
| 856 |
|
| 857 |
/* replace the actual checksum value with 0 to recompute it */ |
| 858 |
if (!(ctx->flags & N_PKT_CTX_FLAG_IP_FRAG)) { |
| 859 |
switch(ctx->ip_l4_proto) { |
| 860 |
case N_IP_PROTO_TCP: |
| 861 |
old_cksum = ctx->tcp->cksum; |
| 862 |
ctx->tcp->cksum = 0; |
| 863 |
break; |
| 864 |
case N_IP_PROTO_UDP: |
| 865 |
old_cksum = ctx->udp->cksum; |
| 866 |
ctx->udp->cksum = 0; |
| 867 |
break; |
| 868 |
} |
| 869 |
} |
| 870 |
|
| 871 |
len = ntohs(ctx->ip->tot_len) - ((ctx->ip->ihl & 0x0F) << 2); |
| 872 |
sum = ip_cksum_partial(ctx->l4,len); |
| 873 |
|
| 874 |
/* include pseudo-header */ |
| 875 |
if (ph) { |
| 876 |
sum += ip_cksum_partial((m_uint8_t *)&ctx->ip->saddr,8); |
| 877 |
sum += ctx->ip_l4_proto + len; |
| 878 |
} |
| 879 |
|
| 880 |
while(sum >> 16) |
| 881 |
sum = (sum & 0xFFFF) + (sum >> 16); |
| 882 |
|
| 883 |
/* restore the old value */ |
| 884 |
if (!(ctx->flags & N_PKT_CTX_FLAG_IP_FRAG)) { |
| 885 |
switch(ctx->ip_l4_proto) { |
| 886 |
case N_IP_PROTO_TCP: |
| 887 |
ctx->tcp->cksum = old_cksum; |
| 888 |
break; |
| 889 |
case N_IP_PROTO_UDP: |
| 890 |
ctx->udp->cksum = old_cksum; |
| 891 |
break; |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
return(~sum); |
| 896 |
} |
| 897 |
|
| 898 |
/* Analyze L4 for an IP packet */ |
| 899 |
int pkt_ctx_ip_analyze_l4(n_pkt_ctx_t *ctx) |
| 900 |
{ |
| 901 |
switch(ctx->ip_l4_proto) { |
| 902 |
case N_IP_PROTO_TCP: |
| 903 |
ctx->flags |= N_PKT_CTX_FLAG_L4_TCP; |
| 904 |
break; |
| 905 |
case N_IP_PROTO_UDP: |
| 906 |
ctx->flags |= N_PKT_CTX_FLAG_L4_UDP; |
| 907 |
break; |
| 908 |
case N_IP_PROTO_ICMP: |
| 909 |
ctx->flags |= N_PKT_CTX_FLAG_L4_ICMP; |
| 910 |
break; |
| 911 |
} |
| 912 |
|
| 913 |
return(TRUE); |
| 914 |
} |
| 915 |
|
| 916 |
/* Analyze a packet */ |
| 917 |
int pkt_ctx_analyze(n_pkt_ctx_t *ctx,m_uint8_t *pkt,size_t pkt_len) |
| 918 |
{ |
| 919 |
n_eth_dot1q_hdr_t *eth = (n_eth_dot1q_hdr_t *)pkt; |
| 920 |
m_uint16_t eth_type; |
| 921 |
m_uint8_t *p; |
| 922 |
|
| 923 |
ctx->pkt = pkt; |
| 924 |
ctx->pkt_len = pkt_len; |
| 925 |
ctx->flags = 0; |
| 926 |
ctx->vlan_id = 0; |
| 927 |
ctx->l3 = NULL; |
| 928 |
ctx->l4 = NULL; |
| 929 |
|
| 930 |
eth_type = ntohs(eth->type); |
| 931 |
p = PTR_ADJUST(m_uint8_t *,eth,N_ETH_HLEN); |
| 932 |
|
| 933 |
if (eth_type >= N_ETH_MTU) { |
| 934 |
if (eth_type == N_ETH_PROTO_DOT1Q) { |
| 935 |
ctx->flags |= N_PKT_CTX_FLAG_VLAN; |
| 936 |
ctx->vlan_id = htons(eth->vlan_id); |
| 937 |
|
| 938 |
/* override the ethernet type */ |
| 939 |
eth_type = ntohs(*(m_uint16_t *)(p+2)); |
| 940 |
|
| 941 |
/* skip 802.1Q header info */ |
| 942 |
p += sizeof(m_uint32_t); |
| 943 |
} |
| 944 |
} |
| 945 |
|
| 946 |
if (eth_type < N_ETH_MTU) { |
| 947 |
/* LLC/SNAP: TODO */ |
| 948 |
return(TRUE); |
| 949 |
} else { |
| 950 |
ctx->flags |= N_PKT_CTX_FLAG_ETHV2; |
| 951 |
} |
| 952 |
|
| 953 |
switch(eth_type) { |
| 954 |
case N_ETH_PROTO_IP: { |
| 955 |
n_ip_hdr_t *ip; |
| 956 |
u_int len,offset; |
| 957 |
|
| 958 |
ctx->flags |= N_PKT_CTX_FLAG_L3_IP; |
| 959 |
ctx->ip = ip = (n_ip_hdr_t *)p; |
| 960 |
|
| 961 |
/* Check header */ |
| 962 |
if (((ip->ihl & 0xF0) != 0x40) || |
| 963 |
((len = ip->ihl & 0x0F) < N_IP_MIN_HLEN) || |
| 964 |
((len << 2) > ntohs(ip->tot_len)) || |
| 965 |
!ip_verify_cksum(ctx->ip)) |
| 966 |
return(TRUE); |
| 967 |
|
| 968 |
ctx->flags |= N_PKT_CTX_FLAG_IPH_OK; |
| 969 |
ctx->ip_l4_proto = ip->proto; |
| 970 |
ctx->l4 = PTR_ADJUST(void *,ip,len << 2); |
| 971 |
|
| 972 |
/* Check if the packet is a fragment */ |
| 973 |
offset = ntohs(ip->frag_off); |
| 974 |
|
| 975 |
if (((offset & N_IP_OFFMASK) != 0) || (offset & N_IP_FLAG_MF)) |
| 976 |
ctx->flags |= N_PKT_CTX_FLAG_IP_FRAG; |
| 977 |
break; |
| 978 |
} |
| 979 |
|
| 980 |
case N_ETH_PROTO_ARP: |
| 981 |
ctx->flags |= N_PKT_CTX_FLAG_L3_ARP; |
| 982 |
ctx->arp = (n_arp_hdr_t *)p; |
| 983 |
return(TRUE); |
| 984 |
|
| 985 |
default: |
| 986 |
/* other: unknown, stop now */ |
| 987 |
return(TRUE); |
| 988 |
} |
| 989 |
|
| 990 |
return(TRUE); |
| 991 |
} |
| 992 |
|
| 993 |
/* Dump packet context */ |
| 994 |
void pkt_ctx_dump(n_pkt_ctx_t *ctx) |
| 995 |
{ |
| 996 |
printf("pkt=%p (len=%lu), flags=0x%8.8x, vlan_id=0x%4.4x, l3=%p, l4=%p\n", |
| 997 |
ctx->pkt,(u_long)ctx->pkt_len,ctx->flags,ctx->vlan_id, |
| 998 |
ctx->l3,ctx->l4); |
| 999 |
} |