Advertisement
Guest User

pcap.d

a guest
Mar 4th, 2012
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. module pcap;
  2. /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
  3. /*
  4. * Copyright (c) 1993, 1994, 1995, 1996, 1997
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. All advertising materials mentioning features or use of this software
  16. * must display the following acknowledgement:
  17. * This product includes software developed by the Computer Systems
  18. * Engineering Group at Lawrence Berkeley Laboratory.
  19. * 4. Neither the name of the University nor of the Laboratory may be used
  20. * to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * @(#) $Header: /tcpdump/master/libpcap/pcap/pcap.h,v 1.4.2.11 2008-10-06 15:38:39 gianluca Exp $ (LBL)
  36. */
  37.  
  38. import bpf;
  39.  
  40. import std.c.time;
  41.  
  42. version(Windows){
  43. //import pcapStdinc;
  44. import std.c.windows.winsock;
  45. }
  46. version(OSX){
  47. import std.c.osx.socket;
  48. }
  49. version(linux){
  50. import std.c.linux.socket;
  51. }
  52. version(FreeBSD){
  53. import std.c.freebsd.socket;
  54. }
  55. //import pcap.bpf;
  56. import std.stdio;
  57.  
  58.  
  59. extern (C) {
  60.  
  61. const PCAP_VERSION_MAJOR=2;
  62. const PCAP_VERSION_MINOR=4;
  63.  
  64. const PCAP_ERRBUF_SIZE=256;
  65.  
  66. //incomplete c types, void * does the job?
  67. alias void * pcap_t;
  68. alias void * pcap_dumper_t;
  69. alias pcap_if pcap_if_t;
  70. alias pcap_addr pcap_addr_t;
  71.  
  72. /*
  73. * The first record in the file contains saved values for some
  74. * of the flags used in the printout phases of tcpdump.
  75. * Many fields here are 32 bit ints so compilers won't insert unwanted
  76. * padding; these files need to be interchangeable across architectures.
  77. *
  78. * Do not change the layout of this structure, in any way (this includes
  79. * changes that only affect the length of fields in this structure).
  80. *
  81. * Also, do not change the interpretation of any of the members of this
  82. * structure, in any way (this includes using values other than
  83. * LINKTYPE_ values, as defined in "savefile.c", in the "linktype"
  84. * field).
  85. *
  86. * Instead:
  87. *
  88. * introduce a new structure for the new format, if the layout
  89. * of the structure changed;
  90. *
  91. * send mail to "tcpdump-workers@lists.tcpdump.org", requesting
  92. * a new magic number for your new capture file format, and, when
  93. * you get the new magic number, put it in "savefile.c";
  94. *
  95. * use that magic number for save files with the changed file
  96. * header;
  97. *
  98. * make the code in "savefile.c" capable of reading files with
  99. * the old file header as well as files with the new file header
  100. * (using the magic number to determine the header format).
  101. *
  102. * Then supply the changes as a patch at
  103. *
  104. * http://sourceforge.net/projects/libpcap/
  105. *
  106. * so that future versions of libpcap and programs that use it (such as
  107. * tcpdump) will be able to read your new capture file format.
  108. */
  109. struct pcap_file_header {
  110. uint magic;
  111. ushort version_major;
  112. ushort version_minor;
  113. int thiszone; /* gmt to local correction */
  114. uint sigfigs; /* accuracy of timestamps */
  115. uint snaplen; /* max length saved portion of each pkt */
  116. uint linktype; /* data link type (LINKTYPE_*) */
  117. }
  118.  
  119. /*
  120. * Macros for the value returned by pcap_datalink_ext().
  121. *
  122. * If LT_FCS_LENGTH_PRESENT(x) is true, the LT_FCS_LENGTH(x) macro
  123. * gives the FCS length of packets in the capture.
  124. */
  125. //not even going to try...
  126. /*#define LT_FCS_LENGTH_PRESENT(x) ((x) & 0x04000000)
  127. #define LT_FCS_LENGTH(x) (((x) & 0xF0000000) >> 28)
  128. #define LT_FCS_DATALINK_EXT(x) ((((x) & 0xF) << 28) | 0x04000000)*/
  129.  
  130. enum pcap_direction_t{
  131. PCAP_D_INOUT = 0,
  132. PCAP_D_IN,
  133. PCAP_D_OUT
  134. }
  135.  
  136. /*
  137. * Generic per-packet information, as supplied by libpcap.
  138. *
  139. * The time stamp can and should be a "struct timeval", regardless of
  140. * whether your system supports 32-bit tv_sec in "struct timeval",
  141. * 64-bit tv_sec in "struct timeval", or both if it supports both 32-bit
  142. * and 64-bit applications. The on-disk format of savefiles uses 32-bit
  143. * tv_sec (and tv_usec); this structure is irrelevant to that. 32-bit
  144. * and 64-bit versions of libpcap, even if they're on the same platform,
  145. * should supply the appropriate version of "struct timeval", even if
  146. * that's not what the underlying packet capture mechanism supplies.
  147. */
  148. struct pcap_pkthdr {
  149.  
  150. timeval ts; /* time stamp */
  151. uint caplen; /* length of portion present */
  152. uint len; /* length this packet (off wire) */
  153. }
  154.  
  155. /*
  156. * As returned by the pcap_stats()
  157. */
  158. struct pcap_stat {
  159. uint ps_recv; /* number of packets received */
  160. uint ps_drop; /* number of packets dropped */
  161. uint ps_ifdrop; /* drops by interface XXX not yet supported */
  162. //NOT SUPPORTED
  163. version(HAVE_REMOTE){
  164. uint ps_capt; /* number of packets that are received by the application; please get rid off the Win32 ifdef */
  165. uint ps_sent; /* number of packets sent by the server on the network */
  166. uint ps_netdrop; /* number of packets lost on the network */
  167. }
  168. }
  169.  
  170.  
  171. /*
  172. * Item in a list of interfaces.
  173. */
  174. struct pcap_if {
  175. pcap_if *next;
  176. char *name; /* name to hand to "pcap_open_live()" */
  177. char *description; /* textual description of interface, or NULL */
  178. pcap_addr *addresses;
  179. uint flags; /* PCAP_IF_ interface flags */
  180. }
  181.  
  182. const PCAP_IF_LOOPBACK= 0x00000001; /* interface is loopback */
  183.  
  184. /*
  185. * Representation of an interface address.
  186. */
  187. struct pcap_addr {
  188. pcap_addr *next;
  189. sockaddr *addr; /* address */
  190. sockaddr *netmask; /* netmask for that address */
  191. sockaddr *broadaddr; /* broadcast address for that address */
  192. sockaddr *dstaddr; /* P2P destination address for that address */
  193. }
  194.  
  195. /*alias void (*pcap_handler)(ubyte *, const struct pcap_pkthdr *,
  196. const ubyte *);*/
  197.  
  198. /*
  199. * Error codes for the pcap API.
  200. * These will all be negative, so you can check for the success or
  201. * failure of a call that returns these codes by checking for a
  202. * negative value.
  203. */
  204. const PCAP_ERROR = -1 ;/* generic error code */
  205. const PCAP_ERROR_BREAK = -2 ;/* loop terminated by pcap_breakloop */
  206. const PCAP_ERROR_NOT_ACTIVATED =-3 ;/* the capture needs to be activated */
  207. const PCAP_ERROR_ACTIVATED =-4 ;/* the operation can't be performed on already activated captures */
  208. const PCAP_ERROR_NO_SUCH_DEVICE =-5 ;/* no such device exists */
  209. const PCAP_ERROR_RFMON_NOTSUP = -6; /* this device doesn't support rfmon (monitor) mode */
  210. const PCAP_ERROR_NOT_RFMON =-7 ;/* operation supported only in monitor mode */
  211. const PCAP_ERROR_PERM_DENIED = -8; /* no permission to open the device */
  212. const PCAP_ERROR_IFACE_NOT_UP = -9; /* interface isn't up */
  213.  
  214. /*
  215. * Warning codes for the pcap API.
  216. * These will all be positive and non-zero, so they won't look like
  217. * errors.
  218. */
  219. const PCAP_WARNING = 1; /* generic warning code */
  220. const PCAP_WARNING_PROMISC_NOTSUP =2; /* this device doesn't support promiscuous mode */
  221.  
  222. char *pcap_lookupdev(char *);
  223. int pcap_lookupnet(const char *, uint *, uint *, char *);
  224.  
  225. pcap_t *pcap_create(const char *, char *);
  226. int pcap_set_snaplen(pcap_t *, int);
  227. int pcap_set_promisc(pcap_t *, int);
  228. int pcap_can_set_rfmon(pcap_t *);
  229. int pcap_set_rfmon(pcap_t *, int);
  230. int pcap_set_timeout(pcap_t *, int);
  231. int pcap_set_buffer_size(pcap_t *, int);
  232. int pcap_activate(pcap_t *);
  233.  
  234. pcap_t *pcap_open_live(const char *, int, int, int, char *);
  235. pcap_t *pcap_open_dead(int, int);
  236. pcap_t *pcap_open_offline(const char *, char *);
  237.  
  238. version(Windows){
  239. pcap_t *pcap_hopen_offline(void *_t, char *);
  240. static pcap_t *pcap_fopen_offline(FILE *, char *);
  241. }
  242. else{
  243. pcap_t *pcap_fopen_offline(FILE *, char *);
  244. }
  245.  
  246. void pcap_close(pcap_t *);
  247. //we don't have a definitio for pcap_handler so
  248. alias void * pcap_handler;
  249. int pcap_loop(pcap_t *, int, pcap_handler, ubyte *);
  250. int pcap_dispatch(pcap_t *, int, pcap_handler, ubyte *);
  251.  
  252. ubyte*
  253. pcap_next(pcap_t *, pcap_pkthdr *);
  254. int pcap_next_ex(pcap_t *, pcap_pkthdr **, const ubyte **);
  255. void pcap_breakloop(pcap_t *);
  256. int pcap_stats(pcap_t *, pcap_stat *);
  257. int pcap_setfilter(pcap_t *, bpf_program *);
  258. int pcap_setdirection(pcap_t *, pcap_direction_t);
  259. int pcap_getnonblock(pcap_t *, char *);
  260. int pcap_setnonblock(pcap_t *, int, char *);
  261. int pcap_inject(pcap_t *, const void *, size_t);
  262. int pcap_sendpacket(pcap_t *, const ubyte *, int);
  263. char *pcap_statustostr(int);
  264. char *pcap_strerror(int);
  265. char *pcap_geterr(pcap_t *);
  266. void pcap_perror(pcap_t *, char *);
  267. int pcap_compile(pcap_t *, bpf_program *, const char *, int,
  268. uint);
  269. int pcap_compile_nopcap(int, int, bpf_program *,
  270. const char *, int, uint);
  271. void pcap_freecode( bpf_program *);
  272. int pcap_offline_filter( bpf_program *, const pcap_pkthdr *,
  273. const ubyte *);
  274. int pcap_datalink(pcap_t *);
  275. int pcap_datalink_ext(pcap_t *);
  276. int pcap_list_datalinks(pcap_t *, int **);
  277. int pcap_set_datalink(pcap_t *, int);
  278. void pcap_free_datalinks(int *);
  279. int pcap_datalink_name_to_val(const char *);
  280. char *pcap_datalink_val_to_name(int);
  281. char *pcap_datalink_val_to_description(int);
  282. int pcap_snapshot(pcap_t *);
  283. int pcap_is_swapped(pcap_t *);
  284. int pcap_major_version(pcap_t *);
  285. int pcap_minor_version(pcap_t *);
  286.  
  287. /* XXX */
  288. FILE *pcap_file(pcap_t *);
  289. int pcap_fileno(pcap_t *);
  290.  
  291. pcap_dumper_t *pcap_dump_open(pcap_t *, const char *);
  292. pcap_dumper_t *pcap_dump_fopen(pcap_t *, FILE *fp);
  293. FILE *pcap_dump_file(pcap_dumper_t *);
  294. long pcap_dump_ftell(pcap_dumper_t *);
  295. int pcap_dump_flush(pcap_dumper_t *);
  296. void pcap_dump_close(pcap_dumper_t *);
  297. void pcap_dump(ubyte *, const pcap_pkthdr *, const ubyte *);
  298.  
  299. int pcap_findalldevs(pcap_if_t **, char *);
  300. void pcap_freealldevs(pcap_if_t *);
  301. char *pcap_lib_version();
  302.  
  303. /* XXX this guy lives in the bpf tree */
  304. uint bpf_filter(const bpf_insn *, const ubyte *, uint, uint);
  305. int bpf_validate(const bpf_insn *f, int len);
  306. char *bpf_image(const bpf_insn *, int);
  307. void bpf_dump(const bpf_program *, int);
  308.  
  309. version(Windows){
  310. /*
  311. * Win32 definitions
  312. */
  313.  
  314. int pcap_setbuff(pcap_t *p, int dim);
  315. int pcap_setmode(pcap_t *p, int mode);
  316. int pcap_setmintocopy(pcap_t *p, int size);
  317. }
  318.  
  319. const MODE_CAPT =0;
  320. const MODE_STAT =1;
  321. const MODE_MON =2;
  322.  
  323.  
  324. version(Posix){
  325.  
  326. /*
  327. * UN*X definitions
  328. */
  329.  
  330. int pcap_get_selectable_fd(pcap_t *);
  331.  
  332. }
  333.  
  334.  
  335. //NOT SUPPORTED
  336. version(HAVE_REMOTE){
  337. import remoteExt;
  338. }
  339.  
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement