int Endpoint::set_address(const char* host, const int port) { reset_address(); // IP Address char address[5]; char *p_address = address; // Dot-decimal notation int result = std::sscanf(host, "%3u.%3u.%3u.%3u", (unsigned int*)&address[0], (unsigned int*)&address[1], (unsigned int*)&address[2], (unsigned int*)&address[3]); if (result != 4) { // Resolve address with DNS struct hostent *host_address = lwip_gethostbyname(host); if (host_address == NULL) return -1; //Could not resolve address p_address = (char*)host_address->h_addr_list[0]; } std::memcpy((char*)&_remoteHost.sin_addr.s_addr, p_address, 4); // Address family _remoteHost.sin_family = AF_INET; // Set port _remoteHost.sin_port = htons(port); return 0; }
int dns(const char * name, struct ip_addr * res) { int i; int c; unsigned char *ip = (unsigned char *)&res->addr; i = 0; c = 0; res->addr = 0; while(name[c] != 0) { if (name[c] != '.') { if (!isdigit(name[c])) goto dns; ip[i] *= 10; ip[i] += name[c] - '0'; } else { if (name[c+1] == '.') goto dns; i++; } if (i >= 5) goto dns; c++; } //res->addr = ntohl(res->addr); return 0; dns: if (dnssrv.sin_port) { if (lwip_gethostbyname(&dnssrv, name, &res->addr) < 0) { printf("gethostbyname: Can't look up name"); return -1; } else { return 0; } } else return -1; }
int main(int argc, char **argv) { uint8 ip[4]; struct sockaddr_in dnssrv; // KOS code net_init(); lwip_kos_init(); // Do the query dnssrv.sin_family = AF_INET; dnssrv.sin_port = htons(53); dnssrv.sin_addr.s_addr = htonl(0x0a030202); if (lwip_gethostbyname(&dnssrv, "www.allusion.net", ip) < 0) perror("Can't look up name"); else { printf("www.allusion.net is %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]); } return 0; }
T_uezError Network_lwIP_ResolveAddress( void *aWorkspace, const char *aName, T_uezNetworkAddr *aAddr) { #if LWIP_DNS struct hostent* hp; T_Network_lwIP_Workspace *p = (T_Network_lwIP_Workspace *)aWorkspace; T_uezError error = UEZ_ERROR_NONE; UEZSemaphoreGrab(p->iSem, UEZ_TIMEOUT_INFINITE); hp = (struct hostent*)lwip_gethostbyname(aName); if (hp->h_length == 4) memcpy(aAddr->v4, (char *)hp->h_addr, hp->h_length); else error = UEZ_ERROR_MISMATCH; UEZSemaphoreRelease(p->iSem); return error; #else return UEZ_ERROR_NOT_AVAILABLE; #endif }
struct hostent *gethostbyname(const char *name) { return lwip_gethostbyname(name); }