Main Page | Modules | Class List | Directories | File List | Class Members | File Members | Related Pages

libnet-functions.h

Go to the documentation of this file.
00001 /*
00002  *  $Id$
00003  *
00004  *  libnet-functions.h - Network routine library function prototype header file
00005  *
00006  *  Copyright (c) 1998, 1999, 2000 Mike D. Schiffman <mike@infonexus.com>
00007  *  All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions
00011  * are met:
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer.
00014  * 2. Redistributions in binary form must reproduce the above copyright
00015  *    notice, this list of conditions and the following disclaimer in the
00016  *    documentation and/or other materials provided with the distribution.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00019  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00022  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  */
00031 
00032 #ifndef __LIBNET_FUNCTIONS_H
00033 #define __LIBNET_FUNCTIONS_H
00034 
00035 
00036 int                                     /* 1 if good, -1 if bad */
00037 libnet_plist_chain_new(
00038     struct libnet_plist_chain **,       /* pointer to the head of the list */
00039     char *                              /* token list pointer */
00040     );
00041 
00042 
00043 int                                     /* 1 if more nodes, 0 if not */
00044 libnet_plist_chain_next_pair(
00045     struct libnet_plist_chain *,        /* pointer to the head of the list */
00046     u_short *bport,                     /* holds bport */
00047     u_short *eport                      /* holds eport */
00048     );
00049 
00050 
00051 int
00052 libnet_plist_chain_dump(
00053     struct libnet_plist_chain *         /* pointer to the head of the list */
00054     );
00055 
00056 
00057 u_char *
00058 libnet_plist_chain_dump_string(
00059     struct libnet_plist_chain *         /* pointer to the head of the list */
00060     );
00061 
00062 
00063 int
00064 libnet_plist_chain_free(
00065     struct libnet_plist_chain *         /* pointer to the head of the list */
00066     );
00067 
00068 /*
00069  *  Standard error handling code.
00070  */
00071 
00072 void
00073 libnet_error(
00074     int,                /* severity */
00075     char *,             /* error message */
00076     ...                 /* varargs */
00077     );
00078 
00079 
00080 /*
00081  *  Seeds the pseudorandom number generator with gettimeofday.
00082  */
00083 
00084 int
00085 libnet_seed_prand();
00086 
00087 
00088 /*
00089  *  Returns a psuedorandom positive integer.
00090  */
00091 
00092 u_long
00093 libnet_get_prand(
00094     int                 /* One of the PR* constants */
00095     );
00096 
00097 
00098 /*
00099  *  Calculates IPv4 family checksum on packet headers.
00100  */
00101 
00102 int                     /* 1 on success, -1 on failure */
00103 libnet_do_checksum(
00104     u_char *,           /* Pointer to the packet buffer */
00105     int,                /* Protocol */
00106     int                 /* Packet size */
00107     );
00108 
00109 
00110 /*
00111  *  Network byte order into IP address
00112  *  Previous versions had a memory leak (returned a strdup'd pointer -- strdup
00113  *  has an implicit malloc which wasn't getting freed).  This static var hack
00114  *  thingy was used to preserve existing code without having to change much.
00115  *  You can simply use the return value of the function directly allowing you
00116  *  to write tighter, more obvious code (rather then having to do allocate an
00117  *  additional buffer for the output).
00118  *  Thanks to Red for the idea.
00119  */
00120 
00121 u_char *                /* Pointer to hostname or dotted decimal IP address */
00122 libnet_host_lookup(
00123     u_long,             /* Network byte ordered (big endian) IP address */
00124     u_short             /* Use domain names or no */
00125     );
00126 
00127 
00128 /*
00129  *  Network byte order into IP address
00130  *  Threadsafe version.
00131  */
00132 
00133 void
00134 libnet_host_lookup_r(
00135     u_long,             /* Network byte ordered (big endian) IP address */
00136     u_short,            /* Use domain names or no */
00137     u_char *            /* Pointer to hostname or dotted decimal IP address */
00138     );
00139 
00140 
00141 /*
00142  *  IP address into network byte order
00143  */
00144 
00145 u_long                  /* Network byte ordered IP address or -1 on error */
00146 libnet_name_resolve(
00147     u_char *,           /* Pointer the hostname or dotted decimal IP address */
00148     u_short             /* Use domain names or no */
00149     );
00150 
00151 
00152 /*
00153  *  IP checksum wrapper.
00154  */
00155 
00156 u_short                 /* Standard IP checksum of header and data */
00157 libnet_ip_check(
00158     u_short *,          /* Pointer to the buffer to be summed */
00159     int                 /* Packet length */
00160     );
00161 
00162 
00163 /*
00164  *  IP checksum.
00165  */
00166 
00167 int                     /* Standard IP checksum */
00168 libnet_in_cksum(
00169     u_short *,          /* Pointer to the buffer to be summed */
00170     int                 /* Packet length */
00171     );
00172 
00173 
00174 /*
00175  *  Opens a socket for writing raw IP datagrams to.  Set IP_HDRINCL to let the 
00176  *  kernel know we've got it all under control.
00177  */
00178 
00179 int                     /* Opened file desciptor, or -1 on error */
00180 libnet_open_raw_sock(
00181     int                 /* Protocol of raw socket (from /etc/protocols) */
00182     );
00183 
00184 
00185 int                     /* 1 upon success, or -1 on error */
00186 libnet_close_raw_sock(
00187     int                 /* File descriptor */
00188     );
00189 
00190 
00191 int
00192 libnet_select_device(
00193     struct sockaddr_in *sin,
00194     u_char **device,
00195     u_char *ebuf
00196     );
00197 
00198 /*
00199  *  Ethernet packet assembler.
00200  */
00201 
00202 int                     /* -1 on failure (null buf passed in), 1 on success */
00203 libnet_build_ethernet(
00204     u_char *,           /* Pointer to a 6 byte ethernet address */
00205     u_char *,           /* Pointer to a 6 byte ethernet address */
00206     u_short,            /* Packet IP type */
00207     const u_char *,     /* Payload (or NULL) */
00208     int,                /* Payload size */
00209     u_char *            /* Packet header buffer */
00210     );
00211 
00212 
00213 /*
00214  *  ARP packet assembler.
00215  */
00216 
00217 int                     /* -1 on failure (null buf passed in), 1 on success */
00218 libnet_build_arp(
00219     u_short,            /* hardware address type */
00220     u_short,            /* protocol address type */
00221     u_char,             /* hardware address length */
00222     u_char,             /* protocol address length */
00223     u_short,            /* ARP operation type */
00224     u_char *,           /* sender hardware address */
00225     u_char *,           /* sender protocol address */
00226     u_char *,           /* target hardware address */
00227     u_char *,           /* target protocol address */
00228     const u_char *,     /* payload or NULL if none */
00229     int,                /* payload length */
00230     u_char *            /* packet buffer memory */
00231     );
00232 
00233 /*
00234  *  TCP packet assembler.
00235  */
00236 
00237 int                     /* -1 on failure (null buf passed in), 1 on success */
00238 libnet_build_tcp(
00239     u_short,            /* Source port */
00240     u_short,            /* Destination port */
00241     u_long,             /* Sequence Number */
00242     u_long,             /* Acknowledgement Number */
00243     u_char,             /* Control bits */
00244     u_short,            /* Advertised Window Size */
00245     u_short,            /* Urgent Pointer */
00246     const u_char *,     /* Pointer to packet data (or NULL) */
00247     int,                /* Packet payload size */
00248     u_char *            /* Pointer to packet header memory */
00249     );
00250 
00251 
00252 /*
00253  * UDP packet assembler.
00254  */
00255 
00256 int                     /* -1 on failure (null buf passed in), 1 on success */
00257 libnet_build_udp(
00258     u_short,            /* Source port */
00259     u_short,            /* Destination port */
00260     const u_char *,     /* Pointer to packet data (or NULL) */
00261     int,                /* Packet payload size */
00262     u_char *            /* Pointer to packet header memory */
00263     );
00264 
00265 /*
00266  *  ICMP_ECHO packet assembler.
00267  */
00268 
00269 int                     /* -1 on failure (null buf passed in), 1 on success */
00270 libnet_build_icmp_echo(
00271     u_char,             /* icmp type */
00272     u_char,             /* icmp code */
00273     u_short,            /* id */
00274     u_short,            /* sequence number */
00275     const u_char *,     /* Pointer to packet data (or NULL) */
00276     int,                /* Packet payload size */
00277     u_char *            /* Pointer to packet header memory */
00278     );
00279 
00280 /*
00281  *  ICMP_MASK packet assembler.
00282  */
00283 
00284 int                     /* -1 on failure (null buf passed in), 1 on success */
00285 libnet_build_icmp_mask(
00286     u_char,             /* icmp type */
00287     u_char,             /* icmp code */
00288     u_short,            /* id */
00289     u_short,            /* sequence number */
00290     u_long,             /* address mask */
00291     const u_char *,     /* Pointer to packet data (or NULL) */
00292     int,                /* Packet payload size */
00293     u_char *            /* Pointer to packet header memory */
00294     );
00295 
00296 
00297 /*
00298  *  ICMP_UNREACH packet assembler.
00299  */
00300 
00301 int                     /* -1 on failure (null buf passed in), 1 on success */
00302 libnet_build_icmp_unreach(
00303     u_char,             /* icmp type */
00304     u_char,             /* icmp code */
00305     u_short,            /* Original Length of packet data */
00306     u_char,             /* Original IP tos */
00307     u_short,            /* Original IP ID */
00308     u_short,            /* Original Fragmentation flags and offset */
00309     u_char,             /* Original TTL */
00310     u_char,             /* Original Protocol */
00311     u_long,             /* Original Source IP Address */
00312     u_long,             /* Original Destination IP Address */
00313     const u_char *,     /* Pointer to original packet data (or NULL) */
00314     int,                /* Packet payload size (or 0) */
00315     u_char *            /* Pointer to packet header memory */
00316     );
00317 
00318 /*
00319  *  ICMP_REDIRECT packet assembler.
00320  */
00321 
00322 int                     /* -1 on failure (null buf passed in), 1 on success */
00323 libnet_build_icmp_redirect(
00324     u_char,             /* icmp type */
00325     u_char,             /* icmp code */
00326     u_long,             /* Gateway host that should be used */
00327     u_short,            /* Original Length of packet data */
00328     u_char,             /* Original IP tos */
00329     u_short,            /* Original IP ID */
00330     u_short,            /* Original Fragmentation flags and offset */
00331     u_char,             /* Original TTL */
00332     u_char,             /* Original Protocol */
00333     u_long,             /* Original Source IP Address */
00334     u_long,             /* Original Destination IP Address */
00335     const u_char *,     /* Pointer to original packet data (or NULL) */
00336     int,                /* Packet payload size (or 0) */
00337     u_char *            /* Pointer to packet header memory */
00338     );
00339 
00340 
00341 /*
00342  *  ICMP_TIMXCEED packet assembler.
00343  */
00344 
00345 int                     /* -1 on failure (null buf passed in), 1 on success */
00346 libnet_build_icmp_timeexceed(
00347     u_char,             /* icmp type */
00348     u_char,             /* icmp code */
00349     u_short,            /* Original Length of packet data */
00350     u_char,             /* Original IP tos */
00351     u_short,            /* Original IP ID */
00352     u_short,            /* Original Fragmentation flags and offset */
00353     u_char,             /* Original TTL */
00354     u_char,             /* Original Protocol */
00355     u_long,             /* Original Source IP Address */
00356     u_long,             /* Original Destination IP Address */
00357     const u_char *,     /* Pointer to original packet data (or NULL) */
00358     int,                /* Packet payload size (or 0) */
00359     u_char *            /* Pointer to packet header memory */
00360     );
00361 
00362 /*
00363  *  ICMP_TIMESTAMP packet assembler.
00364  */
00365 
00366 int                     /* -1 on failure (null buf passed in), 1 on success */
00367 libnet_build_icmp_timestamp(
00368     u_char,             /* icmp type */
00369     u_char,             /* icmp code */
00370     u_short,            /* id */
00371     u_short,            /* sequence number */
00372     n_time,             /* original timestamp */
00373     n_time,             /* receive timestamp */
00374     n_time,             /* transmit timestamp */
00375     const u_char *,     /* Pointer to packet data (or NULL) */
00376     int,                /* Packet payload size */
00377     u_char *            /* Pointer to packet header memory */
00378     );
00379 
00380 /*
00381  *  IGMP packet assembler.
00382  */
00383 
00384 int                     /* -1 on failure (null buf passed in), 1 on success */
00385 libnet_build_igmp(
00386     u_char,             /* igmp type */
00387     u_char,             /* igmp code */
00388     u_long,             /* ip addr */
00389     const u_char *,     /* Pointer to packet data (or NULL) */
00390     int,                /* Packet payload size */
00391     u_char *            /* Pointer to packet header memory */
00392     );
00393 
00394 
00395 /*
00396  *  IP packet assembler.
00397  */
00398 
00399 int                     /* -1 on failure (null buf passed in), 1 on success */
00400 libnet_build_ip(
00401     u_short,            /* Length of packet data */
00402     u_char,             /* IP tos */
00403     u_short,            /* IP ID */
00404     u_short,            /* Fragmentation flags and offset */
00405     u_char,             /* TTL */
00406     u_char,             /* Protocol */
00407     u_long,             /* Source IP Address */
00408     u_long,             /* Destination IP Address */
00409     const u_char *,     /* Pointer to packet data (or NULL) */
00410     int,                /* Packet payload size */
00411     u_char *            /* Pointer to packet header memory */
00412     );
00413 
00414 
00415 /*
00416  *  DNS pacekt assembler.
00417  */
00418 
00419 int                     /* -1 on failure (null buf passed in), 1 on success */
00420 libnet_build_dns(
00421     u_short,            /* Packet ID */
00422     u_short,            /* Flags */
00423     u_short,            /* Number of questions */
00424     u_short,            /* Number of answer resource records */
00425     u_short,            /* Number of authority resource records */
00426     u_short,            /* Number of additional resource records */
00427     const u_char *,     /* Payload (or NULL) */
00428     int,                /* Payload size */
00429     u_char *            /* Header memory */
00430     );
00431 
00432 
00433 /*
00434  *  RIP packet assembler.
00435  */
00436 
00437 int                     /* -1 on failure (null buf passed in), 1 on success */
00438 libnet_build_rip(
00439     u_char,             /* Command */
00440     u_char,             /* Version */
00441     u_short,            /* Zero (v1) or Routing Domain (v2) */
00442     u_short,            /* Address family */
00443     u_short,            /* Zero (v1) or Route Tag (v2) */
00444     u_long,             /* IP address */
00445     u_long,             /* Zero (v1) or Subnet Mask (v2) */
00446     u_long,             /* Zero (v1) or Next hop IP address (v2) */
00447     u_long,             /* Metric */
00448     const u_char *,     /* Payload (or NULL) */
00449     int,                /* Payload size */
00450     u_char *            /* Header memory */
00451     );
00452 
00453 
00454 /*
00455  *  Insert IP options to a prebuilt IP packet.
00456  */
00457 
00458 int                     /* 1 on success, -1 on failure */
00459 libnet_insert_ipo(
00460     struct ipoption *,  /* Pointer to the ip options structure */ 
00461     u_char,             /* IP option list size */
00462     u_char *            /* Pointer to packet buf */
00463     );
00464 
00465 /*
00466  *  Insert TCP options to a prebuilt IP packet.
00467  */
00468 
00469 int                     /* 1 on success, -1 on failure */
00470 libnet_insert_tcpo(
00471     struct tcpoption *, /* Pointer to the tcp options structure */ 
00472     u_char,             /* TCP option list size */
00473     u_char *            /* Pointer to packet buf */
00474     );
00475 
00476 /*
00477  *  Writes a prebuild IP packet to the network with a supplied raw socket.
00478  *  To write a link layer packet, use the write_link_layer function.
00479  */
00480 
00481 int                     /* number of bytes written if successful, -1 on error */
00482 libnet_write_ip(
00483     int sock,           /* Previously opened raw socket */
00484     u_char *,           /* Pointer a complete IP datagram */
00485     int                 /* Packet size */
00486     );
00487 
00488 /*
00489  *  Writes a prebuild IP/ethernet packet to the network with a supplied
00490  *  link_layer interface.  To write just an IP packet, use the write_link_layer
00491  *  function.
00492  */
00493 
00494 int                     /* number of bytes written if successful, -1 on error */
00495 libnet_write_link_layer(
00496     struct libnet_link_int *,  /* Pointer to a link interface structure */
00497     const u_char *,     /* Pointer to the device */
00498     u_char *,           /* Pointer the u_char buf (the packet)to be written */
00499     int                 /* Packet length */
00500     );
00501 
00502 
00503 /*
00504  *  Opens a link layer interface.  Analogous to open_raw_sock.
00505  */
00506 
00507 struct libnet_link_int *       /* Pointer to a link layer interface struct */
00508 libnet_open_link_interface(
00509     char *,             /* Device name */
00510     char *              /* Error buffer */
00511     );
00512 
00513 
00514 int                     /* 1 on success, -1 on failure */
00515 libnet_close_link_interface(
00516     struct libnet_link_int *   /* Pointer to a link layer interface struct */
00517     );
00518 
00519 
00520 char *                  /* String error message */
00521 ll_strerror(
00522     int                 /* Errno */
00523     );
00524 
00525 
00526 /*
00527  *  Returns the IP address of the interface.
00528  */
00529 
00530 u_long                  /* 0 upon error, address upon success */
00531 libnet_get_ipaddr(
00532     struct libnet_link_int *,  /* Pointer to a link interface structure */
00533     const u_char *,     /* Device */
00534     char *              /* Error buf */
00535     );
00536 
00537 
00538 /*
00539  *  Returns the MAC address of the interface.
00540  */
00541 
00542 struct ether_addr *     /* 0 upon error, address upon success */
00543 libnet_get_hwaddr(
00544     struct libnet_link_int *,  /* Pointer to a link interface structure */
00545     const u_char *,     /* Device */
00546     char *              /* Error buf */
00547     );
00548 
00549 
00550 /*
00551  *  Simple interface for initializing a packet.
00552  *  Basically a malloc wrapper.  
00553  */
00554 
00555 int                     /* -1 on error, 1 on ok */
00556 libnet_init_packet(
00557     int,                /* 0 and we make a good guess, otherwise you choose. */
00558     u_char **           /* Pointer to the pointer to the packet */
00559     );      
00560 
00561 
00562 /*
00563  *  Simple interface for destoying a packet.
00564  *  Don't call this without a corresponding call to init_packet() first.
00565  */
00566 
00567 int                         /* -1 if arena is NULL, 1 if ok */
00568 libnet_destroy_packet(
00569     u_char **               /* Pointer to the packet addr. */
00570     );
00571 
00572 
00573 /*
00574  *  Memory pool initialization routine.
00575  */
00576 
00577 int
00578 libnet_init_packet_arena(
00579     struct libnet_arena **, /* Pointer to an arena pointer */
00580     int,                /* 0 and we make a good guess, otherwise you choose. */
00581     u_short
00582     );
00583 
00584 
00585 /*
00586  *  Returns the next chunk of memory from the pool.
00587  */
00588 
00589 u_char *
00590 libnet_next_packet_from_arena(
00591     struct libnet_arena **, /* Pointer to an arena pointer */
00592     int                 /* 0 and we make a good guess, otherwise you choose. */
00593     );
00594 
00595 
00596 /*
00597  *  Memory pool destructor routine.
00598  */
00599 
00600 int                         /* -1 if arena is NULL, 1 if ok */
00601 libnet_destroy_packet_arena(
00602     struct libnet_arena **  /* Pointer to an arena pointer */
00603     );
00604 
00605 
00606 /* 
00607  *  More or less taken from tcpdump code.
00608  */
00609 
00610 void
00611 libnet_hex_dump(
00612     u_char *,               /* Packet to be dumped */
00613     int,                    /* Packet size (in bytes */
00614     int,                    /* To swap or not to swap */
00615     FILE *                  /* Stream pointer to dump to */
00616     );
00617 
00618 
00619 #endif  /* __LIBNET_FUNCTIONS_H */
00620 
00621 /* EOF */

Generated on Sun May 14 14:51:19 2006 by  doxygen 1.4.2