1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
|
/*
* Copyright (c) 2007
* Shrew Soft Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Redistributions in any form must be accompanied by information on
* how to obtain complete source code for the software and any
* accompanying software that uses the software. The source code
* must either be included in the distribution or be available for no
* more than the cost of distribution plus a nominal fee, and must be
* freely redistributable under reasonable conditions. For an
* executable file, complete source code means the source code for all
* modules it contains. It does not include source code for modules or
* files that typically accompany the major components of the operating
* system on which the executable file runs.
*
* THIS SOFTWARE IS PROVIDED BY SHREW SOFT INC ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL SHREW SOFT INC
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
* AUTHOR : Matthew Grooms
* mgrooms@shrew.net
*
*/
#include "libip.h"
#include <stdio.h>
#define MAX_DNS_NAME 255
PACKET_DNS::_PACKET_DNS()
{
ident = 0;
flags = 0;
ques = 0;
answ = 0;
ath_rr = 0;
add_rr = 0;
}
PACKET_DNS::~_PACKET_DNS()
{
list_ques.clean();
list_answ.clean();
list_ath_rr.clean();
list_add_rr.clean();
}
bool PACKET_DNS::read_name( char * name, long & size )
{
uint8_t tag1;
uint8_t tag2;
long oset = 0;
//
// reserve room for our null char
//
size--;
//
// step through all sequence tags
//
while( get_byte( tag1 ) )
{
//
// is this a reserved value
//
if( ( ( tag1 & 0xc0 ) == 0x80 ) ||
( ( tag1 & 0xc0 ) == 0x40 ) )
break;
//
// is this an end tag
//
if( tag1 == 0 )
{
//
// end of our segment, null
// terminate and return
//
name[ oset ] = 0;
return true;
}
//
// append a period if neccessary
//
if( oset )
{
if( size < 1 )
break;
name[ oset ] = '.';
oset++;
size--;
}
//
// is this a name segment or
// name segment pointer
//
if( ( tag1 & 0xc0 ) == 0xc0 )
{
size_t tmp_oset = 0;
size_t new_oset = 0;
//
// its a pointer, recurse
//
if( !get_byte( tag2 ) )
break;
new_oset |= ( tag1 & 0x3f );
new_oset <<= 8;
new_oset |= tag2;
if( new_oset >= data_size )
break;
tmp_oset = data_oset;
data_oset = new_oset;
bool rslt = read_name( name + oset, size );
data_oset = tmp_oset;
if( rslt )
return true;
}
else
{
//
// its a segment, append
//
long temp = tag1;
if( temp >= size )
break;
if( !get( &name[ oset ], temp ) )
break;
oset += temp;
size -= temp;
}
}
return false;
}
bool PACKET_DNS::read_query( DNS_QUERY ** query )
{
//
// read the name
//
char name_data[ MAX_DNS_NAME ];
long name_size = MAX_DNS_NAME;
if( !read_name( name_data, name_size ) )
return false;
uint16_t type;
if( !get_word( type ) )
return false;
uint16_t clss;
if( !get_word( clss ) )
return false;
//
// create our query struct
//
DNS_QUERY * tmp_query = new DNS_QUERY;
if( tmp_query == NULL )
return false;
tmp_query->name = new char[ name_size + 1 ];
memcpy( tmp_query->name, name_data, name_size );
tmp_query->name[ name_size ] = 0;
tmp_query->type = type;
tmp_query->clss = clss;
*query = tmp_query;
return true;
}
bool PACKET_DNS::read_record( DNS_RECORD ** record )
{
//
// read the name
//
char name_data[ MAX_DNS_NAME ];
long name_size = MAX_DNS_NAME;
if( !read_name( name_data, name_size ) )
return false;
uint16_t type;
if( !get_word( type ) )
return false;
uint16_t clss;
if( !get_word( clss ) )
return false;
uint32_t rttl;
if( !get_quad( rttl ) )
return false;
uint16_t rlen;
if( !get_word( rlen ) )
return false;
get_null( rlen );
//
// create our record struct
//
DNS_RECORD * tmp_record = new DNS_RECORD;
if( tmp_record == NULL )
return false;
tmp_record->name = new char[ name_size + 1 ];
memcpy( tmp_record->name, name_data, name_size );
tmp_record->name[ name_size ] = 0;
tmp_record->type = type;
tmp_record->clss = clss;
tmp_record->rttl = rttl;
tmp_record->rlen = rlen;
*record = tmp_record;
return true;
}
bool PACKET_DNS::read()
{
//
// read the header
//
DNS_HEADER dns_head;
if( !get( &dns_head, sizeof( dns_head ) ) )
return false;
ident = ntohs( dns_head.ident );
flags = ntohs( dns_head.flags );
ques = ntohs( dns_head.ques );
answ = ntohs( dns_head.answ );
ath_rr = ntohs( dns_head.ath_rr );
add_rr = ntohs( dns_head.add_rr );
//
// read question section
//
long index;
for( index = 0; index < ques; index++ )
{
DNS_QUERY * query;
if( !read_query( &query ) )
return false;
list_ques.add_entry( query );
}
//
// read answer section
//
for( index = 0; index < answ; index++ )
{
DNS_RECORD * record;
if( !read_record( &record ) )
return false;
list_answ.add_entry( record );
}
//
// authoritative section
//
for( index = 0; index < ath_rr; index++ )
{
DNS_RECORD * record;
if( !read_record( &record ) )
return false;
list_ath_rr.add_entry( record );
}
//
// additional section
//
for( index = 0; index < add_rr; index++ )
{
DNS_RECORD * record;
if( !read_record( &record ) )
return false;
list_add_rr.add_entry( record );
}
return true;
}
bool PACKET_DNS::write()
{
return true;
}
bool PACKET_DNS::get_question( DNS_QUERY ** query, long index )
{
*query = static_cast<DNS_QUERY*>( list_ques.get_entry( index ) );
return ( *query != NULL );
}
bool PACKET_DNS::get_answer( DNS_RECORD ** record, long index )
{
*record = static_cast<DNS_RECORD*>(list_answ.get_entry( index ) );
return ( *record != NULL );
}
bool PACKET_DNS::get_authority( DNS_RECORD ** record, long index )
{
*record = static_cast<DNS_RECORD*>( list_ath_rr.get_entry( index ) );
return ( *record != NULL );
}
bool PACKET_DNS::get_additional( DNS_RECORD ** record, long index )
{
*record = static_cast<DNS_RECORD*>( list_add_rr.get_entry( index ) );
return ( *record != NULL );
}
|