forked from troglobit/mdnsd
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmdnsd.h
235 lines (194 loc) · 6.84 KB
/
mdnsd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#ifndef MDNSD_H_
#define MDNSD_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "mdnsd_config.h"
#include "1035.h"
#if !defined(__bool_true_false_are_defined) && defined(_MSC_VER) && _MSC_VER < 1600
// VS 2008 has no stdbool.h
#define bool short
#define true 1
#define false 0
#else
#include <stdbool.h>
#endif
#include <stdio.h>
#define QCLASS_IN (1)
#if MDNSD_LOGLEVEL <= 100
#define MDNSD_LOG_TRACE(...) do { \
printf("mdnsd: TRACE - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_TRACE(...) do {} while(0)
#endif
#if MDNSD_LOGLEVEL <= 200
#define MDNSD_LOG_DEBUG(...) do { \
printf("mdnsd: DEBUG - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_DEBUG(...) do {} while(0)
#endif
#if MDNSD_LOGLEVEL <= 300
#define MDNSD_LOG_INFO(...) do { \
printf("mdnsd: INFO - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_INFO(...) do {} while(0)
#endif
#if MDNSD_LOGLEVEL <= 400
#define MDNSD_LOG_WARNING(...) do { \
printf("mdnsd: WARN - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_WARNING(...) do {} while(0)
#endif
#if MDNSD_LOGLEVEL <= 500
#define MDNSD_LOG_ERROR(...) do { \
printf("mdnsd: ERROR - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_ERROR(...) do {} while(0)
#endif
#if MDNSD_LOGLEVEL <= 600
#define MDNSD_LOG_FATAL(...) do { \
printf("mdnsd: FATAL - "); printf(__VA_ARGS__); printf("\n"); } while(0)
#else
#define MDNSD_LOG_FATAL(...) do {} while(0)
#endif
/* Main daemon data */
typedef struct mdns_daemon mdns_daemon_t;
/* Record entry */
typedef struct mdns_record mdns_record_t;
/* Callback for received record. Data is passed from the register call */
typedef void (*mdnsd_record_received_callback)(const struct resource* r, void* data);
/* Answer data */
typedef struct mdns_answer {
char *name;
unsigned short type;
unsigned long ttl;
unsigned short rdlen;
unsigned char *rdata;
struct in_addr ip; /* A */
struct in6_addr ip6; /* AAAA */
char *rdname; /* NS/CNAME/PTR/SRV */
struct {
//cppcheck-suppress unusedStructMember
unsigned short int priority, weight, port;
} srv; /* SRV */
} mdns_answer_t;
/**
* Global functions
*/
/**
* Create a new mdns daemon for the given class of names (usually 1) and
* maximum frame size
*/
mdns_daemon_t MDNSD_EXPORT * mdnsd_new(int clazz, int frame);
/**
* Gracefully shutdown the daemon, use mdnsd_out() to get the last
* packets
*/
void MDNSD_EXPORT mdnsd_shutdown(mdns_daemon_t *d);
/**
* Flush all cached records (network/interface changed)
*/
void MDNSD_EXPORT mdnsd_flush(mdns_daemon_t *d);
/**
* Free given mdns_daemon_t *(should have used mdnsd_shutdown() first!)
*/
void MDNSD_EXPORT mdnsd_free(mdns_daemon_t *d);
/**
* Register callback which is called when a record is received. The data parameter is passed to the callback.
* Calling this multiple times overwrites the previous register.
*/
void MDNSD_EXPORT mdnsd_register_receive_callback(mdns_daemon_t *d, mdnsd_record_received_callback cb, void* data);
/**
* I/O functions
*/
/**
* Oncoming message from host (to be cached/processed)
*/
int MDNSD_EXPORT mdnsd_in(mdns_daemon_t *d, struct message *m, struct sockaddr *ip, unsigned short int port);
/**
* Outgoing messge to be delivered to host, returns >0 if one was
* returned and m/ip/port set
*/
int MDNSD_EXPORT mdnsd_out(mdns_daemon_t *d, struct message *m, struct sockaddr *ip, unsigned short int *port);
/**
* returns the max wait-time until mdnsd_out() needs to be called again
*/
struct timeval MDNSD_EXPORT * mdnsd_sleep(mdns_daemon_t *d);
/**
* Q/A functions
*/
/**
* Register a new query
*
* The answer() callback is called whenever one is found/changes/expires
* (immediate or anytime after, mdns_answer_t valid until ->ttl==0)
* either answer returns -1, or another mdnsd_query() with a %NULL answer
* will remove/unregister this query
*/
void MDNSD_EXPORT mdnsd_query(mdns_daemon_t *d, const char *host, int type, int (*answer)(mdns_answer_t *a, void *arg), void *arg);
/**
* Returns the first (if last == NULL) or next answer after last from
* the cache mdns_answer_t only valid until an I/O function is called
*/
mdns_answer_t MDNSD_EXPORT *mdnsd_list(mdns_daemon_t *d, const char *host, int type, mdns_answer_t *last);
/**
* Returns the next record of the given record, i.e. the value of next field.
* @param r the base record
* @return r->next
*/
mdns_record_t MDNSD_EXPORT *mdnsd_record_next(const mdns_record_t* r) ;
/**
* Gets the record data
*/
const mdns_answer_t MDNSD_EXPORT *mdnsd_record_data(const mdns_record_t* r) ;
/**
* Publishing functions
*/
/**
* Create a new unique record
*
* Call mdnsd_list() first to make sure the record is not used yet.
*
* The conflict() callback is called at any point when one is detected
* and unable to recover after the first data is set_*(), any future
* changes effectively expire the old one and attempt to create a new
* unique record
*/
mdns_record_t MDNSD_EXPORT * mdnsd_unique(mdns_daemon_t *d, const char *host, unsigned short int type, unsigned long int ttl, void (*conflict)(char *host, int type, void *arg), void *arg);
/**
* Create a new shared record
*/
mdns_record_t MDNSD_EXPORT * mdnsd_shared(mdns_daemon_t *d, const char *host, unsigned short int type, unsigned long int ttl);
/**
* Get a previously created record based on the host name. NULL if not found. Does not return records for other hosts.
* If multiple records are found, use record->next to iterate over all the results.
*/
mdns_record_t MDNSD_EXPORT * mdnsd_get_published(const mdns_daemon_t *d, const char *host);
/**
* Check if there is already a query for the given host
*/
int MDNSD_EXPORT mdnsd_has_query(const mdns_daemon_t *d, const char *host);
/**
* de-list the given record
*/
void MDNSD_EXPORT mdnsd_done(mdns_daemon_t *d, mdns_record_t *r);
/**
* These all set/update the data for the given record, nothing is
* published until they are called
*/
void MDNSD_EXPORT mdnsd_set_raw(mdns_daemon_t *d, mdns_record_t *r, const char *data, unsigned short int len);
void MDNSD_EXPORT mdnsd_set_host(mdns_daemon_t *d, mdns_record_t *r, const char *name);
void MDNSD_EXPORT mdnsd_set_ip(mdns_daemon_t *d, mdns_record_t *r, struct in_addr ip);
void MDNSD_EXPORT mdnsd_set_srv(mdns_daemon_t *d, mdns_record_t *r, unsigned short int priority, unsigned short int weight, unsigned short int port, const char *name);
/**
* Process input queue and output queue. Should be called at least the time which is returned in nextSleep.
* Returns 0 on success, 1 on read error, 2 on write error
*/
unsigned short int MDNSD_EXPORT mdnsd_step(mdns_daemon_t *d, int mdns_socket, bool processIn, bool processOut, struct timeval *nextSleep);
#ifdef MDNSD_DEBUG_DUMP_PKGS_FILE
void mdnsd_debug_dumpCompleteChunk(mdns_daemon_t *d, const char* data, size_t len);
#endif
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* MDNSD_H_ */