Created
September 20, 2016 08:06
-
-
Save ityuhui/94b04407c55f909a5165608d3955b463 to your computer and use it in GitHub Desktop.
Windows 根据MAC地址获得MTU
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* For Windows Only | |
* Find the network interface whose MAC address is "macstr", return the MTU size of this interface. | |
* | |
* Input parameters: | |
* ----macstr: IP address | |
* ----macsize: the buffer size of macstr | |
* | |
* Output parameters: | |
* None | |
* | |
* Return value: | |
* MTU size | |
*/ | |
static int | |
getInterfaceMTUByMAC(const char *macstr, int macsize) | |
{ | |
static char fname[] = "getInterfaceMTUByMAC()"; | |
int ret = -1; | |
// Declare and initialize variables. | |
DWORD dwSize = 0; | |
DWORD dwRetVal = 0; | |
unsigned int i; | |
/* variables used for GetIfTable and GetIfEntry */ | |
MIB_IFTABLE *pIfTable; | |
MIB_IFROW *pIfRow; | |
// Allocate memory for our pointers. | |
pIfTable = (MIB_IFTABLE *)HeapAlloc(GetProcessHeap(), 0, (sizeof(MIB_IFTABLE))); | |
if (pIfTable == NULL) { | |
ls_syslog(LOG_ERR, "%s: Error allocating memory needed to call GetIfTable.", fname); | |
return -1; | |
} | |
// Make an initial call to GetIfTable to get the | |
// necessary size into dwSize | |
dwSize = sizeof(MIB_IFTABLE); | |
if (GetIfTable(pIfTable, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) { | |
HeapFree(GetProcessHeap(), 0, (pIfTable)); | |
pIfTable = (MIB_IFTABLE *)HeapAlloc(GetProcessHeap(), 0, (dwSize)); | |
if (pIfTable == NULL) { | |
ls_syslog(LOG_ERR, "%s: Error allocating memory needed to call GetIfTable.", fname); | |
return -1; | |
} | |
} | |
// Make a second call to GetIfTable to get the actual | |
// data we want. | |
if ((dwRetVal = GetIfTable(pIfTable, &dwSize, FALSE)) == NO_ERROR) { | |
for (i = 0; i < pIfTable->dwNumEntries; i++) { | |
pIfRow = (MIB_IFROW *)& pIfTable->table[i]; | |
if (MAC_ADDRESS_LENGTH == pIfRow->dwPhysAddrLen) { | |
char thisAdptMAC[MAC_MAX_SZIE]; | |
memset(thisAdptMAC, 0 , sizeof(thisAdptMAC)); | |
sprintf(thisAdptMAC, "%02x:%02x:%02x:%02x:%02x:%02x", | |
pIfRow->bPhysAddr[0], | |
pIfRow->bPhysAddr[1], | |
pIfRow->bPhysAddr[2], | |
pIfRow->bPhysAddr[3], | |
pIfRow->bPhysAddr[4], | |
pIfRow->bPhysAddr[5] | |
); | |
if ( 0 == strncmp(thisAdptMAC, macstr, MAC_MAX_SZIE)) { | |
ret = pIfRow->dwMtu; | |
} | |
} | |
} | |
} else { | |
ls_syslog(LOG_ERR, "%s: GetIfTable failed with error: %d", fname, dwRetVal); | |
if (pIfTable != NULL) { | |
HeapFree(GetProcessHeap(), 0, (pIfTable)); | |
pIfTable = NULL; | |
} | |
ret = -1; | |
// Here you can use FormatMessage to find out why | |
// it failed. | |
} | |
if (pIfTable != NULL) { | |
HeapFree(GetProcessHeap(), 0, (pIfTable)); | |
pIfTable = NULL; | |
} | |
return ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment