1 #ifndef foodnshfoo 2 #define foodnshfoo 3 4 /*** 5 This file is part of avahi. 6 7 avahi is free software; you can redistribute it and/or modify it 8 under the terms of the GNU Lesser General Public License as 9 published by the Free Software Foundation; either version 2.1 of the 10 License, or (at your option) any later version. 11 12 avahi is distributed in the hope that it will be useful, but WITHOUT 13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 15 Public License for more details. 16 17 You should have received a copy of the GNU Lesser General Public 18 License along with avahi; if not, write to the Free Software 19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 20 USA. 21 ***/ 22 23 #include "rr.h" 24 #include "hashmap.h" 25 26 #define AVAHI_DNS_PACKET_HEADER_SIZE 12 27 #define AVAHI_DNS_PACKET_EXTRA_SIZE 48 28 #define AVAHI_DNS_LABELS_MAX 127 29 #define AVAHI_DNS_RDATA_MAX 0xFFFF 30 #define AVAHI_DNS_PACKET_SIZE_MAX (AVAHI_DNS_PACKET_HEADER_SIZE + 256 + 2 + 2 + 4 + 2 + AVAHI_DNS_RDATA_MAX) 31 32 typedef struct AvahiDnsPacket { 33 size_t size, rindex, max_size; 34 AvahiHashmap *name_table; /* for name compression */ 35 uint8_t *data; 36 } AvahiDnsPacket; 37 38 #define AVAHI_DNS_PACKET_DATA(p) ((p)->data ? (p)->data : ((uint8_t*) p) + sizeof(AvahiDnsPacket)) 39 40 AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu); 41 AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu); 42 AvahiDnsPacket* avahi_dns_packet_new_response(unsigned mtu, int aa); 43 44 AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa); 45 46 void avahi_dns_packet_free(AvahiDnsPacket *p); 47 void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v); 48 uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx); 49 void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx); 50 51 uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, size_t l); 52 53 void avahi_dns_packet_cleanup_name_table(AvahiDnsPacket *p); 54 55 uint8_t *avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v); 56 uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v); 57 uint8_t *avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name); 58 uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *d, size_t l); 59 uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response); 60 uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl); 61 uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s); 62 63 int avahi_dns_packet_is_query(AvahiDnsPacket *p); 64 int avahi_dns_packet_check_valid(AvahiDnsPacket *p); 65 int avahi_dns_packet_check_valid_multicast(AvahiDnsPacket *p); 66 67 int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v); 68 int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v); 69 int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l); 70 int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void* ret_data, size_t l); 71 AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response); 72 AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush); 73 int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l); 74 75 const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p); 76 77 int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length); 78 79 int avahi_dns_packet_is_empty(AvahiDnsPacket *p); 80 size_t avahi_dns_packet_space(AvahiDnsPacket *p); 81 82 #define AVAHI_DNS_FIELD_ID 0 83 #define AVAHI_DNS_FIELD_FLAGS 1 84 #define AVAHI_DNS_FIELD_QDCOUNT 2 85 #define AVAHI_DNS_FIELD_ANCOUNT 3 86 #define AVAHI_DNS_FIELD_NSCOUNT 4 87 #define AVAHI_DNS_FIELD_ARCOUNT 5 88 89 #define AVAHI_DNS_FLAG_QR (1 << 15) 90 #define AVAHI_DNS_FLAG_OPCODE (15 << 11) 91 #define AVAHI_DNS_FLAG_RCODE (15) 92 #define AVAHI_DNS_FLAG_TC (1 << 9) 93 #define AVAHI_DNS_FLAG_AA (1 << 10) 94 95 #define AVAHI_DNS_FLAGS(qr, opcode, aa, tc, rd, ra, z, ad, cd, rcode) \ 96 (((uint16_t) !!qr << 15) | \ 97 ((uint16_t) (opcode & 15) << 11) | \ 98 ((uint16_t) !!aa << 10) | \ 99 ((uint16_t) !!tc << 9) | \ 100 ((uint16_t) !!rd << 8) | \ 101 ((uint16_t) !!ra << 7) | \ 102 ((uint16_t) !!ad << 5) | \ 103 ((uint16_t) !!cd << 4) | \ 104 ((uint16_t) (rcode & 15))) 105 106 #define AVAHI_MDNS_SUFFIX_LOCAL "local" 107 #define AVAHI_MDNS_SUFFIX_ADDR_IPV4 "254.169.in-addr.arpa" 108 #define AVAHI_MDNS_SUFFIX_ADDR_IPV6 "0.8.e.f.ip6.arpa" 109 110 #endif 111 112