]> git.kernelconcepts.de Git - mdnsd.git/blob - 1035.h
Applied Simons initial checkin, first slightly modifications
[mdnsd.git] / 1035.h
1 #ifndef _1035_h
2 #define _1035_h
3
4 // be familiar with rfc1035 if you want to know what all the variable names mean, but this hides most of the dirty work
5 // all of this code depends on the buffer space a packet is in being 4096 and zero'd before the packet is copied in
6 // also conveniently decodes srv rr's, type 33, see rfc2782
7
8 // should be reasonably large, for udp
9 #define MAX_PACKET_LEN 4000
10
11 struct question
12 {
13     unsigned char *name;
14     unsigned short int type, class;
15 };
16
17 #define QTYPE_A 1
18 #define QTYPE_NS 2
19 #define QTYPE_CNAME 5
20 #define QTYPE_PTR 12
21 #define QTYPE_SRV 33
22
23 struct resource
24 {
25     unsigned char *name;
26     unsigned short int type, class;
27     unsigned long int ttl;
28     unsigned short int rdlength;
29     unsigned char *rdata;
30     union {
31         struct { unsigned long int ip; char *name; } a;
32         struct { unsigned char *name; } ns;
33         struct { unsigned char *name; } cname;
34         struct { unsigned char *name; } ptr;
35         struct { unsigned short int priority, weight, port; unsigned char *name; } srv;
36     } known;
37 };
38
39 struct message
40 {
41     // external data
42     unsigned short int id;
43     struct { unsigned short qr:1, opcode:4, aa:1, tc:1, rd:1, ra:1, z:3, rcode:4; } header;
44     unsigned short int qdcount, ancount, nscount, arcount;
45     struct question *qd;
46     struct resource *an, *ns, *ar;
47
48     // internal variables
49     unsigned char *_buf, *_labels[20];
50     int _len, _label;
51
52     // packet acts as padding, easier mem management
53     unsigned char _packet[MAX_PACKET_LEN];
54 };
55
56 // returns the next short/long off the buffer (and advances it)
57 unsigned short int net2short(unsigned char **buf);
58 unsigned long int net2long(unsigned char **buf);
59
60 // copies the short/long into the buffer (and advances it)
61 void short2net(unsigned short int i, unsigned char **buf);
62 void long2net(unsigned long int l, unsigned char **buf);
63
64 // parse packet into message, packet must be at least MAX_PACKET_LEN and message must be zero'd for safety
65 void message_parse(struct message *m, unsigned char *packet);
66
67 // create a message for sending out on the wire
68 struct message *message_wire(void);
69
70 // append a question to the wire message
71 void message_qd(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class);
72
73 // append a resource record to the message, all called in order!
74 void message_an(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
75 void message_ns(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
76 void message_ar(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
77
78 // append various special types of resource data blocks
79 void message_rdata_long(struct message *m, unsigned long int l);
80 void message_rdata_name(struct message *m, unsigned char *name);
81 void message_rdata_srv(struct message *m, unsigned short int priority, unsigned short int weight, unsigned short int port, unsigned char *name);
82 void message_rdata_raw(struct message *m, unsigned char *rdata, unsigned short int rdlength);
83
84 // return the wire format (and length) of the message, just free message when done
85 unsigned char *message_packet(struct message *m);
86 int message_packet_len(struct message *m);
87
88
89 #endif