File: human_addr.c

package info (click to toggle)
ocserv 1.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,924 kB
  • sloc: ansic: 46,747; sh: 12,630; makefile: 412; xml: 29
file content (128 lines) | stat: -rw-r--r-- 3,243 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2015 Red Hat, Inc.
 *
 * 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 2 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#include "../src/ip-util.h"
#include "../src/ip-util.c"

int syslog_open = 0;

static unsigned ip_to_sockaddr(const char *ip, unsigned port, struct sockaddr_storage *ss)
{
	int ret;
	struct sockaddr_in6 *s6 = (void*)ss;
	struct sockaddr_in *s4 = (void*)ss;

	memset(ss, 0, sizeof(*ss));
	if (strchr(ip, '.') == 0) {
		s6->sin6_family = AF_INET6;

		ret = inet_pton(AF_INET6, ip, &s6->sin6_addr);
		if (ret == 0) {
			fprintf(stderr, "error in inet_pton6(%s)\n", ip);
			exit(1);
		}
		s6->sin6_port = htons(port);

		return sizeof(struct sockaddr_in6);
	} else {
		s4->sin_family = AF_INET;

		ret = inet_pton(AF_INET, ip, &s4->sin_addr);
		if (ret == 0) {
			fprintf(stderr, "error in inet_pton(%s)\n", ip);
			exit(1);
		}
		s4->sin_port = htons(port);

		return sizeof(struct sockaddr_in);
	}
}

static void check(const char *ip)
{
	struct sockaddr_storage ss;
	socklen_t len;
	char *p;
	char buf[128];

	len = ip_to_sockaddr(ip, 443, &ss);
	p = human_addr2((struct sockaddr*)&ss, len, buf, sizeof(buf), 0);
	if (p == NULL) {
		fprintf(stderr, "human_addr2 couldn't convert: %s\n", ip);
		exit(1);
	}

	if (strcmp(ip, buf) != 0) {
		fprintf(stderr, "human_addr2 returned different value (have: %s, expected: %s)\n", buf, ip);
		exit(1);

	}
}

static void check_port(const char *ip, unsigned port)
{
	struct sockaddr_storage ss;
	socklen_t len;
	char *p;
	char buf[128];
	char buf2[128];

	len = ip_to_sockaddr(ip, port, &ss);
	p = human_addr2((struct sockaddr*)&ss, len, buf, sizeof(buf), 1);
	if (p == NULL) {
		fprintf(stderr, "human_addr2 couldn't convert: %s\n", ip);
		exit(1);
	}

	if (strchr(ip, ':') != 0) {
		snprintf(buf2, sizeof(buf2), "[%s]:%u", ip, port);
	} else {
		snprintf(buf2, sizeof(buf2), "%s:%u", ip, port);
	}

	if (strcmp(buf2, buf) != 0) {
		fprintf(stderr, "human_addr2 returned different value (have: %s, expected: %s)\n", buf, buf2);
		exit(1);

	}
}

int main(void)
{
	check("172.18.52.43");
	check("192.168.1.1");
	check("10.100.100.2");
	check("fd4f:edc6:b75:5dfd:3cd9:b8ae:97ec:52da");
	check("fc6f:8eca:d6a2:2559:90e3:1b33:8e6:ae59");
	check("fd44:1f40:e28a:1928:773c:9a1e:76dc:9a1");

	check_port("172.18.52.43", 128);
	check_port("192.168.1.1", 256);
	check_port("10.100.100.2", 443);
	check_port("fd4f:edc6:b75:5dfd:3cd9:b8ae:97ec:52da", 512);
	check_port("fc6f:8eca:d6a2:2559:90e3:1b33:8e6:ae59", 443);
	check_port("fd44:1f40:e28a:1928:773c:9a1e:76dc:9a1", 1024);

	return 0;
}