1 /*
2  *  $Id: libnet-macros.h,v 1.7 2004/04/13 17:32:28 mike Exp $
3  *
4  *  libnet-macros.h - Network routine library macro header file
5  *
6  *  Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
7  *  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #ifndef __LIBNET_MACROS_H
33 #define __LIBNET_MACROS_H
34 /**
35  * @file libnet-macros.h
36  * @brief libnet macros and symbolic constants
37  */
38 
39 /* for systems without snprintf */
40 #if defined(NO_SNPRINTF)
41 #define snprintf(buf, len, args...) sprintf(buf, ##args)
42 #endif
43 
44 
45 /**
46  * Used for libnet's name resolution functions, specifies that no DNS lookups
47  * should be performed and the IP address should be kept in numeric form.
48  */
49 #define LIBNET_DONT_RESOLVE 0
50 
51 /**
52  * Used for libnet's name resolution functions, specifies that a DNS lookup
53  * can be performed if needed to resolve the IP address to a canonical form.
54  */
55 #define LIBNET_RESOLVE      1
56 
57 /**
58  * Used several places, to specify "on" or "one"
59  */
60 #define LIBNET_ON	0
61 
62 /**
63  * Used several places, to specify "on" or "one"
64  */
65 #define LIBNET_OFF	1
66 
67 /**
68  * IPv6 error code
69  */
70 #ifndef IN6ADDR_ERROR_INIT
71 #define IN6ADDR_ERROR_INIT { { { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
72                                  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \
73                                  0xff, 0xff } } }
74 #endif
75 
76 /**
77  * Used for libnet_get_prand() to specify function disposition
78  */
79 #define LIBNET_PR2          0
80 #define LIBNET_PR8          1
81 #define LIBNET_PR16         2
82 #define LIBNET_PRu16        3
83 #define LIBNET_PR32         4
84 #define LIBNET_PRu32        5
85 #define LIBNET_PRAND_MAX    0xffffffff
86 
87 /**
88  * The biggest an IP packet can be -- 65,535 bytes.
89  */
90 #define LIBNET_MAX_PACKET   0xffff
91 #ifndef IP_MAXPACKET
92 #define IP_MAXPACKET        0xffff
93 #endif
94 
95 
96 /* ethernet addresses are 6 octets long */
97 #ifndef ETHER_ADDR_LEN
98 #define ETHER_ADDR_LEN      0x6
99 #endif
100 
101 /* FDDI addresses are 6 octets long */
102 #ifndef FDDI_ADDR_LEN
103 #define FDDI_ADDR_LEN       0x6
104 #endif
105 
106 /* token ring addresses are 6 octets long */
107 #ifndef TOKEN_RING_ADDR_LEN
108 #define TOKEN_RING_ADDR_LEN 0x6
109 #endif
110 
111 /* LLC Organization Code is 3 bytes long */
112 #define LIBNET_ORG_CODE_SIZE  0x3
113 
114 /**
115  * The libnet error buffer is 256 bytes long.
116  */
117 #define LIBNET_ERRBUF_SIZE      0x100
118 
119 /**
120  * IP and TCP options can be up to 40 bytes long.
121  */
122 #define LIBNET_MAXOPTION_SIZE   0x28
123 
124 /* some BSD variants have this endianess problem */
125 #if (LIBNET_BSD_BYTE_SWAP)
126 #define FIX(n)      ntohs(n)
127 #define UNFIX(n)    htons(n)
128 #else
129 #define FIX(n)      (n)
130 #define UNFIX(n)    (n)
131 #endif
132 
133 /* used internally for packet builders */
134 #define LIBNET_DO_PAYLOAD(l, p)                                              \
135 if (payload_s && !payload)                                                   \
136 {                                                                            \
137     snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,                                 \
138             "%s(): payload inconsistency\n", __func__);                      \
139     goto bad;                                                                \
140 }                                                                            \
141 if (payload_s)                                                               \
142 {                                                                            \
143     n = libnet_pblock_append(l, p, payload, payload_s);                      \
144     if (n == (uint32_t) - 1)                                                 \
145     {                                                                        \
146         goto bad;                                                            \
147     }                                                                        \
148 }                                                                            \
149 
150 
151 /* used internally for checksum stuff */
152 #define LIBNET_CKSUM_CARRY(x) \
153     (x = (x >> 16) + (x & 0xffff), (~(x + (x >> 16)) & 0xffff))
154 
155 /* used interally for OSPF stuff */
156 #define LIBNET_OSPF_AUTHCPY(x, y) \
157     memcpy((uint8_t *)x, (uint8_t *)y, sizeof(y))
158 #define LIBNET_OSPF_CKSUMBUF(x, y) \
159     memcpy((uint8_t *)x, (uint8_t *)y, sizeof(y))
160 
161 /* used internally for NTP leap indicator, version, and mode */
162 #define LIBNET_NTP_DO_LI_VN_MODE(li, vn, md) \
163     ((uint8_t)((((li) << 6) & 0xc0) | (((vn) << 3) & 0x38) | ((md) & 0x7)))
164 
165 /* Not all systems have IFF_LOOPBACK */
166 #ifdef IFF_LOOPBACK
167 #define LIBNET_ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
168 #else
169 #define LIBNET_ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo") == 0)
170 #endif
171 
172 /* advanced mode check */
173 #define LIBNET_ISADVMODE(x) (x & 0x08)
174 
175 /* context queue macros and constants */
176 #define LIBNET_LABEL_SIZE   64
177 #define LIBNET_LABEL_DEFAULT "cardshark"
178 #define CQ_LOCK_UNLOCKED    (u_int)0x00000000
179 #define CQ_LOCK_READ        (u_int)0x00000001
180 #define CQ_LOCK_WRITE       (u_int)0x00000002
181 
182 /**
183  * Provides an interface to iterate through the context queue of libnet
184  * contexts. Before calling this macro, be sure to set the queue using
185  * libnet_cq_head().
186  */
187 #define for_each_context_in_cq(l) \
188     for (l = libnet_cq_head(); libnet_cq_last(); l = libnet_cq_next())
189 
190 /* return 1 if write lock is set on cq */
191 #define cq_is_wlocked() (l_cqd.cq_lock & CQ_LOCK_WRITE)
192 
193 /* return 1 if read lock is set on cq */
194 #define cq_is_rlocked() (l_cqd.cq_lock & CQ_LOCK_READ)
195 
196 /* return 1 if any lock is set on cq */
197 #define cq_is_locked() (l_cqd.cq_lock & (CQ_LOCK_READ | CQ_LOCK_WRITE))
198 
199 /* check if a context queue is locked */
200 #define check_cq_lock(x) (l_cqd.cq_lock & x)
201 
202 #endif  /* __LIBNET_MACROS_H */
203 
204 /* EOF */
205