]> git.karo-electronics.de Git - mdnsd.git/blob - mdnsd.h
Add futher files to .gitignore
[mdnsd.git] / mdnsd.h
1 #ifndef _MDNSD_H_
2 #define _MDNSD_H_
3
4 /*
5  * Copyright (C) 2003 Jeremie Miller <jer@jabber.org>
6  * Copyright (c) 2009 Simon Budig <simon@budig.org>
7  * Copyright (C) 2013 Ole Reinhardt <ole.reinhardt@embedded-it.de>
8  *
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the copyright holders nor the names of
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * For additional information see http://www.ethernut.de/
38  */
39
40 /* This code is based on
41  * Based on BSD licensed mdnsd implementation by Jer <jer@jabber.org>
42  * http://dotlocal.org/mdnsd/
43  *
44  * Unfortunately this site is now longer alive. You can still find it at:
45  * http://web.archive.org/web/20080705131510/http://dotlocal.org/mdnsd/
46  *
47  * mdnsd - embeddable Multicast DNS Daemon
48  * =======================================
49  *
50  * "mdnsd" is a very lightweight, simple, portable, and easy to integrate
51  * open source implementation of Multicast DNS (part of Zeroconf, also called
52  * Rendezvous by Apple) for developers. It supports both acting as a Query and
53  * a Responder, allowing any software to participate fully on the .localnetwork
54  * just by including a few files and calling a few functions.  All of the
55  * complexity of handling the Multicast DNS retransmit timing, duplicate
56  * suppression, probing, conflict detection, and other facets of the DNS
57  * protocol is hidden behind a very simple and very easy to use interface,
58  * described in the header file. The single small c source file has almost no
59  * dependencies, and is portable to almost any embedded platform.
60  * Multiple example applications and usages are included in the download,
61  * including a simple persistent query browser and a tool to advertise .local
62  * web sites.
63  *
64  * The code is licensed under both the GPL and BSD licenses, for use in any
65  * free software or commercial application. If there is a licensing need not
66  * covered by either of those, alternative licensing is available upon request.
67  *
68  */
69
70 /*!
71  * \file include/pro/mdnsd.h
72  * \brief Multicast DNS Deamon
73  *
74  * \verbatim
75  *
76  * $Id$
77  *
78  * \endverbatim
79  */
80
81 #include "rfc1035.h"
82 #include <sys/time.h>
83 #include <netinet/in.h>
84 #include <arpa/inet.h>
85
86 #ifdef UNUSED
87 #elif defined(__GNUC__)
88 # define UNUSED(x) UNUSED_ ## x __attribute__((unused))
89 #elif defined(__LCLINT__)
90 # define UNUSED(x) /*@unused@*/ x
91 #else
92 # define UNUSED(x) x
93 #endif
94
95 #define MDNS_PORT         5353
96 #define MDNS_MULTICAST_IP "224.0.0.251"
97
98 /* Size of query/publish hashes */
99 #define SPRIME 108
100
101 /* size of cache hash */
102 #define LPRIME 1009
103
104 /* brute force garbage cleanup frequency, rarely needed (daily default) */
105 #define GC     86400
106
107
108 /*!
109  * \brief MDNS answer data
110  */
111 typedef struct _mdnsda_struct  TMdnsdAnswer;
112 struct _mdnsda_struct
113 {
114     char    *name;
115     uint16_t type;
116     uint32_t ttl;
117     uint16_t rdlen;
118     char    *rdata;
119     struct in_addr ip; // A
120     uint8_t *rdname;   // NS/CNAME/PTR/SRV
121     struct {
122         uint16_t priority;
123         uint16_t weight;
124         uint16_t port;
125     } srv; // SRV
126 };
127
128
129 /*!
130  * \brief MDNS record entry
131  */
132 typedef struct _mdnsdr_struct  TMdnsdRecord;
133 struct _mdnsdr_struct
134 {
135     TMdnsdAnswer rr;
136     int8_t unique; // # of checks performed to ensure
137     int    tries;
138     void  (*conflict)(TMdnsdRecord *record,char *name, int type, void *arg);
139     void   *arg;
140     TMdnsdRecord *next;
141     TMdnsdRecord *list;
142 };
143
144
145 /*!
146  * \brief MDNS query struct
147  */
148 typedef struct _query_struct   TQuery;
149 struct _query_struct
150 {
151     char    *name;
152     int      type;
153     uint32_t nexttry;
154     int      tries;
155     int (*answer)(TMdnsdAnswer *answer, void *arg);
156     void    *arg;
157     TQuery  *next;
158     TQuery  *list;
159 };
160
161
162 /*!
163  * \brief Unicast record data
164  */
165 typedef struct _unicast_struct TUnicast;
166 struct _unicast_struct
167 {
168     int       id;
169     struct in_addr to;
170     uint16_t  port;
171     TMdnsdRecord *record;
172     TUnicast *next;
173 };
174
175
176 /*!
177  * \brief Cached record entry struct
178  */
179 typedef struct _cached_struct  TCached;
180 struct _cached_struct
181 {
182     TMdnsdAnswer rr;
183     TQuery      *query;
184     TCached     *next;
185 };
186
187
188 /*!
189  * \brief Main MDNS deamon data
190  */
191 typedef struct _mdnsd_struct   TMdnsd;
192 struct _mdnsd_struct
193 {
194     int8_t shutdown;
195     uint32_t expireall, checkqlist;
196     struct timeval now;
197     struct timeval sleep;
198     struct timeval pause;
199     struct timeval probe;
200     struct timeval publish;
201     int    class;
202     int    frame;
203     TCached      *cache[LPRIME];
204     TMdnsdRecord *published[SPRIME];
205     TMdnsdRecord *probing;
206     TMdnsdRecord *a_now;
207     TMdnsdRecord *a_pause;
208     TMdnsdRecord *a_publish;
209     TUnicast     *uanswers;
210     TQuery       *queries[SPRIME];
211     TQuery       *qlist;
212 };
213
214 /* Global functions */
215 TMdnsd *MdnsdNew(int class, int frame);
216 void MdnsdShutdown(TMdnsd *mdnsd);
217 void MdnsdFlush(TMdnsd *mdnsd);
218 void MdnsdFree(TMdnsd *mdnsd);
219
220 /* I/O functions */
221 void MdnsdInput(TMdnsd *mdnsd, DNSMESSAGE *m, struct in_addr ip, uint16_t port);
222 int MdnsdOutput(TMdnsd *mdnsd, DNSMESSAGE *m, struct in_addr *ip, uint16_t *port);
223 struct timeval *MdnsdGetMaxSleepTime(TMdnsd *mdnsd);
224
225 /* Qery / Answer functions */
226 void MdnsdQuery(TMdnsd *mdnsd, char *host, int type, int (*answer)(TMdnsdAnswer *a, void *arg), void *arg);
227 TMdnsdAnswer *MdnsdListCachedAnswers(TMdnsd *mdnsd, char *host, int type, TMdnsdAnswer *last);
228
229 /* Publishing functions */
230 TMdnsdRecord *MdnsdAllocUnique(TMdnsd *mdnsd, char *host, int type, uint32_t ttl, void (*conflict)(TMdnsdRecord *record, char *host, int type, void *arg), void *arg);
231 TMdnsdRecord *MdnsdAllocShared(TMdnsd *mdnsd, char *host, int type, uint32_t ttl);
232 void MdnsdDone(TMdnsd *mdnsd, TMdnsdRecord *record);
233
234 /* These all set/update the data for the given record, nothing is published until they are called */
235 void MdnsdSetRaw(TMdnsd *mdnsd, TMdnsdRecord *record, uint8_t *data, int len);
236 void MdnsdSetHost(TMdnsd *mdnsd, TMdnsdRecord *record, char *name);
237 void MdnsdSetIp(TMdnsd *mdnsd, TMdnsdRecord *record, struct in_addr ip);
238 void MdnsdSetSrv(TMdnsd *mdnsd, TMdnsdRecord *record, int priority, int weight, uint16_t port, char *name);
239
240 #endif