~ubuntu-branches/ubuntu/karmic/xprobe/karmic

« back to all changes in this revision

Viewing changes to libs-external/USI++/usi++/usi-structs.h

  • Committer: Bazaar Package Importer
  • Author(s): Richard Atterer
  • Date: 2005-02-22 22:54:24 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050222225424-6cqy8rr45pkna819
Tags: 0.2.2-1
New upstream version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*** This Programs/Libraries are (C)opyright by Sebastian Krahmer.
 
2
 *** You may use it under the terms of the GPL. You should have
 
3
 *** already received the file COPYING that shows you your rights.
 
4
 *** Please look at COPYING for further license-details.
 
5
 ***  
 
6
 *** THERE IS ABSOLUTELY NO WARRANTY. SO YOU USE IT AT YOUR OWN RISK.
 
7
 *** IT WAS WRITTEN IN THE HOPE THAT IT WILL BE USEFULL. I AM NOT RESPONSIBLE
 
8
 *** FOR ANY DAMAGE YOU MAYBE GET DUE TO USING MY PROGRAMS.
 
9
 ***/
 
10
 
 
11
 
 
12
#ifndef _USI_STRUCTS_H_
 
13
#define _USI_STRUCTS_H_
 
14
 
 
15
#include <sys/types.h>
 
16
#include <stdio.h>
 
17
#include <sys/socket.h>
 
18
#include <netdb.h>
 
19
#include <netinet/in.h>
 
20
#include <arpa/inet.h>
 
21
extern "C" {
 
22
#include <pcap.h>
 
23
}
 
24
#include "usi++"
 
25
 
 
26
#ifndef MAXHOSTLEN 
 
27
#define MAXHOSTLEN 1000
 
28
#endif
 
29
 
 
30
 
 
31
/* putting an own version of
 
32
 * iphdr, udphdr, tcphdr, icmphdr and pseudohdr
 
33
 * in usipp namespace to avoid collision with
 
34
 * kernel ones. Mostly the IP etc. structs from system to system differ
 
35
 * and are often broken.
 
36
 */
 
37
 
 
38
namespace usipp {
 
39
 
 
40
#ifndef ETH_ALEN
 
41
#define ETH_ALEN 6
 
42
#endif
 
43
 
 
44
/*  This is a name for the 48 bit ethernet address available on many
 
45
 *  systems.  
 
46
 */
 
47
struct ether_addr
 
48
{
 
49
        u_int8_t ether_addr_octet[ETH_ALEN];
 
50
};
 
51
 
 
52
struct ether_header
 
53
{
 
54
        u_int8_t  ether_dhost[ETH_ALEN];        // destination eth addr 
 
55
        u_int8_t  ether_shost[ETH_ALEN];        // source ether addr    
 
56
        u_int16_t ether_type;                   // packet type ID field 
 
57
};
 
58
 
 
59
 
 
60
/*
 
61
 *      These are the defined Ethernet Protocol ID's.
 
62
 */
 
63
#define ETH_P_LOOP      0x0060          // Ethernet Loopback packet     
 
64
#define ETH_P_ECHO      0x0200          // Ethernet Echo packet         
 
65
#ifndef ETH_P_PUP
 
66
#define ETH_P_PUP       0x0200          // Xerox PUP packet             
 
67
#endif
 
68
#define ETH_P_IP        0x0800          // Internet Protocol packet     
 
69
#define ETH_P_X25       0x0805          // CCITT X.25           
 
70
#define ETH_P_ARP       0x0806          // Address Resolution packet    
 
71
#define ETH_P_BPQ       0x08FF          // G8BPQ AX.25 Ethernet Packet  [ NOT AN OFFICIALLY REGISTERED ID ] */
 
72
#define ETH_P_DEC       0x6000          // DEC Assigned proto           
 
73
#define ETH_P_DNA_DL    0x6001          // DEC DNA Dump/Load            
 
74
#define ETH_P_DNA_RC    0x6002          // DEC DNA Remote Console       
 
75
#define ETH_P_DNA_RT    0x6003          // DEC DNA Routing              
 
76
#define ETH_P_LAT       0x6004          // DEC LAT                      
 
77
#define ETH_P_DIAG      0x6005          // DEC Diagnostics              
 
78
#define ETH_P_CUST      0x6006          // DEC Customer use             
 
79
#define ETH_P_SCA       0x6007          // DEC Systems Comms Arch       
 
80
#define ETH_P_RARP      0x8035          // Reverse Addr Res packet      
 
81
#define ETH_P_ATALK     0x809B          // Appletalk DDP
 
82
#define ETH_P_AARP      0x80F3          // Appletalk AARP
 
83
#define ETH_P_IPX       0x8137          // IPX over DIX 
 
84
#define ETH_P_IPV6      0x86DD          // IPv6 over bluebook
 
85
 
 
86
/*
 
87
 *      Non DIX types. Won't clash for 1500 types.
 
88
 */
 
89
 
 
90
#define ETH_P_802_3     0x0001          // Dummy type for 802.3 frames 
 
91
#define ETH_P_AX25      0x0002          // Dummy protocol id for AX.25 
 
92
#define ETH_P_ALL       0x0003          // Every packet (be careful!!!) 
 
93
#define ETH_P_802_2     0x0004          // 802.2 frames                 
 
94
#define ETH_P_SNAP      0x0005          // Internal only        
 
95
#define ETH_P_DDCMP     0x0006          // DEC DDCMP: Internal only     
 
96
#define ETH_P_WAN_PPP   0x0007          // Dummy type for WAN PPP frames
 
97
#define ETH_P_PPP_MP    0x0008          // Dummy type for PPP MP frames 
 
98
#define ETH_P_LOCALTALK 0x0009          // Localtalk pseudo type        
 
99
#define ETH_P_PPPTALK   0x0010          // Dummy type for Atalk over PPP
 
100
#define ETH_P_TR_802_2  0x0011          // 802.2 frames                 
 
101
#define ETH_P_MOBITEX   0x0015          // Mobitex (kaz@cafe.net)
 
102
#define ETH_P_CONTROL   0x0016          // Card specific control frames 
 
103
#define ETH_P_IRDA      0x0017          // Linux/IR                     
 
104
 
 
105
 
 
106
/*  See RFC 826 for protocol description.  ARP packets are variable
 
107
 *  in size; the arphdr structure defines the fixed-length portion.
 
108
 *  Protocol type values are the same as those for 10 Mb/s Ethernet.
 
109
 *  It is followed by the variable-sized fields ar_sha, arp_spa,
 
110
 *  arp_tha and arp_tpa in that order, according to the lengths
 
111
 *  specified.  Field names used correspond to RFC 826.  
 
112
 */
 
113
struct arphdr {
 
114
        u_int16_t ar_hrd;       // Format of hardware address.  
 
115
        u_int16_t ar_pro;       // Format of protocol address.  
 
116
        unsigned char ar_hln;   // Length of hardware address.  
 
117
        unsigned char ar_pln;   // Length of protocol address.  
 
118
        u_int16_t ar_op;        // ARP opcode (command).  
 
119
#if 0
 
120
    /* Ethernet looks like this : This bit is variable sized
 
121
       however...  
 
122
     */
 
123
        unsigned char __ar_sha[ETH_ALEN];       // Sender hardware address.  
 
124
        unsigned char __ar_sip[4];              // Sender IP address.  
 
125
        unsigned char __ar_tha[ETH_ALEN];       // Target hardware address.  
 
126
        unsigned char __ar_tip[4];              // Target IP address.  
 
127
#endif
 
128
};
 
129
 
 
130
 
 
131
/* ARP protocol opcodes. */
 
132
#define ARPOP_REQUEST   1               // ARP request. 
 
133
#define ARPOP_REPLY     2               // ARP reply.  
 
134
#define ARPOP_RREQUEST  3               // RARP request.  
 
135
#define ARPOP_RREPLY    4               // RARP reply.  
 
136
 
 
137
/* ARP protocol HARDWARE identifiers. */
 
138
#define ARPHRD_NETROM   0               // From KA9Q: NET/ROM pseudo. 
 
139
#define ARPHRD_ETHER    1               // Ethernet 10/100Mbps.  
 
140
#define ARPHRD_EETHER   2               // Experimental Ethernet.  
 
141
#define ARPHRD_AX25     3               // AX.25 Level 2.  
 
142
#define ARPHRD_PRONET   4               // PROnet token ring.  
 
143
#define ARPHRD_CHAOS    5               // Chaosnet.  
 
144
#define ARPHRD_IEEE802  6               // IEEE 802.2 Ethernet/TR/TB.  
 
145
#define ARPHRD_ARCNET   7               // ARCnet.  
 
146
#define ARPHRD_APPLETLK 8               // APPLEtalk.  
 
147
#define ARPHRD_DLCI     15              // Frame Relay DLCI.  
 
148
#define ARPHRD_METRICOM 23              // Metricom STRIP (new IANA id).  
 
149
 
 
150
/* Dummy types for non ARP hardware */
 
151
#define ARPHRD_SLIP     256
 
152
#define ARPHRD_CSLIP    257
 
153
#define ARPHRD_SLIP6    258
 
154
#define ARPHRD_CSLIP6   259
 
155
#define ARPHRD_RSRVD    260             /* Notional KISS type.  */
 
156
#define ARPHRD_ADAPT    264
 
157
#define ARPHRD_ROSE     270
 
158
#define ARPHRD_X25      271             /* CCITT X.25.  */
 
159
#define ARPHRD_PPP      512
 
160
#ifndef ARPHRD_HDLC
 
161
#define ARPHRD_HDLC     513             /* (Cisco) HDLC.  */
 
162
#endif
 
163
#define ARPHRD_LAPB     516             /* LAPB.  */
 
164
 
 
165
#define ARPHRD_TUNNEL   768             /* IPIP tunnel.  */
 
166
#define ARPHRD_TUNNEL6  769             /* IPIP6 tunnel.  */
 
167
#define ARPHRD_FRAD     770             /* Frame Relay Access Device.  */
 
168
#define ARPHRD_SKIP     771             /* SKIP vif.  */
 
169
#define ARPHRD_LOOPBACK 772             /* Loopback device.  */
 
170
#define ARPHRD_LOCALTLK 773             /* Localtalk device.  */
 
171
#define ARPHRD_FDDI     774             /* Fiber Distributed Data Interface. */
 
172
#define ARPHRD_BIF      775             /* AP1000 BIF.  */
 
173
#define ARPHRD_SIT      776             /* sit0 device - IPv6-in-IPv4.  */
 
174
#define ARPHRD_IPDDP    777             /* IP-in-DDP tunnel.  */
 
175
#define ARPHRD_IPGRE    778             /* GRE over IP.  */
 
176
#define ARPHRD_PIMREG   779             /* PIMSM register interface.  */
 
177
#define ARPHRD_HIPPI    780             /* High Performance Parallel I'face. */
 
178
#define ARPHRD_ASH      781             /* (Nexus Electronics) Ash.  */
 
179
#define ARPHRD_ECONET   782             /* Acorn Econet.  */
 
180
#define ARPHRD_IRDA     783             /* Linux/IR.  */
 
181
#define ARPHRD_FCPP     784             /* Point to point fibrechanel.  */
 
182
#define ARPHRD_FCAL     785             /* Fibrechanel arbitrated loop.  */
 
183
#define ARPHRD_FCPL     786             /* Fibrechanel public loop.  */
 
184
#define ARPHRD_FCPFABRIC 787            /* Fibrechanel fabric.  */
 
185
 
 
186
/* See RFC 826 for protocol description.  Structure below is adapted
 
187
 * to resolving internet addresses.  Field names used correspond to
 
188
 * RFC 826.
 
189
 */
 
190
struct  ether_arp {
 
191
        struct  arphdr ea_hdr;          // fixed-size header 
 
192
        u_int8_t arp_sha[ETH_ALEN];     // sender hardware address 
 
193
        u_int8_t arp_spa[4];            // sender protocol address 
 
194
        u_int8_t arp_tha[ETH_ALEN];     // target hardware address 
 
195
        u_int8_t arp_tpa[4];            // target protocol address 
 
196
};
 
197
#define arp_hrd ea_hdr.ar_hrd
 
198
#define arp_pro ea_hdr.ar_pro
 
199
#define arp_hln ea_hdr.ar_hln
 
200
#define arp_pln ea_hdr.ar_pln
 
201
#define arp_op  ea_hdr.ar_op
 
202
 
 
203
 
 
204
/**/
 
205
struct icmphdr {
 
206
        u_int8_t type;
 
207
        u_int8_t code;
 
208
        u_int16_t sum;
 
209
 
 
210
        union {
 
211
                struct {
 
212
                        u_int16_t id;
 
213
                        u_int16_t sequence;
 
214
                } echo;
 
215
                u_int32_t gateway;
 
216
                struct {
 
217
                        u_int16_t unused;
 
218
                        u_int16_t mtu;
 
219
                } frag;
 
220
        } un;
 
221
};
 
222
 
 
223
#define ICMP_ECHOREPLY          0       /* Echo Reply                   */
 
224
#define ICMP_DEST_UNREACH       3       /* Destination Unreachable      */
 
225
#define ICMP_SOURCE_QUENCH      4       /* Source Quench                */
 
226
#define ICMP_REDIRECT           5       /* Redirect (change route)      */
 
227
#define ICMP_ECHO               8       /* Echo Request                 */
 
228
#define ICMP_TIME_EXCEEDED      11      /* Time Exceeded                */
 
229
#define ICMP_PARAMETERPROB      12      /* Parameter Problem            */
 
230
#define ICMP_TIMESTAMP          13      /* Timestamp Request            */
 
231
#define ICMP_TIMESTAMPREPLY     14      /* Timestamp Reply              */
 
232
#define ICMP_INFO_REQUEST       15      /* Information Request          */
 
233
#define ICMP_INFO_REPLY         16      /* Information Reply            */
 
234
#define ICMP_ADDRESS            17      /* Address Mask Request         */
 
235
#define ICMP_ADDRESSREPLY       18      /* Address Mask Reply           */
 
236
#define NR_ICMP_TYPES           18
 
237
 
 
238
 
 
239
/* Codes for UNREACH. */
 
240
#define ICMP_NET_UNREACH        0       /* Network Unreachable          */
 
241
#define ICMP_HOST_UNREACH       1       /* Host Unreachable             */
 
242
#define ICMP_PROT_UNREACH       2       /* Protocol Unreachable         */
 
243
#define ICMP_PORT_UNREACH       3       /* Port Unreachable             */
 
244
#define ICMP_FRAG_NEEDED        4       /* Fragmentation Needed/DF set  */
 
245
#define ICMP_SR_FAILED          5       /* Source Route failed          */
 
246
#define ICMP_NET_UNKNOWN        6
 
247
#define ICMP_HOST_UNKNOWN       7
 
248
#define ICMP_HOST_ISOLATED      8
 
249
#define ICMP_NET_ANO            9
 
250
#define ICMP_HOST_ANO           10
 
251
#define ICMP_NET_UNR_TOS        11
 
252
#define ICMP_HOST_UNR_TOS       12
 
253
#define ICMP_PKT_FILTERED       13      /* Packet filtered */
 
254
#define ICMP_PREC_VIOLATION     14      /* Precedence violation */
 
255
#define ICMP_PREC_CUTOFF        15      /* Precedence cut off */
 
256
#define NR_ICMP_UNREACH         15      /* instead of hardcoding immediate value */
 
257
 
 
258
/* Codes for REDIRECT. */
 
259
#define ICMP_REDIR_NET          0       /* Redirect Net                 */
 
260
#define ICMP_REDIR_HOST         1       /* Redirect Host                */
 
261
#define ICMP_REDIR_NETTOS       2       /* Redirect Net for TOS         */
 
262
#define ICMP_REDIR_HOSTTOS      3       /* Redirect Host for TOS        */
 
263
 
 
264
/* Codes for TIME_EXCEEDED. */
 
265
#define ICMP_EXC_TTL            0       /* TTL count exceeded           */
 
266
#define ICMP_EXC_FRAGTIME       1       /* Fragment Reass time exceeded */
 
267
 
 
268
#define PING_PAYLOAD_SIZE 56
 
269
#define TIMESTAMP_PAYLOAD_SIZE 12
 
270
#define ADDRMASK_PAYLOAD_SIZE 4
 
271
 
 
272
 
 
273
struct udphdr {
 
274
        u_int16_t       source;
 
275
        u_int16_t       dest;
 
276
        u_int16_t       len;
 
277
        u_int16_t       check;
 
278
};
 
279
 
 
280
/*
 
281
 *  The pseudo-header is used to calculate checksums over UDP
 
282
 *  and TCP packets.
 
283
 */
 
284
struct pseudohdr {
 
285
        u_int32_t saddr;
 
286
        u_int32_t daddr;
 
287
        u_int8_t zero;
 
288
        u_int8_t proto;
 
289
        u_int16_t len;
 
290
};
 
291
 
 
292
 
 
293
struct tcphdr
 
294
{
 
295
        u_int16_t th_sport;             // source port 
 
296
        u_int16_t th_dport;             // destination port 
 
297
        u_int32_t th_seq;               // sequence number 
 
298
        u_int32_t th_ack;               // acknowledgement number
 
299
//#if __BYTE_ORDER == __LITTLE_ENDIAN
 
300
#ifndef WORDS_BIGENDIAN
 
301
        u_int8_t th_x2:4;               // (unused) 
 
302
        u_int8_t th_off:4;              // data offset 
 
303
//#elif __BYTE_ORDER == __BIG_ENDIAN
 
304
#else
 
305
        u_int8_t th_off:4;              // data offset 
 
306
        u_int8_t th_x2:4;               // (unused) 
 
307
#endif
 
308
        u_int8_t th_flags;
 
309
#ifndef TH_FIN
 
310
#define TH_FIN  0x01
 
311
#endif
 
312
#ifndef TH_SYN
 
313
#define TH_SYN  0x02
 
314
#endif
 
315
#ifndef TH_RST
 
316
#define TH_RST  0x04
 
317
#endif
 
318
#ifndef TH_PUSH
 
319
#define TH_PUSH 0x08
 
320
#endif
 
321
#ifndef TH_ACK
 
322
#define TH_ACK  0x10
 
323
#endif
 
324
#ifndef TH_URG
 
325
#define TH_URG  0x20
 
326
#endif
 
327
        u_int16_t th_win;               // window 
 
328
        u_int16_t th_sum;               // checksum 
 
329
        u_int16_t th_urp;               // urgent pointer 
 
330
};
 
331
 
 
332
 
 
333
struct iphdr
 
334
{
 
335
//#if __BYTE_ORDER == __LITTLE_ENDIAN
 
336
#ifndef WORDS_BIGENDIAN
 
337
        u_int32_t ihl:4;
 
338
        u_int32_t version:4;
 
339
//#elif __BYTE_ORDER == __BIG_ENDIAN
 
340
#else
 
341
        u_int32_t version:4;
 
342
        u_int32_t ihl:4;
 
343
//#else
 
344
//# error       "Please fix <bits/endian.h>"
 
345
#endif
 
346
        u_int8_t tos;
 
347
        u_int16_t tot_len;
 
348
        u_int16_t id;
 
349
        u_int16_t frag_off;
 
350
#ifndef IP_RF
 
351
#define IP_RF 0x8000
 
352
#endif
 
353
#ifndef IP_DF 
 
354
#define IP_DF 0x4000
 
355
#endif
 
356
#ifndef IP_MF
 
357
#define IP_MF 0x2000
 
358
#endif
 
359
#ifndef IP_OFFMASK
 
360
#define IP_OFFMASK 0x1fff
 
361
#endif
 
362
        u_int8_t ttl;
 
363
        u_int8_t protocol;
 
364
        u_int16_t check;
 
365
        u_int32_t saddr;
 
366
        u_int32_t daddr;
 
367
    /* The options start here. */
 
368
};
 
369
 
 
370
 
 
371
/* describes a fragment for re-assembling routines
 
372
 */
 
373
struct fragments {
 
374
        int id;         // the IP id-filed
 
375
        int len;        // how much data received yet?
 
376
        int origLen;    // and how much has it to be?
 
377
        int userLen;    // and how much did we saved?
 
378
        char *data;     // the packet itself
 
379
};
 
380
 
 
381
// from netinet/tcp.h
 
382
#define TCPOPT_EOL              0
 
383
#define TCPOPT_NOP              1
 
384
#define TCPOPT_MAXSEG           2
 
385
#define TCPOLEN_MAXSEG          4
 
386
#define TCPOPT_WINDOW           3
 
387
#define TCPOLEN_WINDOW          3
 
388
#define TCPOPT_SACK_PERMITTED   4               /* Experimental */
 
389
#define TCPOLEN_SACK_PERMITTED  2
 
390
#define TCPOPT_SACK             5               /* Experimental */
 
391
#define TCPOPT_TIMESTAMP        8
 
392
#define TCPOLEN_TIMESTAMP       10
 
393
#define TCPOLEN_TSTAMP_APPA     (TCPOLEN_TIMESTAMP+2) /* appendix A */
 
394
 
 
395
 
 
396
/*
 
397
 */
 
398
union tcp_options {
 
399
                           // nothing for kind 0 and 1
 
400
        char one_byte;     // kind: 3
 
401
        u_int16_t one_word;    // kind: 2
 
402
        u_int32_t two_dwords[2]; // kind: 8 (timestamp)
 
403
        char unknown[20];  // default
 
404
};
 
405
 
 
406
 
 
407
} // namespace usipp
 
408
 
 
409
#endif  // _USI_STRUCTS_H_