aboutsummaryrefslogtreecommitdiffstats
path: root/test/packetdrill/so_testing.c
blob: ee6a9eed5d763b262e7ce36f3fb8d9756e923a1f (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
/*
 * Copyright 2015 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.
 */
/*
 * Author: xiaoj@google.com (Xiao Jia)
 *
 * Testing against a shared object (*.so) file.
 */

#include "so_testing.h"

#include <dlfcn.h>

#include "logging.h"
#include "netdev.h"
#include "packetdrill.h"
#include "run.h"

struct so_netdev {
	struct netdev netdev;		/* "inherit" from netdev */
	struct packetdrill_interface *ifc;	/* to be filled in later */
};

/* "Downcast" an abstract netdev to our flavor. */
static inline struct so_netdev *to_so_netdev(struct netdev *netdev)
{
	return (struct so_netdev *)netdev;
}

static void so_netdev_free(struct netdev *a_netdev)
{
	struct so_netdev *netdev = to_so_netdev(a_netdev);

	memset(netdev, 0, sizeof(*netdev));  /* paranoia */
	free(netdev);
}

static int so_netdev_send(struct netdev *a_netdev, struct packet *packet)
{
	struct so_netdev *netdev = to_so_netdev(a_netdev);

	assert(packet->ip_bytes > 0);
	/* We do IPv4 and IPv6 */
	assert(packet->ipv4 || packet->ipv6);
	/* We only do TCP and ICMP */
	assert(packet->tcp || packet->udp || packet->icmpv4 || packet->icmpv6);

	return netdev->ifc->netdev_send(netdev->ifc->userdata,
					packet_start(packet),
					packet->ip_bytes);
}

static int so_netdev_receive(struct netdev *a_netdev, struct packet **packet,
			     char **error)
{
	struct so_netdev *netdev = to_so_netdev(a_netdev);
	enum packet_parse_result_t result;
	enum packet_layer_t layer = PACKET_LAYER_3_IP;
	size_t in_bytes;

	assert(*packet == NULL);	/* should be no packet yet */

	for (;;) {
		*packet = packet_new(PACKET_READ_BYTES);
		in_bytes = (*packet)->buffer_bytes;

		/* Sniff the next outbound packet from the stack under test. */
		if (netdev->ifc->netdev_receive(netdev->ifc->userdata,
						(*packet)->buffer, &in_bytes,
						&((*packet)->time_usecs)))
			goto next;

		result = parse_packet(*packet, in_bytes, layer, error);

		if (result == PACKET_OK)
			return STATUS_OK;

		if (result == PACKET_BAD)
			return STATUS_ERR;

		DEBUGP("parse_result:%d; error parsing packet: %s\n",
		       result, *error);
next:
		packet_free(*packet);
		*packet = NULL;
	}

	assert(!"should not be reached");
	return STATUS_ERR;	/* not reached */
}

static struct netdev_ops so_netdev_ops = {
	.free = so_netdev_free,
	.send = so_netdev_send,
	.receive = so_netdev_receive,
};

struct netdev *so_netdev_new(struct config *config)
{
	struct so_netdev *netdev = calloc(1, sizeof(struct so_netdev));

	netdev->netdev.ops = &so_netdev_ops;
	return (struct netdev *)netdev;
}

struct so_instance *so_instance_new(void)
{
	return calloc(1, sizeof(struct so_instance));
}

int so_instance_init(struct so_instance *instance,
		     const struct config *config,
		     const struct script *script,
		     const struct state *state)
{
#if 0
	packetdrill_interface_init_t init;
	char *error;

	instance->handle = dlopen(config->so_filename,
				  RTLD_NOW | RTLD_LOCAL | RTLD_NODELETE |
				  RTLD_DEEPBIND);
	if (!instance->handle)
		die("%s\n", dlerror());
	dlerror();  /* clear any existing error */

	init = dlsym(instance->handle, "packetdrill_interface_init");
	error = dlerror();
	if (error)
		die("%s\n", error);

	init(config->so_flags, &instance->ifc);
#else
	extern void packetdrill_interface_init(const char *flags, struct packetdrill_interface *ifc);
	instance->handle = (void *)1;
	packetdrill_interface_init(config->so_flags, &instance->ifc);
#endif
	to_so_netdev(state->netdev)->ifc = &instance->ifc;
	return STATUS_OK;
}

void so_instance_free(struct so_instance *instance)
{
	if (!instance)
		return;

	instance->ifc.free(instance->ifc.userdata);

//	if (instance->handle)
//		dlclose(instance->handle);

	memset(instance, 0, sizeof(*instance));
	free(instance);
}