Skip to content

Commit

Permalink
Add recv() API wrapper
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Wiberg <[email protected]>
  • Loading branch information
troglobit committed Sep 1, 2021
1 parent 81f1a72 commit 9d6c2d7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# accept-guard

Service access control by wrapping the Linux `accept()`, `recvfrom()`,
and `recvmsg()` system calls, for TCP and UDP respectively.
Service access control (ACL) by wrapping the Linux `accept()`, `recv()`,
`recvfrom()`, and `recvmsg()` system calls, for TCP and UDP.

The accept guard wrapper allows access to services based on a list of
interfaces and ports. It is loaded using the `LD_PRELOAD` environment
Expand Down
14 changes: 14 additions & 0 deletions accept-guard.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct acl {
static struct acl acl[MAX_IFACES];

static int (*org_accept) (int, struct sockaddr *, socklen_t *);
static ssize_t (*org_recv) (int, void *, size_t, int);
static ssize_t (*org_recvfrom) (int, void *, size_t, int, struct sockaddr *, socklen_t *);
static ssize_t (*org_recvmsg) (int, struct msghdr *, int);

Expand Down Expand Up @@ -287,6 +288,19 @@ static ssize_t do_recv(int sd, int rc, int flags, int ifindex)
return rc;
}

ssize_t recv(int sd, void *buf, size_t len, int flags)
{
int ifindex;

org_recv = dlsym(RTLD_NEXT, "recv");
org_recvfrom = dlsym(RTLD_NEXT, "recvfrom");
org_recvmsg = dlsym(RTLD_NEXT, "recvmsg");

ifindex = peek_ifindex(sd);

return do_recv(sd, org_recv(sd, buf, len, flags), flags, ifindex);
}

ssize_t recvfrom(int sd, void *buf, size_t len, int flags, struct sockaddr *addr, socklen_t *addrlen)
{
int ifindex;
Expand Down
2 changes: 1 addition & 1 deletion test/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void tcp(int family, char *addr, char *port)
if (len == -1)
err(1, "Failed communicating with server at %s:%s", addr, port);

len = read(sd, buf, sizeof(buf));
len = recv(sd, buf, sizeof(buf), 0);
if (len <= 0) {
usleep(10000);
err(1, "Failed reading response from server at %s:%s", addr, port);
Expand Down
2 changes: 1 addition & 1 deletion test/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void tcp(int family, char *port)
continue;
}

n = read(client, buf, sizeof(buf));
n = recv(client, buf, sizeof(buf), 0);
if (n == -1) {
warn("Failed reading from client socket");
continue;
Expand Down

0 comments on commit 9d6c2d7

Please sign in to comment.