Main Page | Namespace List | Class List | File List | Class Members | Related Pages

src/c-api/1035.h

00001 #ifndef _1035_h
00002 #define _1035_h
00003 
00004 // be familiar with rfc1035 if you want to know what all the variable names mean, but this hides most of the dirty work
00005 // all of this code depends on the buffer space a packet is in being 4096 and zero'd before the packet is copied in
00006 // also conveniently decodes srv rr's, type 33, see rfc2782
00007 
00008 // should be reasonably large, for udp
00009 #define MAX_PACKET_LEN 4000
00010 
00011 struct question
00012 {
00013   unsigned char *name;
00014   unsigned short int type;
00015   unsigned short int q_class;
00016 };
00017 
00018 #define QTYPE_A 1
00019 #define QTYPE_NS 2
00020 #define QTYPE_CNAME 5
00021 #define QTYPE_PTR 12
00022 #define QTYPE_SRV 33
00023 
00024 struct resource
00025 {
00026   unsigned char *name;
00027   unsigned short int type;
00028   unsigned short int r_class;
00029   unsigned long int ttl;
00030   unsigned short int rdlength;
00031   unsigned char *rdata;
00032   union {
00033     struct { unsigned long int ip; char *name; } a;
00034     struct { unsigned char *name; } ns;
00035     struct { unsigned char *name; } cname;
00036     struct { unsigned char *name; } ptr;
00037     struct { unsigned short int priority, weight, port; unsigned char *name; } srv;
00038   } known;
00039 };
00040 
00041 struct message
00042 {
00043     // external data
00044     unsigned short int id;
00045     struct { 
00046       unsigned short qr:1, 
00047         opcode:4, 
00048         aa:1, 
00049         tc:1, 
00050         rd:1, 
00051         ra:1, 
00052         z:3, 
00053         rcode:4; 
00054     } header;
00055     unsigned short int qdcount, ancount, nscount, arcount;
00056     struct question *qd;
00057     struct resource *an, *ns, *ar;
00058 
00059     // internal variables
00060     unsigned char *_buf, *_labels[20];
00061     int _len, _label;
00062     
00063     // packet acts as padding, easier mem management
00064     unsigned char _packet[MAX_PACKET_LEN];
00065 
00066     // target is not in mutlicast
00067     char* target_host;
00068     int target_port;
00069 };
00070 
00071 // returns the next short/long off the buffer (and advances it)
00072 unsigned short int net2short(unsigned char **buf);
00073 unsigned long int net2long(unsigned char **buf);
00074 
00075 // copies the short/long into the buffer (and advances it)
00076 void short2net(unsigned short int i, unsigned char **buf);
00077 void long2net(unsigned long int l, unsigned char **buf);
00078 
00079 // parse packet into message, packet must be at least MAX_PACKET_LEN and message must be zero'd for safety
00080 void message_parse(struct message *m, unsigned char *packet);
00081 
00082 // create a message for sending out on the wire
00083 struct message *message_wire(void);
00084 
00085 // append a question to the wire message
00086 void message_qd(struct message *m, 
00087                 unsigned char *name, 
00088                 unsigned short int type, 
00089                 unsigned short int aClass);
00090 
00091 // append a resource record to the message, all called in order!
00092 // @todo replace by variant with target_host
00093 void message_an(struct message *m, 
00094                 unsigned char *name, 
00095                 unsigned short int type, 
00096                 unsigned short int aClass, 
00097                 unsigned long int ttl);
00098 
00102 void _message_an(struct message *m,
00103                  unsigned char *name,
00104                  unsigned short int type,
00105                  unsigned short int aClass,
00106                  unsigned long int ttl,
00107                  char* target_host,
00108                  unsigned int target_port);
00109 
00110 // @todo replace by variant with target_host
00111 void message_ns(struct message *m, 
00112                 unsigned char *name, 
00113                 unsigned short int type, 
00114                 unsigned short int aClass, 
00115                 unsigned long int ttl);
00116 
00117 void _message_ns(struct message *m, 
00118                  unsigned char *name, 
00119                  unsigned short int type, 
00120                  unsigned short int aClass, 
00121                  unsigned long int ttl,
00122                  char* target_host,
00123                  int target_port);
00124 
00125 // @todo replace by variant with target_host
00126 void message_ar(struct message *m, 
00127                 unsigned char *name, 
00128                 unsigned short int type, 
00129                 unsigned short int aClass, 
00130                 unsigned long int ttl);
00131 
00132 void _message_ar(struct message *m, 
00133                  unsigned char *name, 
00134                  unsigned short int type, 
00135                  unsigned short int aClass, 
00136                  unsigned long int ttl,
00137                  char* target_host,
00138                  int target_port);
00139 
00140 // append various special types of resource data blocks
00141 void message_rdata_long(struct message *m, unsigned long int l);
00142 void message_rdata_name(struct message *m, unsigned char *name);
00143 void message_rdata_srv(struct message *m, unsigned short int priority, unsigned short int weight, unsigned short int port, unsigned char *name);
00144 void message_rdata_raw(struct message *m, unsigned char *rdata, unsigned short int rdlength);
00145 
00146 // return the wire format (and length) of the message, just free message when done
00147 unsigned char *message_packet(struct message *m);
00148 int message_packet_len(struct message *m);
00149 
00150 
00151 #endif

Generated on Fri May 19 23:27:33 2006 for Tiny DNSSD by  doxygen 1.4.4