Annotation of embedaddon/libnet/src/libnet_build_dns.c, revision 1.1.1.3
1.1 misho 1: /*
1.1.1.2 misho 2: * $Id: libnet_build_dns.c,v 1.10 2004/04/13 17:32:28 mike Exp $
1.1 misho 3: *
4: * libnet
5: * libnet_build_dns.c - DNS packet assembler
6: *
7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8: * All rights reserved.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
31: */
32:
1.1.1.3 ! misho 33: #include "common.h"
1.1 misho 34:
35:
36: libnet_ptag_t
1.1.1.2 misho 37: libnet_build_dnsv4(uint16_t h_len, uint16_t id, uint16_t flags,
38: uint16_t num_q, uint16_t num_anws_rr, uint16_t num_auth_rr,
39: uint16_t num_addi_rr, const uint8_t *payload, uint32_t payload_s,
1.1 misho 40: libnet_t *l, libnet_ptag_t ptag)
41: {
42:
1.1.1.2 misho 43: uint32_t n, h;
1.1.1.3 ! misho 44: uint32_t offset;
1.1 misho 45: libnet_pblock_t *p;
46: struct libnet_dnsv4_hdr dns_hdr;
47:
48: if (l == NULL)
49: {
50: return (-1);
51: }
52:
53: if (h_len != LIBNET_UDP_DNSV4_H && h_len != LIBNET_TCP_DNSV4_H)
54: {
55: snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
56: "%s(): invalid header length: %d", __func__, h_len);
57: return (-1);
58: }
59: offset = (h_len == LIBNET_UDP_DNSV4_H ? sizeof(dns_hdr.h_len) : 0);
60: n = h_len + payload_s;
61: h = 0; /* no checksum */
62:
63: /*
64: * Find the existing protocol block if a ptag is specified, or create
65: * a new one.
66: */
67: p = libnet_pblock_probe(l, ptag, n, LIBNET_PBLOCK_DNSV4_H);
68: if (p == NULL)
69: {
70: return (-1);
71: }
1.1.1.2 misho 72:
1.1 misho 73: /*
74: * The sizeof(dns_hdr.h_len) is not counted is the packet size
75: * for TCP packet.
76: * And since this will be ignored for udp packets, let's compute it
77: * anyway.
78: */
1.1.1.2 misho 79: memset(&dns_hdr, 0, sizeof(dns_hdr));
1.1.1.3 ! misho 80: dns_hdr.h_len = htons((u_short)(n - sizeof (dns_hdr.h_len)));
1.1 misho 81: dns_hdr.id = htons(id);
82: dns_hdr.flags = htons(flags);
83: dns_hdr.num_q = htons(num_q);
84: dns_hdr.num_answ_rr = htons(num_anws_rr);
85: dns_hdr.num_auth_rr = htons(num_auth_rr);
86: dns_hdr.num_addi_rr = htons(num_addi_rr);
87:
88: /*
89: * A dirty trick: DNS can be either TCP or UDP based, and depending on
90: * that, the header changes. A 'length' field is present in TCP packets,
91: * but not in UDP packets. As they are the first 2 bytes of the header,
92: * they are skipped if the packet is UDP...
93: */
1.1.1.2 misho 94: n = libnet_pblock_append(l, p, ((uint8_t *)&dns_hdr) + offset, h_len);
1.1 misho 95: if (n == -1)
96: {
97: goto bad;
98: }
99:
1.1.1.2 misho 100: /* boilerplate payload sanity check / append macro */
101: LIBNET_DO_PAYLOAD(l, p);
1.1 misho 102:
103: return (ptag ? ptag : libnet_pblock_update(l, p, h, LIBNET_PBLOCK_DNSV4_H));
104: bad:
105: libnet_pblock_delete(l, p);
106: return (-1);
107: }
108:
1.1.1.3 ! misho 109: /**
! 110: * Local Variables:
! 111: * indent-tabs-mode: nil
! 112: * c-file-style: "stroustrup"
! 113: * End:
! 114: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>