aboutsummaryrefslogtreecommitdiffstats
path: root/xlib.c
blob: e48ec39c43764b61839b8a51cbdc31e549409eab (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
39
40
41
42
43
44
45
46
47
48
49
50

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "xlib.h"

void *xmalloc(size_t size)
{
	void *p = malloc(size);
	if (p == NULL)
		err(EXIT_FAILURE, "malloc");
	return p;
}

void *xrealloc(void *ptr, size_t size)
{
	void *p = realloc(ptr, size);
	if (p == NULL)
		err(EXIT_FAILURE, "realloc");
	return p;
}

char *xstrdup(const char *str)
{
	char *s = strdup(str);
	if (s == NULL)
		err(EXIT_FAILURE, "strdup");
	return s;
}

int init_sockaddr(struct sockaddr_in *addr, const char *host)
{
	memset((char *) addr, 0, sizeof(struct sockaddr_in));
	addr->sin_family = AF_INET;
	if (inet_aton(host, &addr->sin_addr) == 0) {
		struct hostent *hp;		
		hp = gethostbyname(host);
		if (!hp) 
			return -1;
		memcpy(&addr->sin_addr, hp->h_addr, 4);
	}
	return 0;
}