OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / Libnet / include / libnet / libnet-functions.h
1 /*
2  *  $Id: libnet-functions.h,v 1.1.1.1 2000/05/25 00:28:49 route Exp $
3  *
4  *  libnet-functions.h - Network routine library function prototype header file
5  *
6  *  Copyright (c) 1998 - 2001 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_FUNCTIONS_H
33 #define __LIBNET_FUNCTIONS_H
34
35
36 int                                     /* 1 if good, -1 if bad */
37 libnet_plist_chain_new(
38     struct libnet_plist_chain **,       /* pointer to the head of the list */
39     char *                              /* token list pointer */
40     );
41
42
43 int                                     /* 1 if more nodes, 0 if not */
44 libnet_plist_chain_next_pair(
45     struct libnet_plist_chain *,        /* pointer to the head of the list */
46     u_short *,                          /* holds bport */
47     u_short *                           /* holds eport */
48     );
49
50
51 int
52 libnet_plist_chain_dump(
53     struct libnet_plist_chain *         /* pointer to the head of the list */
54     );
55
56
57 u_char *
58 libnet_plist_chain_dump_string(
59     struct libnet_plist_chain *         /* pointer to the head of the list */
60     );
61
62
63 int
64 libnet_plist_chain_free(
65     struct libnet_plist_chain *         /* pointer to the head of the list */
66     );
67
68 /*
69  *  Standard error handling code.
70  */
71
72 void
73 libnet_error(
74     int,                /* severity */
75     char *,             /* error message */
76     ...                 /* varargs */
77     );
78
79
80 /*
81  *  Seeds the pseudorandom number generator with gettimeofday.
82  */
83
84 int
85 libnet_seed_prand();
86
87
88 /*
89  *  Returns a psuedorandom positive integer.
90  */
91
92 u_long
93 libnet_get_prand(
94     int                 /* One of the PR* constants */
95     );
96
97
98 /*
99  *  Calculates IPv4 family checksum on packet headers.
100  */
101
102 int                     /* 1 on success, -1 on failure */
103 libnet_do_checksum(
104     u_char *,           /* Pointer to the packet buffer */
105     int,                /* Protocol */
106     int                 /* Packet size */
107     );
108
109
110 /*
111  *  Network byte order into IP address
112  *  Previous versions had a memory leak (returned a strdup'd pointer -- strdup
113  *  has an implicit malloc which wasn't getting freed).  This static var hack
114  *  thingy was used to preserve existing code without having to change much.
115  *  You can simply use the return value of the function directly allowing you
116  *  to write tighter, more obvious code (rather then having to do allocate an
117  *  additional buffer for the output).
118  *  Thanks to Red for the idea.
119  */
120
121 u_char *                /* Pointer to hostname or dotted decimal IP address */
122 libnet_host_lookup(
123     u_long,             /* Network byte ordered (big endian) IP address */
124     u_short             /* Use domain names or no */
125     );
126
127
128 /*
129  *  Network byte order into IP address
130  *  Threadsafe version.
131  */
132
133 void
134 libnet_host_lookup_r(
135     u_long,             /* Network byte ordered (big endian) IP address */
136     u_short,            /* Use domain names or no */
137     u_char *            /* Pointer to hostname or dotted decimal IP address */
138     );
139
140
141 /*
142  *  IP address into network byte order
143  */
144
145 u_long                  /* Network byte ordered IP address or -1 on error */
146 libnet_name_resolve(
147     u_char *,           /* Pointer the hostname or dotted decimal IP address */
148     u_short             /* Use domain names or no */
149     );
150
151
152 /*
153  *  IP checksum wrapper.
154  */
155
156 u_short                 /* Standard IP checksum of header and data */
157 libnet_ip_check(
158     u_short *,          /* Pointer to the buffer to be summed */
159     int                 /* Packet length */
160     );
161
162
163 /*
164  *  IP checksum.
165  */
166
167 int                     /* Standard IP checksum */
168 libnet_in_cksum(
169     u_short *,          /* Pointer to the buffer to be summed */
170     int                 /* Packet length */
171     );
172
173
174 /*
175  *  Opens a socket for writing raw IP datagrams to.  Set IP_HDRINCL to let the 
176  *  kernel know we've got it all under control.
177  */
178
179 int                     /* Opened file desciptor, or -1 on error */
180 libnet_open_raw_sock(
181     int                 /* Protocol of raw socket (from /etc/protocols) */
182     );
183
184
185 int                     /* 1 upon success, or -1 on error */
186 libnet_close_raw_sock(
187     int                 /* File descriptor */
188     );
189
190
191 int
192 libnet_select_device(
193     struct sockaddr_in *,
194     char **,
195     char *
196     );
197
198 /*
199  *  Ethernet packet assembler.
200  */
201
202 int                     /* -1 on failure (null buf passed in), 1 on success */
203 libnet_build_ethernet(
204     u_char *,           /* Pointer to a 6 byte ethernet address */
205     u_char *,           /* Pointer to a 6 byte ethernet address */
206     u_short,            /* Packet IP type */
207     const u_char *,     /* Payload (or NULL) */
208     int,                /* Payload size */
209     u_char *            /* Packet header buffer */
210     );
211
212
213 /*
214  *  ARP packet assembler.
215  */
216
217 int                     /* -1 on failure (null buf passed in), 1 on success */
218 libnet_build_arp(
219     u_short,            /* hardware address type */
220     u_short,            /* protocol address type */
221     u_char,             /* hardware address length */
222     u_char,             /* protocol address length */
223     u_short,            /* ARP operation type */
224     u_char *,           /* sender hardware address */
225     u_char *,           /* sender protocol address */
226     u_char *,           /* target hardware address */
227     u_char *,           /* target protocol address */
228     const u_char *,     /* payload or NULL if none */
229     int,                /* payload length */
230     u_char *            /* packet buffer memory */
231     );
232
233 /*
234  *  TCP packet assembler.
235  */
236
237 int                     /* -1 on failure (null buf passed in), 1 on success */
238 libnet_build_tcp(
239     u_short,            /* Source port */
240     u_short,            /* Destination port */
241     u_long,             /* Sequence Number */
242     u_long,             /* Acknowledgement Number */
243     u_char,             /* Control bits */
244     u_short,            /* Advertised Window Size */
245     u_short,            /* Urgent Pointer */
246     const u_char *,     /* Pointer to packet data (or NULL) */
247     int,                /* Packet payload size */
248     u_char *            /* Pointer to packet header memory */
249     );
250
251
252 /*
253  * UDP packet assembler.
254  */
255
256 int                     /* -1 on failure (null buf passed in), 1 on success */
257 libnet_build_udp(
258     u_short,            /* Source port */
259     u_short,            /* Destination port */
260     const u_char *,     /* Pointer to packet data (or NULL) */
261     int,                /* Packet payload size */
262     u_char *            /* Pointer to packet header memory */
263     );
264
265 /*
266  *  ICMP_ECHO packet assembler.
267  */
268
269 int                     /* -1 on failure (null buf passed in), 1 on success */
270 libnet_build_icmp_echo(
271     u_char,             /* icmp type */
272     u_char,             /* icmp code */
273     u_short,            /* id */
274     u_short,            /* sequence number */
275     const u_char *,     /* Pointer to packet data (or NULL) */
276     int,                /* Packet payload size */
277     u_char *            /* Pointer to packet header memory */
278     );
279
280 /*
281  *  ICMP_MASK packet assembler.
282  */
283
284 int                     /* -1 on failure (null buf passed in), 1 on success */
285 libnet_build_icmp_mask(
286     u_char,             /* icmp type */
287     u_char,             /* icmp code */
288     u_short,            /* id */
289     u_short,            /* sequence number */
290     u_long,             /* address mask */
291     const u_char *,     /* Pointer to packet data (or NULL) */
292     int,                /* Packet payload size */
293     u_char *            /* Pointer to packet header memory */
294     );
295
296
297 /*
298  *  ICMP_UNREACH packet assembler.
299  */
300
301 int                     /* -1 on failure (null buf passed in), 1 on success */
302 libnet_build_icmp_unreach(
303     u_char,             /* icmp type */
304     u_char,             /* icmp code */
305     u_short,            /* Original Length of packet data */
306     u_char,             /* Original IP tos */
307     u_short,            /* Original IP ID */
308     u_short,            /* Original Fragmentation flags and offset */
309     u_char,             /* Original TTL */
310     u_char,             /* Original Protocol */
311     u_long,             /* Original Source IP Address */
312     u_long,             /* Original Destination IP Address */
313     const u_char *,     /* Pointer to original packet data (or NULL) */
314     int,                /* Packet payload size (or 0) */
315     u_char *            /* Pointer to packet header memory */
316     );
317
318 /*
319  *  ICMP_REDIRECT packet assembler.
320  */
321
322 int                     /* -1 on failure (null buf passed in), 1 on success */
323 libnet_build_icmp_redirect(
324     u_char,             /* icmp type */
325     u_char,             /* icmp code */
326     u_long,             /* Gateway host that should be used */
327     u_short,            /* Original Length of packet data */
328     u_char,             /* Original IP tos */
329     u_short,            /* Original IP ID */
330     u_short,            /* Original Fragmentation flags and offset */
331     u_char,             /* Original TTL */
332     u_char,             /* Original Protocol */
333     u_long,             /* Original Source IP Address */
334     u_long,             /* Original Destination IP Address */
335     const u_char *,     /* Pointer to original packet data (or NULL) */
336     int,                /* Packet payload size (or 0) */
337     u_char *            /* Pointer to packet header memory */
338     );
339
340
341 /*
342  *  ICMP_TIMXCEED packet assembler.
343  */
344
345 int                     /* -1 on failure (null buf passed in), 1 on success */
346 libnet_build_icmp_timeexceed(
347     u_char,             /* icmp type */
348     u_char,             /* icmp code */
349     u_short,            /* Original Length of packet data */
350     u_char,             /* Original IP tos */
351     u_short,            /* Original IP ID */
352     u_short,            /* Original Fragmentation flags and offset */
353     u_char,             /* Original TTL */
354     u_char,             /* Original Protocol */
355     u_long,             /* Original Source IP Address */
356     u_long,             /* Original Destination IP Address */
357     const u_char *,     /* Pointer to original packet data (or NULL) */
358     int,                /* Packet payload size (or 0) */
359     u_char *            /* Pointer to packet header memory */
360     );
361
362 /*
363  *  ICMP_TIMESTAMP packet assembler.
364  */
365
366 int                     /* -1 on failure (null buf passed in), 1 on success */
367 libnet_build_icmp_timestamp(
368     u_char,             /* icmp type */
369     u_char,             /* icmp code */
370     u_short,            /* id */
371     u_short,            /* sequence number */
372     n_time,             /* original timestamp */
373     n_time,             /* receive timestamp */
374     n_time,             /* transmit timestamp */
375     const u_char *,     /* Pointer to packet data (or NULL) */
376     int,                /* Packet payload size */
377     u_char *            /* Pointer to packet header memory */
378     );
379
380 /*
381  *  IGMP packet assembler.
382  */
383
384 int                     /* -1 on failure (null buf passed in), 1 on success */
385 libnet_build_igmp(
386     u_char,             /* igmp type */
387     u_char,             /* igmp code */
388     u_long,             /* ip addr */
389     const u_char *,     /* Pointer to packet data (or NULL) */
390     int,                /* Packet payload size */
391     u_char *            /* Pointer to packet header memory */
392     );
393
394
395 /*
396  *  IPv4 packet assembler.
397  */
398
399 int                     /* -1 on failure (null buf passed in), 1 on success */
400 libnet_build_ip(
401     u_short,            /* Length of packet data */
402     u_char,             /* IP tos */
403     u_short,            /* IP ID */
404     u_short,            /* Fragmentation flags and offset */
405     u_char,             /* TTL */
406     u_char,             /* Protocol */
407     u_long,             /* Source IP Address */
408     u_long,             /* Destination IP Address */
409     const u_char *,     /* Pointer to packet data (or NULL) */
410     int,                /* Packet payload size */
411     u_char *            /* Pointer to packet header memory */
412     );
413
414 int                     /* -1 on failure (null buf passed in), 1 on success */
415 libnet_build_ipv4(
416     u_short,            /* Length of packet data */
417     u_char,             /* IP tos */
418     u_short,            /* IP ID */
419     u_short,            /* Fragmentation flags and offset */
420     u_char,             /* TTL */
421     u_char,             /* Protocol */
422     u_long,             /* Source IP Address */
423     u_long,             /* Destination IP Address */
424     const u_char *,     /* Pointer to packet data (or NULL) */
425     int,                /* Packet payload size */
426     u_char *            /* Pointer to packet header memory */
427     );
428
429
430 /*
431  *  DNS pacekt assembler.
432  */
433
434 int                     /* -1 on failure (null buf passed in), 1 on success */
435 libnet_build_dns(
436     u_short,            /* Packet ID */
437     u_short,            /* Flags */
438     u_short,            /* Number of questions */
439     u_short,            /* Number of answer resource records */
440     u_short,            /* Number of authority resource records */
441     u_short,            /* Number of additional resource records */
442     const u_char *,     /* Payload (or NULL) */
443     int,                /* Payload size */
444     u_char *            /* Header memory */
445     );
446
447
448 /*
449  *  RIP packet assembler.
450  */
451
452 int                     /* -1 on failure (null buf passed in), 1 on success */
453 libnet_build_rip(
454     u_char,             /* Command */
455     u_char,             /* Version */
456     u_short,            /* Zero (v1) or Routing Domain (v2) */
457     u_short,            /* Address family */
458     u_short,            /* Zero (v1) or Route Tag (v2) */
459     u_long,             /* IP address */
460     u_long,             /* Zero (v1) or Subnet Mask (v2) */
461     u_long,             /* Zero (v1) or Next hop IP address (v2) */
462     u_long,             /* Metric */
463     const u_char *,     /* Payload (or NULL) */
464     int,                /* Payload size */
465     u_char *            /* Header memory */
466     );
467
468
469 /*
470  *  VRRP packet assembler.
471  */
472
473 int                     /* -1 on failure (null buf passed in), 1 on success */
474 libnet_build_vrrp(
475     u_char,
476     u_char,
477     u_char,
478     u_char,
479     u_char,
480     const u_char *,     /* Payload (or NULL) */
481     int,                /* Payload size */
482     u_char *            /* Header memory */
483     );
484
485 int
486 libnet_init_vrrp_packet(size_t,
487     u_char **,
488     u_char
489     );
490
491 /*
492  *  Insert IP options to a prebuilt IP packet.
493  */
494
495 int                     /* 1 on success, -1 on failure */
496 libnet_insert_ipo(
497     struct ipoption *,  /* Pointer to the ip options structure */ 
498     u_char,             /* IP option list size */
499     u_char *            /* Pointer to packet buf */
500     );
501
502 /*
503  *  Insert TCP options to a prebuilt IP packet.
504  */
505
506 int                     /* 1 on success, -1 on failure */
507 libnet_insert_tcpo(
508     struct tcpoption *, /* Pointer to the tcp options structure */ 
509     u_char,             /* TCP option list size */
510     u_char *            /* Pointer to packet buf */
511     );
512
513 /*
514  *  Writes a prebuild IP packet to the network with a supplied raw socket.
515  *  To write a link layer packet, use the write_link_layer function.
516  */
517
518 int                     /* number of bytes written if successful, -1 on error */
519 libnet_write_ip(
520     int sock,           /* Previously opened raw socket */
521     u_char *,           /* Pointer a complete IP datagram */
522     int                 /* Packet size */
523     );
524
525 /*
526  *  Writes a prebuild IP/ethernet packet to the network with a supplied
527  *  link_layer interface.  To write just an IP packet, use the write_link_layer
528  *  function.
529  */
530
531 int                     /* number of bytes written if successful, -1 on error */
532 libnet_write_link_layer(
533     struct libnet_link_int *,  /* Pointer to a link interface structure */
534     const char *,       /* Pointer to the device */
535     u_char *,           /* Pointer the u_char buf (the packet)to be written */
536     int                 /* Packet length */
537     );
538
539
540 /*
541  *  Opens a link layer interface.  Analogous to open_raw_sock.
542  */
543
544 struct libnet_link_int *       /* Pointer to a link layer interface struct */
545 libnet_open_link_interface(
546     char *,             /* Device name */
547     char *              /* Error buffer */
548     );
549
550
551 int                     /* 1 on success, -1 on failure */
552 libnet_close_link_interface(
553     struct libnet_link_int *   /* Pointer to a link layer interface struct */
554     );
555
556
557 char *                  /* String error message */
558 ll_strerror(
559     int                 /* Errno */
560     );
561
562
563 /*
564  *  Returns the IP address of the interface.
565  */
566
567 u_long                  /* 0 upon error, address upon success */
568 libnet_get_ipaddr(
569     struct libnet_link_int *,  /* Pointer to a link interface structure */
570     const char *,       /* Device */
571     char *              /* Error buf */
572     );
573
574
575 /*
576  *  Returns the MAC address of the interface.
577  */
578
579 struct ether_addr *     /* 0 upon error, address upon success */
580 libnet_get_hwaddr(
581     struct libnet_link_int *,  /* Pointer to a link interface structure */
582     const char *,       /* Device */
583     char *              /* Error buf */
584     );
585
586
587 /*
588  *  Simple interface for initializing a packet.
589  *  Basically a malloc wrapper.  
590  */
591
592 int                     /* -1 on error, 1 on ok */
593 libnet_init_packet(
594     int,                /* 0 and we make a good guess, otherwise you choose. */
595     u_char **           /* Pointer to the pointer to the packet */
596     );      
597
598
599 /*
600  *  Simple interface for destoying a packet.
601  *  Don't call this without a corresponding call to init_packet() first.
602  */
603
604 int                         /* -1 if arena is NULL, 1 if ok */
605 libnet_destroy_packet(
606     u_char **               /* Pointer to the packet addr. */
607     );
608
609
610 /*
611  *  Memory pool initialization routine.
612  */
613
614 int
615 libnet_init_packet_arena(
616     struct libnet_arena **, /* Pointer to an arena pointer */
617     int,                /* 0 and we make a good guess, otherwise you choose. */
618     u_short
619     );
620
621
622 /*
623  *  Returns the next chunk of memory from the pool.
624  */
625
626 u_char *
627 libnet_next_packet_from_arena(
628     struct libnet_arena **, /* Pointer to an arena pointer */
629     int                 /* 0 and we make a good guess, otherwise you choose. */
630     );
631
632
633 /*
634  *  Memory pool destructor routine.
635  */
636
637 int                         /* -1 if arena is NULL, 1 if ok */
638 libnet_destroy_packet_arena(
639     struct libnet_arena **  /* Pointer to an arena pointer */
640     );
641
642
643 /* 
644  *  More or less taken from tcpdump code.
645  */
646
647 void
648 libnet_hex_dump(
649     u_char *,               /* Packet to be dumped */
650     int,                    /* Packet size (in bytes */
651     int,                    /* To swap or not to swap */
652     FILE *                  /* Stream pointer to dump to */
653     );
654
655
656 #endif  /* __LIBNET_FUNCTIONS_H */
657
658 /* EOF */