aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/wrap.c
blob: 112af2a987751a9242a41e2bc7feae6244afcb20 (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
/*
 * Copyright 2013 Google 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
/*
 * Wrappers for making L3-independent syscalls.
 */

#include "wrap.h"

#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "checksum.h"
#include "gre.h"
#include "logging.h"
#include "netdev.h"
#include "packet.h"
#include "packet_checksum.h"
#include "packet_to_string.h"
#include "run.h"
#include "script.h"
#include "tcp_options_iterator.h"
#include "tcp_options_to_string.h"
#include "tcp_packet.h"

int wrap_socket(enum ip_version_t ip_version, int type)
{
	int fd = -1;

	switch (ip_version) {
	case IP_VERSION_4:
		fd = socket(AF_INET, type, 0);
		if (fd < 0)
			die_perror("socket(AF_INET)");
		break;

	case IP_VERSION_4_MAPPED_6:
	case IP_VERSION_6:
		fd = socket(AF_INET6, type, 0);
		if (fd < 0)
			die_perror("socket(AF_INET6)");
		break;

	default:
		die("bad ip_version (%d) in config\n", ip_version);
		break;
	}

	return fd;
}

u16 wrap_bind_listen(int s, enum ip_version_t ip_version, u16 port)
{
	switch (ip_version) {
	case IP_VERSION_4: {
		struct sockaddr_in addr;
		socklen_t addrlen = sizeof(addr);

		memset(&addr, 0, addrlen);
#ifndef linux
		addr.sin_len	= addrlen;
#endif
		addr.sin_family	= AF_INET;
		addr.sin_port	= htons(port);

		if (bind(s, (struct sockaddr *)&addr, addrlen) < 0)
			die_perror("bind(AF_INET)");

		memset(&addr, 0, sizeof(addr));
		if (getsockname(s, (struct sockaddr *)&addr, &addrlen) < 0)
			die_perror("getsockname(AF_INET)");
		assert(addr.sin_family == AF_INET);

		if (listen(s, 100) < 0)
			die_perror("listen(AF_INET)");

		return ntohs(addr.sin_port);
	}

	case IP_VERSION_4_MAPPED_6:
	case IP_VERSION_6: {
		struct sockaddr_in6 addr6;
		socklen_t addrlen = sizeof(addr6);

		memset(&addr6, 0, addrlen);
		addr6.sin6_family = AF_INET6;
		addr6.sin6_port = htons(port);

		if (bind(s, (struct sockaddr *)&addr6, addrlen) < 0)
			die_perror("bind(AF_INET6)");

		memset(&addr6, 0, sizeof(addr6));
		if (getsockname(s, (struct sockaddr *)&addr6, &addrlen) < 0)
			die_perror("getsockname(AF_INET6)");
		assert(addr6.sin6_family == AF_INET6);

		if (listen(s, 100) < 0)
			die_perror("listen(AF_INET6)");

		return ntohs(addr6.sin6_port);
	}

	default:
		die("bad ip_version (%d) in config\n", ip_version);
		return 0;
	}
}