📄 mapi.c
字号:
} /* * MOS * Opens a socket for talking with the networking kernel */int OpenSocket() { printf("Wireless Extentions API\n Copyright (c) 2001 By Moustafa A. Youssef- MIND Lab\nBased on\n"); printf("Wireless Tools By Jean II - HPLB '99\n"); if((SocketFileDescriptor = sockets_open()) < 0){ perror("socket"); return -1; } return 0; } void CloseSocket() { close(SocketFileDescriptor); } /**********************************************************************************//***** Spy Subroutines ****//**********************************************************************************/struct sockaddr RegisteredAddresses[IW_MAX_SPY];int NRegistered;/*------------------------------------------------------------------*//* * MOS * Add an address to the list of addresses *Input: - Address: MAC or IP address - InterfaceName: Interface Name; Output: - -1 if error*/int RegisterAddress(char * Address, char * InterfaceName) /* Dev name */{ struct iwreq wrq; char Buffer[(sizeof(struct iw_quality) + sizeof(struct sockaddr)) * IW_MAX_SPY]; /* Check if we have valid address types */ if(check_addr_type(SocketFileDescriptor, InterfaceName) < 0) { fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n", InterfaceName); return(-1); } /* add all addresses already in the driver */ strncpy(wrq.ifr_name, InterfaceName, IFNAMSIZ); wrq.u.data.pointer = (caddr_t) Buffer; wrq.u.data.length = 0; wrq.u.data.flags = 0; if(ioctl(SocketFileDescriptor, SIOCGIWSPY, &wrq) < 0) { fprintf(stderr, "Interface doesn't accept reading addresses...\n"); fprintf(stderr, "SIOCGIWSPY: %s\n", strerror(errno)); return(-1); } /* Copy old addresses */ NRegistered = wrq.u.data.length; memcpy(RegisteredAddresses, Buffer, NRegistered * sizeof(struct sockaddr)); /* Read other args on command line */ if (NRegistered < IW_MAX_SPY) { if(in_addr(SocketFileDescriptor, InterfaceName, Address, &(RegisteredAddresses[NRegistered])) < 0) return (-1); //printf("%ld\n", RegisteredAddresses[NRegistered].sa_data); NRegistered++; }else { fprintf(stderr, "Maximium number of registered addresses (%d) reached\n", IW_MAX_SPY); return (-1);} /* Check the number of addresses */ /* Time to do send addresses to the driver */ strncpy(wrq.ifr_name, InterfaceName, IFNAMSIZ); wrq.u.data.pointer = (caddr_t) RegisteredAddresses; wrq.u.data.length = NRegistered; wrq.u.data.flags = 0; if(ioctl(SocketFileDescriptor, SIOCSIWSPY, &wrq) < 0) { fprintf(stderr, "Interface doesn't accept addresses...\n"); fprintf(stderr, "SIOCSIWSPY: %s\n", strerror(errno)); return(-1); } return(0);}void Initialize(){ NRegistered= 0;}/*------------------------------------------------------------------*//* * Display the spy list of addresses and the associated stats * Input: - InterfaceName: Interface Name Output: - Address: List of Addresses - Stats: List of statistics - returns number of addreses in teh spy list, -1 if error */int GetAddressStats(char * InterfaceName, char* Address, struct iw_quality* Stats){ struct iwreq wrq; char Buffer[(sizeof(struct iw_quality) + sizeof(struct sockaddr)) * IW_MAX_SPY]; struct sockaddr HWA[IW_MAX_SPY]; struct iw_quality Qual[IW_MAX_SPY]; iwrange Range; int HasRange = 0; int n; int i; /* Collect stats */ strncpy(wrq.ifr_name, InterfaceName, IFNAMSIZ); wrq.u.data.pointer = (caddr_t) Buffer; wrq.u.data.length = 0; wrq.u.data.flags = 0; if(ioctl(SocketFileDescriptor, SIOCGIWSPY, &wrq) < 0) { fprintf(stderr, "%-8.8s Interface doesn't support wireless statistic collection\n\n", InterfaceName); return (-1); } /* Number of addresses */ n = wrq.u.data.length; /* Check if we have valid address types */ if(check_addr_type(SocketFileDescriptor, InterfaceName) < 0) { fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n\n", InterfaceName); return (-1); } /* if(in_addr(SocketFileDescriptor, InterfaceName, Address, &BinAddress) < 0) return (-1); printf("%d\n", BinAddress.sa_data); printf("%d\n", BinAddress.sa_data); */ /* Get range info if we can */ if(get_range_info(SocketFileDescriptor, InterfaceName, &(Range)) >= 0) HasRange = 1; /* Display it */ if(n == 0){ printf("%-8.8s No statistics to collect\n", InterfaceName); return (-1); } /* The two lists */ memcpy(HWA, Buffer, n * sizeof(struct sockaddr)); memcpy(Qual, Buffer + n*sizeof(struct sockaddr), n*sizeof(struct iw_quality)); for(i = 0; i < n; i++) { printf("%s %s\n", Address, pr_ether(HWA[i].sa_data)); if (!strcmp(Address, pr_ether(HWA[i].sa_data))){ Stats->qual= Qual[i].qual; Stats->level= Qual[i].level; Stats->noise= Qual[i].noise; Stats->updated= Qual[i].updated; return 0; } /* if(HasRange && (Qual[i].level != 0)) // If the statistics are in dBm if(Qual[i].level > range.max_Qual.level) printf(" %s : Quality %d/%d ; Signal %d dBm ; Noise %d dBm %s\n", pr_ether(HWA[i].sa_data), Qual[i].qual, range.max_Qual.Qual, Qual[i].level - 0x100, Qual[i].noise - 0x100, Qual[i].updated & 0x7 ? "(updated)" : ""); else printf(" %s : Quality %d/%d ; Signal %d/%d ; Noise %d/%d %s\n", pr_ether(HWA[i].sa_data), Qual[i].qual, range.max_Qual.Qual, Qual[i].level, range.max_Qual.level, Qual[i].noise, range.max_Qual.noise, Qual[i].updated & 0x7 ? "(updated)" : ""); else printf(" %s : Quality %d ; Signal %d ; Noise %d %s\n", pr_ether(HWA[i].sa_data), Qual[i].Qual, qual[i].level, Qual[i].noise, Qual[i].updated & 0x7 ? "(updated)" : ""); */ } printf("Address not found\n");return (-1); // address not found }////////////////////////////* * Get all the access points information * Returns number of AP found, -1 if error * Input: - InterfaceName: Interface Name Output: - Address: List of Addresses - Qual: List of statistics - returns number of addreses in teh spy list, -1 if error */int GetAllAPInformation(char* InterfaceName, struct sockaddr * Address, struct iw_quality* Qual, int* HasQual){ struct iwreq wrq; char Buffer[(sizeof(struct iw_quality)+ sizeof(struct sockaddr)) * IW_MAX_AP]; /* Collect stats */ strncpy(wrq.ifr_name, InterfaceName, IFNAMSIZ); wrq.u.data.pointer = (caddr_t) Buffer; wrq.u.data.length = 0; wrq.u.data.flags = 0; if(ioctl(SocketFileDescriptor, SIOCGIWAPLIST, &wrq) < 0) { fprintf(stderr, "%-8.8s Interface doesn't have a list of Access Points\n\n", InterfaceName); return -1; } memcpy(Address, (char *) Buffer,sizeof(struct sockaddr) * wrq.u.data.length); *HasQual= wrq.u.data.flags; if (wrq.u.data.flags) memcpy(Qual, (char*) (Buffer + (sizeof(struct sockaddr) * wrq.u.data.length)), sizeof(struct iw_quality) * wrq.u.data.length); return wrq.u.data.length; }/*------------------------------------------------------------------*//* * Display the list of ap addresses and the associated stats * Exacly the same as the spy list, only with different IOCTL and messages */void DisplayAllAPInfo(char * InterfaceName, int n, struct sockaddr * Address, struct iw_quality* Qual, int HasQual){ struct sockaddr * HWA; iwrange Range; int HasRange = 0; int i; /* The two lists */ HWA = Address; /* Check if we have valid address types */ if(check_addr_type(SocketFileDescriptor, InterfaceName) < 0) { fprintf(stderr, "%-8.8s Interface doesn't support MAC & IP addresses\n\n", InterfaceName); return; } /* Get range info if we can */ if(get_range_info(SocketFileDescriptor, InterfaceName, &(Range)) >= 0) HasRange = 1; /* Display it */ if(n == 0) printf("%-8.8s No Access Point in range\n", InterfaceName); else printf("%-8.8s Access Points in range:\n", InterfaceName); for(i = 0; i < n; i++) { if(HasQual) if(HasRange) /* If the statistics are in dBm */ if(Qual[i].level > Range.max_qual.level) printf(" %s : Quality %d/%d ; Signal %d dBm ; Noise %d dBm %s\n", pr_ether(HWA[i].sa_data), Qual[i].qual, Range.max_qual.qual, Qual[i].level - 0x100, Qual[i].noise - 0x100, Qual[i].updated & 0x7 ? "(updated)" : ""); else printf(" %s : Quality %d/%d ; Signal %d/%d ; Noise %d/%d %s\n", pr_ether(HWA[i].sa_data), Qual[i].qual, Range.max_qual.qual, Qual[i].level, Range.max_qual.level, Qual[i].noise, Range.max_qual.noise, Qual[i].updated & 0x7 ? "(updated)" : ""); else printf(" %s : Quality %d ; Signal %d ; Noise %d %s\n", pr_ether(HWA[i].sa_data), Qual[i].qual, Qual[i].level, Qual[i].noise, Qual[i].updated & 0x7 ? "(updated)" : ""); else printf(" %s\n", pr_ether(HWA[i].sa_data)); } printf("\n");}/////////////////////////////////////////////////////////////////////////////////////////////* * Issues a private command to the driver */int PrivateCommand(char * InterfaceName, /* Dev name */ char * Args[], /* Command line args */ int Count) /* Args count */{ u_char Buffer[1024]; struct iwreq wrq; int i = 0; /* Start with first arg */ int k; iwprivargs Priv[16]; int Number; /* Read the private ioctls */ Number = get_priv_info(SocketFileDescriptor, InterfaceName, Priv); /* Is there any ? */ if(Number <= 0) { /* Could skip this message ? */ fprintf(stderr, "%-8.8s no private ioctls.\n\n", InterfaceName); return(-1); } /* Search the correct ioctl */ k = -1; while((++k < Number) && strcmp(Priv[k].name, Args[i])); /* If not found... */ if(k == Number) { fprintf(stderr, "Invalid command : %s\n", Args[i]); return(-1); } /* Next arg */ i++; /* If we have to set some data */ if((Priv[k].set_args & IW_PRIV_TYPE_MASK) && (Priv[k].set_args & IW_PRIV_SIZE_MASK)) { switch(Priv[k].set_args & IW_PRIV_TYPE_MASK) { case IW_PRIV_TYPE_BYTE: /* Number of args to fetch */ wrq.u.data.length = Count - 1; if(wrq.u.data.length > (Priv[k].set_args & IW_PRIV_SIZE_MASK)) wrq.u.data.length = Priv[k].set_args & IW_PRIV_SIZE_MASK; /* Fetch args */ for(; i < wrq.u.data.length + 1; i++) sscanf(Args[i], "%d", (int *)(Buffer + i - 1)); break; case IW_PRIV_TYPE_INT: /* Number of args to fetch */ wrq.u.data.length = Count - 1; if(wrq.u.data.length > (Priv[k].set_args & IW_PRIV_SIZE_MASK)) wrq.u.data.length = Priv[k].set_args & IW_PRIV_SIZE_MASK; /* Fetch args */ for(; i < wrq.u.data.length + 1; i++) sscanf(Args[i], "%d", ((u_int *) Buffer) + i - 1); break; case IW_PRIV_TYPE_CHAR: if(i < Count) { /* Size of the string to fetch */ wrq.u.data.length = strlen(Args[i]) + 1; if(wrq.u.data.length > (Priv[k].set_args & IW_PRIV_SIZE_MASK)) wrq.u.data.length = Priv[k].set_args & IW_PRIV_SIZE_MASK; /* Fetch string */ memcpy(Buffer, Args[i], wrq.u.data.length); Buffer[sizeof(Buffer) - 1] = '\0'; i++; } else { wrq.u.data.length = 1; Buffer[0] = '\0'; } break; default: fprintf(stderr, "Not yet implemented...\n"); return(-1); } if((Priv[k].set_args & IW_PRIV_SIZE_FIXED) && (wrq.u.data.length != (Priv[k].set_args & IW_PRIV_SIZE_MASK))) { printf("The command %s need exactly %d argument...\n", Priv[k].name, Priv[k].set_args & IW_PRIV_SIZE_MASK); return(-1); } } /* if args to set */ else { wrq.u.data.length = 0L; } strncpy(wrq.ifr_name, InterfaceName, IFNAMSIZ); if((Priv[k].set_args & IW_PRIV_SIZE_FIXED) && (byte_size(Priv[k].set_args) < IFNAMSIZ)) memcpy(wrq.u.name, Buffer, IFNAMSIZ); else { wrq.u.data.pointer = (caddr_t) Buffer; wrq.u.data.flags = 0; } /* Perform the private ioctl */ if(ioctl(SocketFileDescriptor, Priv[k].cmd, &wrq) < 0) { fprintf(stderr, "Interface doesn't accept private ioctl...\n"); fprintf(stderr, "%X: %s\n", Priv[k].cmd, strerror(errno)); return(-1); } /* If we have to get some data */ if((Priv[k].get_args & IW_PRIV_TYPE_MASK) && (Priv[k].get_args & IW_PRIV_SIZE_MASK)) { int j; int n = 0; /* Number of args */ printf("%-8.8s %s:", InterfaceName, Priv[k].name); if((Priv[k].get_args & IW_PRIV_SIZE_FIXED) && (byte_size(Priv[k].get_args) < IFNAMSIZ)) { memcpy(Buffer, wrq.u.name, IFNAMSIZ); n = Priv[k].get_args & IW_PRIV_SIZE_MASK; } else n = wrq.u.data.length; switch(Priv[k].get_args & IW_PRIV_TYPE_MASK) { case IW_PRIV_TYPE_BYTE: /* Display args */ for(j = 0; j < n; j++) printf("%d ", Buffer[j]); printf("\n"); break; case IW_PRIV_TYPE_INT: /* Display args */ for(j = 0; j < n; j++) printf("%d ", ((u_int *) Buffer)[i]); printf("\n"); break; case IW_PRIV_TYPE_CHAR: /* Display args */ Buffer[wrq.u.data.length - 1] = '\0'; printf("%s\n", Buffer); break; default: fprintf(stderr, "Not yet implemented...\n"); return(-1); } } /* if args to set */ return(0);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -