_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

Classes | Macros | Typedefs | Enumerations | Functions | Variables
mdns.h File Reference
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>

Go to the source code of this file.

Classes

struct  mdns_string_t
 
struct  mdns_string_pair_t
 
struct  mdns_record_srv_t
 
struct  mdns_record_txt_t
 
struct  mdns_header_t
 

Macros

#define MDNS_INVALID_POS   ((size_t)-1)
 
#define MDNS_STRING_CONST(s)   (s), (sizeof((s)) - 1)
 
#define MDNS_STRING_FORMAT(s)   (int)((s).length), s.str
 
#define MDNS_POINTER_OFFSET(p, ofs)   ((void*)((char*)(p) + (ptrdiff_t)(ofs)))
 
#define MDNS_POINTER_OFFSET_CONST(p, ofs)   ((const void*)((const char*)(p) + (ptrdiff_t)(ofs)))
 
#define MDNS_POINTER_DIFF(a, b)   ((size_t)((const char*)(a) - (const char*)(b)))
 
#define MDNS_PORT   5353
 
#define MDNS_UNICAST_RESPONSE   0x8000U
 
#define MDNS_CACHE_FLUSH   0x8000U
 

Typedefs

typedef enum mdns_record_type mdns_record_type_t
 
typedef enum mdns_entry_type mdns_entry_type_t
 
typedef enum mdns_class mdns_class_t
 
typedef int(* mdns_record_callback_fn) (int sock, const struct sockaddr *from, size_t addrlen, mdns_entry_type_t entry, uint16_t query_id, uint16_t rtype, uint16_t rclass, uint32_t ttl, const void *data, size_t size, size_t name_offset, size_t name_length, size_t record_offset, size_t record_length, void *user_data)
 
typedef struct mdns_string_t mdns_string_t
 
typedef struct mdns_string_pair_t mdns_string_pair_t
 
typedef struct mdns_record_srv_t mdns_record_srv_t
 
typedef struct mdns_record_txt_t mdns_record_txt_t
 
typedef size_t mdns_size_t
 

Enumerations

enum  mdns_record_type {
  MDNS_RECORDTYPE_IGNORE = 0, MDNS_RECORDTYPE_A = 1, MDNS_RECORDTYPE_PTR = 12, MDNS_RECORDTYPE_TXT = 16,
  MDNS_RECORDTYPE_AAAA = 28, MDNS_RECORDTYPE_SRV = 33
}
 
enum  mdns_entry_type { MDNS_ENTRYTYPE_QUESTION = 0, MDNS_ENTRYTYPE_ANSWER = 1, MDNS_ENTRYTYPE_AUTHORITY = 2, MDNS_ENTRYTYPE_ADDITIONAL = 3 }
 
enum  mdns_class { MDNS_CLASS_IN = 1 }
 

Functions

static int mdns_socket_open_ipv4 (struct sockaddr_in *saddr)
 Open and setup a IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,. More...
 
static int mdns_socket_setup_ipv4 (int sock, struct sockaddr_in *saddr)
 Setup an already opened IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,. More...
 
static int mdns_socket_open_ipv6 (struct sockaddr_in6 *saddr)
 Open and setup a IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,. More...
 
static int mdns_socket_setup_ipv6 (int sock, struct sockaddr_in6 *saddr)
 Setup an already opened IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,. More...
 
static void mdns_socket_close (int sock)
 Close a socket opened with mdns_socket_open_ipv4 and mdns_socket_open_ipv6. More...
 
static size_t mdns_socket_listen (int sock, void *buffer, size_t capacity, mdns_record_callback_fn callback, void *user_data)
 Listen for incoming multicast DNS-SD and mDNS query requests. The socket should have been. More...
 
static int mdns_discovery_send (int sock)
 Send a multicast DNS-SD reqeuest on the given socket to discover available services. Returns. More...
 
static size_t mdns_discovery_recv (int sock, void *buffer, size_t capacity, mdns_record_callback_fn callback, void *user_data)
 Recieve unicast responses to a DNS-SD sent with mdns_discovery_send. Any data will be piped to. More...
 
static int mdns_discovery_answer (int sock, const void *address, size_t address_size, void *buffer, size_t capacity, const char *record, size_t length)
 Send a unicast DNS-SD answer with a single record to the given address. Returns 0 if success,. More...
 
static int mdns_query_send (int sock, mdns_record_type_t type, const char *name, size_t length, void *buffer, size_t capacity, uint16_t query_id)
 Send a multicast mDNS query on the given socket for the given service name. The supplied buffer. More...
 
static size_t mdns_query_recv (int sock, void *buffer, size_t capacity, mdns_record_callback_fn callback, void *user_data, int query_id)
 Receive unicast responses to a mDNS query sent with mdns_discovery_recv, optionally filtering. More...
 
static int mdns_query_answer (int sock, const void *address, size_t address_size, void *buffer, size_t capacity, uint16_t query_id, const char *service, size_t service_length, const char *hostname, size_t hostname_length, uint32_t ipv4, const uint8_t *ipv6, uint16_t port, const char *txt, size_t txt_length)
 Send a unicast or multicast mDNS query answer with a single record to the given address. The. More...
 
static mdns_string_t mdns_string_extract (const void *buffer, size_t size, size_t *offset, char *str, size_t capacity)
 
static int mdns_string_skip (const void *buffer, size_t size, size_t *offset)
 
static int mdns_string_equal (const void *buffer_lhs, size_t size_lhs, size_t *ofs_lhs, const void *buffer_rhs, size_t size_rhs, size_t *ofs_rhs)
 
static voidmdns_string_make (void *data, size_t capacity, const char *name, size_t length)
 
static voidmdns_string_make_ref (void *data, size_t capacity, size_t ref_offset)
 
static voidmdns_string_make_with_ref (void *data, size_t capacity, const char *name, size_t length, size_t ref_offset)
 
static mdns_string_t mdns_record_parse_ptr (const void *buffer, size_t size, size_t offset, size_t length, char *strbuffer, size_t capacity)
 
static mdns_record_srv_t mdns_record_parse_srv (const void *buffer, size_t size, size_t offset, size_t length, char *strbuffer, size_t capacity)
 
static struct sockaddr_in * mdns_record_parse_a (const void *buffer, size_t size, size_t offset, size_t length, struct sockaddr_in *addr)
 
static struct sockaddr_in6 * mdns_record_parse_aaaa (const void *buffer, size_t size, size_t offset, size_t length, struct sockaddr_in6 *addr)
 
static size_t mdns_record_parse_txt (const void *buffer, size_t size, size_t offset, size_t length, mdns_record_txt_t *records, size_t capacity)
 
static int mdns_is_string_ref (uint8_t val)
 
static mdns_string_pair_t mdns_get_next_substring (const void *rawdata, size_t size, size_t offset)
 
static size_t mdns_string_find (const char *str, size_t length, char c, size_t offset)
 
static size_t mdns_records_parse (int sock, const struct sockaddr *from, size_t addrlen, const void *buffer, size_t size, size_t *offset, mdns_entry_type_t type, uint16_t query_id, size_t records, mdns_record_callback_fn callback, void *user_data)
 
static int mdns_unicast_send (int sock, const void *address, size_t address_size, const void *buffer, size_t size)
 
static int mdns_multicast_send (int sock, const void *buffer, size_t size)
 

Variables

static const uint8_t mdns_services_query []
 

Macro Definition Documentation

◆ MDNS_CACHE_FLUSH

#define MDNS_CACHE_FLUSH   0x8000U

◆ MDNS_INVALID_POS

#define MDNS_INVALID_POS   ((size_t)-1)

◆ MDNS_POINTER_DIFF

#define MDNS_POINTER_DIFF (   a,
 
)    ((size_t)((const char*)(a) - (const char*)(b)))

◆ MDNS_POINTER_OFFSET

#define MDNS_POINTER_OFFSET (   p,
  ofs 
)    ((void*)((char*)(p) + (ptrdiff_t)(ofs)))

◆ MDNS_POINTER_OFFSET_CONST

#define MDNS_POINTER_OFFSET_CONST (   p,
  ofs 
)    ((const void*)((const char*)(p) + (ptrdiff_t)(ofs)))

◆ MDNS_PORT

#define MDNS_PORT   5353

◆ MDNS_STRING_CONST

#define MDNS_STRING_CONST (   s)    (s), (sizeof((s)) - 1)

◆ MDNS_STRING_FORMAT

#define MDNS_STRING_FORMAT (   s)    (int)((s).length), s.str

◆ MDNS_UNICAST_RESPONSE

#define MDNS_UNICAST_RESPONSE   0x8000U

Typedef Documentation

◆ mdns_class_t

typedef enum mdns_class mdns_class_t

◆ mdns_entry_type_t

◆ mdns_record_callback_fn

typedef int(* mdns_record_callback_fn) (int sock, const struct sockaddr *from, size_t addrlen, mdns_entry_type_t entry, uint16_t query_id, uint16_t rtype, uint16_t rclass, uint32_t ttl, const void *data, size_t size, size_t name_offset, size_t name_length, size_t record_offset, size_t record_length, void *user_data)

◆ mdns_record_srv_t

◆ mdns_record_txt_t

◆ mdns_record_type_t

◆ mdns_size_t

typedef size_t mdns_size_t

◆ mdns_string_pair_t

◆ mdns_string_t

typedef struct mdns_string_t mdns_string_t

Enumeration Type Documentation

◆ mdns_class

enum mdns_class
Enumerator
MDNS_CLASS_IN 

◆ mdns_entry_type

Enumerator
MDNS_ENTRYTYPE_QUESTION 
MDNS_ENTRYTYPE_ANSWER 
MDNS_ENTRYTYPE_AUTHORITY 
MDNS_ENTRYTYPE_ADDITIONAL 

◆ mdns_record_type

Enumerator
MDNS_RECORDTYPE_IGNORE 
MDNS_RECORDTYPE_A 
MDNS_RECORDTYPE_PTR 
MDNS_RECORDTYPE_TXT 
MDNS_RECORDTYPE_AAAA 
MDNS_RECORDTYPE_SRV 

Function Documentation

◆ mdns_discovery_answer()

static int mdns_discovery_answer ( int  sock,
const void address,
size_t  address_size,
void buffer,
size_t  capacity,
const char *  record,
size_t  length 
)
static

Send a unicast DNS-SD answer with a single record to the given address. Returns 0 if success,.

◆ mdns_discovery_recv()

static size_t mdns_discovery_recv ( int  sock,
void buffer,
size_t  capacity,
mdns_record_callback_fn  callback,
void user_data 
)
static

Recieve unicast responses to a DNS-SD sent with mdns_discovery_send. Any data will be piped to.

◆ mdns_discovery_send()

static int mdns_discovery_send ( int  sock)
static

Send a multicast DNS-SD reqeuest on the given socket to discover available services. Returns.

◆ mdns_get_next_substring()

static mdns_string_pair_t mdns_get_next_substring ( const void rawdata,
size_t  size,
size_t  offset 
)
static

◆ mdns_is_string_ref()

static int mdns_is_string_ref ( uint8_t  val)
static

◆ mdns_multicast_send()

static int mdns_multicast_send ( int  sock,
const void buffer,
size_t  size 
)
static

◆ mdns_query_answer()

static int mdns_query_answer ( int  sock,
const void address,
size_t  address_size,
void buffer,
size_t  capacity,
uint16_t  query_id,
const char *  service,
size_t  service_length,
const char *  hostname,
size_t  hostname_length,
uint32_t  ipv4,
const uint8_t *  ipv6,
uint16_t  port,
const char *  txt,
size_t  txt_length 
)
static

Send a unicast or multicast mDNS query answer with a single record to the given address. The.

◆ mdns_query_recv()

static size_t mdns_query_recv ( int  sock,
void buffer,
size_t  capacity,
mdns_record_callback_fn  callback,
void user_data,
int  query_id 
)
static

Receive unicast responses to a mDNS query sent with mdns_discovery_recv, optionally filtering.

◆ mdns_query_send()

static int mdns_query_send ( int  sock,
mdns_record_type_t  type,
const char *  name,
size_t  length,
void buffer,
size_t  capacity,
uint16_t  query_id 
)
static

Send a multicast mDNS query on the given socket for the given service name. The supplied buffer.

Optional unicast response based on local port, class IN

◆ mdns_record_parse_a()

static struct sockaddr_in * mdns_record_parse_a ( const void buffer,
size_t  size,
size_t  offset,
size_t  length,
struct sockaddr_in *  addr 
)
static

◆ mdns_record_parse_aaaa()

static struct sockaddr_in6 * mdns_record_parse_aaaa ( const void buffer,
size_t  size,
size_t  offset,
size_t  length,
struct sockaddr_in6 *  addr 
)
static

◆ mdns_record_parse_ptr()

static mdns_string_t mdns_record_parse_ptr ( const void buffer,
size_t  size,
size_t  offset,
size_t  length,
char *  strbuffer,
size_t  capacity 
)
static

◆ mdns_record_parse_srv()

static mdns_record_srv_t mdns_record_parse_srv ( const void buffer,
size_t  size,
size_t  offset,
size_t  length,
char *  strbuffer,
size_t  capacity 
)
static

◆ mdns_record_parse_txt()

static size_t mdns_record_parse_txt ( const void buffer,
size_t  size,
size_t  offset,
size_t  length,
mdns_record_txt_t records,
size_t  capacity 
)
static

◆ mdns_records_parse()

static size_t mdns_records_parse ( int  sock,
const struct sockaddr *  from,
size_t  addrlen,
const void buffer,
size_t  size,
size_t *  offset,
mdns_entry_type_t  type,
uint16_t  query_id,
size_t  records,
mdns_record_callback_fn  callback,
void user_data 
)
static

◆ mdns_socket_close()

static void mdns_socket_close ( int  sock)
static

Close a socket opened with mdns_socket_open_ipv4 and mdns_socket_open_ipv6.

◆ mdns_socket_listen()

static size_t mdns_socket_listen ( int  sock,
void buffer,
size_t  capacity,
mdns_record_callback_fn  callback,
void user_data 
)
static

Listen for incoming multicast DNS-SD and mDNS query requests. The socket should have been.

◆ mdns_socket_open_ipv4()

static int mdns_socket_open_ipv4 ( struct sockaddr_in *  saddr)
static

Open and setup a IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,.

◆ mdns_socket_open_ipv6()

static int mdns_socket_open_ipv6 ( struct sockaddr_in6 *  saddr)
static

Open and setup a IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,.

◆ mdns_socket_setup_ipv4()

static int mdns_socket_setup_ipv4 ( int  sock,
struct sockaddr_in *  saddr 
)
static

Setup an already opened IPv4 socket for mDNS/DNS-SD. To bind the socket to a specific interface,.

◆ mdns_socket_setup_ipv6()

static int mdns_socket_setup_ipv6 ( int  sock,
struct sockaddr_in6 *  saddr 
)
static

Setup an already opened IPv6 socket for mDNS/DNS-SD. To bind the socket to a specific interface,.

◆ mdns_string_equal()

static int mdns_string_equal ( const void buffer_lhs,
size_t  size_lhs,
size_t *  ofs_lhs,
const void buffer_rhs,
size_t  size_rhs,
size_t *  ofs_rhs 
)
static

◆ mdns_string_extract()

static mdns_string_t mdns_string_extract ( const void buffer,
size_t  size,
size_t *  offset,
char *  str,
size_t  capacity 
)
static

◆ mdns_string_find()

static size_t mdns_string_find ( const char *  str,
size_t  length,
char  c,
size_t  offset 
)
static

◆ mdns_string_make()

static void * mdns_string_make ( void data,
size_t  capacity,
const char *  name,
size_t  length 
)
static

◆ mdns_string_make_ref()

static void * mdns_string_make_ref ( void data,
size_t  capacity,
size_t  ref_offset 
)
static

◆ mdns_string_make_with_ref()

static void * mdns_string_make_with_ref ( void data,
size_t  capacity,
const char *  name,
size_t  length,
size_t  ref_offset 
)
static

◆ mdns_string_skip()

static int mdns_string_skip ( const void buffer,
size_t  size,
size_t *  offset 
)
static

◆ mdns_unicast_send()

static int mdns_unicast_send ( int  sock,
const void address,
size_t  address_size,
const void buffer,
size_t  size 
)
static

Variable Documentation

◆ mdns_services_query

const uint8_t mdns_services_query[]
static
Initial value:
= {
0x00, 0x00,
0x00, 0x00,
0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', 0x07, '_', 'd', 'n', 's', '-', 's', 'd',
0x04, '_', 'u', 'd', 'p', 0x05, 'l', 'o', 'c', 'a', 'l', 0x00,
MDNS_RECORDTYPE_PTR
@ MDNS_RECORDTYPE_PTR
Definition: mdns.h:55
MDNS_CLASS_IN
@ MDNS_CLASS_IN
Definition: mdns.h:71
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.