📄 cardif_linux.c
字号:
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; if (!xsup_assert((interface != NULL), "interface != NULL", FALSE)) return XEMALLOC; 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.}int cardif_get_wpa_ie(struct interface_data *intdata, char *iedata, int *ielen){ if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE)) return XEMALLOC; if (!xsup_assert((iedata != NULL), "iedata != NULL", FALSE)) return XEMALLOC; if (!xsup_assert((ielen != NULL), "ielen != NULL", FALSE)) return XEMALLOC; return wireless->get_wpa_ie(intdata, iedata, ielen);}int cardif_get_wpa2_ie(struct interface_data *intdata, char *iedata, int *ielen){ if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE)) return XEMALLOC; if (!xsup_assert((iedata != NULL), "iedata != NULL", FALSE)) return XEMALLOC; if (!xsup_assert((ielen != NULL), "ielen != NULL", FALSE)) return XEMALLOC; if (!wireless) { debug_printf(DEBUG_NORMAL, "Invalid wireless function pointers.\n"); return XEMALLOC; } if (iedata == NULL) { debug_printf(DEBUG_NORMAL, "Invalid bucket for IE data! (%s:%d)\n", __FUNCTION__, __LINE__); return XEMALLOC; } if (!wireless->get_wpa2_ie) { debug_printf(DEBUG_NORMAL, "No valid function to get WPA2 IE!\n"); return XEMALLOC; } return wireless->get_wpa2_ie(intdata, iedata, ielen);}/************************************************************** * * This function should clear out all keys that have been applied to the card. * It should be indepentant of the type (WEP/TKIP/CCMP) of key that was * applied. * **************************************************************/int cardif_clear_keys(struct interface_data *intdata){ int retVal = 0, i; if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE)) return XEMALLOC; // Clear the TX key. retVal = cardif_delete_key(intdata, 0, 1); if (retVal != XENONE) { debug_printf(DEBUG_NORMAL, "Error clearing default TX key!\n"); return retVal; } // Now, clear out the broadcast/multicast/group key(s). for (i=0;i<4;i++) { retVal = cardif_delete_key(intdata, i, 0); if (retVal != XENONE) { debug_printf(DEBUG_NORMAL, "Error clearing key %d!\n", i); return retVal; } } return XENONE;}/******************************************************************* * * Attempt to reassociate to the network we were previously connected to. * *******************************************************************/void cardif_reassociate(struct interface_data *intiface, uint8_t reason){ if (!xsup_assert((intiface != NULL), "intiface != NULL", FALSE)) return; if (!config_ssid_using_wep()) { debug_printf(DEBUG_NORMAL, "SSID '%s' is WPA/WPA2 capable. WPA/WPA2 is " "enabled on this connection.\n", intiface->cur_essid); // Since we are doing WPA/WPA2, we need to disassociate from // the network, and reassociate with WPA/WPA2 set up. cardif_enable_wpa(intiface); cardif_enable_wpa_state(intiface); cardif_clear_keys(intiface); } cardif_associate(intiface); }/***************************************************************** * * Disable encryption on the card. (Set the interface to open mode.) * *****************************************************************/int cardif_enc_disable(struct interface_data *intdata){ if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE)) return XEMALLOC; return wireless->enc_disable(intdata);}/****************************************************************** * * Determine what abilities this card has. (WPA, WPA2, TKIP, CCMP, WEP40, * etc.) * ******************************************************************/void cardif_get_abilities(struct interface_data *intdata){ if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE)) return; if (!wireless->enc_capabilities) { intdata->enc_capa = 0; return; } wireless->enc_capabilities(intdata);}/****************************************************************** * * Change the BSSID that we are currently connected to. * ******************************************************************/void cardif_setBSSID(struct interface_data *ctx, uint8_t *new_bssid){ if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return; if (!xsup_assert((new_bssid != NULL), "new_bssid != NULL", FALSE)) return; if (!wireless->setbssid) { return; } wireless->setbssid(ctx, new_bssid);}/************************************************************************* * * Change the operstate of the interface. * *************************************************************************/void cardif_operstate(struct interface_data *ctx, uint8_t newstate){ if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return; if (!wireless->set_operstate) { debug_printf(DEBUG_INT, "No function defined to set operstate. (This " "is probably nothing to worry about.\n"); return; } wireless->set_operstate(ctx, newstate);}/****************************************************************** * * Wait for an interface to be "attached" to the system before starting to * attempt authentication with it. This is a blocking call that should * *ONLY* return when the interface is available. (Note : 'available' * does not mean that the interface is UP. The 802.1X state machine will * deal with the interface if it is down. We just need to wait for an * interface to exist so that we can use it. * ******************************************************************/void cardif_wait_for_int(char *intname){ int idx = -1; if (!xsup_assert((intname != NULL), "intname != NULL", FALSE)) return; idx = if_nametoindex(intname); if (idx < 1) { debug_printf(DEBUG_NORMAL, "Waiting for interface to be inserted, or " "driver to be loaded.\n"); while (if_nametoindex(intname) < 1) { sleep(1); } }}/******************************************************************** * * The passive scan timer expried. So, we need to issue a scan request, * and reset our timer to recheck the scan results periodically. * ********************************************************************/void cardif_passive_scan_timeout(struct interface_data *ctx){ struct config_globals *globals; uint8_t *mac; char *ssid; if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE)) return;#warning FINISH! We get scan data results, but we still need to do something with them. if (!TEST_FLAG(ctx->flags, PASV_SCANNING)) { if (!TEST_FLAG(ctx->flags, SCANNING)) { timer_reset_timer_count(PASSIVE_SCAN_TIMER, 5); cardif_do_wireless_scan(ctx, 1); SET_FLAG(ctx->flags, PASV_SCANNING); } else { debug_printf(DEBUG_NORMAL, "Got a request to start a new passive scan " "when a previous one has not completed!\n"); } } else { // If the scanning flag is no longer set, then we need to make a decision // about how to associate. debug_printf(DEBUG_NORMAL, "Looking for the best network to connect to.\n"); // Clear the passive scanning flag. UNSET_FLAG(ctx->flags, PASV_SCANNING); // Reset the timer so that we scan again. globals = config_get_globals(); if (!globals) { debug_printf(DEBUG_NORMAL, "No global data! Passive scanning will" " be broken until the next time an authentication " "completes.\n"); } else { debug_printf(DEBUG_EVERYTHING, "Resetting passive scan timer.\n"); timer_reset_timer_count(PASSIVE_SCAN_TIMER, globals->passive_timeout); } ssid = config_ssid_get_desired_ssid(ctx); if (strcmp(ssid, ctx->cur_essid) != 0) { debug_printf(DEBUG_NORMAL, "The best AP to connect to appears to be" " in a different ESSID! It is likely that your card" " doesn't support the needed passive scanning flags." "\n"); // Don't do anything with the result. } else { // We got a valid result. So, see if it is a different AP. If it // is, then jump to it. mac = config_ssid_get_mac(); if (memcmp(ctx->dest_mac, mac, 6) != 0) { debug_printf(DEBUG_INT, "Jumpping to a BSSID with a better " "signal. (BSSID : "); debug_hex_printf(DEBUG_INT, mac, 6); debug_printf_nl(DEBUG_INT, ")\n"); // Then change our BSSIDs. cardif_setBSSID(ctx, mac); } else { debug_printf(DEBUG_EVERYTHING, "We are connected to the best " "BSSID already.\n"); } } }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -