Oliver Smith | 3a9f267 | 2019-11-20 10:56:35 +0100 | [diff] [blame] | 1 | /* Copyright 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * |
| 3 | * All Rights Reserved |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <stdbool.h> |
| 22 | #include <stdint.h> |
| 23 | #include <osmocom/core/msgb.h> |
| 24 | #include <osmocom/core/endian.h> |
| 25 | #include <osmocom/mslookup/mdns.h> |
| 26 | |
| 27 | /* RFC 1035 2.3.4 */ |
| 28 | #define OSMO_MDNS_RFC_MAX_NAME_LEN 255 |
| 29 | |
| 30 | /* RFC 1035 3.3 <character-string> */ |
| 31 | #define OSMO_MDNS_RFC_MAX_CHARACTER_STRING_LEN 256 |
| 32 | |
| 33 | enum osmo_mdns_rfc_record_type { |
| 34 | OSMO_MDNS_RFC_RECORD_TYPE_UNKNOWN = 0, |
| 35 | |
| 36 | /* RFC 1035 3.2.2 */ |
| 37 | OSMO_MDNS_RFC_RECORD_TYPE_A = 1, /* IPv4 address */ |
| 38 | OSMO_MDNS_RFC_RECORD_TYPE_TXT = 16, /* Text strings */ |
| 39 | |
| 40 | /* RFC 3596 2.1 */ |
| 41 | OSMO_MDNS_RFC_RECORD_TYPE_AAAA = 28, /* IPv6 address */ |
| 42 | |
| 43 | /* RFC 1035 3.2.3 */ |
| 44 | OSMO_MDNS_RFC_RECORD_TYPE_ALL = 255, /* Request only: ask for all */ |
| 45 | }; |
| 46 | |
| 47 | enum osmo_mdns_rfc_class { |
| 48 | OSMO_MDNS_RFC_CLASS_UNKNOWN = 0, |
| 49 | |
| 50 | /* RFC 1035 3.2.4 */ |
| 51 | OSMO_MDNS_RFC_CLASS_IN = 1, /* Internet and IP networks */ |
| 52 | |
| 53 | /* RFC 1035 3.2.5 */ |
| 54 | OSMO_MDNS_RFC_CLASS_ALL = 255, /* Request only: ask for all */ |
| 55 | }; |
| 56 | |
| 57 | /* RFC 1035 4.1.1 */ |
| 58 | struct osmo_mdns_rfc_header { |
| 59 | #if OSMO_IS_LITTLE_ENDIAN |
| 60 | uint16_t id; |
| 61 | uint8_t rd:1, |
| 62 | tc:1, |
| 63 | aa:1, |
| 64 | opcode:4, |
| 65 | qr:1; /* QR (0: query, 1: response) */ |
| 66 | uint8_t rcode:4, |
| 67 | z:3, |
| 68 | ra:1; |
| 69 | uint16_t qdcount; /* Number of questions */ |
| 70 | uint16_t ancount; /* Number of answers */ |
| 71 | uint16_t nscount; /* Number of authority records */ |
| 72 | uint16_t arcount; /* Number of additional records */ |
| 73 | #elif OSMO_IS_BIG_ENDIAN |
| 74 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianess.py) */ |
| 75 | uint16_t id; |
| 76 | uint8_t qr:1, opcode:4, aa:1, tc:1, rd:1; |
| 77 | uint8_t ra:1, z:3, rcode:4; |
| 78 | uint16_t qdcount; |
| 79 | uint16_t ancount; |
| 80 | uint16_t nscount; |
| 81 | uint16_t arcount; |
| 82 | #endif |
| 83 | } __attribute__ ((packed)); |
| 84 | |
| 85 | /* RFC 1035 4.1.2 */ |
| 86 | struct osmo_mdns_rfc_question { |
| 87 | char *domain; /* Domain to be encoded as qname (e.g. "gsup.hlr.1234567.imsi") */ |
| 88 | enum osmo_mdns_rfc_record_type qtype; |
| 89 | enum osmo_mdns_rfc_class qclass; |
| 90 | }; |
| 91 | |
| 92 | /* RFC 1035 4.1.3 */ |
| 93 | struct osmo_mdns_rfc_record { |
| 94 | char *domain; /* Domain to be encoded as name (e.g. "gsup.hlr.1234567.imsi") */ |
| 95 | enum osmo_mdns_rfc_record_type type; |
| 96 | enum osmo_mdns_rfc_class class; |
| 97 | uint32_t ttl; |
| 98 | uint16_t rdlength; |
| 99 | uint8_t *rdata; |
| 100 | }; |
| 101 | |
| 102 | char *osmo_mdns_rfc_qname_encode(void *ctx, const char *domain); |
| 103 | char *osmo_mdns_rfc_qname_decode(void *ctx, const char *qname, size_t qname_len); |
| 104 | |
| 105 | void osmo_mdns_rfc_header_encode(struct msgb *msg, const struct osmo_mdns_rfc_header *hdr); |
| 106 | int osmo_mdns_rfc_header_decode(const uint8_t *data, size_t data_len, struct osmo_mdns_rfc_header *hdr); |
| 107 | |
| 108 | int osmo_mdns_rfc_question_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_question *qst); |
| 109 | struct osmo_mdns_rfc_question *osmo_mdns_rfc_question_decode(void *ctx, const uint8_t *data, size_t data_len); |
| 110 | |
| 111 | int osmo_mdns_rfc_record_encode(void *ctx, struct msgb *msg, const struct osmo_mdns_rfc_record *rec); |
| 112 | struct osmo_mdns_rfc_record *osmo_mdns_rfc_record_decode(void *ctx, const uint8_t *data, size_t data_len, |
| 113 | size_t *record_len); |