struct hostent* smb_gethostbyname(const char* _name) { WSAEVENT hEvent = NULL; XNDNS* pDns = NULL; INT err; HostEnt* server = (HostEnt*)malloc(sizeof(HostEnt)); gethostname(server->name, 128); if(!strcmp(server->name, _name)) { XNADDR xna; DWORD dwState; do { dwState = XNetGetTitleXnAddr(&xna); } while (dwState==XNET_GET_XNADDR_PENDING); server->addr_list[0] = server->addr; memcpy(server->addr, &(xna.ina.s_addr), 4); server->server.h_name = server->name; server->server.h_aliases = 0; server->server.h_addrtype = AF_INET; server->server.h_length = 4; server->server.h_addr_list = (char**)malloc(sizeof(char*)*4); server->server.h_addr_list[0] = server->addr_list[0]; server->server.h_addr_list[1] = 0; return (struct hostent*)server; } hEvent = WSACreateEvent(); err = XNetDnsLookup(_name, hEvent, &pDns); WaitForSingleObject(hEvent, INFINITE); if( pDns && pDns->iStatus == 0 ) { strcpy(server->name, _name); server->addr_list[0] = server->addr; memcpy(server->addr, &(pDns->aina[0].s_addr), 4); server->server.h_name = server->name; server->server.h_aliases = 0; server->server.h_addrtype = AF_INET; server->server.h_length = 4; server->server.h_addr_list = (char**)malloc(sizeof(char*)*4); server->server.h_addr_list[0] = server->addr_list[0]; server->server.h_addr_list[1] = 0; XNetDnsRelease(pDns); WSACloseEvent(hEvent); return (struct hostent*)server; } if( pDns ) XNetDnsRelease(pDns); if( hEvent ) WSACloseEvent(hEvent); free(server); return NULL; }
struct hostent *gethostbyname(const char *name) { WSAEVENT event; static struct hostent he; static struct in_addr addr; static char *addr_ptr = NULL; XNDNS *dns = NULL; he.h_addr_list = &addr_ptr; addr_ptr = (char*)&addr; if (!name) return NULL; event = WSACreateEvent(); XNetDnsLookup(name, event, &dns); if (!dns) goto error; WaitForSingleObject((HANDLE)event, INFINITE); if (dns->iStatus) goto error; memcpy(&addr, dns->aina, sizeof(addr)); WSACloseEvent(event); XNetDnsRelease(dns); return &he; error: if (event) WSACloseEvent(event); return NULL; }
// // GetHostByName // struct hostent *Xbox::GetHostByName(const char *name) { static struct hostent *he = NULL; unsigned long addr = INADDR_NONE; WSAEVENT hEvent; XNDNS *pDns = NULL; INT err; if(!name) return NULL; // This data is static and it should not be freed. if(!he) { he = (struct hostent *)malloc(sizeof(struct hostent)); if(!he) { // Failed to allocate! return NULL; } he->h_addr_list = (char **)malloc(sizeof(char*)); he->h_addr_list[0] = (char *)malloc(sizeof(struct in_addr)); } if(isdigit(name[0])) addr = inet_addr(name); if(addr != INADDR_NONE) *(int *)he->h_addr_list[0] = addr; else { hEvent = WSACreateEvent(); err = XNetDnsLookup(name, hEvent, &pDns); WaitForSingleObject( (HANDLE)hEvent, INFINITE); if(!pDns || pDns->iStatus != 0) return NULL; memcpy(he->h_addr_list[0], pDns->aina, sizeof(struct in_addr)); XNetDnsRelease(pDns); WSACloseEvent(hEvent); } return he; }
virtual void execute() { #ifndef TORQUE_OS_XENON // do it struct hostent* hostent = gethostbyname(mRequest.remoteAddr); if (hostent == NULL) { // oh well! leave the lookup data unmodified (h_length) should // still be -1 from initialization mRequest.complete = true; } else { // copy the stuff we need from the hostent dMemset(mRequest.out_h_addr, 0, sizeof(mRequest.out_h_addr)); dMemcpy(mRequest.out_h_addr, hostent->h_addr, hostent->h_length); mRequest.out_h_length = hostent->h_length; mRequest.complete = true; } #else XNDNS *pxndns = NULL; HANDLE hEvent = CreateEvent(NULL, false, false, NULL); XNetDnsLookup(mRequest.remoteAddr, hEvent, &pxndns); while(pxndns->iStatus == WSAEINPROGRESS) WaitForSingleObject(hEvent, INFINITE); if(pxndns->iStatus == 0 && pxndns->cina > 0) { dMemset(mRequest.out_h_addr, 0, sizeof(mRequest.out_h_addr)); // This is a suspect section. I need to revisit. [2/22/2010 Pat] dMemcpy(mRequest.out_h_addr, pxndns->aina, sizeof(IN_ADDR)); mRequest.out_h_length = sizeof(IN_ADDR); } mRequest.complete = true; XNetDnsRelease(pxndns); CloseHandle(hEvent); #endif }
bool ezSockets::Connect(const MString& host, unsigned short port) { if(!Check()) return false; #if defined(_XBOX) if(!isdigit(host[0])) // don't do a DNS lookup for an IP address { XNDNS *pxndns = NULL; XNetDnsLookup(host.c_str(), NULL, &pxndns); while (pxndns->iStatus == WSAEINPROGRESS) { // Do something else while lookup is in progress } if (pxndns->iStatus == 0) memcpy(&addr.sin_addr, &pxndns->aina[0], sizeof(struct in_addr)); else return false; XNetDnsRelease(pxndns); } else addr.sin_addr.s_addr = inet_addr(host.c_str()); #else struct hostent* phe; phe = gethostbyname(host.c_str()); if (phe == NULL) return false; memcpy(&addr.sin_addr, phe->h_addr, sizeof(struct in_addr)); #endif addr.sin_family = AF_INET; addr.sin_port = htons(port); if(::connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == SOCKET_ERROR) return false; state = skCONNECTED; return true; }
bool Net::stringToAddress(const char *addressString, NetAddress *address) { if(!dStrnicmp(addressString, "ipx:", 4)) // ipx support deprecated return false; if(!dStrnicmp(addressString, "ip:", 3)) addressString += 3; // eat off the ip: sockaddr_in ipAddr; char remoteAddr[256]; if(strlen(addressString) > 255) return false; dStrcpy(remoteAddr, addressString); char *portString = dStrchr(remoteAddr, ':'); if(portString) *portString++ = '\0'; if(!dStricmp(remoteAddr, "broadcast")) ipAddr.sin_addr.s_addr = htonl(INADDR_BROADCAST); else { ipAddr.sin_addr.s_addr = inet_addr(remoteAddr); if (ipAddr.sin_addr.s_addr == INADDR_NONE) // error { // On the Xbox, 'gethostbyname' does not exist so... #ifndef TORQUE_OS_XENON struct hostent *hp; if((hp = gethostbyname(remoteAddr)) == 0) return false; else memcpy(&ipAddr.sin_addr.s_addr, hp->h_addr, sizeof(in_addr)); #else // On the Xbox do XNetDnsLookup XNDNS *pxndns = NULL; HANDLE hEvent = CreateEvent(NULL, false, false, NULL); XNetDnsLookup(remoteAddr, hEvent, &pxndns); // Wait for event (passing NULL as a handle to XNetDnsLookup will NOT // cause it to behave synchronously, so do not remove the handle/wait while(pxndns->iStatus == WSAEINPROGRESS) WaitForSingleObject(hEvent, INFINITE); bool foundAddr = pxndns->iStatus == 0 && pxndns->cina > 0; if(foundAddr) { // Lets just grab the first address returned, for now memcpy(&ipAddr.sin_addr, pxndns->aina, sizeof(IN_ADDR)); } XNetDnsRelease(pxndns); CloseHandle(hEvent); // If we didn't successfully resolve the DNS lookup, bail after the // handles are released if(!foundAddr) return false; #endif } } if(portString) ipAddr.sin_port = htons(dAtoi(portString)); else ipAddr.sin_port = htons(defaultPort); ipAddr.sin_family = AF_INET; IPSocketToNetAddress(&ipAddr, address); return true; }