File indexing completed on 2024-05-19 04:06:37

0001 /*
0002  * Copyright (C) 2005       Jeremie Miller
0003  * Copyright (C) 2005,2006  Justin Karneges
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining a
0006  * copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sublicense, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be included
0014  * in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0017  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0019  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0020  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0021  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0022  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023  */
0024 
0025 #ifndef JDNS_MDNSD_H
0026 #define JDNS_MDNSD_H
0027 
0028 #include "jdns_p.h"
0029 
0030 struct mytimeval
0031 {
0032     unsigned long int tv_sec;   /* seconds */
0033     unsigned long int tv_usec;  /* microseconds */
0034 };
0035 
0036 typedef struct mdnsd_struct *mdnsd; /* main daemon data */
0037 typedef struct mdnsdr_struct *mdnsdr; /* record entry */
0038 /* answer data */
0039 typedef struct mdnsda_struct
0040 {
0041     unsigned char *name;
0042     unsigned short int type;
0043     unsigned long int ttl;
0044     unsigned long int real_ttl;
0045     unsigned short int rdlen;
0046     unsigned char *rdata;
0047     unsigned long int ip; /* A */
0048     unsigned char *rdname; /* NS/CNAME/PTR/SRV */
0049     struct { unsigned short int priority, weight, port; } srv; /* SRV */
0050 } *mdnsda;
0051 
0052 /*/////////
0053 // Global functions
0054 //
0055 // create a new mdns daemon for the given class of names (usually 1) and maximum frame size */
0056 mdnsd mdnsd_new(int class, int frame, int port, int (*time_now)(mdnsd d, void *arg), int (*rand_int)(mdnsd d, void *arg), void *arg);
0057 /*
0058 // gracefully shutdown the daemon, use mdnsd_out() to get the last packets */
0059 void mdnsd_shutdown(mdnsd d);
0060 /*
0061 // flush all cached records (network/interface changed) */
0062 void mdnsd_flush(mdnsd d);
0063 /*
0064 // free given mdnsd (should have used mdnsd_shutdown() first!) */
0065 void mdnsd_free(mdnsd d);
0066 /*
0067 ///////////
0068 
0069 ///////////
0070 // I/O functions
0071 //
0072 // incoming message from host (to be cached/processed) */
0073 void mdnsd_in(mdnsd d, const jdns_packet_t *m, const jdns_response_t *resp, const jdns_address_t *addr, unsigned short int port);
0074 /*
0075 // outgoing messge to be delivered to host, returns >0 if one was returned and m/ip/port set */
0076 int mdnsd_out(mdnsd d, jdns_packet_t **m, jdns_address_t **addr, unsigned short int *port);
0077 /*
0078 // returns the max wait-time until mdnsd_out() needs to be called again */
0079 struct mytimeval *mdnsd_sleep(mdnsd d);
0080 /*
0081 ////////////
0082 
0083 ///////////
0084 // Q/A functions
0085 // 
0086 // register a new query
0087 //   answer(record, arg) is called whenever one is found/changes/expires (immediate or anytime after, mdnsda valid until ->ttl==0)
0088 //   either answer returns -1, or another mdnsd_query with a NULL answer will remove/unregister this query */
0089 void mdnsd_query(mdnsd d, char *host, int type, int (*answer)(mdnsda a, void *arg), void *arg);
0090 /*
0091 // returns the first (if last == NULL) or next answer after last from the cache
0092 //   mdnsda only valid until an I/O function is called */
0093 mdnsda mdnsd_list(mdnsd d, char *host, int type, mdnsda last);
0094 /*
0095 ///////////
0096 
0097 ///////////
0098 // Publishing functions
0099 //
0100 // create a new unique record (try mdnsda_list first to make sure it's not used)
0101 //   conflict(arg) called at any point when one is detected and unable to recover
0102 //   after the first data is set_*(), any future changes effectively expire the old one and attempt to create a new unique record */
0103 mdnsdr mdnsd_unique(mdnsd d, char *host, int type, long int ttl, void (*pubresult)(int result, char *host, int type, void *arg), void *arg);
0104 /* 
0105 // create a new shared record */
0106 mdnsdr mdnsd_shared(mdnsd d, char *host, int type, long int ttl);
0107 /*
0108 // de-list the given record */
0109 void mdnsd_done(mdnsd d, mdnsdr r);
0110 /*
0111 // these all set/update the data for the given record, nothing is published until they are called */
0112 void mdnsd_set_raw(mdnsd d, mdnsdr r, char *data, int len);
0113 void mdnsd_set_host(mdnsd d, mdnsdr r, char *name);
0114 void mdnsd_set_ip(mdnsd d, mdnsdr r, unsigned long int ip);
0115 void mdnsd_set_srv(mdnsd d, mdnsdr r, int priority, int weight, int port, char *name);
0116 /*
0117 ///////////
0118 */
0119 
0120 #endif