gethostbyname_r()

Mark Papadakis markp at pathfinder.gr
Thu Mar 22 17:30:02 EET 2001


Greetings,

Try this function, tim eixa grapsei palia kai duleue mia xara. 
host einai to hostname
buf einai pointer se ena pointer tu buffer pou theleis na xrhsimopoithei. Mhn ksexaseis na kaneis free ta resources pou ginontai allocated ( to buffer kathos kai to hostent * pou pairneis ).

paradeigma:
char *buf;
struct hostent *he;

he = _megethostbyname("www.trinity.gr", (char **)&buf);
..
..
free(buf);
free(he);

Episis, na thimasai oti oi gethostbyname*() functions mporun na kanun resolve 1 host / 1 process at the same time, opote an exeis X threads kai kaneis pollapla calls, den tha ginun tautoxrona, alla to ena meta to allo. Etsi an xreiazetai 10 seconds na ginei resolve kati, ola ta alla threads pou episis kanun resolve tha perimenun. H lysh einai na xrhsimopoieisis ena asynchronous DNS resolver ( adns, an kai den to exw xrhsimopoieisei ), i na exeis polaplus resolvers ( processes ) kai na stelneis ta requests stus available resolvers kai na pairneis ta results mesw pipes. Egw xrhsimopoiw auth th lysh se ena multithreaded programma pou apaitei sinexomena resolve requests kai duleuei arketa kala.
An telika den bgaleis akrh, pes mu na su steilw mia library pu egrapsa gia auto to skopo.

struct hostent *_mygethostbyname(char *host, char **buf )
{

        struct hostent *hentp, hent, *hp;
        size_t hstbuflen;
        int herr;
        char *tmphstbuf;

        hstbuflen = 1024;
        tmphstbuf = (char *)malloc(hstbuflen);

        while( gethostbyname_r( host, &hent, tmphstbuf, hstbuflen, &hentp, &herr) )
        {
                if(herr==ERANGE)
                {
                        /* Need larger buffer */

                        hstbuflen<<=1;
                        tmphstbuf = realloc(tmphstbuf, hstbuflen);
                }
                else
                {
                        free(tmphstbuf);
                        *buf=NULL;
                        return(NULL);
                }
        }

        *buf = tmphstbuf;

        hp = (struct hostent *)Malloc(sizeof(struct hostent));
        memcpy(hp, hentp, sizeof(struct hostent));

        return(hp);
}




Mark Papadakis
R&D Director - Web Division
Phaistos Networks, S.A. - http://www.phaistosnetworks.gr 
markp at phaistosnetworks.gr T:+30-892-23855  F:+30-892-22670
- A DOL Digital Company





More information about the Linux-greek-users mailing list