END_TEST void *add_and_lookup_stuff(void *parm) { int i = 0; while((i < 1000) || (everybody_started == false)) { struct addrinfo *pAddr = (struct addrinfo *)calloc(1,sizeof(addrinfo)); struct sockaddr_in *pINetAddr; const char *word = getRandomWord((unsigned int *)parm); i++; if(NULL == get_cached_addrinfo(word)) { pAddr->ai_addr = (struct sockaddr *)calloc(1,sizeof(struct sockaddr_in)); pAddr->ai_family = AF_INET; pINetAddr = (struct sockaddr_in *)pAddr->ai_addr; pAddr->ai_canonname = strdup(word); pINetAddr->sin_addr.s_addr = rand_r((unsigned int *)parm); pAddr = insert_addr_name_info(pAddr,pAddr->ai_canonname); } else { freeaddrinfo(pAddr); pAddr = get_cached_addrinfo_full(word); } fail_unless((pAddr != NULL)); if(pAddr != NULL) { char *p1; char *p2; struct sockaddr_in *p3; struct addrinfo *p4; pINetAddr = (struct sockaddr_in *)pAddr->ai_addr; p1 = get_cached_nameinfo(pINetAddr); p2 = get_cached_fullhostname(word,pINetAddr); p3 = get_cached_addrinfo(word); p4 = get_cached_addrinfo_full(word); fail_unless(((p1 != NULL)&&(p2 != NULL)&&(p3 != NULL)&&(p4 != NULL))); } else { } } return NULL; }
bool getMacAddr( std::string &interface, unsigned char *mac_addr) { char buff[1024]; struct addrinfo *pAddr = get_cached_addrinfo_full(server_name); FILE *pPipe = popen("/sbin/ip addr","r"); if (pPipe == NULL) return false; char *iface = NULL; char *macAddr = NULL; while (fgets(buff,sizeof(buff),pPipe) != NULL) { char *tok = strtok(buff," "); if (buff[0] != ' ') { tok = strtok(NULL," :"); if (strlen(tok) != 0) { if (iface != NULL) free(iface); iface = strdup(tok); } } else if (!strcmp(tok,"link/ether")) { tok = strtok(NULL," "); if (strlen(tok) != 0) { if (macAddr != NULL) free(macAddr); macAddr = strdup(tok); } } else if (!strcmp(tok,"inet")) { tok = strtok(NULL," "); char *iaddr = strdup(tok); for (char *ind = iaddr;*ind;ind++) { if (*ind == '/') { *ind = '\0'; break; } } in_addr_t in_addr = inet_addr(iaddr); free(iaddr); struct addrinfo *pAddrInd = pAddr; while ((pAddrInd != NULL)&&(macAddr != NULL)&&(iface != NULL)) { struct in_addr saddr; saddr = ((struct sockaddr_in *)pAddrInd->ai_addr)->sin_addr; if (in_addr == saddr.s_addr) { unsigned int sa[6]; int cnt = sscanf(macAddr,"%2x:%2x:%2x:%2x:%2x:%2x", &sa[0], &sa[1], &sa[2], &sa[3], &sa[4], &sa[5]); free(macAddr); macAddr = NULL; if (cnt != 6) { free(iface); iface = NULL; break; } for (int i = 0;i < 6;i++) { *mac_addr++ = (unsigned char)sa[i]; } interface = iface; free(iface); iface = NULL; break; } pAddrInd = pAddrInd->ai_next; } } else { if (iface != NULL) { free(iface); iface = NULL; } if (macAddr != NULL) { free(macAddr); macAddr = NULL; } } } if (macAddr != NULL) { free(macAddr); macAddr = NULL; } if (iface != NULL) { free(iface); iface = NULL; } pclose(pPipe); return (interface.length() != 0); }