summaryrefslogtreecommitdiffstats
path: root/testsuite/foobarserver/test_main.c
blob: 84c40c4dec4e668bfbf17bf6c5b83a781064d43d (plain) (blame)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
 * @file
 *
 * @brief The multicast DNS (mDNS) protocol is tested.
 *
 * To run this test is also required to run foobarclient.
 * A TCP socket (SOCK_STREAM) is open from port FOOBAR_PORT_BEGIN to
 * port FOOBAR_PORT_END - 1 on at a time.
 * The following functions are called: mDNS_RegisterService(),
 * mDNS_DeregisterService() and mDNS_RenameAndReregisterService().
 */

/*
 * Copyright (c) 2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <mDNSEmbeddedAPI.h>
#include <mDNSPosix.h>

#include <sys/select.h>
#include <sys/socket.h>

#include <netinet/in.h>

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define TEST_NAME "LIBBSD FOOBAR SERVER"
#define TEST_STATE_USER_INPUT 1

#define FOOBAR_PORT_BEGIN 10815

#define FOOBAR_PORT_END 20815

static mDNS mDNSStorage;

static mDNS_PlatformSupport PlatformStorage;

static void
foobar_callback(mDNS *m, ServiceRecordSet *srs, mStatus status)
{
	const mDNSu8 *name = srs->RR_SRV.resrec.name->c;

	switch (status) {
	case mStatus_NoError:
		printf("foobar server: name registered: %s\n", name);
		break;
	case mStatus_NameConflict:
		printf("foobar server: name conflict: %s\n", name);
		status = mDNS_RenameAndReregisterService(m, srs, mDNSNULL);
		assert(status == mStatus_NoError);
		break;
	case mStatus_MemFree:
		printf("foobar server: free: %s\n", name);
		free(srs);
		break;
	default:
		printf("foobar server: unexpected status: %s\n", name);
		break;
	}
}

static ServiceRecordSet *
foobar_register(mDNSu16 port)
{
	ServiceRecordSet *srs;
	mStatus status;
	domainlabel name;
	domainname type;
	domainname domain;

	srs = calloc(1, sizeof(*srs));
	assert(srs != NULL);

	MakeDomainLabelFromLiteralString(&name, "foobar");
	MakeDomainNameFromDNSNameString(&type, "_foobar._tcp");
	MakeDomainNameFromDNSNameString(&domain, "local.");

	status = mDNS_RegisterService(&mDNSStorage, srs, &name, &type, &domain,
	    NULL, mDNSOpaque16fromIntVal(port), NULL, 0, NULL, 0,
	    mDNSInterface_Any, foobar_callback, srs, 0);
	assert(status == mStatus_NoError);

	return srs;
}

static void
foobar_deregister(ServiceRecordSet *srs)
{
	mStatus status;

	status = mDNS_DeregisterService(&mDNSStorage, srs);
	assert(status == mStatus_NoError);
}

static void *
foobar_thread(void *arg)
{
	static const char foobar[] = "FooBar!";

	mDNSu16 port = FOOBAR_PORT_BEGIN;

	while (1) {
		ServiceRecordSet *srs;
		struct sockaddr_in addr;
		struct sockaddr_in addr2;
		socklen_t addr2_len;
		int sd;
		int sd2;
		int rv;
		ssize_t n;

		sd = socket(PF_INET, SOCK_STREAM, 0);
		assert(sd >= 0);

		memset(&addr, 0, sizeof(addr));
		addr.sin_family = AF_INET;
		addr.sin_port = htons(port);
		addr.sin_addr.s_addr = htonl(INADDR_ANY);

		rv = bind(sd, (const struct sockaddr *) &addr, sizeof(addr));
		assert(rv == 0);

		rv = listen(sd, 0);
		assert(rv == 0);

		srs = foobar_register(port);

		addr2_len = sizeof(addr2);
		sd2 = accept(sd, (struct sockaddr *) &addr2, &addr2_len);
		assert(sd2 >= 0);

		n = write(sd2, &foobar[0], sizeof(foobar));
		assert(n == (ssize_t) sizeof(foobar));

		foobar_deregister(srs);

		rv = close(sd2);
		assert(rv == 0);

		rv = close(sd);
		assert(rv == 0);

		if (port < FOOBAR_PORT_END) {
			++port;
		} else {
			port = FOOBAR_PORT_BEGIN;
		}
	}

	return NULL;
}

static void
foobar_create_thread(void)
{
	int eno;
	pthread_t t;

	eno = pthread_create(&t, NULL, foobar_thread, NULL);
	assert(eno == 0);
}

static void
test_main(void)
{
	const char name[] = "foobarserver";
	int rv;
	mStatus status;

	rv = sethostname(&name[0], sizeof(name) - 1);
	assert(rv == 0);

	status = mDNS_Init(&mDNSStorage, &PlatformStorage, mDNS_Init_NoCache,
	    mDNS_Init_ZeroCacheSize, mDNS_Init_AdvertiseLocalAddresses,
	    mDNS_Init_NoInitCallback, mDNS_Init_NoInitCallbackContext);
	assert(status == mStatus_NoError);

	foobar_create_thread();

	while (1) {
		struct timeval timeout = { .tv_sec = 0x3fffffff, .tv_usec = 0 };
		sigset_t signals;
		mDNSBool got_something;

		mDNSPosixRunEventLoopOnce(&mDNSStorage, &timeout, &signals, &got_something);
	}
}

#define DEFAULT_NETWORK_DHCPCD_ENABLE
#define DEFAULT_NETWORK_DHCPCD_NO_DHCP_DISCOVERY
#define DEFAULT_NETWORK_SHELL

#include <rtems/bsd/test/default-network-init.h>