Compare commits
No commits in common. "master" and "TizenRT" have entirely different histories.
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +0,0 @@
|
||||
#syntax: glob
|
||||
*.a
|
||||
*.o
|
44
Kconfig.protocol
Normal file
44
Kconfig.protocol
Normal file
@ -0,0 +1,44 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see kconfig-language at https://www.kernel.org/doc/Documentation/kbuild/kconfig-language.txt
|
||||
#
|
||||
|
||||
config NETUTILS_MDNS
|
||||
bool "Multicast DNS"
|
||||
default n
|
||||
---help---
|
||||
Enables support for Multicast DNS. The multicast Domain Name System (mDNS)
|
||||
resolves host names to IP addresses within small networks that do not include
|
||||
a local name server.
|
||||
|
||||
if NETUTILS_MDNS
|
||||
|
||||
config NETUTILS_MDNS_RESPONDER_SUPPORT
|
||||
bool "mDNS Responder"
|
||||
default n
|
||||
---help---
|
||||
Enable mDNS Responder
|
||||
|
||||
config NETUTILS_MDNS_XMDNS
|
||||
bool "xmDNS for supporting site domain"
|
||||
default n
|
||||
---help---
|
||||
Enable xmDNS for supporting site domain
|
||||
|
||||
if NETUTILS_MDNS_XMDNS
|
||||
config NETUTILS_MDNS_XMDNS_MULTICAST_ADDR
|
||||
string "xmDNS Multicast Address"
|
||||
default "224.0.0.251"
|
||||
---help---
|
||||
xmDNS Multicast Address
|
||||
|
||||
config NETUTILS_MDNS_XMDNS_PORT_NUM
|
||||
int "xmDNS Port Number"
|
||||
default 5353
|
||||
---help---
|
||||
xmDNS Port Number
|
||||
|
||||
endif # NETUTILS_MDNS_XMDNS
|
||||
|
||||
endif # NETUTILS_MDNS
|
||||
|
25
LICENSE.txt
25
LICENSE.txt
@ -1,25 +0,0 @@
|
||||
Copyright (c) 2011, Darell Tan
|
||||
All rights reserved.
|
||||
|
||||
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.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
||||
|
22
Make.defs
Normal file
22
Make.defs
Normal file
@ -0,0 +1,22 @@
|
||||
###########################################################################
|
||||
#
|
||||
# Copyright 2017 Samsung Electronics All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
# either express or implied. See the License for the specific
|
||||
# language governing permissions and limitations under the License.
|
||||
#
|
||||
###########################################################################
|
||||
|
||||
ifeq ($(CONFIG_NETUTILS_MDNS),y)
|
||||
CONFIGURED_EXT += mdns
|
||||
endif
|
||||
|
119
Makefile
119
Makefile
@ -1,38 +1,109 @@
|
||||
###########################################################################
|
||||
#
|
||||
# Makefile for tinysvcmdns
|
||||
# Copyright 2016 Samsung Electronics All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
# either express or implied. See the License for the specific
|
||||
# language governing permissions and limitations under the License.
|
||||
#
|
||||
###########################################################################
|
||||
############################################################################
|
||||
# external/mdns/Makefile
|
||||
#
|
||||
# Copyright (C) 2012 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
# 3. Neither the name NuttX nor the names of its contributors may be
|
||||
# used to endorse or promote products derived from this software
|
||||
# without specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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
|
||||
# COPYRIGHT OWNER 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
CFLAGS += -Wall -pedantic -std=gnu99
|
||||
#CFLAGS += -g
|
||||
CFLAGS += -O2 -DNDEBUG
|
||||
LDLIBS = -lpthread
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
|
||||
ifneq ($(CROSS_COMPILE),)
|
||||
CC = gcc
|
||||
CC := $(CROSS_COMPILE)$(CC)
|
||||
AR := $(CROSS_COMPILE)$(AR)
|
||||
ASRCS =
|
||||
CSRCS = mdns.c mdnsd.c
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifeq ($(CONFIG_WINDOWS_NATIVE),y)
|
||||
BIN = ..\libexternal$(LIBEXT)
|
||||
else
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = ..\\libexternal$(LIBEXT)
|
||||
else
|
||||
BIN = ../libexternal$(LIBEXT)
|
||||
endif
|
||||
endif
|
||||
|
||||
BIN=testmdnsd
|
||||
DEPPATH = --dep-path .
|
||||
|
||||
LIBTINYSVCMDNS_OBJS = mdns.o mdnsd.o
|
||||
# Common build
|
||||
|
||||
.PHONY: all clean
|
||||
VPATH =
|
||||
|
||||
all: $(BIN) libtinysvcmdns.a
|
||||
all: .built
|
||||
.PHONY: depend clean distclean
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
$(call ARCHIVE, $(BIN), $(OBJS))
|
||||
$(Q) touch .built
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
$(Q) $(MKDEP) $(DEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
$(Q) touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
-$(RM) *.o
|
||||
-$(RM) *.bin
|
||||
-$(RM) mdns
|
||||
-$(RM) $(BIN)
|
||||
-$(RM) libtinysvcmdns.a
|
||||
$(call DELFILE, .built)
|
||||
$(call CLEAN)
|
||||
|
||||
mdns: mdns.o
|
||||
distclean: clean
|
||||
$(call DELFILE, Make.dep)
|
||||
$(call DELFILE, .depend)
|
||||
|
||||
mdnsd: mdns.o mdnsd.o
|
||||
|
||||
testmdnsd: testmdnsd.o libtinysvcmdns.a
|
||||
|
||||
libtinysvcmdns.a: $(patsubst %, libtinysvcmdns.a(%), $(LIBTINYSVCMDNS_OBJS))
|
||||
-include Make.dep
|
||||
|
||||
|
@ -1,79 +0,0 @@
|
||||
NOTICE
|
||||
=======
|
||||
|
||||
This project is un-maintained, and has been since 2013.
|
||||
|
||||
There are known vulnerabilities that have been disclosed, and quite possibly
|
||||
others that have yet to be disclosed. Refer to the issue tracker.
|
||||
|
||||
You are advised to NOT use this library for any new projects / products.
|
||||
|
||||
|
||||
tinysvcmdns
|
||||
============
|
||||
tinysvcmdns is a tiny MDNS responder implementation for publishing services.
|
||||
|
||||
This implementation is only concerned with publishing services, without a
|
||||
system-wide daemon like Bonjour or Avahi. Its other goal is to be extremely
|
||||
small, embeddable, and have no external dependencies.
|
||||
|
||||
It only answers queries related to its own hostname (the A record), the
|
||||
service PTRs, and the "_services.dns-sd._udp.local" name, which advertises
|
||||
all services on a particular host.
|
||||
|
||||
Services consist of a single SRV and TXT record.
|
||||
|
||||
Decoding of MDNS packets is only done to retrieve the questions and answer RRs.
|
||||
The purpose for decoding answer RRs is to make sure the service PTR is not
|
||||
sent out if it is already included in the answer RRs.
|
||||
|
||||
It also only utilizes multicast packets, so no "QU" queries are accepted.
|
||||
|
||||
There is no name collision detection, so this means no queries are generated
|
||||
before publishing the services. However compliant responders will avoid using
|
||||
our names, since the implementation will respond to queries that match our
|
||||
name.
|
||||
|
||||
|
||||
TODO
|
||||
-----
|
||||
* better, more stable & complete API
|
||||
* name collision detection
|
||||
|
||||
|
||||
FILES
|
||||
------
|
||||
* mdns.c - provides data structures, parsing & encoding of MDNS packets
|
||||
* mdnsd.c - implements the server socket, communication and thread
|
||||
* testmdnsd.c - an example that creates an instance until terminated
|
||||
|
||||
|
||||
LICENSE
|
||||
--------
|
||||
tinysvcmdns is licensed under the 3-clause ("modified") BSD License.
|
||||
|
||||
Copyright (C) 2011 Darell Tan
|
||||
|
||||
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.
|
||||
3. The name of the author may not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
||||
|
131
mdns.h
131
mdns.h
@ -1,8 +1,25 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright 2016 Samsung Electronics All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
@ -25,6 +42,10 @@
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**
|
||||
* @file mdns.h
|
||||
* @brief functions related to mdns resource record
|
||||
*/
|
||||
|
||||
#ifndef __MDNS_H__
|
||||
#define __MDNS_H__
|
||||
@ -32,6 +53,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock.h>
|
||||
@ -39,25 +61,47 @@
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#define MALLOC_ZERO_STRUCT(x, type) \
|
||||
x = malloc(sizeof(struct type)); \
|
||||
memset(x, 0, sizeof(struct type));
|
||||
#define MDNS_DEBUG_PRINTF 0
|
||||
|
||||
#define DECL_MALLOC_ZERO_STRUCT(x, type) \
|
||||
struct type * MALLOC_ZERO_STRUCT(x, type)
|
||||
|
||||
#ifndef NDEBUG
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...) ((void) 0)
|
||||
#if MDNS_DEBUG_PRINTF==1
|
||||
#define MDNS_RR_DEBUG 0
|
||||
#define MDNS_MEMORY_DEBUG 0
|
||||
#endif
|
||||
|
||||
#if MDNS_DEBUG_PRINTF==1 && MDNS_MEMORY_DEBUG==1
|
||||
void *mdns_malloc(const char *func_name, int line, unsigned int size);
|
||||
void mdns_free(const char *func_name, int line, void *ptr);
|
||||
char *mdns_strdup(const char *func_name, int line, const char *str);
|
||||
void mdns_show_meminfo(void);
|
||||
|
||||
#define MDNS_MALLOC(size) mdns_malloc(__FUNCTION__, __LINE__, size)
|
||||
#define MDNS_FREE(ptr) mdns_free(__FUNCTION__, __LINE__, ptr)
|
||||
#define MDNS_STRDUP(str) mdns_strdup(__FUNCTION__, __LINE__, str)
|
||||
|
||||
#else
|
||||
#define MDNS_MALLOC(size) malloc(size)
|
||||
#define MDNS_FREE(ptr) free(ptr)
|
||||
#define MDNS_STRDUP(size) strdup(size)
|
||||
#endif
|
||||
|
||||
#define MALLOC_ZERO_STRUCT(x, type) \
|
||||
x = MDNS_MALLOC(sizeof(struct type)); \
|
||||
memset(x, 0, sizeof(struct type));
|
||||
|
||||
#define DECL_MALLOC_ZERO_STRUCT(x, type) \
|
||||
struct type * MALLOC_ZERO_STRUCT(x, type)
|
||||
|
||||
#if MDNS_DEBUG_PRINTF==1
|
||||
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define DEBUG_PRINTF(...) ((void) 0)
|
||||
#endif
|
||||
|
||||
struct rr_data_srv {
|
||||
uint16_t priority;
|
||||
uint16_t weight;
|
||||
uint16_t port;
|
||||
uint8_t *target; // host
|
||||
uint8_t *target; // host
|
||||
};
|
||||
|
||||
struct rr_data_txt {
|
||||
@ -66,15 +110,15 @@ struct rr_data_txt {
|
||||
};
|
||||
|
||||
struct rr_data_nsec {
|
||||
//uint8_t *name; // same as record
|
||||
//uint8_t *name; // same as record
|
||||
|
||||
// NSEC occupies the 47th bit, 5 bytes
|
||||
//uint8_t bitmap_len; // = 5
|
||||
uint8_t bitmap[5]; // network order: first byte contains LSB
|
||||
//uint8_t bitmap_len; // = 5
|
||||
uint8_t bitmap[5]; // network order: first byte contains LSB
|
||||
};
|
||||
|
||||
struct rr_data_ptr {
|
||||
uint8_t *name; // NULL if entry is to be used
|
||||
uint8_t *name; // NULL if entry is to be used
|
||||
struct rr_entry *entry;
|
||||
};
|
||||
|
||||
@ -90,13 +134,13 @@ struct rr_entry {
|
||||
uint8_t *name;
|
||||
|
||||
enum rr_type {
|
||||
RR_A = 0x01,
|
||||
RR_PTR = 0x0C,
|
||||
RR_TXT = 0x10,
|
||||
RR_AAAA = 0x1C,
|
||||
RR_SRV = 0x21,
|
||||
RR_NSEC = 0x2F,
|
||||
RR_ANY = 0xFF,
|
||||
RR_A = 0x01,
|
||||
RR_PTR = 0x0C,
|
||||
RR_TXT = 0x10,
|
||||
RR_AAAA = 0x1C,
|
||||
RR_SRV = 0x21,
|
||||
RR_NSEC = 0x2F,
|
||||
RR_ANY = 0xFF,
|
||||
} type;
|
||||
|
||||
uint32_t ttl;
|
||||
@ -112,12 +156,14 @@ struct rr_entry {
|
||||
// RR data
|
||||
union {
|
||||
struct rr_data_nsec NSEC;
|
||||
struct rr_data_srv SRV;
|
||||
struct rr_data_txt TXT;
|
||||
struct rr_data_ptr PTR;
|
||||
struct rr_data_a A;
|
||||
struct rr_data_srv SRV;
|
||||
struct rr_data_txt TXT;
|
||||
struct rr_data_ptr PTR;
|
||||
struct rr_data_a A;
|
||||
struct rr_data_aaaa AAAA;
|
||||
} data;
|
||||
|
||||
time_t update_time;
|
||||
};
|
||||
|
||||
struct rr_list {
|
||||
@ -133,21 +179,21 @@ struct rr_group {
|
||||
struct rr_group *next;
|
||||
};
|
||||
|
||||
#define MDNS_FLAG_RESP (1 << 15) // Query=0 / Response=1
|
||||
#define MDNS_FLAG_AA (1 << 10) // Authoritative
|
||||
#define MDNS_FLAG_TC (1 << 9) // TrunCation
|
||||
#define MDNS_FLAG_RD (1 << 8) // Recursion Desired
|
||||
#define MDNS_FLAG_RA (1 << 7) // Recursion Available
|
||||
#define MDNS_FLAG_Z (1 << 6) // Reserved (zero)
|
||||
#define MDNS_FLAG_RESP (1 << 15) // Query=0 / Response=1
|
||||
#define MDNS_FLAG_AA (1 << 10) // Authoritative
|
||||
#define MDNS_FLAG_TC (1 << 9) // TrunCation
|
||||
#define MDNS_FLAG_RD (1 << 8) // Recursion Desired
|
||||
#define MDNS_FLAG_RA (1 << 7) // Recursion Available
|
||||
#define MDNS_FLAG_Z (1 << 6) // Reserved (zero)
|
||||
|
||||
#define MDNS_FLAG_GET_RCODE(x) (x & 0x0F)
|
||||
#define MDNS_FLAG_GET_OPCODE(x) ((x >> 11) & 0x0F)
|
||||
#define MDNS_FLAG_GET_RCODE(x) (x & 0x0F)
|
||||
#define MDNS_FLAG_GET_OPCODE(x) ((x >> 11) & 0x0F)
|
||||
|
||||
// gets the PTR target name, either from "name" member or "entry" member
|
||||
#define MDNS_RR_GET_PTR_NAME(rr) (rr->data.PTR.name != NULL ? rr->data.PTR.name : rr->data.PTR.entry->name)
|
||||
|
||||
struct mdns_pkt {
|
||||
uint16_t id; // transaction ID
|
||||
uint16_t id; // transaction ID
|
||||
uint16_t flags;
|
||||
uint16_t num_qn;
|
||||
uint16_t num_ans_rr;
|
||||
@ -162,8 +208,9 @@ struct mdns_pkt {
|
||||
|
||||
struct mdns_pkt *mdns_parse_pkt(uint8_t *pkt_buf, size_t pkt_len);
|
||||
|
||||
void mdns_init_query(struct mdns_pkt *pkt, uint16_t id);
|
||||
void mdns_init_reply(struct mdns_pkt *pkt, uint16_t id);
|
||||
size_t mdns_encode_pkt(struct mdns_pkt *answer, uint8_t *pkt_buf, size_t pkt_len);
|
||||
size_t mdns_encode_pkt(struct mdns_pkt *encoded_pkt, uint8_t *pkt_buf, size_t pkt_len);
|
||||
|
||||
void mdns_pkt_destroy(struct mdns_pkt *p);
|
||||
void rr_group_destroy(struct rr_group *group);
|
||||
@ -171,17 +218,20 @@ struct rr_group *rr_group_find(struct rr_group *g, uint8_t *name);
|
||||
struct rr_entry *rr_entry_find(struct rr_list *rr_list, uint8_t *name, uint16_t type);
|
||||
struct rr_entry *rr_entry_match(struct rr_list *rr_list, struct rr_entry *entry);
|
||||
void rr_group_add(struct rr_group **group, struct rr_entry *rr);
|
||||
void rr_group_del(struct rr_group **group, struct rr_entry *rr);
|
||||
|
||||
int rr_list_count(struct rr_list *rr);
|
||||
int rr_list_append(struct rr_list **rr_head, struct rr_entry *rr);
|
||||
struct rr_entry *rr_list_remove(struct rr_list **rr_head, struct rr_entry *rr);
|
||||
void rr_list_destroy(struct rr_list *rr, char destroy_items);
|
||||
|
||||
struct rr_entry *qn_create(uint8_t *name, enum rr_type type, int unicast_query);
|
||||
struct rr_entry *rr_create_ptr(uint8_t *name, struct rr_entry *d_rr);
|
||||
struct rr_entry *rr_create_srv(uint8_t *name, uint16_t port, uint8_t *target);
|
||||
struct rr_entry *rr_create_aaaa(uint8_t *name, struct in6_addr *addr);
|
||||
struct rr_entry *rr_create_a(uint8_t *name, uint32_t addr);
|
||||
struct rr_entry *rr_create(uint8_t *name, enum rr_type type);
|
||||
struct rr_entry *rr_duplicate(struct rr_entry *rr_src);
|
||||
void rr_set_nsec(struct rr_entry *rr_nsec, enum rr_type type);
|
||||
void rr_add_txt(struct rr_entry *rr_txt, const char *txt);
|
||||
|
||||
@ -195,8 +245,9 @@ uint8_t *dup_nlabel(const uint8_t *n);
|
||||
uint8_t *join_nlabel(const uint8_t *n1, const uint8_t *n2);
|
||||
|
||||
// compares 2 names
|
||||
static inline int cmp_nlabel(const uint8_t *L1, const uint8_t *L2) {
|
||||
return strcmp((char *) L1, (char *) L2);
|
||||
static inline int cmp_nlabel(const uint8_t *L1, const uint8_t *L2)
|
||||
{
|
||||
return strcmp((char *)L1, (char *)L2);
|
||||
}
|
||||
|
||||
#endif /*!__MDNS_H__*/
|
||||
#endif /*!__MDNS_H__ */
|
||||
|
144
mdnsd.h
144
mdnsd.h
@ -1,8 +1,25 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright 2016 Samsung Electronics All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
||||
* either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
****************************************************************************/
|
||||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
@ -25,34 +42,125 @@
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
/**
|
||||
* @defgroup MDNS Multicast DNS
|
||||
* Multicast Domain Name System
|
||||
* @ingroup NETWORK
|
||||
* @{
|
||||
*/
|
||||
/**
|
||||
* @file protocols/mdnsd.h
|
||||
* @brief APIs for Multicast DNS
|
||||
*/
|
||||
|
||||
#ifndef __MDNSD_H__
|
||||
#define __MDNSD_H__
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
struct mdnsd;
|
||||
struct mdns_service;
|
||||
#define MAX_NUMBER_OF_SERVICE_DISCOVERY_RESULT 10
|
||||
#define MAX_SERVICE_DISCOVERY_TIME_MS (60 * 1000)
|
||||
|
||||
// starts a MDNS responder instance
|
||||
// returns NULL if unsuccessful
|
||||
struct mdnsd *mdnsd_start();
|
||||
#define MDNS_HOSTNAME_RESOLVER_TIMEOUT_MSEC (3 * 1000)
|
||||
#define MDNS_HOSTNAME_RESOLVER_MAX_TRY_COUNT 5
|
||||
#define MDNS_HOSTNAME_RESOLVER_WAIT_TIME_MSEC 250
|
||||
|
||||
// stops the given MDNS responder instance
|
||||
void mdnsd_stop(struct mdnsd *s);
|
||||
#define MDNS_SERVICE_DISCOVERY_MAX_TRY_COUNT 5
|
||||
#define MDNS_SERVICE_DISCOVERY_WAIT_TIME_MSEC 250
|
||||
|
||||
// sets the hostname for the given MDNS responder instance
|
||||
void mdnsd_set_hostname(struct mdnsd *svr, const char *hostname, uint32_t ip);
|
||||
/**
|
||||
* @brief Structure of MDNS service information
|
||||
*/
|
||||
struct mdns_service_info {
|
||||
char *type;
|
||||
char *instance_name;
|
||||
char *hostname;
|
||||
unsigned int ipaddr; /* ipv4 */
|
||||
unsigned int port;
|
||||
};
|
||||
|
||||
// adds an additional RR
|
||||
void mdnsd_add_rr(struct mdnsd *svr, struct rr_entry *rr);
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN extern "C"
|
||||
extern "C"
|
||||
{
|
||||
#else
|
||||
#define EXTERN extern
|
||||
#endif
|
||||
|
||||
// registers a service with the MDNS responder instance
|
||||
struct mdns_service *mdnsd_register_svc(struct mdnsd *svr, const char *instance_name,
|
||||
const char *type, uint16_t port, const char *hostname, const char *txt[]);
|
||||
#if defined(CONFIG_NETUTILS_MDNS_RESPONDER_SUPPORT)
|
||||
|
||||
// destroys the mdns_service struct returned by mdnsd_register_svc()
|
||||
void mdns_service_destroy(struct mdns_service *srv);
|
||||
/**
|
||||
* @brief mdnsd_start() starts the MDNS daemon.
|
||||
*
|
||||
* @param[in] desired_hostname the desired host name as string type. if same name is in the network, an alternative name will be set as hostname.
|
||||
* @param[in] netif_name network interface name as string type
|
||||
* @return On success, 0 is returned. On failure, a negative value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_start(const char *desired_hostname, const char *netif_name);
|
||||
|
||||
/**
|
||||
* @brief mdnsd_stop() stops the MDNS daemon.
|
||||
*
|
||||
* @return On success, 0 is returned. On failure, a negative value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_stop(void);
|
||||
|
||||
/**
|
||||
* @brief mdnsd_get_hostname() gets the current host name as MDNS type.
|
||||
*
|
||||
* @param[out] hostname_result 32-bytes string buffer for the host name result
|
||||
* @return On success, 0 is returned. On failure, a negative value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_get_hostname(char *hostname_result);
|
||||
|
||||
/**
|
||||
* @brief mdnsd_register_service() register a service to expose through mDNS-SD.
|
||||
*
|
||||
* @param[in] instance instance name to expose
|
||||
* @param[in] type type of service. e.g. _http._tcp.local
|
||||
* @param[in] port port to which the service is reachable
|
||||
* @param[in] hostname if NULL, use the hostname configured when starting the daemon,
|
||||
* or use this parameter otherwise
|
||||
* @param[in] txt text records to add to the service announcement. Can be NULL.
|
||||
* @return On success, 0 is returned. On failure, a negative errno value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_register_service(const char *instance, const char *type,
|
||||
uint16_t port, const char *hostname, const char *txt[]);
|
||||
|
||||
#endif /* CONFIG_NETUTILS_MDNS_RESPONDER_SUPPORT */
|
||||
|
||||
/**
|
||||
* @brief mdnsd_resolve_hostname() gets ip address with the given hostname.
|
||||
*
|
||||
* @param[in] hostname host name as string type
|
||||
* @param[out] ipaddr the pointer of ip address result
|
||||
* @return On success, 0 is returned. On failure, a negative value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_resolve_hostname(char *hostname, int *ipaddr);
|
||||
|
||||
/**
|
||||
* @brief mdnsd_discover_service() discovers services with the given service type string
|
||||
*
|
||||
* @param[in] service_type mdns service type string
|
||||
* @param[in] discover_time_msec time in milliseconds for discovering service
|
||||
* @param[out] service_list the array of service list
|
||||
* @param[out] num_of_services number of services
|
||||
* @return On success, 0 is returned. On failure, a negative value is returned.
|
||||
*
|
||||
*/
|
||||
int mdnsd_discover_service(char *service_type, int discover_time_msec, struct mdns_service_info **service_list, int *num_of_services);
|
||||
|
||||
|
||||
#endif/*!__MDNSD_H__*/
|
||||
#undef EXTERN
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /*!__MDNSD_H__ */
|
||||
|
||||
/** @} */// end of MDNS group
|
||||
|
97
testmdnsd.c
97
testmdnsd.c
@ -1,97 +0,0 @@
|
||||
/*
|
||||
* tinysvcmdns - a tiny MDNS implementation for publishing services
|
||||
* Copyright (C) 2011 Darell Tan
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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.
|
||||
*/
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#include <in6addr.h>
|
||||
#include <ws2tcpip.h>
|
||||
#else
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include "mdns.h"
|
||||
#include "mdnsd.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// create host entries
|
||||
char *hostname = "some-random-host.local";
|
||||
|
||||
struct mdnsd *svr = mdnsd_start();
|
||||
if (svr == NULL) {
|
||||
printf("mdnsd_start() error\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("mdnsd_start OK. press ENTER to add hostname & service\n");
|
||||
getchar();
|
||||
|
||||
mdnsd_set_hostname(svr, hostname, inet_addr("192.168.0.29"));
|
||||
|
||||
struct rr_entry *a2_e = NULL;
|
||||
a2_e = rr_create_a(create_nlabel(hostname), inet_addr("192.168.0.31"));
|
||||
mdnsd_add_rr(svr, a2_e);
|
||||
|
||||
struct rr_entry *aaaa_e = NULL;
|
||||
|
||||
struct addrinfo hints;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET6;
|
||||
hints.ai_flags = AI_NUMERICHOST;
|
||||
struct addrinfo* results;
|
||||
getaddrinfo(
|
||||
"fe80::e2f8:47ff:fe20:28e0",
|
||||
NULL,
|
||||
&hints,
|
||||
&results);
|
||||
struct sockaddr_in6* addr = (struct sockaddr_in6*)results->ai_addr;
|
||||
struct in6_addr v6addr = addr->sin6_addr;
|
||||
freeaddrinfo(results);
|
||||
|
||||
aaaa_e = rr_create_aaaa(create_nlabel(hostname), &v6addr);
|
||||
|
||||
mdnsd_add_rr(svr, aaaa_e);
|
||||
|
||||
const char *txt[] = {
|
||||
"path=/mywebsite",
|
||||
NULL
|
||||
};
|
||||
struct mdns_service *svc = mdnsd_register_svc(svr, "My Website",
|
||||
"_http._tcp.local", 8080, NULL, txt);
|
||||
mdns_service_destroy(svc);
|
||||
|
||||
printf("added service and hostname. press ENTER to exit\n");
|
||||
getchar();
|
||||
|
||||
mdnsd_stop(svr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user