blob: 1cd3e4809a8a9559e5a70d7955962930593bfd34 [file] [log] [blame]
Szymon Starzyckibc849f12013-09-13 15:37:08 -07001/*
2 * Copyright (c) 2009-2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <stdio.h>
33#include <string.h>
34#include <dns_sd.h>
35#include <cutils/properties.h>
36#include <unistd.h>
37
38#include "debug.h"
39#include "network_discovery.h"
40#include "utils.h"
41
42#define MDNS_SERVICE_NAME "mdnsd"
43#define MDNS_SERVICE_STATUS "init.svc.mdnsd"
44#define FASTBOOTD_TYPE "_fastbootd._tcp"
45#define FASTBOOTD_DOMAIN "local."
46#define FASTBOOTD_NAME "fastbootd"
47
48
49static void reg_reply(DNSServiceRef sdref, const DNSServiceFlags flags, DNSServiceErrorType errorCode,
50 const char *name, const char *regtype, const char *domain, void *context)
51{
52 (void)sdref; // Unused
53 (void)flags; // Unused
54 (void)context; // Unused
55 if (errorCode == kDNSServiceErr_ServiceNotRunning) {
56 fprintf(stderr, "Error code %d\n", errorCode);
57 }
58
59
60 printf("Got a reply for service %s.%s%s: ", name, regtype, domain);
61
62 if (errorCode == kDNSServiceErr_NoError)
63 {
64 if (flags & kDNSServiceFlagsAdd)
65 printf("Name now registered and active\n");
66 else
67 printf("Name registration removed\n");
68 if (errorCode == kDNSServiceErr_NameConflict)
69 printf("Name in use, please choose another\n");
70 else
71 printf("Error %d\n", errorCode);
72
73 if (!(flags & kDNSServiceFlagsMoreComing)) fflush(stdout);
74 }
75}
76
77static int register_service() {
78 DNSServiceRef sdref = NULL;
79 const char *domain = FASTBOOTD_DOMAIN;
80 const char *type = FASTBOOTD_TYPE;
81 const char *host = NULL;
82 char name[PROP_VALUE_MAX];
83 uint16_t port = 22;
84 int flags = 0;
85 DNSServiceErrorType result;
86 property_get("ro.serialno", name, "");
87 if (!strcmp(name, "")) {
88 D(ERR, "No property serialno");
89 return -1;
90 }
91
92 result = DNSServiceRegister(&sdref, flags, kDNSServiceInterfaceIndexAny,
93 name, type, domain, host, port,
94 0, NULL, reg_reply, NULL);
95 if (result != kDNSServiceErr_NoError) {
96 D(ERR, "Unable to register service");
97 return -1;
98 }
99 return 0;
100}
101
102
103int network_discovery_init()
104{
105 D(INFO, "Starting discovery");
106 if (service_start(MDNS_SERVICE_NAME)) {
107 D(ERR, "Unable to start discovery");
108 return -1;
109 }
110
111 if (register_service()) {
112 D(ERR, "Unable to register service");
113 return -1;
114 }
115
116 return 0;
117}
118