source: rtems-libbsd/testsuite/foobarserver/test_main.c @ 3951c97

4.115-freebsd-12freebsd-9.3
Last change on this file since 3951c97 was 3951c97, checked in by Sebastian Huber <sebastian.huber@…>, on Jan 22, 2014 at 8:43:51 AM

mDNS: Add basic support

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