Пример #1
0
NSAPI_PUBLIC char *util_hostname(void)
{
    char str[MAXHOSTNAMELEN];
    PRHostEnt  *p;
    PRHostEnt   hent;
    char        buf[PR_AR_MAXHOSTENTBUF];
    PRInt32     err;

    gethostname(str, MAXHOSTNAMELEN);
#ifdef NS_OLDES3X
    p = PR_AR_GetHostByName(
                str,
                &hent,
                buf,
                PR_AR_MAXHOSTENTBUF,
                &err,
                PR_AR_DEFAULT_TIMEOUT,
                AF_INET);

    if (p == NULL) 
        return NULL;
    return net_find_fqdn(p);
#else
    err = PR_AR_GetHostByName(str, buf, PR_AR_MAXHOSTENTBUF,
                              &hent, PR_AR_DEFAULT_TIMEOUT, AF_INET);
    if (err != PR_AR_OK)
        return NULL;
    return net_find_fqdn(&hent);
#endif /* NS_OLDES3X */

}
Пример #2
0
NSAPI_PUBLIC char *util_hostname(void)
{
    char str[MAXHOSTNAMELEN];
    PRHostEnt   hent;
    char        buf[PR_NETDB_BUF_SIZE];
    PRStatus    err;

    gethostname(str, MAXHOSTNAMELEN);
    err = PR_GetHostByName(
                str,
                buf,
                PR_NETDB_BUF_SIZE,
                &hent);

    if (err == PR_FAILURE) 
        return NULL;
    return net_find_fqdn(&hent);
}
char *dns_ip2host(char *ip, int verify)
{
    PRNetAddr iaddr;
    PRHostEnt* hptr = NULL;
    char* hn = NULL;
    unsigned long laddr = 0;
    char myhostname[256];
    PRHostEnt   hent;
    char        buf[PR_AR_MAXHOSTENTBUF];
    PRInt32     err = 0;

    if (PR_StringToNetAddr(ip, &iaddr) == PR_FAILURE)
        goto bong;

    /*
     * See if it happens to be the localhost IP address, and try
     * the local host name if so.
     */
    if (laddr == 0)
    {
	    laddr = inet_addr("127.0.0.1");
	    myhostname[0] = 0;
	    gethostname(myhostname, sizeof(myhostname));
    }

    /* Have to match the localhost IP address and have a hostname */

    if (((unsigned long)iaddr.inet.ip == laddr) && (myhostname[0] != 0))
    {

        /*
         * Now try for a fully-qualified domain name, starting with
         * the local hostname.
         */
        if (PR_AR_GetHostByName(myhostname, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT, AF_INET) == PR_AR_OK)
        {
            hptr = &hent;
        }
        else
        {
            hptr = 0;
        }
        /* Don't verify if we get a fully-qualified name this way */
        verify = 0;
    }
    else {
        if (PR_AR_GetHostByAddr(&iaddr, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT) == PR_AR_OK)
        {
            hptr = &hent;
        }
        else
        {
            hptr = 0;
        }
    }

    if ((!hptr) || !(hn = net_find_fqdn(hptr)))
        goto bong;

    if(verify)
    {
        char **haddr = 0;
        if (PR_AR_GetHostByName(hn, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT, AF_INET) == PR_AR_OK)
        {
            hptr = &hent;
        }
        else
        {
            hptr = 0;
        }
        if(hptr)
        {
            for(haddr = hptr->h_addr_list; *haddr; haddr++)
            {
                if(((struct in_addr *)(*haddr))->s_addr == (unsigned long)iaddr.inet.ip)
                    break;
            }
        }
        if((!hptr) || (!(*haddr)))
            goto bong;
    }

    return hn;
  bong:
    return NULL;
}
Пример #4
0
char *dns_ip2host(const char *ip, int verify)
{
    PRNetAddr iaddr;
    PRHostEnt *hptr;
    char *hn;
#ifdef DNS_CACHE
    dns_cache_entry_t *dns_entry;
#endif
    unsigned long laddr = 0;
    char myhostname[256];
    PRHostEnt   hent;
    char        buf[PR_AR_MAXHOSTENTBUF];
    PRInt32     err;

    if (PR_StringToNetAddr(ip, &iaddr) == PR_FAILURE)
        goto bong;

#ifdef DNS_CACHE
    if ( (dns_entry = dns_cache_lookup_ip((unsigned int)iaddr.inet.ip)) )
    {
        hn = NULL;
        if ( dns_entry->host && 
             /* Only use entry if the cache entry has been verified or if 
              * verify is off...
              */
             (dns_entry->verified || !verify) ) {
            hn = STRDUP( dns_entry->host );
	    (void)dns_cache_use_decrement(dns_entry);
	    return hn;
        }
	dns_cache_delete(dns_entry);
	dns_entry = 0;
    }
#endif

    /*
     * See if it happens to be the localhost IP address, and try
     * the local host name if so.
     */
    if (laddr == 0) {
	laddr = inet_addr("127.0.0.1");
	myhostname[0] = 0;
	gethostname(myhostname, sizeof(myhostname));
    }

    /* Have to match the localhost IP address and have a hostname */

    if (((unsigned long)iaddr.inet.ip == laddr) && (myhostname[0] != 0))
    {

        /*
         * Now try for a fully-qualified domain name, starting with
         * the local hostname.
         */
        if (PR_AR_GetHostByName(myhostname, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT, AF_INET) == PR_AR_OK) {
            hptr = &hent;
        }
        else {
            hptr = 0;
        }
        /* Don't verify if we get a fully-qualified name this way */
        verify = 0;
    }
    else {
        if (PR_AR_GetHostByAddr(&iaddr, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT) == PR_AR_OK) {
            hptr = &hent;
        }
        else {
            hptr = 0;
        }
    }

    if ((!hptr) || !(hn = net_find_fqdn(hptr))) goto bong;

    if(verify) {
        char **haddr = 0;
        if (PR_AR_GetHostByName(hn, buf, PR_AR_MAXHOSTENTBUF,
                                &hent, PR_AR_DEFAULT_TIMEOUT, AF_INET) == PR_AR_OK) {
            hptr = &hent;
        }
        else {
            hptr = 0;
        }
        if(hptr) {
            for(haddr = hptr->h_addr_list; *haddr; haddr++) {
                if(((struct in_addr *)(*haddr))->s_addr == (unsigned long)iaddr.inet.ip)
                    break;
            }
        }
        if((!hptr) || (!(*haddr)))
            goto bong;
    }

#ifdef DNS_CACHE
    if ( (dns_entry = dns_cache_insert(hn, (unsigned int)iaddr.inet.ip, verify)) )
    {
        (void) dns_cache_use_decrement(dns_entry);
    } 
#endif /* DNS_CACHE */
    return hn;
  bong:
#ifdef DNS_CACHE
    /* Insert the lookup failure */
    if ( (dns_entry = dns_cache_insert(NULL, (unsigned int)iaddr.inet.ip, verify)) )
    {
        (void) dns_cache_use_decrement(dns_entry);
    } 
#endif /* DNS_CACHE */
    return NULL;
}
Пример #5
0
char *dns_ip2host(char *ip, int verify)
{
    /*    struct in_addr iaddr;  */
    PRNetAddr iaddr;
    char *hn;
    static unsigned long laddr = 0;
    static char myhostname[256];
    PRHostEnt   hent;
    char        buf[PR_NETDB_BUF_SIZE];
    PRStatus    err;


    err = PR_InitializeNetAddr(PR_IpAddrNull, 0, &iaddr);

	/* richm: ipv6 cleanup - use inet_aton or other more appropriate function
	   instead of inet_addr */
    if((iaddr.inet.ip = inet_addr(ip)) == (in_addr_t)-1)
        goto bong;

    /*
     * See if it happens to be the localhost IP address, and try
     * the local host name if so.
     */
    if (laddr == 0) {
	laddr = inet_addr("127.0.0.1");
	myhostname[0] = 0;
	PR_GetSystemInfo(PR_SI_HOSTNAME, myhostname, sizeof(myhostname));
    }

    /* Have to match the localhost IP address and have a hostname */
    if ((iaddr.inet.ip == laddr) && (myhostname[0] != 0)) {
        /*
         * Now try for a fully-qualified domain name, starting with
         * the local hostname.
         */
        err =  PR_GetHostByName(myhostname,
				buf,
				PR_NETDB_BUF_SIZE,
				&hent);

        /* Don't verify if we get a fully-qualified name this way */
        verify = 0;
    }
    else {
      err = PR_GetHostByAddr(&iaddr, 
			     buf,
			     PR_NETDB_BUF_SIZE,
			     &hent);
    }

    if ((err == PR_FAILURE) || !(hn = net_find_fqdn(&hent))) goto bong;


    if(verify) {
        char **haddr = 0;
       	err = PR_GetHostByName(hn,
			       buf,
			       PR_NETDB_BUF_SIZE,
			       &hent);
 
        if(err == PR_SUCCESS) {
            for(haddr = hent.h_addr_list; *haddr; haddr++) {
                if(((struct in_addr *)(*haddr))->s_addr == iaddr.inet.ip)
                    break;
            }
        }

        if((err == PR_FAILURE) || (!(*haddr)))
            goto bong;
    }

    return hn;
  bong:
    return NULL;
}