blob: a4207d44d132fcd248f2b07e411cc45634194028 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/poll.h>
#include <unistd.h>
#include <errno.h>
extern int h_errno;
struct hostent* gethostbyaddr(const void *addr, socklen_t len, int type) {
static struct hostent hostbuf;
struct hostent *hp;
size_t hstbuflen;
char *tmphstbuf;
int res;
int herr;
hstbuflen = 1024;
/* Allocate buffer, remember to free it to avoid a memory leakage. */
tmphstbuf = malloc (hstbuflen);
while ((res = gethostbyaddr_r (addr, len, type, &hostbuf, tmphstbuf, hstbuflen,
&hp, &herr)) == ERANGE)
{
/* Enlarge the buffer. */
hstbuflen *= 2;
tmphstbuf = realloc (tmphstbuf, hstbuflen);
}
/* Check for errors. */
if (res || hp == NULL)
return NULL;
return hp;
}
|