|
| 1 | +(* |
| 2 | + * Copyright (c) 2015-2022 Christiano F. Haesbaert <haesbaert@haesbaert.org> |
| 3 | + * |
| 4 | + * Permission to use, copy, modify, and distribute this software for any |
| 5 | + * purpose with or without fee is hereby granted, provided that the above |
| 6 | + * copyright notice and this permission notice appear in all copies. |
| 7 | + * |
| 8 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | + *) |
| 16 | + |
| 17 | +(** A portable API to send and receive raw packets. |
| 18 | +
|
| 19 | + There are times when one needs to construct the full ethernet frame to |
| 20 | + send/receive. Most unixes support BPF (BSD Packet Filter) to achieve it, but |
| 21 | + linux provides the same functionality via AF_SOCKET. This API works with |
| 22 | + either a BPF or AF_SOCKET backend, so it should work on every usable UNIX, |
| 23 | + as well as linux out there. |
| 24 | +*) |
| 25 | + |
| 26 | +type t |
| 27 | +(** Type of a rawlink. *) |
| 28 | + |
| 29 | +val open_link : ?filter:string -> ?promisc:bool -> string -> sw:Eio.Switch.t -> t |
| 30 | +(** [open_link ~filter ~promisc interface sw]. Creates a rawlink on the |
| 31 | + specified [interface], a BPF program [filter] can be passed to |
| 32 | + filter out incoming packets. If [promisc] is true, sets [interface] |
| 33 | + to promiscuous mode, defaults to false.*) |
| 34 | + |
| 35 | +val close_link : t -> unit |
| 36 | +(** [close_link]. Closes a rawlink. *) |
| 37 | + |
| 38 | +val read_packet : t -> Cstruct.t |
| 39 | +(** [read_packet t]. Reads a full packet, may raise Unix.Unix_error. *) |
| 40 | + |
| 41 | +val send_packet : t -> Cstruct.t -> unit |
| 42 | +(** [send_packet t]. Sends a full packet, may raise Unix.Unix_error. *) |
| 43 | + |
| 44 | +val dhcp_server_filter : unit -> string |
| 45 | +(** [dhcp_server_filter]. Returns a BPF program suitable to be passed in |
| 46 | + [open_link ~filter], it accepts UDP packets destined to |
| 47 | + port 67 (DHCP server). *) |
| 48 | + |
| 49 | +val dhcp_client_filter : unit -> string |
| 50 | +(** [dhcp_client_filter]. Returns a BPF program suitable to be passed in |
| 51 | + [open_link ~filter], it accepts UDP packets destined to |
| 52 | + port 68 (DHCP client). *) |
0 commit comments