File: packet.h

package info (click to toggle)
ldns 1.7.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,416 kB
  • sloc: ansic: 43,318; python: 7,675; sh: 3,642; pascal: 2,351; perl: 2,188; makefile: 1,439; xml: 518
file content (893 lines) | stat: -rw-r--r-- 26,025 bytes parent folder | download | duplicates (2)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*
 * packet.h
 *
 * DNS packet definitions
 *
 * a Net::DNS like library for C
 *
 * (c) NLnet Labs, 2005-2006
 *
 * See the file LICENSE for the license
 */

/**
 * \file
 *
 * Contains the definition of ldns_pkt and its parts, as well
 * as functions to manipulate those.
 */


#ifndef LDNS_PACKET_H
#define LDNS_PACKET_H

#define LDNS_MAX_PACKETLEN         65535

/* allow flags to be given to ldns_pkt_query_new */
#define LDNS_QR		1       /* Query Response flag */
#define LDNS_AA		2       /* Authoritative Answer - server flag */
#define LDNS_TC		4       /* TrunCated - server flag */
#define LDNS_RD		8       /* Recursion Desired - query flag */
#define LDNS_CD		16      /* Checking Disabled - query flag */
#define LDNS_RA		32      /* Recursion Available - server flag */
#define LDNS_AD		64      /* Authenticated Data - server flag */

#include <ldns/error.h>
#include <ldns/common.h>
#include <ldns/rr.h>
#include <sys/time.h>

#ifdef __cplusplus
extern "C" {
#endif

/* opcodes for pkt's */
enum ldns_enum_pkt_opcode {
	LDNS_PACKET_QUERY = 0,
	LDNS_PACKET_IQUERY = 1,
	LDNS_PACKET_STATUS = 2, /* there is no 3?? DNS is weird */
	LDNS_PACKET_NOTIFY = 4,
	LDNS_PACKET_UPDATE = 5
};
typedef enum ldns_enum_pkt_opcode ldns_pkt_opcode;

/* rcodes for pkts */
enum ldns_enum_pkt_rcode {
	LDNS_RCODE_NOERROR = 0,
	LDNS_RCODE_FORMERR = 1,
	LDNS_RCODE_SERVFAIL = 2,
	LDNS_RCODE_NXDOMAIN = 3,
	LDNS_RCODE_NOTIMPL = 4,
	LDNS_RCODE_REFUSED = 5,
	LDNS_RCODE_YXDOMAIN = 6,
	LDNS_RCODE_YXRRSET = 7,
	LDNS_RCODE_NXRRSET = 8,
	LDNS_RCODE_NOTAUTH = 9,
	LDNS_RCODE_NOTZONE = 10
};
typedef enum ldns_enum_pkt_rcode ldns_pkt_rcode;

/**
 *  Header of a dns packet
 *
 * Contains the information about the packet itself, as specified in RFC1035
<pre>
4.1.1. Header section format

The header contains the following fields:

                                    1  1  1  1  1  1
      0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                      ID                       |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |QR|   Opcode  |AA|TC|RD|RA|   Z    |   RCODE   |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    QDCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    ANCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    NSCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    |                    ARCOUNT                    |
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+

where:

ID              A 16 bit identifier assigned by the program that
                generates any kind of query.  This identifier is copied
                the corresponding reply and can be used by the requester
                to match up replies to outstanding queries.

QR              A one bit field that specifies whether this message is a
                query (0), or a response (1).

OPCODE          A four bit field that specifies kind of query in this
                message.  This value is set by the originator of a query
                and copied into the response.  The values are:

                0               a standard query (QUERY)

                1               an inverse query (IQUERY)

                2               a server status request (STATUS)

                3-15            reserved for future use

AA              Authoritative Answer - this bit is valid in responses,
                and specifies that the responding name server is an
                authority for the domain name in question section.

                Note that the contents of the answer section may have
                multiple owner names because of aliases.  The AA bit

                corresponds to the name which matches the query name, or
                the first owner name in the answer section.

TC              TrunCation - specifies that this message was truncated
                due to length greater than that permitted on the
                transmission channel.

RD              Recursion Desired - this bit may be set in a query and
                is copied into the response.  If RD is set, it directs
                the name server to pursue the query recursively.
                Recursive query support is optional.

RA              Recursion Available - this be is set or cleared in a
                response, and denotes whether recursive query support is
                available in the name server.

Z               Reserved for future use.  Must be zero in all queries
                and responses.

RCODE           Response code - this 4 bit field is set as part of
                responses.  The values have the following
                interpretation:

                0               No error condition

                1               Format error - The name server was
                                unable to interpret the query.

                2               Server failure - The name server was
                                unable to process this query due to a
                                problem with the name server.

                3               Name Error - Meaningful only for
                                responses from an authoritative name
                                server, this code signifies that the
                                domain name referenced in the query does
                                not exist.

                4               Not Implemented - The name server does
                                not support the requested kind of query.

                5               Refused - The name server refuses to
                                perform the specified operation for
                                policy reasons.  For example, a name
                                server may not wish to provide the
                                information to the particular requester,
                                or a name server may not wish to perform
                                a particular operation (e.g., zone

                                transfer) for particular data.

                6-15            Reserved for future use.

QDCOUNT         an unsigned 16 bit integer specifying the number of
                entries in the question section.

ANCOUNT         an unsigned 16 bit integer specifying the number of
                resource records in the answer section.

NSCOUNT         an unsigned 16 bit integer specifying the number of name
                server resource records in the authority records
                section.

ARCOUNT         an unsigned 16 bit integer specifying the number of
                resource records in the additional records section.

</pre>
 */
struct ldns_struct_hdr
{
	/**  Id of a packet */
	uint16_t _id;
	/**  Query bit (0=query, 1=answer) */
	bool _qr;
	/**  Authoritative answer */
	bool _aa;
	/**  Packet truncated */
	bool _tc;
	/**  Recursion desired */
	bool _rd;
	/**  Checking disabled */
	bool _cd;
	/**  Recursion available */
	bool _ra;
	/**  Authentic data */
	bool _ad;
	/**  Query type */
	ldns_pkt_opcode _opcode;	 /* XXX 8 bits? */
	/**  Response code */
	uint8_t _rcode;
	/**  question sec */
	uint16_t _qdcount;
	/**  answer sec */
	uint16_t _ancount;
	/**  auth sec */
	uint16_t _nscount;
	/**  add sec */
	uint16_t _arcount;
};
typedef struct ldns_struct_hdr ldns_hdr;

/**
 * DNS packet
 *
 * This structure contains a complete DNS packet (either a query or an answer)
 *
 * It is the complete representation of what you actually send to a
 * nameserver, and what it sends back (assuming you are the client here).
 */
struct ldns_struct_pkt
{
	/** Header section */
	ldns_hdr *_header;
	/* extra items needed in a packet */
	/** an rdf (A or AAAA) with the IP address of the server it is from */
	ldns_rdf *_answerfrom;
        /** Timestamp of the time the packet was sent or created */
	struct timeval timestamp;
	/** The duration of the query this packet is an answer to */
	uint32_t _querytime;
	/** The size of the wire format of the packet in octets */
	size_t _size;
	/** Optional tsig rr */
	ldns_rr *_tsig_rr;
	/** EDNS0 available buffer size, see RFC2671 */
	uint16_t _edns_udp_size;
	/** EDNS0 Extended rcode */
	uint8_t _edns_extended_rcode;
	/** EDNS Version */
	uint8_t _edns_version;
        /* OPT pseudo-RR presence flag */
        uint8_t _edns_present;
	/** Reserved EDNS data bits */
	uint16_t _edns_z;
	/** Arbitrary EDNS rdata */
	ldns_rdf *_edns_data;
	/**  Question section */
	ldns_rr_list	*_question;
	/**  Answer section */
	ldns_rr_list	*_answer;
	/**  Authority section */
	ldns_rr_list	*_authority;
	/**  Additional section */
	ldns_rr_list	*_additional;
};
typedef struct ldns_struct_pkt ldns_pkt;

/**
 * The sections of a packet
 */
enum ldns_enum_pkt_section {
	LDNS_SECTION_QUESTION = 0,
	LDNS_SECTION_ANSWER = 1,
	LDNS_SECTION_AUTHORITY = 2,
	LDNS_SECTION_ADDITIONAL = 3,
	/** bogus section, if not interested */
	LDNS_SECTION_ANY = 4,
	/** used to get all non-question rrs from a packet */
	LDNS_SECTION_ANY_NOQUESTION = 5
};
typedef enum ldns_enum_pkt_section ldns_pkt_section;	

/**
 * The different types of packets
 */
enum ldns_enum_pkt_type {
	LDNS_PACKET_QUESTION,
	LDNS_PACKET_REFERRAL,
	LDNS_PACKET_ANSWER,
	LDNS_PACKET_NXDOMAIN,
	LDNS_PACKET_NODATA,
	LDNS_PACKET_UNKNOWN
};
typedef enum ldns_enum_pkt_type ldns_pkt_type;

/* prototypes */

/* read */

/**
 * Read the packet id
 * \param[in] p the packet
 * \return the packet id
 */
uint16_t ldns_pkt_id(const ldns_pkt *p);
/**
 * Read the packet's qr bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_qr(const ldns_pkt *p);
/**
 * Read the packet's aa bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_aa(const ldns_pkt *p);
/**
 * Read the packet's tc bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_tc(const ldns_pkt *p);
/**
 * Read the packet's rd bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_rd(const ldns_pkt *p);
/**
 * Read the packet's cd bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_cd(const ldns_pkt *p);
/**
 * Read the packet's ra bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_ra(const ldns_pkt *p);
/**
 * Read the packet's ad bit
 * \param[in] p the packet
 * \return value of the bit
 */
bool ldns_pkt_ad(const ldns_pkt *p);
/**
 * Read the packet's code
 * \param[in] p the packet
 * \return the opcode
 */
ldns_pkt_opcode ldns_pkt_get_opcode(const ldns_pkt *p);
/**
 * Return the packet's respons code
 * \param[in] p the packet
 * \return the respons code
 */
ldns_pkt_rcode ldns_pkt_get_rcode(const ldns_pkt *p);
/**
 * Return the packet's qd count 
 * \param[in] p the packet
 * \return the qd count
 */
uint16_t ldns_pkt_qdcount(const ldns_pkt *p);
/**
 * Return the packet's an count
 * \param[in] p the packet
 * \return the an count
 */
uint16_t ldns_pkt_ancount(const ldns_pkt *p);
/**
 * Return the packet's ns count
 * \param[in] p the packet
 * \return the ns count
 */
uint16_t ldns_pkt_nscount(const ldns_pkt *p);
/**
 * Return the packet's ar count
 * \param[in] p the packet
 * \return the ar count
 */
uint16_t ldns_pkt_arcount(const ldns_pkt *p);

/** 
 * Return the packet's answerfrom
 * \param[in] p packet
 * \return the name of the server
 */
ldns_rdf *ldns_pkt_answerfrom(const ldns_pkt *p);

/**
 * Return the packet's timestamp
 * \param[in] p the packet
 * \return the timestamp
 */
struct timeval ldns_pkt_timestamp(const ldns_pkt *p);
/**
 * Return the packet's querytime
 * \param[in] p the packet
 * \return the querytime
 */
uint32_t ldns_pkt_querytime(const ldns_pkt *p);

/**
 * Return the packet's size in bytes
 * \param[in] p the packet
 * \return the size
 */
size_t ldns_pkt_size(const ldns_pkt *p);

/**
 * Return the number of RRs in the given section.
 * Returns the sum of all RRs when LDNS_SECTION_ANY is given.
 * Returns the sum of all non-question RRs when LDNS_SECTION_ANY_NOQUESTION
 * is given.
 * \param[in] p the packet
 * \param[in] s the section
 * \return the number of RRs in the given section
 */
uint16_t ldns_pkt_section_count(const ldns_pkt *p, ldns_pkt_section s);

/**
 * Return the packet's tsig pseudo rr's
 * \param[in] p the packet
 * \return the tsig rr
 */
ldns_rr *ldns_pkt_tsig(const ldns_pkt *p);

/**
 * Return the packet's question section
 * \param[in] p the packet
 * \return the section
 */
ldns_rr_list *ldns_pkt_question(const ldns_pkt *p);
/**
 * Return the packet's answer section
 * \param[in] p the packet
 * \return the section
 */
ldns_rr_list *ldns_pkt_answer(const ldns_pkt *p);
/**
 * Return the packet's authority section
 * \param[in] p the packet
 * \return the section
 */
ldns_rr_list *ldns_pkt_authority(const ldns_pkt *p);
/**
 * Return the packet's additional section
 * \param[in] p the packet
 * \return the section
 */
ldns_rr_list *ldns_pkt_additional(const ldns_pkt *p);
/**
 * Return the packet's question, answer, authority and additional sections
 * concatenated, in a new rr_list clone.
 * \param[in] p the packet
 * \return the rrs
 */
ldns_rr_list *ldns_pkt_all(const ldns_pkt *p);
/**
 * Return the packet's answer, authority and additional sections concatenated, 
 * in a new rr_list clone.  Like ldns_pkt_all but without the questions.
 * \param[in] p the packet
 * \return the rrs except the question rrs
 */
ldns_rr_list *ldns_pkt_all_noquestion(const ldns_pkt *p);

/**
 * return all the rr_list's in the packet. Clone the lists, instead
 * of returning pointers. 
 * \param[in] p the packet to look in
 * \param[in] s what section(s) to return
 * \return ldns_rr_list with the rr's or NULL if none were found
 */
ldns_rr_list *ldns_pkt_get_section_clone(const ldns_pkt *p, ldns_pkt_section s);

/**
 * return all the rr with a specific name from a packet. Optionally
 * specify from which section in the packet
 * \param[in] p the packet
 * \param[in] r the name
 * \param[in] s the packet's section
 * \return a list with the rr's or NULL if none were found
 */
ldns_rr_list *ldns_pkt_rr_list_by_name(const ldns_pkt *p, const ldns_rdf *r, ldns_pkt_section s);
/**
 * return all the rr with a specific type from a packet. Optionally
 * specify from which section in the packet
 * \param[in] p the packet
 * \param[in] t the type
 * \param[in] s the packet's section
 * \return a list with the rr's or NULL if none were found
 */
ldns_rr_list *ldns_pkt_rr_list_by_type(const ldns_pkt *p, ldns_rr_type t, ldns_pkt_section s);
/**
 * return all the rr with a specific type and type from a packet. Optionally
 * specify from which section in the packet
 * \param[in] packet the packet
 * \param[in] ownername the name
 * \param[in] type the type
 * \param[in] sec the packet's section
 * \return a list with the rr's or NULL if none were found
 */
ldns_rr_list *ldns_pkt_rr_list_by_name_and_type(const ldns_pkt *packet, const ldns_rdf *ownername, ldns_rr_type type, ldns_pkt_section sec);


/**
 * check to see if an rr exist in the packet
 * \param[in] pkt the packet to examine
 * \param[in] sec in which section to look
 * \param[in] rr the rr to look for
 */
bool ldns_pkt_rr(const ldns_pkt *pkt, ldns_pkt_section sec, const ldns_rr *rr);


/**
 * sets the flags in a packet.
 * \param[in] pkt the packet to operate on
 * \param[in] flags ORed values: LDNS_QR| LDNS_AR for instance
 * \return true on success otherwise false
 */
bool ldns_pkt_set_flags(ldns_pkt *pkt, uint16_t flags);

/**
 * Set the packet's id
 * \param[in] p the packet
 * \param[in] id the id to set
 */
void ldns_pkt_set_id(ldns_pkt *p, uint16_t id);
/**
 * Set the packet's id to a random value
 * \param[in] p the packet
 */
void ldns_pkt_set_random_id(ldns_pkt *p);
/**
 * Set the packet's qr bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_qr(ldns_pkt *p, bool b);
/**
 * Set the packet's aa bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_aa(ldns_pkt *p, bool b);
/**
 * Set the packet's tc bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_tc(ldns_pkt *p, bool b);
/**
 * Set the packet's rd bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_rd(ldns_pkt *p, bool b);
/**
 * Set the packet's cd bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_cd(ldns_pkt *p, bool b);
/**
 * Set the packet's ra bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_ra(ldns_pkt *p, bool b);
/**
 * Set the packet's ad bit
 * \param[in] p the packet
 * \param[in] b the value to set (boolean)
 */
void ldns_pkt_set_ad(ldns_pkt *p, bool b);

/**
 * Set the packet's opcode
 * \param[in] p the packet
 * \param[in] c the opcode
 */
void ldns_pkt_set_opcode(ldns_pkt *p, ldns_pkt_opcode c);
/**
 * Set the packet's respons code
 * \param[in] p the packet
 * \param[in] c the rcode
 */
void ldns_pkt_set_rcode(ldns_pkt *p, uint8_t c);
/**
 * Set the packet's qd count
 * \param[in] p the packet
 * \param[in] c the count
 */
void ldns_pkt_set_qdcount(ldns_pkt *p, uint16_t c);
/**
 * Set the packet's an count
 * \param[in] p the packet
 * \param[in] c the count
 */
void ldns_pkt_set_ancount(ldns_pkt *p, uint16_t c);
/**
 * Set the packet's ns count
 * \param[in] p the packet
 * \param[in] c the count
 */
void ldns_pkt_set_nscount(ldns_pkt *p, uint16_t c);
/**
 * Set the packet's arcount
 * \param[in] p the packet
 * \param[in] c the count
 */
void ldns_pkt_set_arcount(ldns_pkt *p, uint16_t c);
/**
 * Set the packet's answering server
 * \param[in] p the packet
 * \param[in] r the address
 */
void ldns_pkt_set_answerfrom(ldns_pkt *p, ldns_rdf *r);
/**
 * Set the packet's query time
 * \param[in] p the packet
 * \param[in] t the querytime in msec
 */
void ldns_pkt_set_querytime(ldns_pkt *p, uint32_t t);
/**
 * Set the packet's size
 * \param[in] p the packet
 * \param[in] s the size
 */
void ldns_pkt_set_size(ldns_pkt *p, size_t s);

/**
 * Set the packet's timestamp
 * \param[in] p the packet
 * \param[in] timeval the timestamp
 */
void ldns_pkt_set_timestamp(ldns_pkt *p, struct timeval timeval);
/**
 * Set a packet's section count to x
 * \param[in] p the packet
 * \param[in] s the section
 * \param[in] x the section count
 */
void ldns_pkt_set_section_count(ldns_pkt *p, ldns_pkt_section s, uint16_t x);
/**
 * Set the packet's tsig rr
 * \param[in] p the packet
 * \param[in] t the tsig rr
 */
void ldns_pkt_set_tsig(ldns_pkt *p, ldns_rr *t);

/**
 * looks inside the packet to determine
 * what kind of packet it is, AUTH, NXDOMAIN, REFERRAL, etc.
 * \param[in] p the packet to examine
 * \return the type of packet
 */
ldns_pkt_type ldns_pkt_reply_type(const ldns_pkt *p);

/**
 * return the packet's edns udp size
 * \param[in] packet the packet
 * \return the size
 */
uint16_t ldns_pkt_edns_udp_size(const ldns_pkt *packet);
/**
 * return the packet's edns extended rcode
 * \param[in] packet the packet
 * \return the rcode
 */
uint8_t ldns_pkt_edns_extended_rcode(const ldns_pkt *packet);
/**
 * return the packet's edns version
 * \param[in] packet the packet
 * \return the version
 */
uint8_t ldns_pkt_edns_version(const ldns_pkt *packet);
/**
 * return the packet's edns z value
 * \param[in] packet the packet
 * \return the z value
 */
uint16_t ldns_pkt_edns_z(const ldns_pkt *packet);
/**
 * return the packet's edns data
 * \param[in] packet the packet
 * \return the data
 */
ldns_rdf *ldns_pkt_edns_data(const ldns_pkt *packet);

/**
 * return the packet's edns do bit
 * \param[in] packet the packet
 * \return the bit's value
 */
bool ldns_pkt_edns_do(const ldns_pkt *packet);
/**
 * Set the packet's edns do bit
 * \param[in] packet the packet
 * \param[in] value the bit's new value
 */
void ldns_pkt_set_edns_do(ldns_pkt *packet, bool value);

/**
 * returns true if this packet needs and EDNS rr to be sent.
 * At the moment the only reason is an expected packet
 * size larger than 512 bytes, but for instance dnssec would
 * be a good reason too.
 *
 * \param[in] packet the packet to check
 * \return true if packet needs edns rr
 */
bool ldns_pkt_edns(const ldns_pkt *packet);

/**
 * Set the packet's edns udp size
 * \param[in] packet the packet
 * \param[in] s the size
 */
void ldns_pkt_set_edns_udp_size(ldns_pkt *packet, uint16_t s);
/**
 * Set the packet's edns extended rcode
 * \param[in] packet the packet
 * \param[in] c the code
 */
void ldns_pkt_set_edns_extended_rcode(ldns_pkt *packet, uint8_t c);
/**
 * Set the packet's edns version
 * \param[in] packet the packet
 * \param[in] v the version
 */
void ldns_pkt_set_edns_version(ldns_pkt *packet, uint8_t v);
/**
 * Set the packet's edns z value
 * \param[in] packet the packet
 * \param[in] z the value
 */
void ldns_pkt_set_edns_z(ldns_pkt *packet, uint16_t z);
/**
 * Set the packet's edns data
 * \param[in] packet the packet
 * \param[in] data the data
 */
void ldns_pkt_set_edns_data(ldns_pkt *packet, ldns_rdf *data);

/**
 * allocates and initializes a ldns_pkt structure.
 * \return pointer to the new packet
 */
ldns_pkt *ldns_pkt_new(void);

/**
 * frees the packet structure and all data that it contains.
 * \param[in] packet The packet structure to free
 * \return void
 */
void ldns_pkt_free(ldns_pkt *packet);

/**
 * creates a query packet for the given name, type, class.
 * \param[out] p the packet to be returned
 * \param[in] rr_name the name to query for (as string)
 * \param[in] rr_type the type to query for
 * \param[in] rr_class the class to query for
 * \param[in] flags packet flags
 * \return LDNS_STATUS_OK or a ldns_status mesg with the error
 */
ldns_status ldns_pkt_query_new_frm_str(ldns_pkt **p, const char *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class , uint16_t flags);

/**
 * creates an IXFR request packet for the given name, class.
 * adds the SOA record to the authority section.
 * \param[out] p the packet to be returned
 * \param[in] rr_name the name to query for (as string)
 * \param[in] rr_class the class to query for
 * \param[in] flags packet flags
 * \param[in] soa soa record to be added to the authority section (not copied).
 * \return LDNS_STATUS_OK or a ldns_status mesg with the error
 */
ldns_status ldns_pkt_ixfr_request_new_frm_str(ldns_pkt **p, const char *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr* soa);

/**
 * creates a packet with a query in it for the given name, type and class.
 * \param[in] rr_name the name to query for (not copied).
 *            The returned packet will take ownership of rr_name, so the caller should not free it.
 * \param[in] rr_type the type to query for
 * \param[in] rr_class the class to query for
 * \param[in] flags packet flags
 * \return ldns_pkt* a pointer to the new pkt
 */
ldns_pkt *ldns_pkt_query_new(ldns_rdf *rr_name, ldns_rr_type rr_type, ldns_rr_class rr_class, uint16_t flags);

/**
 * creates an IXFR request packet for the given name, type and class.
 * adds the SOA record to the authority section.
 * \param[in] rr_name the name to query for (not copied).
 *            The returned packet will take ownership of rr_name, so the caller should not free it.
 * \param[in] rr_class the class to query for
 * \param[in] flags packet flags
 * \param[in] soa soa record to be added to the authority section (not copied).
 * \return ldns_pkt* a pointer to the new pkt
 */
ldns_pkt *ldns_pkt_ixfr_request_new(ldns_rdf *rr_name, ldns_rr_class rr_class, uint16_t flags, ldns_rr* soa);

/**
 * clones the given packet, creating a fully allocated copy
 *
 * \param[in] pkt the packet to clone
 * \return ldns_pkt* pointer to the new packet
 */
ldns_pkt *ldns_pkt_clone(const ldns_pkt *pkt);

/**
 * directly set the additional section
 * \param[in] p packet to operate on
 * \param[in] rr rrlist to set
 */
void ldns_pkt_set_additional(ldns_pkt *p, ldns_rr_list *rr);

/**
 * directly set the answer section
 * \param[in] p packet to operate on
 * \param[in] rr rrlist to set
 */
void ldns_pkt_set_answer(ldns_pkt *p, ldns_rr_list *rr);

/**
 * directly set the question section
 * \param[in] p packet to operate on
 * \param[in] rr rrlist to set
 */
void ldns_pkt_set_question(ldns_pkt *p, ldns_rr_list *rr);

/**
 * directly set the auhority section
 * \param[in] p packet to operate on
 * \param[in] rr rrlist to set
 */
void ldns_pkt_set_authority(ldns_pkt *p, ldns_rr_list *rr);

/**
 * push an rr on a packet
 * \param[in] packet packet to operate on
 * \param[in] section where to put it
 * \param[in] rr rr to push
 * \return a boolean which is true when the rr was added
 */
bool ldns_pkt_push_rr(ldns_pkt *packet, ldns_pkt_section section, ldns_rr *rr);

/**
 * push an rr on a packet, provided the RR is not there.
 * \param[in] pkt packet to operate on
 * \param[in] sec where to put it
 * \param[in] rr rr to push
 * \return a boolean which is true when the rr was added
 */
bool ldns_pkt_safe_push_rr(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr *rr);

/**
 * push a rr_list on a packet
 * \param[in] packet packet to operate on
 * \param[in] section where to put it
 * \param[in] list the rr_list to push
 * \return a boolean which is true when the rr was added
 */
bool ldns_pkt_push_rr_list(ldns_pkt *packet, ldns_pkt_section section, ldns_rr_list *list);

/**
 * push an rr_list to a packet, provided the RRs are not already there.
 * \param[in] pkt packet to operate on
 * \param[in] sec where to put it
 * \param[in] list the rr_list to push
 * \return a boolean which is true when the rr was added
 */
bool ldns_pkt_safe_push_rr_list(ldns_pkt *pkt, ldns_pkt_section sec, ldns_rr_list *list);

/**
 * check if a packet is empty
 * \param[in] p packet
 * \return true: empty, false: not empty
 */
bool ldns_pkt_empty(ldns_pkt *p);

#ifdef __cplusplus
}
#endif

#endif  /* LDNS_PACKET_H */