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;
28 unsigned short int type, class;
29 unsigned long int ttl;
30 unsigned short int rdlength;
33 struct { unsigned long int ip; char *name; } a;
34 struct { unsigned char *name; } ns;
35 struct { unsigned char *name; } cname;
36 struct { unsigned char *name; } ptr;
37 struct { unsigned short int priority, weight, port; unsigned char *name; } srv;
44 unsigned short int id;
45 struct { unsigned short qr:1, opcode:4, aa:1, tc:1, rd:1, ra:1, z:3, rcode:4; } header;
46 unsigned short int qdcount, ancount, nscount, arcount;
48 struct resource *an, *ns, *ar;
51 unsigned char *_buf, *_labels[20];
54 // packet acts as padding, easier mem management
55 unsigned char _packet[MAX_PACKET_LEN];
58 // returns the next short/long off the buffer (and advances it)
59 unsigned short int net2short(unsigned char **buf);
60 unsigned long int net2long(unsigned char **buf);
62 // copies the short/long into the buffer (and advances it)
63 void short2net(unsigned short int i, unsigned char **buf);
64 void long2net(unsigned long int l, unsigned char **buf);
66 // parse packet into message, packet must be at least MAX_PACKET_LEN and message must be zero'd for safety
67 void message_parse(struct message *m, unsigned char *packet);
69 // create a message for sending out on the wire
70 struct message *message_wire(void);
72 // append a question to the wire message
73 void message_qd(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class);
75 // append a resource record to the message, all called in order!
76 void message_an(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
77 void message_ns(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
78 void message_ar(struct message *m, unsigned char *name, unsigned short int type, unsigned short int class, unsigned long int ttl);
80 // append various special types of resource data blocks
81 void message_rdata_long(struct message *m, unsigned long int l);
82 void message_rdata_name(struct message *m, unsigned char *name);
83 void message_rdata_srv(struct message *m, unsigned short int priority, unsigned short int weight, unsigned short int port, unsigned char *name);
84 void message_rdata_raw(struct message *m, unsigned char *rdata, unsigned short int rdlength);
86 // return the wire format (and length) of the message, just free message when done
87 unsigned char *message_packet(struct message *m);
88 int message_packet_len(struct message *m);