summaryrefslogtreecommitdiff
path: root/src/common/sockaddr.c
blob: b4e75ee0e5092708b2df6a2bb3132f41f5deac0f (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#include "common/sockaddr.h"

int sockaddr_init(sockaddr_t *addr, int af)
{
	/* Reset pointer. */
	memset(addr, 0, sizeof(sockaddr_t));
	addr->family = -1;

	/* Initialize address size. */
	switch(af) {
	case AF_INET:
		addr->len = sizeof(struct sockaddr_in);
		addr->prefix = IPV4_PREFIXLEN;
		break;
#ifndef DISABLE_IPV6
	case AF_INET6:
		addr->len = sizeof(struct sockaddr_in6);
		addr->prefix = IPV6_PREFIXLEN;
		break;
#endif
	default:
		return -1;
	}

	/* Update pointer. */
	addr->family = af;
	return sockaddr_update(addr);
}

int sockaddr_isvalid(sockaddr_t *addr)
{
	return addr && addr->family > -1;
}

int sockaddr_copy(sockaddr_t *dst, const sockaddr_t *src)
{
	if (memcpy(dst, src, sizeof(sockaddr_t)) != NULL) {
		return sockaddr_update(dst);
	}
	
	return -1;
}

int sockaddr_update(sockaddr_t *addr)
{
	/* Update internal pointer. */
	switch(addr->len) {
	case sizeof(struct sockaddr_in):
		addr->ptr = (struct sockaddr*)&addr->addr4;
		break;
#ifndef DISABLE_IPV6
	case sizeof(struct sockaddr_in6):
		addr->ptr = (struct sockaddr*)&addr->addr6;
		break;
#endif
	default:
		return -1;
	}

	return 0;
}

int sockaddr_set(sockaddr_t *dst, int family, const char* addr, int port)
{
	if (!dst || !addr || port < 0) {
		return -1;
	}

	/* Initialize. */
	dst->family = -1;
	dst->ptr = 0;
	dst->len = 0;
	sockaddr_init(dst, family);

	/* Initialize depending on address family. */
	void *paddr = 0;
	switch(family) {
	case AF_INET:
		dst->addr4.sin_family = family;
		dst->addr4.sin_port = htons(port);
		paddr = &dst->addr4.sin_addr;
		dst->addr4.sin_addr.s_addr = INADDR_ANY;
		dst->prefix = IPV4_PREFIXLEN;
		break;
#ifndef DISABLE_IPV6
	case AF_INET6:
		dst->addr6.sin6_family = family;
		dst->addr6.sin6_port = htons(port);
		paddr = &dst->addr6.sin6_addr;
		memcpy(&dst->addr6.sin6_addr,
		       &in6addr_any, sizeof(in6addr_any));
		dst->prefix = IPV6_PREFIXLEN;
		break;
#endif
	default:
		return -1;
	}

	/* Convert address. */
	return inet_pton(family, addr, paddr);
}

int sockaddr_setprefix(sockaddr_t *dst, int prefix)
{
	if (dst == NULL || prefix < 0) {
		return -1;
	}
	
	return dst->prefix = prefix;
}

int sockaddr_tostr(const sockaddr_t *addr, char *dst, size_t size)
{
	if (!addr || !dst || size == 0) {
		return -1;
	}

	/* Minimum length. */
	size_t minlen = INET_ADDRSTRLEN;

	/* Check unsupported IPv6. */
#ifdef DISABLE_IPV6
	if (addr->family == AF_INET6) {
		return -1;
	}
#else
	minlen = INET6_ADDRSTRLEN;
#endif

	/* Check minimum length. */
	if (size < minlen) {
		return -1;
	}

	/* Convert. */
#ifdef DISABLE_IPV6
	dst[0] = '\0';
#else
	/* Load IPv6 addr if default. */
	if (addr->family == AF_INET6) {
		inet_ntop(addr->family, &addr->addr6.sin6_addr,
			  dst, size);
	}
#endif
	/* Load IPv4 if set. */
	if (addr->family == AF_INET) {
		inet_ntop(addr->family, &addr->addr4.sin_addr,
			  dst, size);
	}

	return 0;
}

int sockaddr_portnum(const sockaddr_t *addr)
{
	if (!addr) {
		return -1;
	}

	switch(addr->family) {

	/* IPv4 */
	case AF_INET:
		return ntohs(addr->addr4.sin_port);
		break;

	/* IPv6 */
#ifndef DISABLE_IPV6
	case AF_INET6:
		return ntohs(addr->addr6.sin6_port);
		break;
#endif

	/* N/A */
	default:
		return -1;
		break;
	}
}