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
8 // should be reasonably large, for udp
9 #define MAX_PACKET_LEN 4000
14 unsigned short int type, class;
26 unsigned short int type, class;
27 unsigned long int ttl;
28 unsigned short int rdlength;
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;
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;
46 struct resource *an, *ns, *ar;
49 unsigned char *_buf, *_labels[20];
52 // packet acts as padding, easier mem management
53 unsigned char _packet[MAX_PACKET_LEN];
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);
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);
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);
67 // create a message for sending out on the wire
68 struct message *message_wire(void);
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);
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);
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);
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);