//---------------------------------------------------------------------------- // If returned status is NO_ERROR, then pIpAddrTable points to a Ip Address // table. //---------------------------------------------------------------------------- DWORD MyGetIpAddrTable(PMIB_IPADDRTABLE& pIpAddrTable, BOOL fOrder) { DWORD status = NO_ERROR; DWORD statusRetry = NO_ERROR; DWORD dwActualSize = 0; // query for buffer size needed status = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder); if (status == NO_ERROR) { printf("No error\n"); return status; } else if (status == ERROR_INSUFFICIENT_BUFFER) { // need more space pIpAddrTable = (PMIB_IPADDRTABLE) malloc(dwActualSize); assert(pIpAddrTable); statusRetry = GetIpAddrTable(pIpAddrTable, &dwActualSize, fOrder); return statusRetry; } else { return status; } }
IN_ADDR* Get_Table() { PMIB_IPADDRTABLE pIPAddrTable; DWORD dwSize = 0; pIPAddrTable = (MIB_IPADDRTABLE *)MALLOC(sizeof (MIB_IPADDRTABLE)); if (pIPAddrTable) if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { FREE(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *)MALLOC(dwSize); } GetIpAddrTable(pIPAddrTable, &dwSize, 0); IN_ADDR* IPAddr = new IN_ADDR[pIPAddrTable->dwNumEntries]; printf("List of the BroadCast Address: \n"); for (int i = 0; i < (int)pIPAddrTable->dwNumEntries; i++) { IPAddr[i].S_un.S_addr = (u_long)pIPAddrTable->table[i].dwAddr | ~(u_long)pIPAddrTable->table[i].dwMask; printf("\tBroadCast[%d]: \t%s\n", i, inet_ntoa(IPAddr[i])); } FREE(pIPAddrTable); return IPAddr; }
ULONG get_if_index_ip(ULONG if_index) { ULONG size, i; MIB_IPADDRTABLE *buf; ULONG result; size = 0; if (GetIpAddrTable(NULL, &size, FALSE) != ERROR_INSUFFICIENT_BUFFER) return (ULONG)-1; buf = (MIB_IPADDRTABLE *)malloc(size); if (buf == NULL) return (ULONG)-1; if (GetIpAddrTable(buf, &size, FALSE) != NO_ERROR) { free(buf); return (ULONG)-1; } result = 0; for (i = 0; i < buf->dwNumEntries; i++) if (buf->table[i].dwIndex == if_index) { result = buf->table[i].dwAddr; break; } free(buf); return result; }
static os_result addressToIndexAndMask(struct sockaddr *addr, unsigned int *ifIndex, struct sockaddr *mask ) { os_result result = os_resultSuccess; os_boolean found = OS_FALSE; PMIB_IPADDRTABLE pIPAddrTable = NULL; DWORD dwSize = 0; DWORD i; char* errorMessage; int errNo; if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { pIPAddrTable = (MIB_IPADDRTABLE *) os_malloc(dwSize); if (pIPAddrTable != NULL) { if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) != NO_ERROR) { errNo = os_sockError(); errorMessage = os_reportErrnoToString(errNo); os_report(OS_ERROR, "addressToIndexAndMask", __FILE__, __LINE__, 0, "GetIpAddrTable failed: %d %s", errNo, errorMessage); os_free(errorMessage); result = os_resultFail; } } else { os_report(OS_ERROR, "addressToIndexAndMask", __FILE__, __LINE__, 0, "Failed to allocate %d bytes for IP address table", dwSize); result = os_resultFail; } } else { errNo = os_sockError(); errorMessage = os_reportErrnoToString(errNo); os_report(OS_ERROR, "addressToIndexAndMask", __FILE__, __LINE__, 0, "GetIpAddrTable failed: %d %s", errNo, errorMessage); os_free(errorMessage); result = os_resultFail; } if (result == os_resultSuccess) { for (i = 0; !found && i < pIPAddrTable->dwNumEntries; i++ ) { if (((struct sockaddr_in* ) addr )->sin_addr.s_addr == pIPAddrTable->table[i].dwAddr) { *ifIndex = pIPAddrTable->table[i].dwIndex; ((struct sockaddr_in*) mask)->sin_addr.s_addr= pIPAddrTable->table[i].dwMask; found = OS_TRUE; } } } if (pIPAddrTable) { os_free(pIPAddrTable); } if (!found) { result = os_resultFail; } return result; }
/*! * @if jp * @brief 宛先アドレスから利用されるエンドポイントアドレスを得る * @else * @brief Getting network interface name from destination address * @endif */ bool dest_to_endpoint(std::string dest_addr, std::string& endpoint) { Winsock winsock; { struct hostent* hp; hp = ::gethostbyname(dest_addr.c_str()); if (hp == 0) { return false; } int i(0); while (hp->h_addr_list[i] != 0) { if(hp->h_addrtype == AF_INET) { struct sockaddr_in addr; memset((char*)&addr, 0, sizeof(addr)); memcpy((char*)&addr.sin_addr, hp->h_addr_list[i], hp->h_length); dest_addr = inet_ntoa(addr.sin_addr); break; } ++i; } } UINT ipaddress(inet_addr(dest_addr.c_str())); if (ipaddress == INADDR_NONE) { return false; } DWORD bestifindex; if (NO_ERROR != GetBestInterface(ipaddress, &bestifindex)) { return false; } PMIB_IPADDRTABLE ipaddr_table; ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE)); if (ipaddr_table == 0) { return false; } // Make an initial call to GetIpAddrTable to get the // necessary size into the size variable DWORD size(0); if (GetIpAddrTable(ipaddr_table, &size, 0) == ERROR_INSUFFICIENT_BUFFER) { FREE(ipaddr_table); ipaddr_table = (MIB_IPADDRTABLE *) MALLOC(size); } if (ipaddr_table == 0) { return false; } if (GetIpAddrTable(ipaddr_table, &size, 0) != NO_ERROR) { return false; } for (int i(0); i < (int) ipaddr_table->dwNumEntries; ++i) { if (bestifindex == ipaddr_table->table[i].dwIndex) { IN_ADDR inipaddr; inipaddr.S_un.S_addr = (u_long) ipaddr_table->table[i].dwAddr; endpoint = inet_ntoa(inipaddr); return true; } } return false; }
void MonitorIPs::CheckIPAddress() { ULONG ulSize = 0; Log(LOG_DEBUG,__LINE__,">> MonIPs.ChkIPAddrs"); //Get number of bytes required if(GetIpAddrTable(NULL,&ulSize,0)==ERROR_INSUFFICIENT_BUFFER) { //Aloocate required memory PMIB_IPADDRTABLE piat = reinterpret_cast<PMIB_IPADDRTABLE>(LocalAlloc(LMEM_FIXED,ulSize)); if(piat) { //Retrive the list of IPs if(GetIpAddrTable(piat,&ulSize,0)==ERROR_SUCCESS) { WaitForSingleObject(m_hSync,MINUTE); m_ips.clear(); for(DWORD dwIndex=0;dwIndex<piat->dwNumEntries;dwIndex++) { //Trace all IPs string strip; char ip[_MAX_PATH] = {0}; PMIB_IPADDRROW prow = &piat->table[dwIndex]; _snprintf(ip,sizeof(ip)-1,"Addr %u, Idx %u, Mask %u, BCastAddr %u, ReasmSz %u, Tp %X.", prow->dwAddr,prow->dwIndex,prow->dwMask,prow->dwBCastAddr,prow->dwReasmSize,prow->wType); strip.assign(ip); if(prow->wType&MIB_IPADDR_PRIMARY) strip.append("Primary."); if(prow->wType&MIB_IPADDR_DYNAMIC) strip.append("Dynamic."); if(prow->wType&MIB_IPADDR_DISCONNECTED) strip.append("Disconnected."); if(prow->wType&MIB_IPADDR_DELETED) strip.append("Deleted."); if(prow->wType&MIB_IPADDR_TRANSIENT) strip.append("Transient."); if(prow->wType&MIB_IPADDR_DNS_ELIGIBLE) strip.append("Published in DNS."); m_ips.push_back(strip); } ReleaseMutex(m_hSync); } LocalFree(piat); } } HANDLE h; NotifyAddrChange(&h, &m_o); Log(LOG_DEBUG,__LINE__,"<< MonIPs.ChkIPAddrs"); }
static void mib2IpAddrInit(void) { DWORD size = 0, ret = GetIpAddrTable(NULL, &size, TRUE); if (ret == ERROR_INSUFFICIENT_BUFFER) { ipAddrTable = HeapAlloc(GetProcessHeap(), 0, size); if (ipAddrTable) GetIpAddrTable(ipAddrTable, &size, TRUE); } }
bool find_subnet_adapter(unsigned int if_subnet, unsigned int &addr) { // find the adapter with the appropriate subnet PMIB_IPADDRTABLE addr_table; DWORD dwSize = 0; addr_table = (PMIB_IPADDRTABLE)malloc(sizeof(MIB_IPADDRTABLE)); // make an initial call to get the appropriate address table size if (GetIpAddrTable(addr_table, &dwSize, FALSE) == ERROR_INSUFFICIENT_BUFFER) { // free the original buffer free(addr_table); // make space, reallocate addr_table = (PMIB_IPADDRTABLE)malloc(dwSize); } // assert that we could allocate space if (addr_table == NULL) { printf("could not allocate space for addr table\n"); return false; } // get the real data unsigned int if_addr = 0; if (GetIpAddrTable(addr_table, &dwSize, FALSE) == NO_ERROR) { // iterate through the table and find a matching entry for (DWORD i = 0; i < addr_table->dwNumEntries; i++) { unsigned int subnet = addr_table->table[i].dwAddr & addr_table->table[i].dwMask; if (subnet == if_subnet) { if_addr = addr_table->table[i].dwAddr; break; } } // free the allocated memory free(addr_table); } else { printf("error getting ip address table: %d\n", WSAGetLastError()); // free the allocated memory free(addr_table); return false; } // check if we found a match if (if_addr != 0) { addr = if_addr; return true; } else { return false; } }
static void mib2IpAddrInit(void) { DWORD size = 0, ret = GetIpAddrTable(NULL, &size, TRUE); if (ret == ERROR_INSUFFICIENT_BUFFER) { MIB_IPADDRTABLE *table = HeapAlloc(GetProcessHeap(), 0, size); if (table) { if (!GetIpAddrTable(table, &size, TRUE)) ipAddrTable = table; else HeapFree(GetProcessHeap(), 0, table ); } } }
BOOL Test_WSAIoctl_InitTest( OUT PMIB_IPADDRTABLE* ppTable) { PMIB_IPADDRROW pRow; DWORD ret, i1; ULONG TableSize; PMIB_IPADDRTABLE pTable; TableSize = 0; *ppTable = NULL; ret = GetIpAddrTable(NULL, &TableSize, FALSE); if (ret != ERROR_INSUFFICIENT_BUFFER) { skip("GetIpAddrTable failed with %ld. Abort Testing.\n", ret); return FALSE; } /* get sorted ip-address table. Sort order is the ip-address. */ pTable = (PMIB_IPADDRTABLE)malloc(TableSize); *ppTable = pTable; ret = GetIpAddrTable(pTable, &TableSize, TRUE); if (ret != NO_ERROR) { skip("GetIpAddrTable failed with %ld. Abort Testing.\n", ret); return FALSE; } if (winetest_debug >= 2) { trace("Result of GetIpAddrTable:\n"); trace("Count: %ld\n", pTable->dwNumEntries); pRow = pTable->table; for (i1 = 0; i1 < pTable->dwNumEntries; i1++) { trace("** Entry %ld **\n", i1); trace(" dwAddr %lx\n", pRow->dwAddr); trace(" dwIndex %lx\n", pRow->dwIndex); trace(" dwMask %lx\n", pRow->dwMask); trace(" dwBCastAddr %lx\n", pRow->dwBCastAddr); trace(" dwReasmSize %lx\n", pRow->dwReasmSize); trace(" wType %x\n", pRow->wType); pRow++; } trace("END\n"); } return TRUE; }
char * pcap_ex_lookupdev(char *ebuf) { #ifdef _WIN32 /* XXX - holy poo this sux */ static char _ifname[8]; MIB_IPADDRTABLE *ipaddrs; DWORD i, dsz, outip; pcap_if_t *pifs, *pif; struct pcap_addr *pa; char *name = NULL; int idx; /* Find our primary IP address. */ ipaddrs = malloc((dsz = sizeof(*ipaddrs))); while (GetIpAddrTable(ipaddrs, &dsz, 0) == ERROR_INSUFFICIENT_BUFFER) { free(ipaddrs); ipaddrs = malloc(dsz); } outip = 0; for (i = 0; i < ipaddrs->dwNumEntries; i++) { if (ipaddrs->table[i].dwAddr != 0 && ipaddrs->table[i].dwAddr != 0x100007f #if 0 /* XXX -no wType/MIB_IPADDR_PRIMARY in w32api/iprtrmib.h */ && ipaddrs->table[i].unused2 & 0x01 #endif ) { outip = ipaddrs->table[i].dwAddr; break; } } free(ipaddrs); if (outip == 0) { /* XXX - default to first Ethernet interface. */ return ("eth0"); } /* Find matching pcap interface by IP. */ if (_pcap_ex_findalldevs(&pifs, ebuf) == -1) return (name); for (pif = pifs, idx = 0; pif != NULL && name == NULL; pif = pif->next, idx++) { for (pa = pif->addresses; pa != NULL; pa = pa->next) { if (pa->addr->sa_family == AF_INET && ((struct sockaddr_in *)pa->addr)->sin_addr.S_un.S_addr == outip) { sprintf(_ifname, "eth%d", idx); name = _ifname; break; } } } pcap_freealldevs(pifs); return (name); #else return (pcap_lookupdev(ebuf)); #endif }
///////////////////////////////////////////////////////////////////////////////// // Initializes m_localIP variable, for future access to GetLocalIP() ///////////////////////////////////////////////////////////////////////////////// void MyUPnP::InitLocalIP() { #ifndef _DEBUG try #endif { DWORD best_if_index; GetBestInterface(inet_addr("223.255.255.255"), &best_if_index); PMIB_IPADDRTABLE ip_addr_table; char buffer[1024]; ip_addr_table = (PMIB_IPADDRTABLE)buffer; DWORD size = sizeof(buffer); GetIpAddrTable(ip_addr_table, &size, 0); DWORD local_ip = 0; for (DWORD i=0; i<ip_addr_table->dwNumEntries; i++) { if (ip_addr_table->table[i].dwIndex == best_if_index) { local_ip = ip_addr_table->table[i].dwAddr; break; } } if (local_ip) { struct in_addr addr; addr.S_un.S_addr = local_ip; m_slocalIP = inet_ntoa(addr); m_uLocalIP = local_ip; } else { char szHost[256]; if (gethostname(szHost, sizeof szHost) == 0){ hostent* pHostEnt = gethostbyname(szHost); if (pHostEnt != NULL && pHostEnt->h_length == 4 && pHostEnt->h_addr_list[0] != NULL){ UPNPNAT_MAPPING mapping; struct in_addr addr; memcpy(&addr, pHostEnt->h_addr_list[0], sizeof(struct in_addr)); m_slocalIP = inet_ntoa(addr); m_uLocalIP = addr.S_un.S_addr; } else{ m_slocalIP = _T(""); m_uLocalIP = 0; } } else{ m_slocalIP = _T(""); m_uLocalIP = 0; } } } #ifndef _DEBUG catch(...){ m_slocalIP = _T(""); m_uLocalIP = 0; } #endif }
void GetBroadcastIPList(vector<LPCTSTR> &broadcastIPList) { PMIB_IPADDRTABLE pIPTable = nullptr; DWORD dwSize; GetIpAddrTable(pIPTable, &dwSize, true); pIPTable = new MIB_IPADDRTABLE[dwSize]; GetIpAddrTable(pIPTable, &dwSize, true); broadcastIPList.clear(); broadcastIPList.push_back(L"255.255.255.255"); for (DWORD i = 0; i < pIPTable->dwNumEntries; i++) { if (pIPTable->table[i].dwAddr == 16777343) { continue; } int addr[] = { LOWORD(pIPTable->table[i].dwAddr & pIPTable->table[i].dwMask) & 0x00FF, LOWORD(pIPTable->table[i].dwAddr & pIPTable->table[i].dwMask) >> 8, HIWORD(pIPTable->table[i].dwAddr & pIPTable->table[i].dwMask) & 0x00FF, HIWORD(pIPTable->table[i].dwAddr & pIPTable->table[i].dwMask) >> 8 }; for (int j = 3; j >= 0; j--) { if (addr[j] == 0) { addr[j] = 255; } else { break; } } LPTSTR szIPAddr = new TCHAR[255]; wsprintf(szIPAddr, L"%d.%d.%d.%d", addr[0], addr[1], addr[2], addr[3]); broadcastIPList.push_back(szIPAddr); } }
int ip_route_get(const char* destination, char ip[40]) { DWORD index = ~(-1); struct sockaddr_in addrin; MIB_IPADDRTABLE *table = NULL; ULONG dwSize = 0; DWORD errcode = 0; DWORD i = 0; addrin.sin_family = AF_INET; addrin.sin_port = htons(0); inet_pton(AF_INET, destination, &addrin.sin_addr); if(NO_ERROR != GetBestInterfaceEx((struct sockaddr*)&addrin, &index)) return -1; errcode = GetIpAddrTable( table, &dwSize, 0 ); assert(ERROR_INSUFFICIENT_BUFFER == errcode); table = (MIB_IPADDRTABLE*)malloc(dwSize); errcode = GetIpAddrTable( table, &dwSize, 0 ); if(!table || NO_ERROR != errcode) { free(table); return -1; } ip[0] = '\0'; for(i = 0; i < table->dwNumEntries; i++) { if(table->table[i].dwIndex == index) { sprintf(ip, "%d.%d.%d.%d", (table->table[i].dwAddr >> 0) & 0xFF, (table->table[i].dwAddr >> 8) & 0xFF, (table->table[i].dwAddr >> 16) & 0xFF, (table->table[i].dwAddr >> 24) & 0xFF); break; } }
void GetNetworkInterfaces(CStringArray & interfaces, bool includeNames) { // Get interfaces BYTE * buffer = NULL; ULONG size = 0; DWORD error = GetIpAddrTable(NULL, &size, FALSE); if (error == ERROR_INSUFFICIENT_BUFFER) { buffer = new BYTE[size]; if (buffer != NULL) error = GetIpAddrTable((MIB_IPADDRTABLE *)buffer, &size, FALSE); } if (error == ERROR_SUCCESS) { const MIB_IPADDRTABLE * ipaddr = (const MIB_IPADDRTABLE *)buffer; for (unsigned i = 0; i < ipaddr->dwNumEntries; ++i) { in_addr ip; ip.S_un.S_addr = ipaddr->table[i].dwAddr; if (ntohl(ip.S_un.S_addr) != INADDR_LOOPBACK) { CStringA iface; if (ip.S_un.S_addr != INADDR_ANY) iface = inet_ntoa(ip); if (includeNames) { MIB_IFROW info; info.dwIndex = ipaddr->table[i].dwIndex; if (GetIfEntry(&info) == NO_ERROR && info.dwDescrLen > 0) { iface += '%'; iface.Append((const char *)info.bDescr, info.dwDescrLen); } } if (!iface.IsEmpty()) interfaces.Add(CString(iface)); } } } delete [] buffer; }
DWORD GetNumOfIpAddrs(void) { PMIB_IPADDRTABLE pIpAddrTable = NULL; ULONG dwSize; DWORD code; DWORD index; DWORD validAddrs = 0; dwSize = 0; code = GetIpAddrTable(NULL, &dwSize, 0); if (code == ERROR_INSUFFICIENT_BUFFER) { pIpAddrTable = malloc(dwSize); code = GetIpAddrTable(pIpAddrTable, &dwSize, 0); if ( code == NO_ERROR ) { for ( index=0; index < pIpAddrTable->dwNumEntries; index++ ) { if (pIpAddrTable->table[index].dwAddr != 0) validAddrs++; } } free(pIpAddrTable); } return validAddrs; }
static int get_node_id(unsigned char *byMAC) { DWORD i, dwSize; PMIB_IPADDRTABLE pAddr = NULL; MIB_IFROW iInfo; PFIXED_INFO pFI = NULL; /* Get all IP addresses held by this machine; if it's connected to a network, there's at least one that's not localhost */ dwSize = 0; GetIpAddrTable(NULL, &dwSize, TRUE); pAddr = (PMIB_IPADDRTABLE)malloc(sizeof(BYTE) * dwSize); if (!GetIpAddrTable(pAddr, &dwSize, TRUE)) { for (i = 0; i < pAddr->dwNumEntries; ++i) { if (IP_LOCALHOST != pAddr->table[i].dwAddr) { /* Not localhost, so get the interface */ memset(&iInfo, 0, sizeof(MIB_IFROW)); iInfo.dwIndex = pAddr->table[i].dwIndex; GetIfEntry(&iInfo); if (MIB_IF_TYPE_ETHERNET == iInfo.dwType) { /*iInfo.bPhysAddr contains the MAC address of this interface*/ memcpy(byMAC, iInfo.bPhysAddr, iInfo.dwPhysAddrLen); free(pAddr); return 1; } } } } free(pAddr); return 0; }
static MIB_IPADDRTABLE * ssh_ip_route_get_ipaddrtable(void) { MIB_IPADDRTABLE *data = NULL; DWORD error; ULONG size; size = 0; error = GetIpAddrTable(NULL, &size, FALSE); if (error != ERROR_INSUFFICIENT_BUFFER) { if (error == ERROR_NO_DATA) { SSH_DEBUG(SSH_D_FAIL, ("No local IP addresses")); return NULL; } SSH_DEBUG(SSH_D_FAIL, ("GetIpAddrTable: error 0x%08X", (unsigned)error)); return NULL; } if (!(data = ssh_malloc(size))) { SSH_DEBUG(SSH_D_FAIL, ("out of memory allocating IP address table")); return NULL; } error = GetIpAddrTable(data, &size, FALSE); if (error != NO_ERROR) { SSH_DEBUG(SSH_D_FAIL, ("GetIpAddrTable: error 0x%08X", (unsigned)error)); ssh_free(data); return NULL; } return data; }
int php_add4_to_if_index(struct in_addr *addr, php_socket *php_sock, unsigned *if_index) { MIB_IPADDRTABLE *addr_table; ULONG size; DWORD retval; DWORD i; (void) php_sock; /* not necessary */ if (addr->s_addr == INADDR_ANY) { *if_index = 0; return SUCCESS; } size = 4 * (sizeof *addr_table); addr_table = emalloc(size); retry: retval = GetIpAddrTable(addr_table, &size, 0); if (retval == ERROR_INSUFFICIENT_BUFFER) { efree(addr_table); addr_table = emalloc(size); goto retry; } if (retval != NO_ERROR) { php_error_docref(NULL, E_WARNING, "GetIpAddrTable failed with error %lu", retval); return FAILURE; } for (i = 0; i < addr_table->dwNumEntries; i++) { MIB_IPADDRROW r = addr_table->table[i]; if (r.dwAddr == addr->s_addr) { *if_index = r.dwIndex; return SUCCESS; } } { char addr_str[17] = {0}; inet_ntop(AF_INET, addr, addr_str, sizeof(addr_str)); php_error_docref(NULL, E_WARNING, "The interface with IP address %s was not found", addr_str); } return FAILURE; }
static int _refresh_tables(intf_t *intf) { MIB_IFROW *ifrow; ULONG len; u_int i, ret; /* Get interface table. */ for (len = sizeof(intf->iftable[0]); ; ) { if (intf->iftable) free(intf->iftable); intf->iftable = malloc(len); ret = GetIfTable(intf->iftable, &len, FALSE); if (ret == NO_ERROR) break; else if (ret != ERROR_INSUFFICIENT_BUFFER) return (-1); } /* Get IP address table. */ for (len = sizeof(intf->iptable[0]); ; ) { if (intf->iptable) free(intf->iptable); intf->iptable = malloc(len); ret = GetIpAddrTable(intf->iptable, &len, FALSE); if (ret == NO_ERROR) break; else if (ret != ERROR_INSUFFICIENT_BUFFER) return (-1); } /* * Map "unfriendly" win32 interface indices to ours. * XXX - like IP_ADAPTER_INFO ComboIndex */ for (i = 0; i < intf->iftable->dwNumEntries; i++) { ifrow = &intf->iftable->table[i]; if (ifrow->dwType < MIB_IF_TYPE_MAX) { _ifcombo_add(&intf->ifcombo[ifrow->dwType], ifrow->dwIndex); } else return (-1); } return (0); }
int php_if_index_to_addr4(unsigned if_index, php_socket *php_sock, struct in_addr *out_addr) { MIB_IPADDRTABLE *addr_table; ULONG size; DWORD retval; DWORD i; (void) php_sock; /* not necessary */ if (if_index == 0) { out_addr->s_addr = INADDR_ANY; return SUCCESS; } size = 4 * (sizeof *addr_table); addr_table = emalloc(size); retry: retval = GetIpAddrTable(addr_table, &size, 0); if (retval == ERROR_INSUFFICIENT_BUFFER) { efree(addr_table); addr_table = emalloc(size); goto retry; } if (retval != NO_ERROR) { php_error_docref(NULL, E_WARNING, "GetIpAddrTable failed with error %lu", retval); return FAILURE; } for (i = 0; i < addr_table->dwNumEntries; i++) { MIB_IPADDRROW r = addr_table->table[i]; if (r.dwIndex == if_index) { out_addr->s_addr = r.dwAddr; return SUCCESS; } } php_error_docref(NULL, E_WARNING, "No interface with index %u was found", if_index); return FAILURE; }
int main (int argc, char **argv) { LPSTR environment; LPSTR position; LPSTR commandline; MIB_IPADDRTABLE ipatab; DWORD iptab_size; environment = GetEnvironmentStrings(); position = environment; while(strlen(position) > 0) { printf("%s\n", position); position += strlen(position) + 1; } commandline=GetCommandLine(); printf("commandline: %s\n",commandline); GetIpAddrTable(&ipatab, &iptab_size, false); return 0; }
INT_PTR SettingsAdvanced::ProcMessage(UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_INITDIALOG: { LocalizeWindow(hwnd); //-------------------------------------------- HWND hwndToolTip = CreateWindowEx(NULL, TOOLTIPS_CLASS, NULL, WS_POPUP|TTS_NOPREFIX|TTS_ALWAYSTIP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hwnd, NULL, hinstMain, NULL); TOOLINFO ti; zero(&ti, sizeof(ti)); ti.cbSize = sizeof(ti); ti.uFlags = TTF_SUBCLASS|TTF_IDISHWND; ti.hwnd = hwnd; SendMessage(hwndToolTip, TTM_SETMAXTIPWIDTH, 0, 500); SendMessage(hwndToolTip, TTM_SETDELAYTIME, TTDT_AUTOPOP, 14000); //------------------------------------ UINT sceneBufferingTime = GlobalConfig->GetInt(TEXT("General"), TEXT("SceneBufferingTime"), 700); SendMessage(GetDlgItem(hwnd, IDC_SCENEBUFFERTIME), UDM_SETRANGE32, 60, 20000); SendMessage(GetDlgItem(hwnd, IDC_SCENEBUFFERTIME), UDM_SETPOS32, 0, sceneBufferingTime); //------------------------------------ bool bUseMTOptimizations = AppConfig->GetInt(TEXT("General"), TEXT("UseMultithreadedOptimizations"), TRUE) != 0; SendMessage(GetDlgItem(hwnd, IDC_USEMULTITHREADEDOPTIMIZATIONS), BM_SETCHECK, bUseMTOptimizations ? BST_CHECKED : BST_UNCHECKED, 0); HWND hwndTemp = GetDlgItem(hwnd, IDC_PRIORITY); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)Str("Settings.Advanced.Priority.High")); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)Str("Settings.Advanced.Priority.AboveNormal")); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)Str("Settings.Advanced.Priority.Normal")); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)Str("Settings.Advanced.Priority.Idle")); CTSTR pStr = AppConfig->GetStringPtr(TEXT("General"), TEXT("Priority"), TEXT("Normal")); if (scmpi(pStr, TEXT("Idle")) == 0) SendMessage(hwndTemp, CB_SETCURSEL, 3, 0); else if (scmpi(pStr, TEXT("Above Normal")) == 0) SendMessage(hwndTemp, CB_SETCURSEL, 1, 0); else if (scmpi(pStr, TEXT("High")) == 0) SendMessage(hwndTemp, CB_SETCURSEL, 0, 0); else //Normal SendMessage(hwndTemp, CB_SETCURSEL, 2, 0); //------------------------------------ bool bDisablePreviewEncoding = GlobalConfig->GetInt(TEXT("General"), TEXT("DisablePreviewEncoding"), false) != 0; SendMessage(GetDlgItem(hwnd, IDC_DISABLEPREVIEWENCODING), BM_SETCHECK, bDisablePreviewEncoding ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ bool bAllowOtherHotkeyModifiers = GlobalConfig->GetInt(TEXT("General"), TEXT("AllowOtherHotkeyModifiers"), true) != 0; SendMessage(GetDlgItem(hwnd, IDC_ALLOWOTHERHOTKEYMODIFIERS), BM_SETCHECK, bAllowOtherHotkeyModifiers ? BST_CHECKED : BST_UNCHECKED, 0); //-------------------------------------------- hwndTemp = GetDlgItem(hwnd, IDC_X264PROFILE); static const CTSTR profile_names[3] = {TEXT("main"), TEXT("high")}; for(int i=0; i<2; i++) SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)profile_names[i]); LoadSettingComboString(hwndTemp, TEXT("Video Encoding"), TEXT("X264Profile"), TEXT("high")); //-------------------------------------------- hwndTemp = GetDlgItem(hwnd, IDC_PRESET); static const CTSTR preset_names[8] = {TEXT("ultrafast"), TEXT("superfast"), TEXT("veryfast"), TEXT("faster"), TEXT("fast"), TEXT("medium"), TEXT("slow"), TEXT("slower")}; for(int i=0; i<8; i++) SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)preset_names[i]); LoadSettingComboString(hwndTemp, TEXT("Video Encoding"), TEXT("Preset"), TEXT("veryfast")); ti.lpszText = (LPWSTR)Str("Settings.Advanced.VideoEncoderCPUTradeoffTooltip"); ti.uId = (UINT_PTR)hwndTemp; SendMessage(hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti); //------------------------------------ bool bUseCFR = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseCFR"), 1) != 0; SendMessage(GetDlgItem(hwnd, IDC_USECFR), BM_SETCHECK, bUseCFR ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ bool bUseCustomX264Settings = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseCustomSettings")) != 0; String strX264Settings = AppConfig->GetString(TEXT("Video Encoding"), TEXT("CustomSettings")); SendMessage(GetDlgItem(hwnd, IDC_USEVIDEOENCODERSETTINGS), BM_SETCHECK, bUseCustomX264Settings ? BST_CHECKED : BST_UNCHECKED, 0); SetWindowText(GetDlgItem(hwnd, IDC_VIDEOENCODERSETTINGS), strX264Settings); ti.lpszText = (LPWSTR)Str("Settings.Advanced.VideoEncoderSettingsTooltip"); ti.uId = (UINT_PTR)GetDlgItem(hwnd, IDC_VIDEOENCODERSETTINGS); SendMessage(hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti); ti.uId = (UINT_PTR)GetDlgItem(hwnd, IDC_USEVIDEOENCODERSETTINGS); SendMessage(hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti); EnableWindow(GetDlgItem(hwnd, IDC_VIDEOENCODERSETTINGS), bUseCustomX264Settings); //-------------------------------------------- UINT keyframeInt = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("KeyframeInterval"), 0); SendMessage(GetDlgItem(hwnd, IDC_KEYFRAMEINTERVAL), UDM_SETRANGE32, 0, 20); SendMessage(GetDlgItem(hwnd, IDC_KEYFRAMEINTERVAL), UDM_SETPOS32, 0, keyframeInt); //-------------------------------------------- bool bUnlockFPS = AppConfig->GetInt(TEXT("Video"), TEXT("UnlockFPS")) != 0; SendMessage(GetDlgItem(hwnd, IDC_UNLOCKHIGHFPS), BM_SETCHECK, bUnlockFPS ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ bool bHasQSV = CheckQSVHardwareSupport(false); bool bUseQSV = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseQSV")) != 0; bool bHasNVENC = CheckNVENCHardwareSupport(false); bool bUseNVENC = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseNVENC")) != 0; EnableWindow(GetDlgItem(hwnd, IDC_USEQSV), (bHasQSV || bUseQSV) && !bUseNVENC); SendMessage(GetDlgItem(hwnd, IDC_USEQSV), BM_SETCHECK, bUseQSV ? BST_CHECKED : BST_UNCHECKED, 0); bool bQSVUseVideoEncoderSettings = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("QSVUseVideoEncoderSettings")) != 0; SendMessage(GetDlgItem(hwnd, IDC_QSVUSEVIDEOENCODERSETTINGS), BM_SETCHECK, bQSVUseVideoEncoderSettings ? BST_CHECKED : BST_UNCHECKED, 0); ti.lpszText = (LPWSTR)Str("Settings.Advanced.QSVUseVideoEncoderSettingsTooltip"); ti.uId = (UINT_PTR)GetDlgItem(hwnd, IDC_QSVUSEVIDEOENCODERSETTINGS); SendMessage(hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&ti); EnableWindow(GetDlgItem(hwnd, IDC_QSVUSEVIDEOENCODERSETTINGS), bUseQSV && !bUseNVENC); EnableWindow(GetDlgItem(hwnd, IDC_USENVENC), (bHasNVENC || bUseNVENC) && !bUseQSV); SendMessage(GetDlgItem(hwnd, IDC_USENVENC), BM_SETCHECK, bUseNVENC ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ bool bSyncToVideoTime = AppConfig->GetInt(TEXT("Audio"), TEXT("SyncToVideoTime")) != 0; SendMessage(GetDlgItem(hwnd, IDC_SYNCTOVIDEOTIME), BM_SETCHECK, bSyncToVideoTime ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ bool bUseMicQPC = GlobalConfig->GetInt(TEXT("Audio"), TEXT("UseMicQPC")) != 0; SendMessage(GetDlgItem(hwnd, IDC_USEMICQPC), BM_SETCHECK, bUseMicQPC ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ BOOL bMicSyncFixHack = GlobalConfig->GetInt(TEXT("Audio"), TEXT("UseMicSyncFixHack")); SendMessage(GetDlgItem(hwnd, IDC_MICSYNCFIX), BM_SETCHECK, bMicSyncFixHack ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ int bufferTime = GlobalConfig->GetInt(TEXT("General"), TEXT("SceneBufferingTime"), 700); int globalAudioTimeAdjust = GlobalConfig->GetInt(TEXT("Audio"), TEXT("GlobalAudioTimeAdjust")); SendMessage(GetDlgItem(hwnd, IDC_AUDIOTIMEADJUST), UDM_SETRANGE32, -bufferTime, 5000); SendMessage(GetDlgItem(hwnd, IDC_AUDIOTIMEADJUST), UDM_SETPOS32, 0, globalAudioTimeAdjust); //------------------------------------ int lowLatencyFactor = AppConfig->GetInt(TEXT("Publish"), TEXT("LatencyFactor"), 20); SetDlgItemInt(hwnd, IDC_LATENCYTUNE, lowLatencyFactor, TRUE); int bLowLatencyAutoMethod = AppConfig->GetInt(TEXT("Publish"), TEXT("LowLatencyMethod"), 0); SendMessage(GetDlgItem(hwnd, IDC_LATENCYMETHOD), BM_SETCHECK, bLowLatencyAutoMethod ? BST_CHECKED : BST_UNCHECKED, 0); //------------------------------------ MIB_IPADDRTABLE tempTable; DWORD dwSize = 0; if (GetIpAddrTable (&tempTable, &dwSize, TRUE) == ERROR_INSUFFICIENT_BUFFER) { PMIB_IPADDRTABLE ipTable; ipTable = (PMIB_IPADDRTABLE)Allocate(dwSize); if (GetIpAddrTable (ipTable, &dwSize, TRUE) == NO_ERROR) { DWORD i; hwndTemp = GetDlgItem(hwnd, IDC_BINDIP); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)TEXT("Default")); for (i=0; i < ipTable->dwNumEntries; i++) { String strAddress; DWORD strLength = 32; // don't allow binding to localhost if ((ipTable->table[i].dwAddr & 0xFF) == 127) continue; strAddress.SetLength(strLength); SOCKADDR_IN IP; IP.sin_addr.S_un.S_addr = ipTable->table[i].dwAddr; IP.sin_family = AF_INET; IP.sin_port = 0; zero(&IP.sin_zero, sizeof(IP.sin_zero)); WSAAddressToString ((LPSOCKADDR)&IP, sizeof(IP), NULL, strAddress.Array(), &strLength); SendMessage(hwndTemp, CB_ADDSTRING, 0, (LPARAM)strAddress.Array()); } LoadSettingComboString(hwndTemp, TEXT("Publish"), TEXT("BindToIP"), TEXT("Default")); } Free(ipTable); } //need this as some of the dialog item sets above trigger the notifications ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_HIDE); SetChangedSettings(false); return TRUE; } case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_USEVIDEOENCODERSETTINGS: if(HIWORD(wParam) == BN_CLICKED) { BOOL bUseVideoEncoderSettings = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0) == BST_CHECKED; EnableWindow(GetDlgItem(hwnd, IDC_VIDEOENCODERSETTINGS), bUseVideoEncoderSettings); ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break; case IDC_KEYFRAMEINTERVAL_EDIT: case IDC_SCENEBUFFERTIME_EDIT: case IDC_AUDIOTIMEADJUST_EDIT: case IDC_VIDEOENCODERSETTINGS: case IDC_LATENCYTUNE: if(HIWORD(wParam) == EN_CHANGE) { ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break; /*case IDC_TIMER1: case IDC_TIMER2: case IDC_TIMER3: case IDC_DISABLED3DCOMPATIBILITY: if(HIWORD(wParam) == BN_CLICKED) { ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break;*/ case IDC_USESENDBUFFER: if(HIWORD(wParam) == BN_CLICKED) { BOOL bUseSendBuffer = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0) == BST_CHECKED; EnableWindow(GetDlgItem(hwnd, IDC_SENDBUFFERSIZE), bUseSendBuffer); ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break; case IDC_PRESET: if(HIWORD(wParam) == CBN_SELCHANGE) { HWND hwndTemp = (HWND)lParam; String strNewPreset = GetCBText(hwndTemp); if (scmp(strNewPreset.Array(), TEXT("veryfast"))) { static BOOL bHasWarned = FALSE; if (!bHasWarned && MessageBox(hwnd, Str("Settings.Advanced.PresetWarning"), NULL, MB_ICONEXCLAMATION | MB_YESNO) == IDNO) LoadSettingComboString(hwndTemp, TEXT("Video Encoding"), TEXT("Preset"), TEXT("veryfast")); else bHasWarned = TRUE; } SetChangedSettings(true); ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); } break; case IDC_X264PROFILE: case IDC_SENDBUFFERSIZE: case IDC_PRIORITY: case IDC_BINDIP: if(HIWORD(wParam) == CBN_SELCHANGE || HIWORD(wParam) == CBN_EDITCHANGE) { ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break; case IDC_USEQSV: case IDC_USENVENC: if (HIWORD(wParam) == BN_CLICKED) { bool bHasQSV = CheckQSVHardwareSupport(false); bool bHasNVENC = CheckNVENCHardwareSupport(false); bool bUseQSV = SendMessage(GetDlgItem(hwnd, IDC_USEQSV), BM_GETCHECK, 0, 0) == BST_CHECKED; bool bUseNVENC = SendMessage(GetDlgItem(hwnd, IDC_USENVENC), BM_GETCHECK, 0, 0) == BST_CHECKED; bool bUseQSV_prev = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseQSV")) != 0; bool bUseNVENC_prev = AppConfig->GetInt(TEXT("Video Encoding"), TEXT("UseNVENC")) != 0; EnableWindow(GetDlgItem(hwnd, IDC_QSVUSEVIDEOENCODERSETTINGS), (bHasQSV || bUseQSV) && !bUseNVENC); EnableWindow(GetDlgItem(hwnd, IDC_USEQSV), !bUseNVENC && (bHasQSV || bUseQSV_prev)); EnableWindow(GetDlgItem(hwnd, IDC_USENVENC), !bUseQSV && (bHasNVENC || bUseNVENC_prev)); } case IDC_DISABLEPREVIEWENCODING: case IDC_ALLOWOTHERHOTKEYMODIFIERS: case IDC_MICSYNCFIX: case IDC_USEMICQPC: case IDC_SYNCTOVIDEOTIME: case IDC_USECFR: case IDC_USEMULTITHREADEDOPTIMIZATIONS: case IDC_UNLOCKHIGHFPS: case IDC_LATENCYMETHOD: case IDC_QSVUSEVIDEOENCODERSETTINGS: if(HIWORD(wParam) == BN_CLICKED) { ShowWindow(GetDlgItem(hwnd, IDC_INFO), SW_SHOW); SetChangedSettings(true); } break; } } return FALSE; }
/* ssdpDiscoverDevices() : * return a chained list of all devices found or NULL if * no devices was found. * It is up to the caller to free the chained list * delay is in millisecond (poll). * UDA v1.1 says : * The TTL for the IP packet SHOULD default to 2 and * SHOULD be configurable. */ struct UPNPDev * ssdpDiscoverDevices(const char * const deviceTypes[], int delay, const char * multicastif, int localport, int ipv6, unsigned char ttl, int * error, int searchalltypes) { struct UPNPDev * tmp; struct UPNPDev * devlist = 0; unsigned int scope_id = 0; int opt = 1; static const char MSearchMsgFmt[] = "M-SEARCH * HTTP/1.1\r\n" "HOST: %s:" XSTR(SSDP_PORT) "\r\n" "ST: %s\r\n" "MAN: \"ssdp:discover\"\r\n" "MX: %u\r\n" "\r\n"; int deviceIndex; char bufr[1536]; /* reception and emission buffer */ int sudp; int n; struct sockaddr_storage sockudp_r; unsigned int mx; #ifdef NO_GETADDRINFO struct sockaddr_storage sockudp_w; #else int rv; struct addrinfo hints, *servinfo, *p; #endif #ifdef _WIN32 MIB_IPFORWARDROW ip_forward; unsigned long _ttl = (unsigned long)ttl; #endif int linklocal = 1; if(error) *error = MINISSDPC_UNKNOWN_ERROR; if(localport==UPNP_LOCAL_PORT_SAME) localport = SSDP_PORT; #ifdef _WIN32 sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP); #else sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0); #endif if(sudp < 0) { if(error) *error = MINISSDPC_SOCKET_ERROR; return NULL; } /* reception */ memset(&sockudp_r, 0, sizeof(struct sockaddr_storage)); if(ipv6) { struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_r; p->sin6_family = AF_INET6; if(localport > 0 && localport < 65536) p->sin6_port = htons((unsigned short)localport); p->sin6_addr = in6addr_any; /* in6addr_any is not available with MinGW32 3.4.2 */ } else { struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_r; p->sin_family = AF_INET; if(localport > 0 && localport < 65536) p->sin_port = htons((unsigned short)localport); p->sin_addr.s_addr = INADDR_ANY; } #ifdef _WIN32 /* This code could help us to use the right Network interface for * SSDP multicast traffic */ /* Get IP associated with the index given in the ip_forward struct * in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */ if(!ipv6 && (GetBestRoute(inet_addr("223.255.255.255"), 0, &ip_forward) == NO_ERROR)) { DWORD dwRetVal = 0; PMIB_IPADDRTABLE pIPAddrTable; DWORD dwSize = 0; #ifdef DEBUG IN_ADDR IPAddr; #endif int i; #ifdef DEBUG printf("ifIndex=%lu nextHop=%lx \n", ip_forward.dwForwardIfIndex, ip_forward.dwForwardNextHop); #endif pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE)); if(pIPAddrTable) { if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { free(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize); } } if(pIPAddrTable) { dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 ); if (dwRetVal == NO_ERROR) { #ifdef DEBUG printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries); #endif for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) { #ifdef DEBUG printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr; printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask; printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr; printf("\tBroadCast[%d]: \t%s (%ld)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr); printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize); printf("\tType and State[%d]:", i); printf("\n"); #endif if (pIPAddrTable->table[i].dwIndex == ip_forward.dwForwardIfIndex) { /* Set the address of this interface to be used */ struct in_addr mc_if; memset(&mc_if, 0, sizeof(mc_if)); mc_if.s_addr = pIPAddrTable->table[i].dwAddr; setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)); ((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = pIPAddrTable->table[i].dwAddr; #ifndef DEBUG break; #endif } } } free(pIPAddrTable); pIPAddrTable = NULL; } } #endif /* _WIN32 */ #ifdef _WIN32 if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof (opt)) < 0) #else if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) #endif { if(error) *error = MINISSDPC_SOCKET_ERROR; closesocket(sudp); return NULL; } #ifdef _WIN32 if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&_ttl, sizeof(_ttl)) < 0) #else /* _WIN32 */ if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) #endif /* _WIN32 */ { /* not a fatal error */ } if(multicastif) { if(ipv6) { #if !defined(_WIN32) /* according to MSDN, if_nametoindex() is supported since * MS Windows Vista and MS Windows Server 2008. * http://msdn.microsoft.com/en-us/library/bb408409%28v=vs.85%29.aspx */ unsigned int ifindex = if_nametoindex(multicastif); /* eth0, etc. */ if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof(ifindex)) < 0) { } #else #ifdef DEBUG printf("Setting of multicast interface not supported in IPv6 under Windows.\n"); #endif #endif } else { struct in_addr mc_if; mc_if.s_addr = inet_addr(multicastif); /* ex: 192.168.x.x */ if(mc_if.s_addr != INADDR_NONE) { ((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = mc_if.s_addr; if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) { } } else { #ifdef HAS_IP_MREQN /* was not an ip address, try with an interface name */ struct ip_mreqn reqn; /* only defined with -D_BSD_SOURCE or -D_GNU_SOURCE */ memset(&reqn, 0, sizeof(struct ip_mreqn)); reqn.imr_ifindex = if_nametoindex(multicastif); if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&reqn, sizeof(reqn)) < 0) { } #elif !defined(_WIN32) struct ifreq ifr; int ifrlen = sizeof(ifr); strncpy(ifr.ifr_name, multicastif, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ-1] = '\0'; if(ioctl(sudp, SIOCGIFADDR, &ifr, &ifrlen) < 0) { } mc_if.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr; if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) { } #else /* _WIN32 */ #ifdef DEBUG printf("Setting of multicast interface not supported with interface name.\n"); #endif #endif /* #ifdef HAS_IP_MREQN / !defined(_WIN32) */ } } } /* Before sending the packed, we first "bind" in order to be able * to receive the response */ if (bind(sudp, (const struct sockaddr *)&sockudp_r, ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) != 0) { if(error) *error = MINISSDPC_SOCKET_ERROR; closesocket(sudp); return NULL; } if(error) *error = MINISSDPC_SUCCESS; /* Calculating maximum response time in seconds */ mx = ((unsigned int)delay) / 1000u; if(mx == 0) { mx = 1; delay = 1000; } /* receiving SSDP response packet */ for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) { /* sending the SSDP M-SEARCH packet */ n = snprintf(bufr, sizeof(bufr), MSearchMsgFmt, ipv6 ? (linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]") : UPNP_MCAST_ADDR, deviceTypes[deviceIndex], mx); if ((unsigned int)n >= sizeof(bufr)) { if(error) *error = MINISSDPC_MEMORY_ERROR; goto error; } #ifdef DEBUG /*printf("Sending %s", bufr);*/ printf("Sending M-SEARCH request to %s with ST: %s\n", ipv6 ? (linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]") : UPNP_MCAST_ADDR, deviceTypes[deviceIndex]); #endif #ifdef NO_GETADDRINFO /* the following code is not using getaddrinfo */ /* emission */ memset(&sockudp_w, 0, sizeof(struct sockaddr_storage)); if(ipv6) { struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_w; p->sin6_family = AF_INET6; p->sin6_port = htons(SSDP_PORT); inet_pton(AF_INET6, linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR, &(p->sin6_addr)); } else { struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_w; p->sin_family = AF_INET; p->sin_port = htons(SSDP_PORT); p->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR); } n = sendto(sudp, bufr, n, 0, &sockudp_w, ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); if (n < 0) { if(error) *error = MINISSDPC_SOCKET_ERROR; break; } #else /* #ifdef NO_GETADDRINFO */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */ hints.ai_socktype = SOCK_DGRAM; /*hints.ai_flags = */ if ((rv = getaddrinfo_retro(ipv6 ? (linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR) : UPNP_MCAST_ADDR, XSTR(SSDP_PORT), &hints, &servinfo)) != 0) { if(error) *error = MINISSDPC_SOCKET_ERROR; #ifdef _WIN32 fprintf(stderr, "getaddrinfo() failed: %d\n", rv); #else fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); #endif break; } for(p = servinfo; p; p = p->ai_next) { n = (int)sendto(sudp, bufr, n, 0, p->ai_addr, p->ai_addrlen); if (n < 0) { #ifdef DEBUG char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; if (getnameinfo(p->ai_addr, p->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) { fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf); } #endif continue; } } freeaddrinfo(servinfo); if(n < 0) { if(error) *error = MINISSDPC_SOCKET_ERROR; break; } #endif /* #ifdef NO_GETADDRINFO */ /* Waiting for SSDP REPLY packet to M-SEARCH * if searchalltypes is set, enter the loop only * when the last deviceType is reached */ if(!searchalltypes || !deviceTypes[deviceIndex + 1]) do { n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id); if (n < 0) { /* error */ if(error) *error = MINISSDPC_SOCKET_ERROR; goto error; } else if (n == 0) { /* no data or Time Out */ #ifdef DEBUG printf("NODATA or TIMEOUT\n"); #endif /* DEBUG */ if (devlist && !searchalltypes) { /* found some devices, stop now*/ if(error) *error = MINISSDPC_SUCCESS; goto error; } } else { const char * descURL=NULL; int urlsize=0; const char * st=NULL; int stsize=0; const char * usn=NULL; int usnsize=0; parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize, &usn, &usnsize); if(st&&descURL) { #ifdef DEBUG printf("M-SEARCH Reply:\n ST: %.*s\n USN: %.*s\n Location: %.*s\n", stsize, st, usnsize, (usn?usn:""), urlsize, descURL); #endif /* DEBUG */ for(tmp=devlist; tmp; tmp = tmp->pNext) { if(memcmp(tmp->descURL, descURL, urlsize) == 0 && tmp->descURL[urlsize] == '\0' && memcmp(tmp->st, st, stsize) == 0 && tmp->st[stsize] == '\0' && (usnsize == 0 || memcmp(tmp->usn, usn, usnsize) == 0) && tmp->usn[usnsize] == '\0') break; } /* at the exit of the loop above, tmp is null if * no duplicate device was found */ if(tmp) continue; tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize); if(!tmp) { /* memory allocation error */ if(error) *error = MINISSDPC_MEMORY_ERROR; goto error; } tmp->pNext = devlist; tmp->descURL = tmp->buffer; tmp->st = tmp->buffer + 1 + urlsize; tmp->usn = tmp->st + 1 + stsize; memcpy(tmp->buffer, descURL, urlsize); tmp->buffer[urlsize] = '\0'; memcpy(tmp->st, st, stsize); tmp->buffer[urlsize+1+stsize] = '\0'; if(usn != NULL) memcpy(tmp->usn, usn, usnsize); tmp->buffer[urlsize+1+stsize+1+usnsize] = '\0'; tmp->scope_id = scope_id; devlist = tmp; } } } while(n > 0); if(ipv6) { /* switch linklocal flag */ if(linklocal) { linklocal = 0; --deviceIndex; } else { linklocal = 1; } } } error: closesocket(sudp); return devlist; }
/* upnpDiscover() : * return a chained list of all devices found or NULL if * no devices was found. * It is up to the caller to free the chained list * delay is in millisecond (poll) */ LIBSPEC struct UPNPDev * upnpDiscover(int delay, const char * multicastif, const char * minissdpdsock, int sameport, int ipv6, int * error) { struct UPNPDev * tmp; struct UPNPDev * devlist = 0; unsigned int scope_id = 0; int opt = 1; static const char MSearchMsgFmt[] = "M-SEARCH * HTTP/1.1\r\n" "HOST: %s:" XSTR(PORT) "\r\n" "ST: %s\r\n" "MAN: \"ssdp:discover\"\r\n" "MX: %u\r\n" "\r\n"; static const char * const deviceList[] = { #if 0 "urn:schemas-upnp-org:device:InternetGatewayDevice:2", "urn:schemas-upnp-org:service:WANIPConnection:2", #endif "urn:schemas-upnp-org:device:InternetGatewayDevice:1", "urn:schemas-upnp-org:service:WANIPConnection:1", "urn:schemas-upnp-org:service:WANPPPConnection:1", "upnp:rootdevice", 0 }; int deviceIndex = 0; char bufr[1536]; /* reception and emission buffer */ int sudp; int n; struct sockaddr_storage sockudp_r; unsigned int mx; #ifdef NO_GETADDRINFO struct sockaddr_storage sockudp_w; #else int rv; struct addrinfo hints, *servinfo, *p; #endif #ifdef _WIN32 MIB_IPFORWARDROW ip_forward; #endif int linklocal = 1; if(error) *error = UPNPDISCOVER_UNKNOWN_ERROR; /* fallback to direct discovery */ #ifdef _WIN32 sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP); #else sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0); #endif if(sudp < 0) { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; PRINT_SOCKET_ERROR("socket"); return NULL; } /* reception */ memset(&sockudp_r, 0, sizeof(struct sockaddr_storage)); if(ipv6) { struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_r; p->sin6_family = AF_INET6; if(sameport) p->sin6_port = htons(PORT); p->sin6_addr = in6addr_any; /* in6addr_any is not available with MinGW32 3.4.2 */ } else { struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_r; p->sin_family = AF_INET; if(sameport) p->sin_port = htons(PORT); p->sin_addr.s_addr = INADDR_ANY; } #ifdef _WIN32 /* This code could help us to use the right Network interface for * SSDP multicast traffic */ /* Get IP associated with the index given in the ip_forward struct * in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */ if(!ipv6 && (GetBestRoute(inet_addr("223.255.255.255"), 0, &ip_forward) == NO_ERROR)) { DWORD dwRetVal = 0; PMIB_IPADDRTABLE pIPAddrTable; DWORD dwSize = 0; #ifdef DEBUG IN_ADDR IPAddr; #endif int i; #ifdef DEBUG printf("ifIndex=%lu nextHop=%lx \n", ip_forward.dwForwardIfIndex, ip_forward.dwForwardNextHop); #endif pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE)); if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { free(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize); } if(pIPAddrTable) { dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 ); #ifdef DEBUG printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries); #endif for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) { #ifdef DEBUG printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr; printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask; printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr; printf("\tBroadCast[%d]: \t%s (%ld)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr); printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize); printf("\tType and State[%d]:", i); printf("\n"); #endif if (pIPAddrTable->table[i].dwIndex == ip_forward.dwForwardIfIndex) { /* Set the address of this interface to be used */ struct in_addr mc_if; memset(&mc_if, 0, sizeof(mc_if)); mc_if.s_addr = pIPAddrTable->table[i].dwAddr; if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) { PRINT_SOCKET_ERROR("setsockopt"); } ((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = pIPAddrTable->table[i].dwAddr; #ifndef DEBUG break; #endif } } free(pIPAddrTable); pIPAddrTable = NULL; } } #endif #ifdef _WIN32 if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof (opt)) < 0) #else if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0) #endif { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; PRINT_SOCKET_ERROR("setsockopt"); return NULL; } if(multicastif) { if(ipv6) { #if !defined(_WIN32) /* according to MSDN, if_nametoindex() is supported since * MS Windows Vista and MS Windows Server 2008. * http://msdn.microsoft.com/en-us/library/bb408409%28v=vs.85%29.aspx */ unsigned int ifindex = if_nametoindex(multicastif); /* eth0, etc. */ if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof(&ifindex)) < 0) { PRINT_SOCKET_ERROR("setsockopt"); } #else #ifdef DEBUG printf("Setting of multicast interface not supported in IPv6 under Windows.\n"); #endif #endif } else { struct in_addr mc_if; mc_if.s_addr = inet_addr(multicastif); /* ex: 192.168.x.x */ if(mc_if.s_addr != INADDR_NONE) { ((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = mc_if.s_addr; if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) { PRINT_SOCKET_ERROR("setsockopt"); } } else { #ifdef HAS_IP_MREQN /* was not an ip address, try with an interface name */ struct ip_mreqn reqn; /* only defined with -D_BSD_SOURCE or -D_GNU_SOURCE */ memset(&reqn, 0, sizeof(struct ip_mreqn)); reqn.imr_ifindex = if_nametoindex(multicastif); if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&reqn, sizeof(reqn)) < 0) { PRINT_SOCKET_ERROR("setsockopt"); } #else #ifdef DEBUG printf("Setting of multicast interface not supported with interface name.\n"); #endif #endif } } } /* Avant d'envoyer le paquet on bind pour recevoir la reponse */ if (bind(sudp, (const struct sockaddr *)&sockudp_r, ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) != 0) { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; PRINT_SOCKET_ERROR("bind"); closesocket(sudp); return NULL; } if(error) *error = UPNPDISCOVER_SUCCESS; /* Calculating maximum response time in seconds */ mx = ((unsigned int)delay) / 1000u; /* receiving SSDP response packet */ for(n = 0; deviceList[deviceIndex]; deviceIndex++) { if(n == 0) { /* sending the SSDP M-SEARCH packet */ n = snprintf(bufr, sizeof(bufr), MSearchMsgFmt, ipv6 ? (linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]") : UPNP_MCAST_ADDR, deviceList[deviceIndex], mx); #ifdef DEBUG printf("Sending %s", bufr); #endif #ifdef NO_GETADDRINFO /* the following code is not using getaddrinfo */ /* emission */ memset(&sockudp_w, 0, sizeof(struct sockaddr_storage)); if(ipv6) { struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_w; p->sin6_family = AF_INET6; p->sin6_port = htons(PORT); inet_pton(AF_INET6, linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR, &(p->sin6_addr)); } else { struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_w; p->sin_family = AF_INET; p->sin_port = htons(PORT); p->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR); } n = sendto(sudp, bufr, n, 0, &sockudp_w, ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)); if (n < 0) { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; PRINT_SOCKET_ERROR("sendto"); break; } #else /* #ifdef NO_GETADDRINFO */ memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */ hints.ai_socktype = SOCK_DGRAM; /*hints.ai_flags = */ if ((rv = getaddrinfo(ipv6 ? (linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR) : UPNP_MCAST_ADDR, XSTR(PORT), &hints, &servinfo)) != 0) { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; #ifdef _WIN32 fprintf(stderr, "getaddrinfo() failed: %d\n", rv); #else fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); #endif break; } for(p = servinfo; p; p = p->ai_next) { n = sendto(sudp, bufr, n, 0, p->ai_addr, p->ai_addrlen); if (n < 0) { #ifdef DEBUG char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; if (getnameinfo(p->ai_addr, p->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) { fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf); } #endif PRINT_SOCKET_ERROR("sendto"); continue; } } freeaddrinfo(servinfo); if(n < 0) { if(error) *error = UPNPDISCOVER_SOCKET_ERROR; break; } #endif /* #ifdef NO_GETADDRINFO */ } /* Waiting for SSDP REPLY packet to M-SEARCH */ n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id); if (n < 0) { /* error */ if(error) *error = UPNPDISCOVER_SOCKET_ERROR; break; } else if (n == 0) { /* no data or Time Out */ if (devlist) { /* no more device type to look for... */ if(error) *error = UPNPDISCOVER_SUCCESS; break; } if(ipv6) { if(linklocal) { linklocal = 0; --deviceIndex; } else { linklocal = 1; } } } else { const char * descURL=NULL; int urlsize=0; const char * st=NULL; int stsize=0; /*printf("%d byte(s) :\n%s\n", n, bufr);*/ /* affichage du message */ parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize); if(st&&descURL) { #ifdef DEBUG printf("M-SEARCH Reply:\nST: %.*s\nLocation: %.*s\n", stsize, st, urlsize, descURL); #endif for(tmp=devlist; tmp; tmp = tmp->pNext) { if(memcmp(tmp->descURL, descURL, urlsize) == 0 && tmp->descURL[urlsize] == '\0' && memcmp(tmp->st, st, stsize) == 0 && tmp->st[stsize] == '\0') break; } /* at the exit of the loop above, tmp is null if * no duplicate device was found */ if(tmp) continue; tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize); if(!tmp) { /* memory allocation error */ if(error) *error = UPNPDISCOVER_MEMORY_ERROR; break; } tmp->pNext = devlist; tmp->descURL = tmp->buffer; tmp->st = tmp->buffer + 1 + urlsize; memcpy(tmp->buffer, descURL, urlsize); tmp->buffer[urlsize] = '\0'; memcpy(tmp->buffer + urlsize + 1, st, stsize); tmp->buffer[urlsize+1+stsize] = '\0'; tmp->scope_id = scope_id; devlist = tmp; } } } closesocket(sudp); return devlist; }
long NetCollector::CollectFallback(NVDataItem *DataItems) { USES_CONVERSION; DWORD Status; DWORD NumIfEntries; Status = GetNumberOfInterfaces( &NumIfEntries); if (Status != NO_ERROR) return ERROR_SUCCESS; PMIB_IFTABLE IfTable = NULL; PMIB_IPADDRTABLE IPAddrTable = NULL; unsigned long IfLen = 0; unsigned long IpLen = 0; Status = GetIfTable(IfTable, &IfLen, TRUE); if (Status != ERROR_INSUFFICIENT_BUFFER) return ERROR_SUCCESS; Status = GetIpAddrTable(IPAddrTable, &IpLen, FALSE); if (Status != ERROR_INSUFFICIENT_BUFFER) return ERROR_SUCCESS; IfTable = (PMIB_IFTABLE) new char[IfLen]; memset(IfTable, 0 , IfLen); IPAddrTable = (PMIB_IPADDRTABLE) new char[IpLen]; memset(IPAddrTable, 0 , IpLen); Status = GetIfTable(IfTable, &IfLen, TRUE); if (Status != NO_ERROR) { delete IfTable; delete IPAddrTable; return ERROR_SUCCESS; } Status = GetIpAddrTable(IPAddrTable, &IpLen, FALSE); if (Status != NO_ERROR) { delete IfTable; delete IPAddrTable; return ERROR_SUCCESS; } DWORD CurrIf; char TransString[64]; char IPString[64]; PMIB_IFROW CurrEntry = NULL; for (CurrIf = 0, CurrEntry = IfTable->table; CurrIf < IfTable->dwNumEntries; CurrIf ++, CurrEntry ++) { auto_ptr<NVDataItem> SubItem (new NVDataItem(NCI_TAG)); TransString[0] = '\0'; if (Status != NO_ERROR) { delete IfTable; return ERROR_SUCCESS; } #if 0 if (CurrEntry.dwPhysAddrLen < 1) continue; #endif char * IfTypeName; switch (CurrEntry->dwType) { case MIB_IF_TYPE_ETHERNET: IfTypeName = "Ethernet 802.3"; break; case MIB_IF_TYPE_TOKENRING: IfTypeName = "Token Ring 802.5"; break; case MIB_IF_TYPE_FDDI: IfTypeName = "Fiber Distributed Data Interface (FDDI)"; break; case MIB_IF_TYPE_PPP: IfTypeName = "PPP"; break; case MIB_IF_TYPE_SLIP: IfTypeName = "SLIP"; break; default: IfTypeName = "Unknown"; // continue; } SubItem->AddNVItem("InterfaceType", IfTypeName); //if (wcslen(IfTable->Adapter[CurrIf].Name) > 0) { // SubItem->AddNVItem("SysName", W2A(IfTable->Adapter[CurrIf].Name)); //} if (CurrEntry->wszName != NULL && wcslen(CurrEntry->wszName) > 0) { SubItem->AddNVItem("Description", CurrEntry->wszName); } else if (strlen((const char *) CurrEntry->bDescr) > 0) { SubItem->AddNVItem("Description", (const char *) CurrEntry->bDescr); } //_ASSERT (CurrIfTableEntry.dwPhysAddrLen == 6); if (CurrEntry->bPhysAddr[0] == 0 && CurrEntry->bPhysAddr[1] == 0 && CurrEntry->bPhysAddr[2] == 0 && CurrEntry->bPhysAddr[3] == 0 && CurrEntry->bPhysAddr[4] == 0 && CurrEntry->bPhysAddr[5] == 0) { continue; } sprintf (TransString,"%02X:%02X:%02X:%02X:%02X:%02X", CurrEntry->bPhysAddr[0], CurrEntry->bPhysAddr[1], CurrEntry->bPhysAddr[2], CurrEntry->bPhysAddr[3], CurrEntry->bPhysAddr[4], CurrEntry->bPhysAddr[5]); SubItem->AddNVItem("MACAddress", TransString); string IPAddrList; long CurrIPIndex; MIB_IPADDRROW * CurrIPAddrInfo; long FoundIP = 0; for (CurrIPIndex = 0, CurrIPAddrInfo = IPAddrTable->table; CurrIPIndex < IPAddrTable->dwNumEntries; CurrIPIndex ++, CurrIPAddrInfo ++) { if (IPAddrTable->table[CurrIPIndex].dwIndex == CurrEntry->dwIndex) { FoundIP ++; if (FoundIP > 1) IPAddrList.append(","); else { GetIPAddrString (CurrIPAddrInfo->dwMask, IPString); SubItem->AddNVItem("IPSubNet", IPString); } GetIPAddrString(CurrIPAddrInfo->dwAddr, IPString); IPAddrList.append(IPString); } } SubItem->AddNVItem("IPAddress", IPAddrList.c_str()); DataItems->AddSubItem(SubItem.release()); } delete IfTable; delete IPAddrTable; // WinSetupEnumerateDevClass((GUID) GUID_DEVCLASS_NET, NCI_TAG, DataItems, NULL); return ERROR_SUCCESS; }
int build_ifclist(int sock, int max, ip_ifc_info * list) { #ifdef _WIN32 int i; int ind; MIB_IPADDRTABLE * info; ULONG out_buf_len; DWORD ret_val; out_buf_len = sizeof *info; info = (MIB_IPADDRTABLE *)loc_alloc(out_buf_len); ret_val = GetIpAddrTable(info, &out_buf_len, 0); if (ret_val == ERROR_INSUFFICIENT_BUFFER) { loc_free(info); info = (MIB_IPADDRTABLE *)loc_alloc(out_buf_len); ret_val = GetIpAddrTable(info, &out_buf_len, 0); } if (ret_val != NO_ERROR) { trace(LOG_ALWAYS, "GetIpAddrTable() error: %d", ret_val); loc_free(info); return 0; } ind = 0; for (i = 0; i < (int)info->dwNumEntries && ind < max; i++) { list[ind].addr = info->table[i].dwAddr; if (list[ind].addr == 0) continue; list[ind].mask = info->table[i].dwMask; ind++; } loc_free(info); #elif defined(__SYMBIAN32__) int ind = 0; ip_ifc_info* info = get_ip_ifc(); if (info) { trace(LOG_ALWAYS,"The IP address is %d.%d.%d.%d", (info->addr >> 24) & 0xff, (info->addr >> 16) & 0xff, (info->addr >> 8) & 0xff, info->addr & 0xff ); list[ind++] = *info; } #else int ind; char * cp; struct ifconf ifc; char if_bbf[0x2000]; memset(&ifc, 0, sizeof ifc); ifc.ifc_len = sizeof if_bbf; ifc.ifc_buf = if_bbf; if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { trace(LOG_ALWAYS, "error: ioctl(SIOCGIFCONF) returned %d: %s", errno, errno_to_str(errno)); return 0; } ind = 0; cp = (char *)ifc.ifc_req; while (cp < (char *)ifc.ifc_req + ifc.ifc_len && ind < max) { struct ifreq * ifreq_addr = (struct ifreq *)cp; struct ifreq ifreq_mask = *ifreq_addr; size_t size = sizeof(struct ifreq); /* BSD systems allow sockaddrs to be longer than their sizeof */ if (SA_LEN(&ifreq_addr->ifr_addr) > sizeof(ifreq_addr->ifr_addr)) size += SA_LEN(&ifreq_addr->ifr_addr) - sizeof(ifreq_addr->ifr_addr); cp += size; if (ifreq_addr->ifr_addr.sa_family != AF_INET) continue; if (ioctl(sock, SIOCGIFNETMASK, &ifreq_mask) < 0) { trace(LOG_ALWAYS, "error: ioctl(SIOCGIFNETMASK) returned %d: %s", errno, errno_to_str(errno)); continue; } list[ind].addr = ((struct sockaddr_in *)&ifreq_addr->ifr_addr)->sin_addr.s_addr; list[ind].mask = ((struct sockaddr_in *)&ifreq_mask.ifr_netmask)->sin_addr.s_addr; ind++; } #endif return ind; }
void ConnectToServer::asynchronousUpdate() { switch(m_state) { case NONE: { Log::info("ConnectToServer", "Protocol starting"); m_current_protocol_id = m_listener->requestStart(new GetPublicAddress(&m_public_address)); m_state = GETTING_SELF_ADDRESS; break; } case GETTING_SELF_ADDRESS: if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) // now we know the public addr { m_state = SHOWING_SELF_ADDRESS; NetworkManager::getInstance()->setPublicAddress(m_public_address); // set our public address m_current_protocol_id = m_listener->requestStart(new ShowPublicAddress()); Log::info("ConnectToServer", "Public address known"); /* if (m_quick_join) m_current_protocol_id = m_listener->requestStart(new QuickJoinProtocol(&m_server_address, &m_server_id)); else m_current_protocol_id = m_listener->requestStart(new GetPeerAddress(m_server_id, &m_server_address));*/ } break; case SHOWING_SELF_ADDRESS: if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) // now our public address is in the database { Log::info("ConnectToServer", "Public address shown"); if (m_quick_join) { m_current_protocol_id = m_listener->requestStart(new QuickJoinProtocol(&m_server_address, &m_server_id)); m_state = REQUESTING_CONNECTION; } else { m_current_protocol_id = m_listener->requestStart(new GetPeerAddress(m_host_id, &m_server_address)); m_state = GETTING_SERVER_ADDRESS; } } break; case GETTING_SERVER_ADDRESS: if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) // we know the server address { Log::info("ConnectToServer", "Server's address known"); if (m_server_address.ip == m_public_address.ip) // we're in the same lan (same public ip address) !! Log::info("ConnectToServer", "Server appears to be in the same LAN."); m_state = REQUESTING_CONNECTION; m_current_protocol_id = m_listener->requestStart(new RequestConnection(m_server_id)); } break; case REQUESTING_CONNECTION: if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) // server knows we wanna connect { Log::info("ConnectToServer", "Connection request made"); if (m_server_address.ip == 0 || m_server_address.port == 0) { // server data not correct, hide address and stop m_state = HIDING_ADDRESS; Log::error("ConnectToServer", "Server address is "ADDRESS_FORMAT, ADDRESS_ARGS(m_server_address.ip, m_server_address.port)); m_current_protocol_id = m_listener->requestStart(new HidePublicAddress()); return; } if (m_server_address.ip == m_public_address.ip) // we're in the same lan (same public ip address) !! { // just send a broadcast packet, the client will know our ip address and will connect STKHost* host = NetworkManager::getInstance()->getHost(); host->stopListening(); // stop the listening TransportAddress sender; TransportAddress broadcast_address; broadcast_address.ip = -1; // 255.255.255.255 broadcast_address.port = 7321; // 0b10101100000101101101111111111111; // for test char data2[] = "aloha_stk\0"; host->sendRawPacket((uint8_t*)(data2), 10, broadcast_address); Log::info("ConnectToServer", "Waiting broadcast message."); const uint8_t* received_data = host->receiveRawPacket(&sender); // get the sender host->startListening(); // start listening again const char data[] = "aloha_stk\0"; if (strcmp(data, (char*)(received_data)) == 0) { Log::info("ConnectToServer", "LAN Server found : %u:%u", sender.ip, sender.port); #ifndef WIN32 // just check if the ip is ours : if so, then just use localhost (127.0.0.1) struct ifaddrs *ifap, *ifa; struct sockaddr_in *sa; getifaddrs (&ifap); // get the info for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family==AF_INET) { sa = (struct sockaddr_in *) ifa->ifa_addr; if (ntohl(sa->sin_addr.s_addr) == sender.ip) // this interface is ours sender.ip = 0x7f000001; // 127.0.0.1 } } freeifaddrs(ifap); #else // Query the list of all IP addresses on the local host // First call to GetIpAddrTable with 0 bytes buffer // will return insufficient buffer error, and size // will contain the number of bytes needed for all // data. Repeat the process of querying the size // using GetIpAddrTable in a while loop since it // can happen that an interface comes online between // the previous call to GetIpAddrTable and the next // call. MIB_IPADDRTABLE *table = NULL; unsigned long size = 0; int error = GetIpAddrTable(table, &size, 0); // Also add a count to limit the while loop - in // case that something strange is going on. int count = 0; while(error==ERROR_INSUFFICIENT_BUFFER && count < 10) { delete[] table; // deleting NULL is legal table = (MIB_IPADDRTABLE*)new char[size]; error = GetIpAddrTable(table, &size, 0); count ++; } // while insufficient buffer for(unsigned int i=0; i<table->dwNumEntries; i++) { int ip = ntohl(table->table[i].dwAddr); if(sender.ip == ip) // this interface is ours { sender.ip = 0x7f000001; // 127.0.0.1 break; } } delete[] table; #endif m_server_address = sender; m_state = CONNECTING; } } else { m_state = CONNECTING; m_current_protocol_id = m_listener->requestStart(new PingProtocol(m_server_address, 2.0)); } } break; case CONNECTING: // waiting the server to answer our connection { static double timer = 0; if (StkTime::getRealTime() > timer+5.0) // every 5 seconds { timer = StkTime::getRealTime(); NetworkManager::getInstance()->connect(m_server_address); Log::info("ConnectToServer", "Trying to connect to %u:%u", m_server_address.ip, m_server_address.port); } break; } case CONNECTED: { Log::info("ConnectToServer", "Connected"); m_listener->requestTerminate( m_listener->getProtocol(m_current_protocol_id)); // kill the ping protocol because we're connected m_current_protocol_id = m_listener->requestStart(new HidePublicAddress()); ClientNetworkManager::getInstance()->setConnected(true); m_state = HIDING_ADDRESS; break; } case HIDING_ADDRESS: if (m_listener->getProtocolState(m_current_protocol_id) == PROTOCOL_STATE_TERMINATED) // we have hidden our address { Log::info("ConnectToServer", "Address hidden"); m_state = DONE; if (ClientNetworkManager::getInstance()->isConnected()) // lobby room protocol if we're connected only m_listener->requestStart(new ClientLobbyRoomProtocol(m_server_address)); } break; case DONE: m_listener->requestTerminate(this); m_state = EXITING; break; case EXITING: break; } }
int NET_IF_LIST(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { DWORD dwSize, dwRetVal, i, j; char *buf = NULL; size_t buf_alloc = 512, buf_offset = 0; int ret = SYSINFO_RET_FAIL; /* variables used for GetIfTable and GetIfEntry */ MIB_IFTABLE *pIfTable = NULL; MIB_IFROW pIfRow; /* variables used for GetIpAddrTable */ MIB_IPADDRTABLE *pIPAddrTable = NULL; IN_ADDR in_addr; /* Allocate memory for our pointers. */ dwSize = sizeof(MIB_IPADDRTABLE); pIPAddrTable = (MIB_IPADDRTABLE *)zbx_malloc(pIPAddrTable, sizeof(MIB_IPADDRTABLE)); /* Make an initial call to GetIpAddrTable to get the necessary size into the dwSize variable */ if (ERROR_INSUFFICIENT_BUFFER == GetIpAddrTable(pIPAddrTable, &dwSize, 0)) pIPAddrTable = (MIB_IPADDRTABLE *)zbx_realloc(pIPAddrTable, dwSize); /* Make a second call to GetIpAddrTable to get the actual data we want */ if (NO_ERROR != (dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIpAddrTable failed with error: %s", strerror_from_system(dwRetVal)); goto clean; } /* Allocate memory for our pointers. */ dwSize = sizeof(MIB_IFTABLE); pIfTable = (MIB_IFTABLE *)zbx_malloc(pIfTable, dwSize); /* Before calling GetIfEntry, we call GetIfTable to make sure there are entries to get and retrieve the interface index. Make an initial call to GetIfTable to get the necessary size into dwSize */ if (ERROR_INSUFFICIENT_BUFFER == GetIfTable(pIfTable, &dwSize, 0)) pIfTable = (MIB_IFTABLE *)zbx_realloc(pIfTable, dwSize); /* Make a second call to GetIfTable to get the actual data we want. */ if (NO_ERROR != (dwRetVal = GetIfTable(pIfTable, &dwSize, 0))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIfTable failed with error: %s", strerror_from_system(dwRetVal)); goto clean; } buf = (char *)zbx_malloc(buf, sizeof(char) * buf_alloc); if (pIfTable->dwNumEntries > 0) { for (i = 0; i < (int)pIfTable->dwNumEntries; i++) { LPTSTR wdescr; LPSTR utf8_descr; pIfRow.dwIndex = pIfTable->table[i].dwIndex; if (NO_ERROR != (dwRetVal = GetIfEntry(&pIfRow))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIfEntry failed with error: %s", strerror_from_system(dwRetVal)); continue; } zbx_snprintf_alloc(&buf, &buf_alloc, &buf_offset, "%-25s", get_if_type_string(pIfRow.dwType)); zbx_snprintf_alloc(&buf, &buf_alloc, &buf_offset, " %-8s", get_if_adminstatus_string(pIfRow.dwAdminStatus)); for (j = 0; j < pIPAddrTable->dwNumEntries; j++) if (pIPAddrTable->table[j].dwIndex == pIfRow.dwIndex) { in_addr.S_un.S_addr = pIPAddrTable->table[j].dwAddr; zbx_snprintf_alloc(&buf, &buf_alloc, &buf_offset, " %-15s", inet_ntoa(in_addr)); break; } if (j == pIPAddrTable->dwNumEntries) zbx_strcpy_alloc(&buf, &buf_alloc, &buf_offset, " -"); wdescr = zbx_acp_to_unicode(pIfRow.bDescr); utf8_descr = zbx_unicode_to_utf8(wdescr); zbx_snprintf_alloc(&buf, &buf_alloc, &buf_offset, " %s\n", utf8_descr); zbx_free(utf8_descr); zbx_free(wdescr); } } SET_TEXT_RESULT(result, buf); ret = SYSINFO_RET_OK; clean: zbx_free(pIfTable); zbx_free(pIPAddrTable); return ret; }
/* * returns interface statistics by IP address or interface name */ static int get_if_stats(const char *if_name, MIB_IFROW *pIfRow) { DWORD dwSize, dwRetVal, i, j; int ret = FAIL; char ip[16]; /* variables used for GetIfTable and GetIfEntry */ MIB_IFTABLE *pIfTable = NULL; /* variables used for GetIpAddrTable */ MIB_IPADDRTABLE *pIPAddrTable = NULL; IN_ADDR in_addr; /* Allocate memory for our pointers. */ dwSize = sizeof(MIB_IPADDRTABLE); pIPAddrTable = (MIB_IPADDRTABLE *)zbx_malloc(pIPAddrTable, sizeof(MIB_IPADDRTABLE)); /* Make an initial call to GetIpAddrTable to get the necessary size into the dwSize variable */ if (ERROR_INSUFFICIENT_BUFFER == GetIpAddrTable(pIPAddrTable, &dwSize, 0)) pIPAddrTable = (MIB_IPADDRTABLE *)zbx_realloc(pIPAddrTable, dwSize); /* Make a second call to GetIpAddrTable to get the actual data we want */ if (NO_ERROR != (dwRetVal = GetIpAddrTable(pIPAddrTable, &dwSize, 0))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIpAddrTable failed with error: %s", strerror_from_system(dwRetVal)); goto clean; } /* Allocate memory for our pointers. */ dwSize = sizeof(MIB_IFTABLE); pIfTable = (MIB_IFTABLE *)zbx_malloc(pIfTable, dwSize); /* Before calling GetIfEntry, we call GetIfTable to make sure there are entries to get and retrieve the interface index. Make an initial call to GetIfTable to get the necessary size into dwSize */ if (ERROR_INSUFFICIENT_BUFFER == GetIfTable(pIfTable, &dwSize, 0)) pIfTable = (MIB_IFTABLE *)zbx_realloc(pIfTable, dwSize); /* Make a second call to GetIfTable to get the actual data we want. */ if (NO_ERROR != (dwRetVal = GetIfTable(pIfTable, &dwSize, 0))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIfTable failed with error: %s", strerror_from_system(dwRetVal)); goto clean; } for (i = 0; i < pIfTable->dwNumEntries; i++) { LPTSTR wdescr; LPSTR utf8_descr; pIfRow->dwIndex = pIfTable->table[i].dwIndex; if (NO_ERROR != (dwRetVal = GetIfEntry(pIfRow))) { zabbix_log(LOG_LEVEL_DEBUG, "GetIfEntry failed with error: %s", strerror_from_system(dwRetVal)); continue; } wdescr = zbx_acp_to_unicode(pIfRow->bDescr); utf8_descr = zbx_unicode_to_utf8(wdescr); if (0 == strcmp(if_name, utf8_descr)) ret = SUCCEED; zbx_free(utf8_descr); zbx_free(wdescr); if (SUCCEED == ret) break; for (j = 0; j < pIPAddrTable->dwNumEntries; j++) { if (pIPAddrTable->table[j].dwIndex == pIfRow->dwIndex) { in_addr.S_un.S_addr = pIPAddrTable->table[j].dwAddr; zbx_snprintf(ip, sizeof(ip), "%s", inet_ntoa(in_addr)); if (0 == strcmp(if_name, ip)) { ret = SUCCEED; break; } } } if (SUCCEED == ret) break; } clean: zbx_free(pIfTable); zbx_free(pIPAddrTable); return ret; }