summaryrefslogtreecommitdiff
path: root/src/libc/net/arpainet.c
blob: 62efb6266fb07cb6d4678211cfcd969ac0f9c060 (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 <arpa/inet.h>
#include <stdlib.h>

uint32_t htonl(uint32_t n) {
	return ((n & 0xFF000000) >> 24)
	     | ((n & 0x00FF0000) >>  8)
	     | ((n & 0x0000FF00) <<  8)
	     | ((n & 0x000000FF) << 24);
}

uint16_t htons(uint16_t n) {
	return (n >> 8) | (n << 8);
}

uint32_t ntohl(uint32_t n) {
	return htonl(n);
}

uint16_t ntohs(uint16_t n) {
	return htons(n);
}

int inet_aton(const char *s, struct in_addr *dest) {
	if (!s) return -1;

	uint32_t ip = strtol(s, (char**)&s, 0);
	int parts = 1;
	for (; parts < 4; parts++) {
		if (!*s) break;
		if (*s++ != '.') return 0;
		ip <<= 8;
		ip += strtol(s, (char**)&s, 0);
	}
	if (parts > 1)
		ip = ((ip & 0xFFFFFF00) << 8 * (4 - parts)) | (ip & 0xFF);
	dest->s_addr = htonl(ip);
	return 1;
}