File indexing completed on 2024-05-19 04:06:37

0001 /*
0002  * Copyright (C) 2006  Justin Karneges
0003  *
0004  * Permission is hereby granted, free of charge, to any person obtaining a
0005  * copy of this software and associated documentation files (the
0006  * "Software"), to deal in the Software without restriction, including
0007  * without limitation the rights to use, copy, modify, merge, publish,
0008  * distribute, sublicense, and/or sell copies of the Software, and to
0009  * permit persons to whom the Software is furnished to do so, subject to
0010  * the following conditions:
0011  *
0012  * The above copyright notice and this permission notice shall be included
0013  * in all copies or substantial portions of the Software.
0014  *
0015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0018  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
0019  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
0020  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
0021  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0022  */
0023 
0024 #ifndef JDNS_PACKET_H
0025 #define JDNS_PACKET_H
0026 
0027 #include "jdns.h"
0028 
0029 /* -- howto --
0030 //
0031 // writing packets:
0032 //   1) call jdns_packet_new()
0033 //   2) populate the jdns_packet_t structure, using the functions
0034 //      as necessary
0035 //   3) call jdns_packet_export() to populate the raw data of the packet
0036 //
0037 // reading packets:
0038 //   1) call jdns_packet_new()
0039 //   2) call jdns_packet_import() with the raw data
0040 //   3) the jdns_packet_t structure is now populated
0041 //
0042 // IMPORTANT: all names must be valid. that is, ending in a dot character */
0043 
0044 int jdns_packet_name_isvalid(const unsigned char *name, int size); /* 0 if not valid */
0045 
0046 typedef struct jdns_packet_question
0047 {
0048     JDNS_OBJECT
0049     jdns_string_t *qname;
0050     unsigned short int qtype, qclass;
0051 } jdns_packet_question_t;
0052 
0053 jdns_packet_question_t *jdns_packet_question_new();
0054 jdns_packet_question_t *jdns_packet_question_copy(const jdns_packet_question_t *a);
0055 void jdns_packet_question_delete(jdns_packet_question_t *a);
0056 
0057 typedef struct jdns_packet_write jdns_packet_write_t;
0058 typedef struct jdns_packet jdns_packet_t;
0059 
0060 typedef struct jdns_packet_resource
0061 {
0062     JDNS_OBJECT
0063     jdns_string_t *qname;
0064     unsigned short int qtype, qclass;
0065     unsigned long int ttl; /* 31-bit number, top bit always 0 */
0066     unsigned short int rdlength;
0067     unsigned char *rdata;
0068 
0069     /* private */
0070     jdns_list_t *writelog; /* jdns_packet_write_t */
0071 } jdns_packet_resource_t;
0072 
0073 jdns_packet_resource_t *jdns_packet_resource_new();
0074 jdns_packet_resource_t *jdns_packet_resource_copy(const jdns_packet_resource_t *a);
0075 void jdns_packet_resource_delete(jdns_packet_resource_t *a);
0076 void jdns_packet_resource_add_bytes(jdns_packet_resource_t *a, const unsigned char *data, int size);
0077 void jdns_packet_resource_add_name(jdns_packet_resource_t *a, const jdns_string_t *name);
0078 int jdns_packet_resource_read_name(const jdns_packet_resource_t *a, const jdns_packet_t *p, int *at, jdns_string_t **name);
0079 
0080 struct jdns_packet
0081 {
0082     JDNS_OBJECT
0083     unsigned short int id;
0084     struct
0085     {
0086         unsigned short qr, opcode, aa, tc, rd, ra, z, rcode;
0087     } opts;
0088 
0089     /* item counts as specified by the packet.  do not use these
0090     //   for iteration over the item lists, since they can be wrong
0091     //   if the packet is truncated. */
0092     int qdcount, ancount, nscount, arcount;
0093 
0094     /* value lists */
0095     jdns_list_t *questions;         /* jdns_packet_question_t */
0096     jdns_list_t *answerRecords;     /* jdns_packet_resource_t */
0097     jdns_list_t *authorityRecords;  /* jdns_packet_resource_t */
0098     jdns_list_t *additionalRecords; /* jdns_packet_resource_t */
0099 
0100     /* since dns packets are allowed to be truncated, it is possible
0101     //   for a packet to not get fully parsed yet still be considered
0102     //   successfully parsed.  this flag means the packet was fully
0103     //   parsed also. */
0104     int fully_parsed;
0105 
0106     int raw_size;
0107     unsigned char *raw_data;
0108 };
0109 
0110 jdns_packet_t *jdns_packet_new();
0111 jdns_packet_t *jdns_packet_copy(const jdns_packet_t *a);
0112 void jdns_packet_delete(jdns_packet_t *a);
0113 int jdns_packet_import(jdns_packet_t **a, const unsigned char *data, int size); /* 0 on fail */
0114 int jdns_packet_export(jdns_packet_t *a, int maxsize); /* 0 on fail */
0115 
0116 #endif