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 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
|
/*
* Wireless Tools
*
* Jean II - HPLB 97->99 - HPL 99->01
*
* Common subroutines to all the wireless tools...
*
* This file is released under the GPL license.
* Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
*/
#include "iwlib.h" /* Header */
/**************************** VARIABLES ****************************/
const char * iw_operation_mode[] = { "Auto",
"Ad-Hoc",
"Managed",
"Master",
"Repeater",
"Secondary" };
/************************ SOCKET SUBROUTINES *************************/
/*------------------------------------------------------------------*/
/*
* Open a socket.
* Depending on the protocol present, open the right socket. The socket
* will allow us to talk to the driver.
*/
int
iw_sockets_open(void)
{
int ipx_sock = -1; /* IPX socket */
int ax25_sock = -1; /* AX.25 socket */
int inet_sock = -1; /* INET socket */
int ddp_sock = -1; /* Appletalk DDP socket */
/*
* Now pick any (exisiting) useful socket family for generic queries
* Note : don't open all the socket, only returns when one matches,
* all protocols might not be valid.
* Workaround by Jim Kaba <jkaba@sarnoff.com>
* Note : in 99% of the case, we will just open the inet_sock.
* The remaining 1% case are not fully correct...
*/
inet_sock=socket(AF_INET, SOCK_DGRAM, 0);
if(inet_sock!=-1)
return inet_sock;
ipx_sock=socket(AF_IPX, SOCK_DGRAM, 0);
if(ipx_sock!=-1)
return ipx_sock;
ax25_sock=socket(AF_AX25, SOCK_DGRAM, 0);
if(ax25_sock!=-1)
return ax25_sock;
ddp_sock=socket(AF_APPLETALK, SOCK_DGRAM, 0);
/*
* If this is -1 we have no known network layers and its time to jump.
*/
return ddp_sock;
}
/*********************** WIRELESS SUBROUTINES ************************/
/*------------------------------------------------------------------*/
/*
* Get the range information out of the driver
*/
int
iw_get_range_info(int skfd,
char * ifname,
iwrange * range)
{
struct iwreq wrq;
char buffer[sizeof(iwrange) * 2]; /* Large enough */
/* Cleanup */
memset(buffer, 0, sizeof(iwrange) * 2);
wrq.u.data.pointer = (caddr_t) buffer;
wrq.u.data.length = sizeof(iwrange) * 2;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWRANGE, &wrq) < 0)
return(-1);
/* Copy stuff at the right place, ignore extra */
memcpy((char *) range, buffer, sizeof(iwrange));
/* Lot's of people have driver and tools out of sync as far as Wireless
* Extensions are concerned. It's because /usr/include/linux/wireless.h
* and /usr/src/linux/include/linux/wireless.h are different.
* We try to catch this stuff here... */
/* For new versions, we can check the version directly, for old versions
* we use magic. 300 bytes is a also magic number, don't touch... */
if((WIRELESS_EXT > 10) && (wrq.u.data.length >= 300))
{
#if WIRELESS_EXT > 10
/* Version verification - for new versions */
if(range->we_version_compiled != WIRELESS_EXT)
{
fprintf(stderr, "Warning : Device %s has been compiled with version %d\n", ifname, range->we_version_compiled);
fprintf(stderr, "of Wireless Extension, while we are using version %d.\n", WIRELESS_EXT);
fprintf(stderr, "Some things may be broken...\n\n");
}
#endif /* WIRELESS_EXT > 10 */
}
else
{
/* Version verification - for old versions */
if(wrq.u.data.length != sizeof(iwrange))
{
fprintf(stderr, "Warning : Device %s has been compiled with a different version\n", ifname);
fprintf(stderr, "of Wireless Extension than ours (we are using version %d).\n", WIRELESS_EXT);
fprintf(stderr, "Some things may be broken...\n\n");
}
}
/* Note : we are only trying to catch compile difference, not source.
* If the driver source has not been updated to the latest, it doesn't
* matter because the new fields are set to zero */
return(0);
}
/*------------------------------------------------------------------*/
/*
* Get information about what private ioctls are supported by the driver
*/
int
iw_get_priv_info(int skfd,
char * ifname,
iwprivargs * priv)
{
struct iwreq wrq;
/* Ask the driver */
wrq.u.data.pointer = (caddr_t) priv;
wrq.u.data.length = 32;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWPRIV, &wrq) < 0)
return(-1);
/* Return the number of ioctls */
return(wrq.u.data.length);
}
/*------------------------------------------------------------------*/
/*
* Get essential wireless config from the device driver
* We will call all the classical wireless ioctl on the driver through
* the socket to know what is supported and to get the settings...
* Note : compare to the version in iwconfig, we extract only
* what's *really* needed to configure a device...
*/
int
iw_get_basic_config(int skfd,
char * ifname,
wireless_config * info)
{
struct iwreq wrq;
memset((char *) info, 0, sizeof(struct wireless_config));
/* Get wireless name */
if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
/* If no wireless name : no wireless extensions */
return(-1);
else
strcpy(info->name, wrq.u.name);
/* Get network ID */
if(iw_get_ext(skfd, ifname, SIOCGIWNWID, &wrq) >= 0)
{
info->has_nwid = 1;
memcpy(&(info->nwid), &(wrq.u.nwid), sizeof(iwparam));
}
/* Get frequency / channel */
if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) >= 0)
{
info->has_freq = 1;
info->freq = iw_freq2float(&(wrq.u.freq));
}
/* Get encryption information */
wrq.u.data.pointer = (caddr_t) info->key;
wrq.u.data.length = IW_ENCODING_TOKEN_MAX;
wrq.u.data.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) >= 0)
{
info->has_key = 1;
info->key_size = wrq.u.data.length;
info->key_flags = wrq.u.data.flags;
}
/* Get ESSID */
wrq.u.essid.pointer = (caddr_t) info->essid;
wrq.u.essid.length = IW_ESSID_MAX_SIZE;
wrq.u.essid.flags = 0;
if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) >= 0)
{
info->has_essid = 1;
info->essid_on = wrq.u.data.flags;
}
/* Get operation mode */
if(iw_get_ext(skfd, ifname, SIOCGIWMODE, &wrq) >= 0)
{
if((wrq.u.mode < 6) && (wrq.u.mode >= 0))
info->has_mode = 1;
info->mode = wrq.u.mode;
}
return(0);
}
/*------------------------------------------------------------------*/
/*
* Set essential wireless config in the device driver
* We will call all the classical wireless ioctl on the driver through
* the socket to know what is supported and to set the settings...
* We support only the restricted set as above...
*/
int
iw_set_basic_config(int skfd,
char * ifname,
wireless_config * info)
{
struct iwreq wrq;
int ret = 0;
/* Get wireless name (check if interface is valid) */
if(iw_get_ext(skfd, ifname, SIOCGIWNAME, &wrq) < 0)
/* If no wireless name : no wireless extensions */
return(-2);
/* Set Network ID, if available (this is for non-802.11 cards) */
if(info->has_nwid)
{
memcpy(&(wrq.u.nwid), &(info->nwid), sizeof(iwparam));
wrq.u.nwid.fixed = 1; /* Hum... When in Rome... */
if(iw_set_ext(skfd, ifname, SIOCSIWNWID, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWNWID: %s\n", strerror(errno));
ret = -1;
}
}
/* Set frequency / channel */
if(info->has_freq)
{
iw_float2freq(info->freq, &(wrq.u.freq));
if(iw_set_ext(skfd, ifname, SIOCSIWFREQ, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWFREQ: %s\n", strerror(errno));
ret = -1;
}
}
/* Set encryption information */
if(info->has_key)
{
int flags = info->key_flags;
/* Check if there is a key index */
if((flags & IW_ENCODE_INDEX) > 0)
{
/* Set the index */
wrq.u.data.pointer = (caddr_t) NULL;
wrq.u.data.flags = (flags & (IW_ENCODE_INDEX)) | IW_ENCODE_NOKEY;
wrq.u.data.length = 0;
if(iw_set_ext(skfd, ifname, SIOCSIWENCODE, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWENCODE(%d): %s\n",
errno, strerror(errno));
ret = -1;
}
}
/* Mask out index to minimise probability of reject when setting key */
flags = flags & (~IW_ENCODE_INDEX);
/* Set the key itself (set current key in this case) */
wrq.u.data.pointer = (caddr_t) info->key;
wrq.u.data.length = info->key_size;
wrq.u.data.flags = flags;
if(iw_set_ext(skfd, ifname, SIOCSIWENCODE, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWENCODE(%d): %s\n",
errno, strerror(errno));
ret = -1;
}
}
/* Set ESSID (extended network), if available */
if(info->has_essid)
{
wrq.u.essid.pointer = (caddr_t) info->essid;
wrq.u.essid.length = strlen(info->essid) + 1;
wrq.u.data.flags = info->essid_on;
if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWESSID: %s\n", strerror(errno));
ret = -1;
}
}
/* Set the current mode of operation */
if(info->has_mode)
{
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
wrq.u.mode = info->mode;
if(iw_get_ext(skfd, ifname, SIOCSIWMODE, &wrq) < 0)
{
fprintf(stderr, "SIOCSIWMODE: %s\n", strerror(errno));
ret = -1;
}
}
return(ret);
}
/********************** FREQUENCY SUBROUTINES ***********************/
/*------------------------------------------------------------------*/
/*
* Convert a floating point the our internal representation of
* frequencies.
* The kernel doesn't want to hear about floating point, so we use
* this custom format instead.
*/
void
iw_float2freq(double in,
iwfreq * out)
{
out->e = (short) (floor(log10(in)));
if(out->e > 8)
{
out->m = ((long) (floor(in / pow(10,out->e - 6)))) * 100;
out->e -= 8;
}
else
{
out->m = in;
out->e = 0;
}
}
/*------------------------------------------------------------------*/
/*
* Convert our internal representation of frequencies to a floating point.
*/
double
iw_freq2float(iwfreq * in)
{
return ((double) in->m) * pow(10,in->e);
}
/************************ POWER SUBROUTINES *************************/
/*------------------------------------------------------------------*/
/*
* Convert a value in dBm to a value in milliWatt.
*/
int
iw_dbm2mwatt(int in)
{
return((int) (floor(pow(10.0, (((double) in) / 10.0)))));
}
/*------------------------------------------------------------------*/
/*
* Convert a value in milliWatt to a value in dBm.
*/
int
iw_mwatt2dbm(int in)
{
return((int) (ceil(10.0 * log10((double) in))));
}
/********************** STATISTICS SUBROUTINES **********************/
/*------------------------------------------------------------------*/
/*
* Read /proc/net/wireless to get the latest statistics
*/
int
iw_get_stats(int skfd,
char * ifname,
iwstats * stats)
{
#if WIRELESS_EXT > 11
struct iwreq wrq;
wrq.u.data.pointer = (caddr_t) stats;
wrq.u.data.length = 0;
wrq.u.data.flags = 1; /* Clear updated flag */
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
if(iw_get_ext(skfd, ifname, SIOCGIWSTATS, &wrq) < 0)
return(-1);
return(0);
#else /* WIRELESS_EXT > 11 */
FILE * f=fopen("/proc/net/wireless","r");
char buf[256];
char * bp;
int t;
if(f==NULL)
return -1;
/* Loop on all devices */
while(fgets(buf,255,f))
{
bp=buf;
while(*bp&&isspace(*bp))
bp++;
/* Is it the good device ? */
if(strncmp(bp,ifname,strlen(ifname))==0 && bp[strlen(ifname)]==':')
{
/* Skip ethX: */
bp=strchr(bp,':');
bp++;
/* -- status -- */
bp = strtok(bp, " ");
sscanf(bp, "%X", &t);
stats->status = (unsigned short) t;
/* -- link quality -- */
bp = strtok(NULL, " ");
if(strchr(bp,'.') != NULL)
stats->qual.updated |= 1;
sscanf(bp, "%d", &t);
stats->qual.qual = (unsigned char) t;
/* -- signal level -- */
bp = strtok(NULL, " ");
if(strchr(bp,'.') != NULL)
stats->qual.updated |= 2;
sscanf(bp, "%d", &t);
stats->qual.level = (unsigned char) t;
/* -- noise level -- */
bp = strtok(NULL, " ");
if(strchr(bp,'.') != NULL)
stats->qual.updated += 4;
sscanf(bp, "%d", &t);
stats->qual.noise = (unsigned char) t;
/* -- discarded packets -- */
bp = strtok(NULL, " ");
sscanf(bp, "%d", &stats->discard.nwid);
bp = strtok(NULL, " ");
sscanf(bp, "%d", &stats->discard.code);
bp = strtok(NULL, " ");
sscanf(bp, "%d", &stats->discard.misc);
fclose(f);
return 0;
}
}
fclose(f);
return -1;
#endif /* WIRELESS_EXT > 11 */
}
/*------------------------------------------------------------------*/
/*
* Output the link statistics, taking care of formating
*/
void
iw_print_stats(char * buffer,
iwqual * qual,
iwrange * range,
int has_range)
{
/* Just do it */
if(has_range && (qual->level != 0))
{
/* If the statistics are in dBm */
if(qual->level > range->max_qual.level)
{
/* Statistics are in dBm (absolute power measurement) */
sprintf(buffer,
"Quality:%d/%d Signal level:%d dBm Noise level:%d dBm%s",
qual->qual, range->max_qual.qual,
qual->level - 0x100, qual->noise - 0x100,
(qual->updated & 0x7) ? " (updated)" : "");
}
else
{
/* Statistics are relative values (0 -> max) */
sprintf(buffer,
"Quality:%d/%d Signal level:%d/%d Noise level:%d/%d%s",
qual->qual, range->max_qual.qual,
qual->level, range->max_qual.level,
qual->noise, range->max_qual.noise,
(qual->updated & 0x7) ? " (updated)" : "");
}
}
else
{
/* We can't read the range, so we don't know... */
sprintf(buffer, "Quality:%d Signal level:%d Noise level:%d%s",
qual->qual, qual->level, qual->noise,
(qual->updated & 0x7) ? " (updated)" : "");
}
}
/*********************** ENCODING SUBROUTINES ***********************/
/*------------------------------------------------------------------*/
/*
* Output the encoding key, with a nice formating
*/
void
iw_print_key(char * buffer,
unsigned char * key,
int key_size,
int key_flags)
{
int i;
/* Is the key present ??? */
if(key_flags & IW_ENCODE_NOKEY)
{
/* Nope : print dummy */
strcpy(buffer, "**");
buffer +=2;
for(i = 1; i < key_size; i++)
{
if((i & 0x1) == 0)
strcpy(buffer++, "-");
strcpy(buffer, "**");
buffer +=2;
}
}
else
{
/* Yes : print the key */
sprintf(buffer, "%.2X", key[0]);
buffer +=2;
for(i = 1; i < key_size; i++)
{
if((i & 0x1) == 0)
strcpy(buffer++, "-");
sprintf(buffer, "%.2X", key[i]);
buffer +=2;
}
}
}
/******************* POWER MANAGEMENT SUBROUTINES *******************/
/*------------------------------------------------------------------*/
/*
* Output a power management value with all attributes...
*/
void
iw_print_pm_value(char * buffer,
int value,
int flags)
{
/* Modifiers */
if(flags & IW_POWER_MIN)
{
strcpy(buffer, " min");
buffer += 4;
}
if(flags & IW_POWER_MAX)
{
strcpy(buffer, " max");
buffer += 4;
}
/* Type */
if(flags & IW_POWER_TIMEOUT)
{
strcpy(buffer, " timeout:");
buffer += 9;
}
else
{
strcpy(buffer, " period:");
buffer += 8;
}
/* Display value without units */
if(flags & IW_POWER_RELATIVE)
sprintf(buffer, "%g ", ((double) value) / MEGA);
else
{
/* Display value with units */
if(value >= (int) MEGA)
sprintf(buffer, "%gs ", ((double) value) / MEGA);
else
if(value >= (int) KILO)
sprintf(buffer, "%gms ", ((double) value) / KILO);
else
sprintf(buffer, "%dus ", value);
}
}
/*------------------------------------------------------------------*/
/*
* Output a power management mode
*/
void
iw_print_pm_mode(char * buffer,
int flags)
{
/* Print the proper mode... */
switch(flags & IW_POWER_MODE)
{
case IW_POWER_UNICAST_R:
strcpy(buffer, " mode:Unicast only received");
break;
case IW_POWER_MULTICAST_R:
strcpy(buffer, " mode:Multicast only received");
break;
case IW_POWER_ALL_R:
strcpy(buffer, " mode:All packets received");
break;
case IW_POWER_FORCE_S:
strcpy(buffer, " mode:Force sending");
break;
case IW_POWER_REPEATER:
strcpy(buffer, " mode:Repeat multicasts");
break;
}
}
/***************** RETRY LIMIT/LIFETIME SUBROUTINES *****************/
#if WIRELESS_EXT > 10
/*------------------------------------------------------------------*/
/*
* Output a retry value with all attributes...
*/
void
iw_print_retry_value(char * buffer,
int value,
int flags)
{
/* Modifiers */
if(flags & IW_RETRY_MIN)
{
strcpy(buffer, " min");
buffer += 4;
}
if(flags & IW_RETRY_MAX)
{
strcpy(buffer, " max");
buffer += 4;
}
/* Type lifetime of limit */
if(flags & IW_RETRY_LIFETIME)
{
strcpy(buffer, " lifetime:");
buffer += 10;
/* Display value without units */
if(flags & IW_POWER_RELATIVE)
sprintf(buffer, "%g", ((double) value) / MEGA);
else
{
/* Display value with units */
if(value >= (int) MEGA)
sprintf(buffer, "%gs", ((double) value) / MEGA);
else
if(value >= (int) KILO)
sprintf(buffer, "%gms", ((double) value) / KILO);
else
sprintf(buffer, "%dus", value);
}
}
else
sprintf(buffer, " limit:%d", value);
}
#endif /* WIRELESS_EXT > 10 */
/*********************** ADDRESS SUBROUTINES ************************/
/*
* This section is mostly a cut & past from net-tools-1.2.0
* manage address display and input...
*/
/*------------------------------------------------------------------*/
/*
* Check if interface support the right MAC address type...
*/
int
iw_check_mac_addr_type(int skfd,
char * ifname)
{
struct ifreq ifr;
/* Get the type of hardware address */
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if((ioctl(skfd, SIOCGIFHWADDR, &ifr) < 0) ||
(ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER))
{
/* Deep trouble... */
fprintf(stderr, "Interface %s doesn't support MAC addresses\n",
ifname);
return(-1);
}
#ifdef DEBUG
printf("Hardware : %d - %s\n", ifr.ifr_hwaddr.sa_family,
pr_ether(ifr.ifr_hwaddr.sa_data));
#endif
return(0);
}
/*------------------------------------------------------------------*/
/*
* Check if interface support the right interface address type...
*/
int
iw_check_if_addr_type(int skfd,
char * ifname)
{
struct ifreq ifr;
/* Get the type of interface address */
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if((ioctl(skfd, SIOCGIFADDR, &ifr) < 0) ||
(ifr.ifr_addr.sa_family != AF_INET))
{
/* Deep trouble... */
fprintf(stderr, "Interface %s doesn't support IP addresses\n", ifname);
return(-1);
}
#ifdef DEBUG
printf("Interface : %d - 0x%lX\n", ifr.ifr_addr.sa_family,
*((unsigned long *) ifr.ifr_addr.sa_data));
#endif
return(0);
}
#if 0
/*------------------------------------------------------------------*/
/*
* Check if interface support the right address types...
*/
int
iw_check_addr_type(int skfd,
char * ifname)
{
/* Check the interface address type */
if(iw_check_if_addr_type(skfd, ifname) < 0)
return(-1);
/* Check the interface address type */
if(iw_check_mac_addr_type(skfd, ifname) < 0)
return(-1);
return(0);
}
#endif
/*------------------------------------------------------------------*/
/*
* Display an Ethernet address in readable format.
*/
char *
iw_pr_ether(char * buffer,
unsigned char * ptr)
{
sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
(ptr[0] & 0xFF), (ptr[1] & 0xFF), (ptr[2] & 0xFF),
(ptr[3] & 0xFF), (ptr[4] & 0xFF), (ptr[5] & 0xFF)
);
return(buffer);
}
/*------------------------------------------------------------------*/
/*
* Input an Ethernet address and convert to binary.
*/
int
iw_in_ether(char *bufp, struct sockaddr *sap)
{
unsigned char *ptr;
char c, *orig;
int i, val;
sap->sa_family = ARPHRD_ETHER;
ptr = sap->sa_data;
i = 0;
orig = bufp;
while((*bufp != '\0') && (i < ETH_ALEN)) {
val = 0;
c = *bufp++;
if (isdigit(c)) val = c - '0';
else if (c >= 'a' && c <= 'f') val = c - 'a' + 10;
else if (c >= 'A' && c <= 'F') val = c - 'A' + 10;
else {
#ifdef DEBUG
fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
#endif
errno = EINVAL;
return(-1);
}
val <<= 4;
c = *bufp++;
if (isdigit(c)) val |= c - '0';
else if (c >= 'a' && c <= 'f') val |= c - 'a' + 10;
else if (c >= 'A' && c <= 'F') val |= c - 'A' + 10;
else {
#ifdef DEBUG
fprintf(stderr, "in_ether(%s): invalid ether address!\n", orig);
#endif
errno = EINVAL;
return(-1);
}
*ptr++ = (unsigned char) (val & 0377);
i++;
/* We might get a semicolon here - not required. */
if (*bufp == ':') {
if (i == ETH_ALEN) {
#ifdef DEBUG
fprintf(stderr, "in_ether(%s): trailing : ignored!\n",
orig)
#endif
; /* nothing */
}
bufp++;
}
}
/* That's it. Any trailing junk? */
if ((i == ETH_ALEN) && (*bufp != '\0')) {
#ifdef DEBUG
fprintf(stderr, "in_ether(%s): trailing junk!\n", orig);
errno = EINVAL;
return(-1);
#endif
}
#ifdef DEBUG
fprintf(stderr, "in_ether(%s): %s\n", orig, pr_ether(sap->sa_data));
#endif
return(0);
}
/*------------------------------------------------------------------*/
/*
* Input an Internet address and convert to binary.
*/
int
iw_in_inet(char *bufp, struct sockaddr *sap)
{
struct hostent *hp;
struct netent *np;
char *name = bufp;
struct sockaddr_in *sin = (struct sockaddr_in *) sap;
/* Grmpf. -FvK */
sin->sin_family = AF_INET;
sin->sin_port = 0;
/* Default is special, meaning 0.0.0.0. */
if (!strcmp(name, "default")) {
sin->sin_addr.s_addr = INADDR_ANY;
return(1);
}
/* Try the NETWORKS database to see if this is a known network. */
if ((np = getnetbyname(name)) != (struct netent *)NULL) {
sin->sin_addr.s_addr = htonl(np->n_net);
strcpy(name, np->n_name);
return(1);
}
if ((hp = gethostbyname(name)) == (struct hostent *)NULL) {
errno = h_errno;
return(-1);
}
memcpy((char *) &sin->sin_addr, (char *) hp->h_addr_list[0], hp->h_length);
strcpy(name, hp->h_name);
return(0);
}
/*------------------------------------------------------------------*/
/*
* Input an address and convert to binary.
*/
int
iw_in_addr(int skfd,
char * ifname,
char * bufp,
struct sockaddr *sap)
{
/* Check if it is a hardware or IP address */
if(index(bufp, ':') == NULL)
{
struct sockaddr if_address;
struct arpreq arp_query;
/* Check if we have valid interface address type */
if(iw_check_if_addr_type(skfd, ifname) < 0)
{
fprintf(stderr, "%-8.8s Interface doesn't support IP addresses\n", ifname);
return(-1);
}
/* Read interface address */
if(iw_in_inet(bufp, &if_address) < 0)
{
fprintf(stderr, "Invalid interface address %s\n", bufp);
return(-1);
}
/* Translate IP addresses to MAC addresses */
memcpy((char *) &(arp_query.arp_pa),
(char *) &if_address,
sizeof(struct sockaddr));
arp_query.arp_ha.sa_family = 0;
arp_query.arp_flags = 0;
/* The following restrict the search to the interface only */
/* For old kernels which complain, just comment it... */
strncpy(arp_query.arp_dev, ifname, IFNAMSIZ);
if((ioctl(skfd, SIOCGARP, &arp_query) < 0) ||
!(arp_query.arp_flags & ATF_COM))
{
fprintf(stderr, "Arp failed for %s on %s... (%d)\nTry to ping the address before setting it.\n",
bufp, ifname, errno);
return(-1);
}
/* Store new MAC address */
memcpy((char *) sap,
(char *) &(arp_query.arp_ha),
sizeof(struct sockaddr));
#ifdef DEBUG
printf("IP Address %s => Hw Address = %s\n",
bufp, pr_ether(sap->sa_data));
#endif
}
else /* If it's an hardware address */
{
/* Check if we have valid mac address type */
if(iw_check_mac_addr_type(skfd, ifname) < 0)
{
fprintf(stderr, "%-8.8s Interface doesn't support MAC addresses\n", ifname);
return(-1);
}
/* Get the hardware address */
if(iw_in_ether(bufp, sap) < 0)
{
fprintf(stderr, "Invalid hardware address %s\n", bufp);
return(-1);
}
}
#ifdef DEBUG
printf("Hw Address = %s\n", pr_ether(sap->sa_data));
#endif
return(0);
}
/************************* MISC SUBROUTINES **************************/
/*------------------------------------------------------------------*/
/*
*/
int
iw_byte_size(int args)
{
int ret = args & IW_PRIV_SIZE_MASK;
if(((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_INT) ||
((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_FLOAT))
ret <<= 2;
if((args & IW_PRIV_TYPE_MASK) == IW_PRIV_TYPE_NONE)
return 0;
return ret;
}
|