1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief The multicast DNS (mDNS) protocol is tested. |
---|
5 | * |
---|
6 | * To run this test is also required to run foobarclient. |
---|
7 | * A TCP socket (SOCK_STREAM) is open from port FOOBAR_PORT_BEGIN to |
---|
8 | * port FOOBAR_PORT_END - 1 on at a time. |
---|
9 | * The following functions are called: mDNS_RegisterService(), |
---|
10 | * mDNS_DeregisterService() and mDNS_RenameAndReregisterService(). |
---|
11 | */ |
---|
12 | |
---|
13 | /* |
---|
14 | * Copyright (c) 2014 embedded brains GmbH. All rights reserved. |
---|
15 | * |
---|
16 | * embedded brains GmbH |
---|
17 | * Dornierstr. 4 |
---|
18 | * 82178 Puchheim |
---|
19 | * Germany |
---|
20 | * <rtems@embedded-brains.de> |
---|
21 | * |
---|
22 | * Redistribution and use in source and binary forms, with or without |
---|
23 | * modification, are permitted provided that the following conditions |
---|
24 | * are met: |
---|
25 | * 1. Redistributions of source code must retain the above copyright |
---|
26 | * notice, this list of conditions and the following disclaimer. |
---|
27 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
28 | * notice, this list of conditions and the following disclaimer in the |
---|
29 | * documentation and/or other materials provided with the distribution. |
---|
30 | * |
---|
31 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
32 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
33 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
34 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
35 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
36 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
37 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
39 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
40 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
41 | * SUCH DAMAGE. |
---|
42 | */ |
---|
43 | |
---|
44 | #include <mDNSEmbeddedAPI.h> |
---|
45 | #include <mDNSPosix.h> |
---|
46 | |
---|
47 | #include <sys/select.h> |
---|
48 | #include <sys/socket.h> |
---|
49 | |
---|
50 | #include <netinet/in.h> |
---|
51 | |
---|
52 | #include <assert.h> |
---|
53 | #include <errno.h> |
---|
54 | #include <stdbool.h> |
---|
55 | #include <stdio.h> |
---|
56 | #include <stdlib.h> |
---|
57 | #include <string.h> |
---|
58 | #include <unistd.h> |
---|
59 | #include <pthread.h> |
---|
60 | |
---|
61 | #define TEST_NAME "LIBBSD FOOBAR SERVER" |
---|
62 | #define TEST_STATE_USER_INPUT 1 |
---|
63 | |
---|
64 | #define FOOBAR_PORT_BEGIN 10815 |
---|
65 | |
---|
66 | #define FOOBAR_PORT_END 20815 |
---|
67 | |
---|
68 | static mDNS mDNSStorage; |
---|
69 | |
---|
70 | static mDNS_PlatformSupport PlatformStorage; |
---|
71 | |
---|
72 | static void |
---|
73 | foobar_callback(mDNS *m, ServiceRecordSet *srs, mStatus status) |
---|
74 | { |
---|
75 | const mDNSu8 *name = srs->RR_SRV.resrec.name->c; |
---|
76 | |
---|
77 | switch (status) { |
---|
78 | case mStatus_NoError: |
---|
79 | printf("foobar server: name registered: %s\n", name); |
---|
80 | break; |
---|
81 | case mStatus_NameConflict: |
---|
82 | printf("foobar server: name conflict: %s\n", name); |
---|
83 | status = mDNS_RenameAndReregisterService(m, srs, mDNSNULL); |
---|
84 | assert(status == mStatus_NoError); |
---|
85 | break; |
---|
86 | case mStatus_MemFree: |
---|
87 | printf("foobar server: free: %s\n", name); |
---|
88 | free(srs); |
---|
89 | break; |
---|
90 | default: |
---|
91 | printf("foobar server: unexpected status: %s\n", name); |
---|
92 | break; |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | static ServiceRecordSet * |
---|
97 | foobar_register(mDNSu16 port) |
---|
98 | { |
---|
99 | ServiceRecordSet *srs; |
---|
100 | mStatus status; |
---|
101 | domainlabel name; |
---|
102 | domainname type; |
---|
103 | domainname domain; |
---|
104 | |
---|
105 | srs = calloc(1, sizeof(*srs)); |
---|
106 | assert(srs != NULL); |
---|
107 | |
---|
108 | MakeDomainLabelFromLiteralString(&name, "foobar"); |
---|
109 | MakeDomainNameFromDNSNameString(&type, "_foobar._tcp"); |
---|
110 | MakeDomainNameFromDNSNameString(&domain, "local."); |
---|
111 | |
---|
112 | status = mDNS_RegisterService(&mDNSStorage, srs, &name, &type, &domain, |
---|
113 | NULL, mDNSOpaque16fromIntVal(port), NULL, NULL, 0, NULL, 0, |
---|
114 | mDNSInterface_Any, foobar_callback, srs, 0); |
---|
115 | assert(status == mStatus_NoError); |
---|
116 | |
---|
117 | return srs; |
---|
118 | } |
---|
119 | |
---|
120 | static void |
---|
121 | foobar_deregister(ServiceRecordSet *srs) |
---|
122 | { |
---|
123 | mStatus status; |
---|
124 | |
---|
125 | status = mDNS_DeregisterService(&mDNSStorage, srs); |
---|
126 | assert(status == mStatus_NoError); |
---|
127 | } |
---|
128 | |
---|
129 | static void * |
---|
130 | foobar_thread(void *arg) |
---|
131 | { |
---|
132 | static const char foobar[] = "FooBar!"; |
---|
133 | |
---|
134 | mDNSu16 port = FOOBAR_PORT_BEGIN; |
---|
135 | |
---|
136 | while (1) { |
---|
137 | ServiceRecordSet *srs; |
---|
138 | struct sockaddr_in addr; |
---|
139 | struct sockaddr_in addr2; |
---|
140 | socklen_t addr2_len; |
---|
141 | int sd; |
---|
142 | int sd2; |
---|
143 | int rv; |
---|
144 | ssize_t n; |
---|
145 | |
---|
146 | sd = socket(PF_INET, SOCK_STREAM, 0); |
---|
147 | assert(sd >= 0); |
---|
148 | |
---|
149 | memset(&addr, 0, sizeof(addr)); |
---|
150 | addr.sin_family = AF_INET; |
---|
151 | addr.sin_port = htons(port); |
---|
152 | addr.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
153 | |
---|
154 | rv = bind(sd, (const struct sockaddr *) &addr, sizeof(addr)); |
---|
155 | assert(rv == 0); |
---|
156 | |
---|
157 | rv = listen(sd, 0); |
---|
158 | assert(rv == 0); |
---|
159 | |
---|
160 | srs = foobar_register(port); |
---|
161 | |
---|
162 | addr2_len = sizeof(addr2); |
---|
163 | sd2 = accept(sd, (struct sockaddr *) &addr2, &addr2_len); |
---|
164 | assert(sd2 >= 0); |
---|
165 | |
---|
166 | n = write(sd2, &foobar[0], sizeof(foobar)); |
---|
167 | assert(n == (ssize_t) sizeof(foobar)); |
---|
168 | |
---|
169 | foobar_deregister(srs); |
---|
170 | |
---|
171 | rv = close(sd2); |
---|
172 | assert(rv == 0); |
---|
173 | |
---|
174 | rv = close(sd); |
---|
175 | assert(rv == 0); |
---|
176 | |
---|
177 | if (port < FOOBAR_PORT_END) { |
---|
178 | ++port; |
---|
179 | } else { |
---|
180 | port = FOOBAR_PORT_BEGIN; |
---|
181 | } |
---|
182 | } |
---|
183 | |
---|
184 | return NULL; |
---|
185 | } |
---|
186 | |
---|
187 | static void |
---|
188 | foobar_create_thread(void) |
---|
189 | { |
---|
190 | int eno; |
---|
191 | pthread_t t; |
---|
192 | |
---|
193 | eno = pthread_create(&t, NULL, foobar_thread, NULL); |
---|
194 | assert(eno == 0); |
---|
195 | } |
---|
196 | |
---|
197 | static void |
---|
198 | test_main(void) |
---|
199 | { |
---|
200 | const char name[] = "foobarserver"; |
---|
201 | int rv; |
---|
202 | mStatus status; |
---|
203 | |
---|
204 | rv = sethostname(&name[0], sizeof(name) - 1); |
---|
205 | assert(rv == 0); |
---|
206 | |
---|
207 | status = mDNS_Init(&mDNSStorage, &PlatformStorage, mDNS_Init_NoCache, |
---|
208 | mDNS_Init_ZeroCacheSize, mDNS_Init_AdvertiseLocalAddresses, |
---|
209 | mDNS_Init_NoInitCallback, mDNS_Init_NoInitCallbackContext); |
---|
210 | assert(status == mStatus_NoError); |
---|
211 | |
---|
212 | foobar_create_thread(); |
---|
213 | |
---|
214 | while (1) { |
---|
215 | struct timeval timeout = { .tv_sec = 0x3fffffff, .tv_usec = 0 }; |
---|
216 | sigset_t signals; |
---|
217 | mDNSBool got_something; |
---|
218 | |
---|
219 | mDNSPosixRunEventLoopOnce(&mDNSStorage, &timeout, &signals, &got_something); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | #define DEFAULT_NETWORK_DHCPCD_ENABLE |
---|
224 | #define DEFAULT_NETWORK_DHCPCD_NO_DHCP_DISCOVERY |
---|
225 | #define DEFAULT_NETWORK_SHELL |
---|
226 | |
---|
227 | #include <rtems/bsd/test/default-network-init.h> |
---|