-
Notifications
You must be signed in to change notification settings - Fork 11
Add Async support #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
georgyo
wants to merge
1
commit into
haesbaert:master
Choose a base branch
from
georgyo:async
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| open! Core | ||
| open Async | ||
| open Deferred.Let_syntax | ||
|
|
||
| let command = | ||
| Command.async ~summary:"" | ||
| (let%map_open.Command () = Log.Global.set_level_via_param () | ||
| and ifname = anon ("ifname" %: string) in | ||
| fun () -> | ||
| let link = Async_rawlink.open_link ~promisc:true ifname in | ||
| let rec loop () = | ||
| let%bind.Deferred () = | ||
| Async_rawlink.read_packet link >>| fun pkt -> | ||
| printf "got packet with %d bytes\n%!" (Cstruct.length pkt) | ||
| in | ||
| loop () | ||
| in | ||
| loop ()) | ||
|
|
||
| let () = Command_unix.run command |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| open! Core | ||
| open Async | ||
| open Deferred.Let_syntax | ||
| module Lowlevel = Rawlink_lowlevel | ||
|
|
||
| type t = { fd : Async.Fd.t; packets : Cstruct.t list ref; buffer : Cstruct.t } | ||
|
|
||
| let dhcp_server_filter = Lowlevel.dhcp_server_filter | ||
| let dhcp_client_filter = Lowlevel.dhcp_client_filter | ||
|
|
||
| let open_link ?filter ?(promisc = false) ifname = | ||
| let socket = Lowlevel.opensock ?filter ~promisc ifname in | ||
| let () = Core_unix.set_nonblock socket in | ||
| let fd = Fd.create (Fd.Kind.Socket `Active) socket (Info.of_string "link") in | ||
| { fd; packets = ref []; buffer = Cstruct.create 65536 } | ||
|
|
||
| let close_link t = Fd.close t.fd | ||
|
|
||
| let rec read_packet t = | ||
| match !(t.packets) with | ||
| | hd :: tl -> | ||
| t.packets := tl; | ||
| Deferred.return hd | ||
| | [] -> | ||
| let reader = Reader.create t.fd in | ||
| let%bind read_result = Async_cstruct.read reader t.buffer in | ||
| let (_ : unit Reader.Read_result.t) = | ||
| Reader.Read_result.map read_result ~f:(fun n -> | ||
| if n = 0 then failwith "Link socket closed"; | ||
| t.packets := Lowlevel.process_input t.buffer n) | ||
| in | ||
| read_packet t | ||
|
|
||
| let send_packet t buf = | ||
| let len = Cstruct.length buf in | ||
| let writer = Writer.create t.fd in | ||
| Writer.write_bytes ~pos:0 ~len writer (Cstruct.to_bytes ~len buf); | ||
| Writer.flushed writer | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| open! Base | ||
| open! Async | ||
|
|
||
| type t | ||
|
|
||
| val open_link : ?filter:string -> ?promisc:bool -> string -> t | ||
| (** [open_link ~filter ~promisc interface sw]. Creates a rawlink on the | ||
| specified [interface], a BPF program [filter] can be passed to | ||
| filter out incoming packets. If [promisc] is true, sets [interface] | ||
| to promiscuous mode, defaults to false.*) | ||
|
|
||
| val close_link : t -> unit Deferred.t | ||
| (** [close_link]. Closes a rawlink. *) | ||
|
|
||
| val read_packet : t -> Cstruct.t Deferred.t | ||
| (** [read_packet t]. Reads a full packet, may raise Unix.Unix_error. *) | ||
|
|
||
| val send_packet : t -> Cstruct.t -> unit Deferred.t | ||
| (** [send_packet t]. Sends a full packet, may raise Unix.Unix_error. *) | ||
|
|
||
| val dhcp_server_filter : unit -> string | ||
| (** [dhcp_server_filter]. Returns a BPF program suitable to be passed in | ||
| [open_link ~filter], it accepts UDP packets destined to | ||
| port 67 (DHCP server). *) | ||
|
|
||
| val dhcp_client_filter : unit -> string | ||
| (** [dhcp_client_filter]. Returns a BPF program suitable to be passed in | ||
| [open_link ~filter], it accepts UDP packets destined to | ||
| port 68 (DHCP client). *) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| opam-version: "2.0" | ||
| maintainer: [ | ||
| "Christiano F. Haesbaert <haesbaert@haesbaert.org>" | ||
| "George Shammas <george@shamm.as>" | ||
| ] | ||
| authors: "Christiano F. Haesbaert <haesbaert@haesbaert.org>" | ||
|
georgyo marked this conversation as resolved.
|
||
| license: "ISC" | ||
| homepage: "https://github.com/haesbaert/rawlink" | ||
| bug-reports: "https://github.com/haesbaert/rawlink/issues" | ||
| dev-repo: "git+https://github.com/haesbaert/rawlink.git" | ||
| doc: "https://haesbaert.github.io/rawlink/" | ||
| build: [ | ||
| [ "dune" "subst" ] {dev} | ||
| [ "dune" "build" "-p" name "-j" jobs ] ] | ||
| depends: [ | ||
| "ocaml" {>= "4.09.0"} | ||
| "dune" {>= "3.2"} | ||
| "rawlink" {>= "2.1"} | ||
| "cstruct-async" {>= "6.2.0"} | ||
| ] | ||
| depexts: [ | ||
| ["linux-headers"] {os-distribution = "alpine"} | ||
| ] | ||
| synopsis: "Portable library to read and write raw packets with Lwt bindings" | ||
| description: """ | ||
| Rawlink is an ocaml library for sending and receiving raw packets at the link | ||
| layer level. Sometimes you need to have full control of the packet, including | ||
| building the full ethernet frame. | ||
|
|
||
| The API is platform independent, it uses BPF on real UNIXes and AF_SOCKET on | ||
| linux. Some functionality is sacrificed so that the API is portable enough. | ||
|
|
||
| Currently BPF and AF_PACKET are implemented, including filtering capabilities. | ||
| Writing a BPF program is a pain in the ass, so no facilities are provided for | ||
| it. If you need a BPF filter, I suggest you write a small .c file with a | ||
| function that returns the BPF program as a string, check `rawlink_stubs.c` for | ||
| an example. | ||
|
|
||
| This version of Rawlink is to be used with Async, the IO functions will produce | ||
| Async.Deferred. | ||
| """ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know async at all, but is this the only way to do a write ? does it guarantee this is one system call ? These are all datagram-like file descriptors, meaning
one write = one packet.I guess this is ok but maybe there is a
single_writeand whatnot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've looked it over and it does look like I need to use Writer.write_gen_whole to ensure we don't break up the write. Unfortunately that interface seems needlessly obtuse. I'll switch it to that function shortly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
schedule_write does the correct thing, and now much more closely matches what lwt and eio are doing.