-
Notifications
You must be signed in to change notification settings - Fork 49
unix: add Cstruct_unix.{read,write,writev,send,recv} #302
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
djs55
wants to merge
18
commits into
mirage:main
Choose a base branch
from
djs55:writev
base: main
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 3 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
99f6a02
unix: add Cstruct_unix.{read,write,writev,send,recv}
djs55 87382a1
Update unix/writev_stubs.c
djs55 6002721
Update unix/recv_stubs.c
djs55 2390134
recvfrom and sendto with flags
haesbaert f70b3ac
windows: ifdef for socket headers
djs55 7d77f0a
use `uerror` rather than `unix_error(errno,...)` or `caml_uerror`
djs55 04e45e2
Use old backwards-compat alloc_sockaddr, get_sockaddr
djs55 3e6d7e4
add Windows support for Unix sendto/recvfrom
djs55 284960a
Unix_cstruct.send should take a flag list
djs55 ec807a7
Unix_cstruct.recv should take a flag list.
djs55 fa5ffe7
we can assume knowledge of Cstruct.t
djs55 2cf81f3
Move declarations before statements, use uint8_t* for buf
djs55 4a04478
send/recv/read/write return ssize_t
djs55 44ec8fc
Remove strange linebreak inserted by VSCode
djs55 803ba0c
Remove unnecessary `type buffer`
djs55 aeca686
Improve .mli by linking to the Unix functions
djs55 5920235
In Unix_error, include the C stub function name stub_cstruct_foo
djs55 471ca03
$ dune build --auto-promote @fmt
djs55 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
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,50 @@ | ||
| #include <caml/mlvalues.h> | ||
| #include <caml/memory.h> | ||
| #include <caml/custom.h> | ||
| #include <caml/callback.h> | ||
| #include <caml/alloc.h> | ||
| #include <caml/unixsupport.h> | ||
| #include <caml/bigarray.h> | ||
| #include <caml/threads.h> | ||
| #include <caml/socketaddr.h> | ||
|
|
||
| #include <sys/socket.h> | ||
|
|
||
| static int msg_flag_table[] = { | ||
| MSG_OOB, MSG_DONTROUTE, MSG_PEEK /* XXX */ | ||
| }; | ||
|
|
||
| CAMLprim value stub_cstruct_recvfrom(value val_fd, value val_c, value val_flags) | ||
| { | ||
| CAMLparam3(val_fd, val_c, val_flags); | ||
| CAMLlocal5(val_buf, val_ofs, val_len, val_addr, val_res); | ||
| uint8_t *buf; | ||
| size_t len; | ||
| ssize_t n; | ||
| int cv_flags; | ||
| union sock_addr_union addr; | ||
| socklen_param_type addr_len; | ||
|
|
||
| val_buf = Field(val_c, 0); | ||
| val_ofs = Field(val_c, 1); | ||
| val_len = Field(val_c, 2); | ||
| cv_flags = caml_convert_flag_list(val_flags, msg_flag_table); | ||
|
|
||
| buf = (uint8_t *)Caml_ba_data_val(val_buf) + Long_val(val_ofs); | ||
| len = Long_val(val_len); | ||
| addr_len = sizeof(addr); | ||
|
|
||
| caml_release_runtime_system(); | ||
| n = recvfrom(Int_val(val_fd), buf, len, cv_flags, &addr.s_gen, &addr_len); | ||
| caml_acquire_runtime_system(); | ||
|
|
||
| if (n == -1) | ||
| caml_uerror("recvfrom", Nothing); | ||
|
|
||
| val_addr = caml_unix_alloc_sockaddr(&addr, addr_len, -1); | ||
| val_res = caml_alloc_small(2, 0); | ||
| Field(val_res, 0) = Val_int(n); | ||
| Field(val_res, 1) = val_addr; | ||
|
|
||
| CAMLreturn (val_res); | ||
| } | ||
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,45 @@ | ||
| #include <caml/mlvalues.h> | ||
| #include <caml/memory.h> | ||
| #include <caml/custom.h> | ||
| #include <caml/callback.h> | ||
| #include <caml/alloc.h> | ||
| #include <caml/unixsupport.h> | ||
| #include <caml/bigarray.h> | ||
| #include <caml/threads.h> | ||
| #include <caml/socketaddr.h> | ||
|
|
||
| #include <sys/socket.h> | ||
|
|
||
| static int msg_flag_table[] = { /* XXX */ | ||
| MSG_OOB, MSG_DONTROUTE, MSG_PEEK | ||
| }; | ||
|
|
||
| CAMLprim value stub_cstruct_sendto(value val_fd, value val_c, value val_flags, value val_daddr) | ||
| { | ||
| CAMLparam4(val_fd, val_c, val_flags, val_daddr); | ||
| CAMLlocal5(val_buf, val_ofs, val_len, val_addr, val_res); | ||
| union sock_addr_union addr; | ||
| socklen_param_type addr_len; | ||
| uint8_t *buf; | ||
| size_t len; | ||
| ssize_t n; | ||
| int cv_flags; | ||
|
|
||
| val_buf = Field(val_c, 0); | ||
| val_ofs = Field(val_c, 1); | ||
| val_len = Field(val_c, 2); | ||
|
|
||
| buf = (uint8_t *)Caml_ba_data_val(val_buf) + Long_val(val_ofs); | ||
| len = Long_val(val_len); | ||
| caml_unix_get_sockaddr(val_daddr, &addr, &addr_len); | ||
| cv_flags = caml_convert_flag_list(val_flags, msg_flag_table); | ||
|
|
||
| caml_release_runtime_system(); | ||
| n = sendto(Int_val(val_fd), buf, len, cv_flags, &addr.s_gen, addr_len); | ||
| caml_acquire_runtime_system(); | ||
|
|
||
| if (n == -1) | ||
| caml_uerror("sendto", Nothing); | ||
|
|
||
| CAMLreturn (Val_int(n)); | ||
| } |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -39,3 +39,9 @@ val send: Unix.file_descr -> Cstruct.t -> int | |||||
| val recv: Unix.file_descr -> Cstruct.t -> int | ||||||
| (** [recv fd c] receives a message from a socket. This can be used to receive a datagram. | ||||||
| If only a partial receive is possible, the return argument is now many bytes were received. *) | ||||||
|
|
||||||
| val recvfrom: Unix.file_descr -> Cstruct.t -> Unix.msg_flag list -> int * Unix.sockaddr | ||||||
| (** [recvfrom fd c] Like Unix.recvfrom, but for Cstruct. *) | ||||||
|
||||||
| (** [recvfrom fd c] Like Unix.recvfrom, but for Cstruct. *) | |
| (** [recvfrom fd c] Like {! Unix.recvfrom}, but for Cstruct. *) |
will link to it on the ocaml.org docs then.
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
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 do find these Unix_errors are marginally more useful if you can grep for the function they came from