?? cardif_linux.c
字號:
// Specify the interface name we are asking about. strncpy(iwr.ifr_name, thisint->intName, sizeof(iwr.ifr_name)); if (ioctl(thisint->sockInt, SIOCGIWAP, &iwr) < 0) return XENOWIRELESS; memcpy(bssid_dest, iwr.u.ap_addr.sa_data, 6); return XENONE;}/****************************************** * * Set the flag in the state machine that indicates if this interface is up * or down. If there isn't an interface, we should return an error. * ******************************************/int get_if_state(struct interface_data *thisint){ int retVal; struct ifreq ifr; strncpy(ifr.ifr_name, thisint->intName, sizeof(ifr.ifr_name)); retVal = ioctl(thisint->sockInt, SIOCGIFFLAGS, &ifr); if (retVal < 0) { debug_printf(DEBUG_NORMAL, "Interface %s not found!\n", thisint->intName); return FALSE; } if ((ifr.ifr_flags & IFF_UP) == IFF_UP) { return TRUE; } else { thisint->wasDown = TRUE; return FALSE; } return XENONE;}/****************************************** * * Send a frame out of the network card interface. If there isn't an * interface, we should return an error. We should return a different error * if we have a problem sending the frame. * ******************************************/int sendframe(struct interface_data *thisint, char *sendframe, int sendsize){ char nomac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; int retval; if (thisint == NULL) return XEMALLOC; if (sendframe == NULL) { debug_printf(DEBUG_NORMAL, "Cannot send NULL frame!\n"); return XENOFRAMES; } // The frame we are handed in shouldn't have a src/dest, so put it in. memcpy(&sendframe[0], &thisint->dest_mac[0], 6); memcpy(&sendframe[6], &thisint->source_mac[0], 6); if (thisint->userdata != NULL) { if (memcmp(nomac, (char *)&thisint->userdata->dest_mac[0], 6) != 0) { debug_printf(DEBUG_INT, "Static MAC address defined! Using it!\n"); memcpy(&sendframe[0], &thisint->userdata->dest_mac[0], 6); } } debug_printf(DEBUG_EVERYTHING, "Frame to be sent : \n"); debug_hex_dump(DEBUG_EVERYTHING, sendframe, sendsize); retval = sendto(thisint->sockInt, sendframe, sendsize, 0, (struct sockaddr *)&thisint->sll, sizeof(thisint->sll)); if (retval <= 0) { debug_printf(DEBUG_NORMAL, "Couldn't send frame! (%s)\n", strerror(errno)); } return retval;}/****************************************** * * Get a frame from the network. Since we are in promisc. mode, we will get * frames that aren't intended for us. So, check the frame, determine if it * is something we care about, and act accordingly. * ******************************************/int getframe(struct interface_data *thisint, char *resultframe, int *resultsize){ int newsize=0; char dot1x_default_dest[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03}; errno = 0; *resultsize = 1550; newsize = recvfrom(thisint->sockInt, resultframe, *resultsize, 0, 0, 0); if (newsize <= 0) { debug_printf(DEBUG_EXCESSIVE, "Couldn't get frame. (Maybe there weren't any!)\n"); switch (errno) { case EBADF: debug_printf(DEBUG_EXCESSIVE, "Invalid descriptor!\n"); break; case ECONNREFUSED: debug_printf(DEBUG_EXCESSIVE, "Connection refused!\n"); break; case ENOTCONN: debug_printf(DEBUG_EXCESSIVE, "Not connected!\n"); break; case ENOTSOCK: debug_printf(DEBUG_EXCESSIVE, "Not a socket!\n"); break; case EAGAIN: debug_printf(DEBUG_EXCESSIVE, "Socket would block!\n"); break; case EINTR: debug_printf(DEBUG_EXCESSIVE, "Recieve Interrupted!\n"); break; case EFAULT: debug_printf(DEBUG_EXCESSIVE, "Invalid recieve buffer!\n"); break; case EINVAL: debug_printf(DEBUG_EXCESSIVE, "Invalid argument!\n"); break; case ENOMEM: debug_printf(DEBUG_EXCESSIVE, "Couldn't allocate memory!\n"); break; default: debug_printf(DEBUG_EVERYTHING, "Unknown error (%d)\n",newsize); break; } return XENOFRAMES; } else { debug_printf(DEBUG_EVERYTHING, "Got Frame : \n"); debug_hex_dump(DEBUG_EVERYTHING, resultframe, newsize); } // Make sure that the frame we got is for us.. if ((memcmp(&thisint->source_mac[0], &resultframe[0], 6) == 0) || ((memcmp(&resultframe[0], &dot1x_default_dest[0], 6) == 0) && (memcmp(&resultframe[6], &thisint->source_mac[0], 6) != 0))) { *resultsize = newsize; return newsize; } // Otherwise it isn't for us. debug_printf(DEBUG_INT, "Got a frame, not for us.\n"); return XENOFRAMES;}/****************************************** * * Return true if there is a frame in the queue to be processed. * ******************************************/int frameavail(struct interface_data *thisint){ int newsize=0; char resultframe[1520]; newsize = recvfrom(thisint->sockInt, &resultframe, 1520, MSG_PEEK, 0, 0); if (newsize > 0) return TRUE; return FALSE;}/****************************************** * * Validate an interface, based on if it has a MAC address. * ******************************************/int cardif_validate(char *interface){ int sd, res; struct ifreq ifr; strncpy(ifr.ifr_name, interface, sizeof(interface)+1); sd = socket(PF_PACKET, SOCK_RAW, 0); if (sd < 0) return FALSE; res = ioctl(sd, SIOCGIFHWADDR, &ifr); close(sd); if (res < 0) { debug_printf(DEBUG_NORMAL, "Couldn't get information for interface %s!\n",interface); } else { switch (ifr.ifr_hwaddr.sa_family) { case ARPHRD_ETHER: case ARPHRD_IEEE80211: return TRUE; } } return FALSE;}/****************************************** * * Get the name of an interface, based on an index value. * ******************************************/#define PROC_DEV_FILE "/proc/net/dev"int cardif_get_int(int index, char *retInterface){ FILE *fp; int hits; char line[1000], *lineptr; hits = 0; fp = fopen(PROC_DEV_FILE, "r"); if (fp == NULL) { debug_printf(DEBUG_NORMAL, "Couldn't access /proc/net/dev!\n"); exit(250); } bzero(line, 1000); while ((hits <= index) && (fgets(line, 999, fp) != NULL)) { lineptr = strchr(line, ':'); if (lineptr == NULL) continue; *lineptr = '\0'; lineptr = &line[0]; while (*lineptr == ' ') lineptr++; // Strip out blanks. strcpy(retInterface, lineptr); hits++; } if (hits <= index) { debug_printf(DEBUG_INT, "No more interfaces to look at!\n"); return XNOMOREINTS; } debug_printf(DEBUG_INT, "Found interface : %s\n",retInterface); fclose(fp); return XENONE; // No errors.}/******************************************************* * * Check to see if an interface is wireless. On linux, we look in * /proc/net/wireless to see if the interface is registered with the * wireless extensions. * *******************************************************/#define PROC_WIRELESS_FILE "/proc/net/wireless"int cardif_int_is_wireless(char *interface){ FILE *fp; char line[1000], *lineptr=NULL; int done; done = FALSE; fp = fopen(PROC_WIRELESS_FILE, "r"); if (fp == NULL) { debug_printf(DEBUG_NORMAL, "Couldn't access /proc/net/wireless! (You probably don't have wireless extensions enabled!)\n"); return -1; } bzero(line, 1000); while ((!done) && (fgets(line, 999, fp) != NULL)) { lineptr = strchr(line, ':'); if (lineptr != NULL) { *lineptr = '\0'; lineptr = &line[0]; while (*lineptr == ' ') lineptr++; // Strip out blanks. if (lineptr != NULL) { if (strcmp(lineptr, interface) == 0) done=TRUE; } } } fclose(fp); if ((lineptr != NULL) && (strcmp(lineptr, interface) == 0)) { debug_printf(DEBUG_INT, "Interface %s is wireless!\n",interface); return TRUE; } else { debug_printf(DEBUG_INT, "Interface %s is NOT wireless!\n",interface); return FALSE; } return XENONE; // No errors.}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -