]>
git.ipfire.org Git - thirdparty/shairport-sync.git/blob - tinysvcmdns.h
1 // This file is the concatenation of mdnsd.h and mdns.h
2 // from tinysvcmdns with minor modifications
3 // The code was taken from https://bitbucket.org/geekman/tinysvcmdns at revision e34b562
6 * tinysvcmdns - a tiny MDNS implementation for publishing services
7 * Copyright (C) 2011 Darell Tan
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #ifndef _TINYSVCMDNS_H
35 //******************************************************//
37 //******************************************************//
45 #include <arpa/inet.h>
48 #define MALLOC_ZERO_STRUCT(x, type) \
49 x = malloc(sizeof(struct type)); \
51 memset(x, 0, sizeof(struct type));
53 #define DECL_MALLOC_ZERO_STRUCT(x, type) struct type *MALLOC_ZERO_STRUCT(x, type)
59 uint8_t *target
; // host
63 struct rr_data_txt
*next
;
68 // uint8_t *name; // same as record
70 // NSEC occupies the 47th bit, 5 bytes
71 // uint8_t bitmap_len; // = 5
72 uint8_t bitmap
[5]; // network order: first byte contains LSB
76 uint8_t *name
; // NULL if entry is to be used
77 struct rr_entry
*entry
;
85 struct in6_addr
*addr
;
103 // for use in Questions only
106 // for use in Answers only
113 struct rr_data_nsec NSEC
;
114 struct rr_data_srv SRV
;
115 struct rr_data_txt TXT
;
116 struct rr_data_ptr PTR
;
118 struct rr_data_aaaa AAAA
;
124 struct rr_list
*next
;
132 struct rr_group
*next
;
135 #define MDNS_FLAG_RESP (1 << 15) // Query=0 / Response=1
136 #define MDNS_FLAG_AA (1 << 10) // Authoritative
137 #define MDNS_FLAG_TC (1 << 9) // TrunCation
138 #define MDNS_FLAG_RD (1 << 8) // Recursion Desired
139 #define MDNS_FLAG_RA (1 << 7) // Recursion Available
140 #define MDNS_FLAG_Z (1 << 6) // Reserved (zero)
142 #define MDNS_FLAG_GET_RCODE(x) (x & 0x0F)
143 #define MDNS_FLAG_GET_OPCODE(x) ((x >> 11) & 0x0F)
145 // gets the PTR target name, either from "name" member or "entry" member
146 #define MDNS_RR_GET_PTR_NAME(rr) \
147 (rr->data.PTR.name != NULL ? rr->data.PTR.name : rr->data.PTR.entry->name)
150 uint16_t id
; // transaction ID
154 uint16_t num_auth_rr
;
157 struct rr_list
*rr_qn
; // questions
158 struct rr_list
*rr_ans
; // answer RRs
159 struct rr_list
*rr_auth
; // authority RRs
160 struct rr_list
*rr_add
; // additional RRs
163 struct mdns_pkt
*mdns_parse_pkt(uint8_t *pkt_buf
, size_t pkt_len
);
165 void mdns_init_reply(struct mdns_pkt
*pkt
, uint16_t id
);
166 size_t mdns_encode_pkt(struct mdns_pkt
*answer
, uint8_t *pkt_buf
, size_t pkt_len
);
168 void mdns_pkt_destroy(struct mdns_pkt
*p
);
169 void rr_group_destroy(struct rr_group
*group
);
170 struct rr_group
*rr_group_find(struct rr_group
*g
, uint8_t *name
);
171 struct rr_entry
*rr_entry_find(struct rr_list
*rr_list
, uint8_t *name
, uint16_t type
);
172 struct rr_entry
*rr_entry_match(struct rr_list
*rr_list
, struct rr_entry
*entry
);
173 void rr_group_add(struct rr_group
**group
, struct rr_entry
*rr
);
175 int rr_list_count(struct rr_list
*rr
);
176 int rr_list_append(struct rr_list
**rr_head
, struct rr_entry
*rr
);
177 struct rr_entry
*rr_list_remove(struct rr_list
**rr_head
, struct rr_entry
*rr
);
178 void rr_list_destroy(struct rr_list
*rr
, char destroy_items
);
180 struct rr_entry
*rr_create_ptr(uint8_t *name
, struct rr_entry
*d_rr
);
181 struct rr_entry
*rr_create_srv(uint8_t *name
, uint16_t port
, uint8_t *target
);
182 struct rr_entry
*rr_create_aaaa(uint8_t *name
, struct in6_addr
*addr
);
183 struct rr_entry
*rr_create_a(uint8_t *name
, uint32_t addr
);
184 struct rr_entry
*rr_create(uint8_t *name
, enum rr_type type
);
185 void rr_set_nsec(struct rr_entry
*rr_nsec
, enum rr_type type
);
186 void rr_add_txt(struct rr_entry
*rr_txt
, const char *txt
);
188 const char *rr_get_type_name(enum rr_type type
);
190 uint8_t *create_label(const char *txt
);
191 uint8_t *create_nlabel(const char *name
);
192 char *nlabel_to_str(const uint8_t *name
);
193 uint8_t *dup_label(const uint8_t *label
);
194 uint8_t *dup_nlabel(const uint8_t *n
);
195 uint8_t *join_nlabel(const uint8_t *n1
, const uint8_t *n2
);
198 static inline int cmp_nlabel(const uint8_t *L1
, const uint8_t *L2
) {
199 return strcmp((char *)L1
, (char *)L2
);
202 //******************************************************//
204 //******************************************************//
209 // starts a MDNS responder instance
210 // returns NULL if unsuccessful
211 struct mdnsd
*mdnsd_start();
213 // stops the given MDNS responder instance
214 void mdnsd_stop(struct mdnsd
*s
);
216 // sets the hostname for the given MDNS responder instance
217 void mdnsd_set_hostname(struct mdnsd
*svr
, const char *hostname
, uint32_t ip
);
219 // sets the hostname for the given MDNS responder instance, with an ipv6 address
220 void mdnsd_set_hostname_v6(struct mdnsd
*svr
, const char *hostname
, struct in6_addr
*addr
);
222 // adds an additional RR
223 void mdnsd_add_rr(struct mdnsd
*svr
, struct rr_entry
*rr
);
225 // registers a service with the MDNS responder instance
226 struct mdns_service
*mdnsd_register_svc(struct mdnsd
*svr
, const char *instance_name
,
227 const char *type
, uint16_t port
, const char *hostname
,
230 // destroys the mdns_service struct returned by mdnsd_register_svc()
231 void mdns_service_destroy(struct mdns_service
*srv
);
233 #endif // _TINYSVCMDNS_H