static JSBool myIpAddress(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { char *hostname = JS_malloc(cx, 1024); if (!gethostname(hostname, 1023)) { JSString *myhost = JS_NewString(cx, hostname, strlen(hostname)); jsval arg = STRING_TO_JSVAL(myhost); return dnsResolve(cx, obj, 1, &arg, rval); } px_free(hostname); *rval = JSVAL_NULL; return true; }
error_t getHostByName(NetInterface *interface, const char_t *name, IpAddr *ipAddr, uint_t flags) { error_t error; //Default address type depends on TCP/IP stack configuration #if (IPV4_SUPPORT == ENABLED) HostType type = HOST_TYPE_IPV4; #elif (IPV6_SUPPORT == ENABLED) HostType type = HOST_TYPE_IPV6; #else HostType type = HOST_TYPE_ANY; #endif //Default name resolution protocol depends on TCP/IP stack configuration #if (DNS_CLIENT_SUPPORT == ENABLED) HostnameResolver protocol = HOST_NAME_RESOLVER_DNS; #elif (MDNS_CLIENT_SUPPORT == ENABLED) HostnameResolver protocol = HOST_NAME_RESOLVER_MDNS; #elif (NBNS_CLIENT_SUPPORT == ENABLED) HostnameResolver protocol = HOST_NAME_RESOLVER_NBNS; #else HostnameResolver protocol = HOST_NAME_RESOLVER_ANY; #endif //Check parameters if(name == NULL || ipAddr == NULL) return ERROR_INVALID_PARAMETER; //Use default network interface? if(interface == NULL) interface = netGetDefaultInterface(); //The specified name can be either an IP or a host name error = ipStringToAddr(name, ipAddr); //Perform name resolution if necessary if(error) { //The user may provide a hint to choose between IPv4 and IPv6 if(flags & HOST_TYPE_IPV4) type = HOST_TYPE_IPV4; else if(flags & HOST_TYPE_IPV6) type = HOST_TYPE_IPV6; //The user may provide a hint to to select the desired protocol to be used if(flags & HOST_NAME_RESOLVER_DNS) { //Use DNS to resolve the specified host name protocol = HOST_NAME_RESOLVER_DNS; } else if(flags & HOST_NAME_RESOLVER_MDNS) { //Use mDNS to resolve the specified host name protocol = HOST_NAME_RESOLVER_MDNS; } else if(flags & HOST_NAME_RESOLVER_NBNS) { //Use NBNS to resolve the specified host name protocol = HOST_NAME_RESOLVER_NBNS; } else { //Retrieve the length of the host name to be resolved size_t n = strlen(name); //Select the most suitable protocol if(n >= 6 && !strcasecmp(name + n - 6, ".local")) { #if (MDNS_CLIENT_SUPPORT == ENABLED) //Use mDNS to resolve the specified host name protocol = HOST_NAME_RESOLVER_MDNS; #endif } else if(n <= 15 && !strchr(name, '.') && type == HOST_TYPE_IPV4) { #if (NBNS_CLIENT_SUPPORT == ENABLED) //Use NetBIOS Name Service to resolve the specified host name protocol = HOST_NAME_RESOLVER_NBNS; #endif } } #if (DNS_CLIENT_SUPPORT == ENABLED) //Use DNS protocol? if(protocol == HOST_NAME_RESOLVER_DNS) { //Perform host name resolution error = dnsResolve(interface, name, type, ipAddr); } else #endif #if (MDNS_CLIENT_SUPPORT == ENABLED) //Use mDNS protocol? if(protocol == HOST_NAME_RESOLVER_MDNS) { //Perform host name resolution error = mdnsResolve(interface, name, type, ipAddr); } else #endif #if (NBNS_CLIENT_SUPPORT == ENABLED && IPV4_SUPPORT == ENABLED) //Use NetBIOS Name Service protocol? if(protocol == HOST_NAME_RESOLVER_NBNS) { //Perform host name resolution error = nbnsResolve(interface, name, ipAddr); } else #endif //Invalid protocol? { //Report an error error = ERROR_INVALID_PARAMETER; } } //Return status code return error; }