Skip to content

Commit

Permalink
Added hooks to libdesock
Browse files Browse the repository at this point in the history
  • Loading branch information
pd-fkie committed Jun 11, 2022
1 parent 14c2a4b commit 51f6cce
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 67 deletions.
9 changes: 9 additions & 0 deletions libdesock/include/hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef HOOKS_H
#define HOOKS_H

#include <stdlib.h>

ssize_t hook_input (char*, size_t);
ssize_t hook_output (char*, size_t);

#endif
4 changes: 2 additions & 2 deletions libdesock/src/accept.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include <string.h>
#include <netinet/in.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

static int internal_accept (int fd, struct sockaddr* restrict addr, socklen_t * restrict len, int flag) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/bind.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#define _GNU_SOURCE
#include <unistd.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

visible int bind (int fd, const struct sockaddr* addr, socklen_t len) {
if (VALID_FD (fd) && DESOCK_FD (fd)) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <errno.h>
#include <semaphore.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

visible int close (int fd) {
int r = 0;
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <unistd.h>
#include <sys/socket.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

visible int connect (int fd, const struct sockaddr* addr, socklen_t len) {
if (VALID_FD (fd) && DESOCK_FD (fd)) {
Expand Down
16 changes: 14 additions & 2 deletions libdesock/src/desock.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include <netinet/in.h>
#include <sys/un.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

const struct sockaddr_in stub_sockaddr_in = {
.sin_family = AF_INET,
Expand All @@ -26,6 +26,9 @@ const struct sockaddr_in6 stub_sockaddr_in6 = {
.sin6_scope_id = 0
};

/* Given an fd that is being desocketed fill the given sockaddress structure
with the right sockaddr stub from above.
*/
void fill_sockaddr (int fd, struct sockaddr* addr, socklen_t * addr_len) {
if (addr && addr_len) {
switch (fd_table[fd].domain) {
Expand Down Expand Up @@ -71,9 +74,18 @@ void _error (char* fmt_string, ...) {
abort ();
}

/* Highest file descriptor number seen so far */
int max_fd = 0;

/* Indicates whether the next call to accept() should block or not */
int accept_block = 1;

/* Table that holds metadata about desocketed file descriptors */
struct fd_entry fd_table[FD_TABLE_SIZE];

/* Semaphore for synchronization of the connection pool in multi-threaded
applications.
*/
sem_t sem;

__attribute__ ((constructor))
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/dup.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <unistd.h>
#include <string.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

visible int dup (int fd) {
int ret = syscall (SYS_dup, fd);
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <semaphore.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

#ifdef DEBUG
visible int epoll_create (int size) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/getpeername.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <string.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

visible int getpeername (int fd, struct sockaddr* restrict addr, socklen_t * restrict len) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/getsockname.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <string.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

visible int getsockname (int fd, struct sockaddr* restrict addr, socklen_t * restrict len) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
31 changes: 31 additions & 0 deletions libdesock/src/hooks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* The default action for libdesock is to
* read from stdin / write to stdout
* but this behaviour can be changed with the
* following to functions.
* They have to behave like glibc functions:
* On success they must return a value >= 0 indicating
* how many bytes have been read / written.
* On failure they must return -1 with errno set to the
* corresponding error.
*/

#include "hooks.h"
#include "syscall.h"

/* This function is called whenever a read on a network
* connection occurs. Read from stdin instead.
*/
ssize_t hook_input (char* buf, size_t size) {
return syscall_cp(SYS_read, 0, buf, size);
}

/* This function is called whenever a write on a network
* connection occurs. Write to stdout instead.
*/
ssize_t hook_output (char* buf, size_t size) {
#ifdef DEBUG
return syscall_cp(SYS_write, 1, buf, size);
#else
return (ssize_t) size;
#endif
}
4 changes: 2 additions & 2 deletions libdesock/src/listen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include <sys/socket.h>
#include <unistd.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

visible int listen (int fd, int backlog) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
6 changes: 3 additions & 3 deletions libdesock/src/peekbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <errno.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include <peekbuffer.h>
#include "desock.h"
#include "syscall.h"
#include "peekbuffer.h"

char static_buffer[STATIC_BUFFER_SIZE];

Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <signal.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

static int internal_poll (struct pollfd* fds, nfds_t n) {
DEBUG_LOG ("[%d] desock::internal_poll(%p, %d)", gettid (), fds, n);
Expand Down
13 changes: 7 additions & 6 deletions libdesock/src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include <string.h>
#include <errno.h>

#include <syscall.h>
#include <desock.h>
#include <peekbuffer.h>
#include "syscall.h"
#include "desock.h"
#include "peekbuffer.h"
#include "hooks.h"

static long internal_readv (struct iovec* iov, int len, int* full, int peek, int offset) {
int read_in = 0;
Expand All @@ -30,7 +31,7 @@ static long internal_readv (struct iovec* iov, int len, int* full, int peek, int

if (r < iov[i].iov_len) {
errno = 0;
r += syscall_cp (SYS_read, 0, (char *) iov[i].iov_base + r, iov[i].iov_len - r);
r += hook_input((char *) iov[i].iov_base + r, iov[i].iov_len - r);

if (errno) {
return -1;
Expand Down Expand Up @@ -64,7 +65,7 @@ visible ssize_t read (int fd, void* buf, size_t count) {

if (offset < count) {
errno = 0;
offset += syscall_cp (SYS_read, 0, (char *) buf + offset, count - offset);
offset += hook_input((char *) buf + offset, count - offset);

if (errno) {
DEBUG_LOG (" = -1\n");
Expand Down Expand Up @@ -97,7 +98,7 @@ static ssize_t internal_recv (int fd, char* buf, size_t len, int flags) {

if (offset < len) {
errno = 0;
offset += (syscall_cp (SYS_read, 0, buf + offset, len - offset));
offset += hook_input(buf + offset, len - offset);

if (errno) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/select.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <string.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

static int internal_select (int n, fd_set * rfds, fd_set * wfds, fd_set * efds) {
DEBUG_LOG ("[%d] desock::internal_select(%d, %p, %p, %p)", gettid (), n, rfds, wfds, efds);
Expand Down
6 changes: 3 additions & 3 deletions libdesock/src/sendfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#include <sys/types.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include <musl-features.h>
#include "desock.h"
#include "syscall.h"
#include "musl-features.h"

visible ssize_t sendfile (int out_fd, int in_fd, off_t * ofs, size_t count) {
if (VALID_FD (out_fd) && fd_table[out_fd].desock) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/shutdown.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <sys/socket.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

visible int shutdown (int fd, int how) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include <unistd.h>
#include <string.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"

visible int socket (int domain, int type, int protocol) {
DEBUG_LOG ("[%d] desock::socket(%d, %d, %d)", gettid (), domain, type, protocol);
Expand Down
4 changes: 2 additions & 2 deletions libdesock/src/sockopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <string.h>
#include <unistd.h>

#include <desock.h>
#include <syscall.h>
#include "desock.h"
#include "syscall.h"

visible int setsockopt (int fd, int level, int optname, const void* optval, socklen_t optlen) {
if (VALID_FD (fd) && fd_table[fd].desock) {
Expand Down
2 changes: 1 addition & 1 deletion libdesock/src/syscall.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include <syscall.h>
#include "syscall.h"

hidden long __syscall_cp_c ();

Expand Down
29 changes: 7 additions & 22 deletions libdesock/src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
#include <errno.h>
#include <sys/socket.h>

#include <syscall.h>
#include <desock.h>
#include "syscall.h"
#include "desock.h"
#include "hooks.h"

static long internal_writev (const struct iovec* iov, int len) {
int written = 0;

for (int i = 0; i < len; ++i) {
#ifdef DEBUG
int offset = 0, r;

do {
r = syscall_cp (SYS_write, 1, (char *) iov[i].iov_base + offset, iov[i].iov_len - offset);
r = hook_output((char *) iov[i].iov_base + offset, iov[i].iov_len - offset);

if (r == -1) {
return -1;
Expand All @@ -24,51 +24,36 @@ static long internal_writev (const struct iovec* iov, int len) {
written += r;
offset += r;
} while (offset < iov[i].iov_len && r > 0);
#else
written += iov[i].iov_len;
#endif
}

return written;
}

visible ssize_t write (int fd, const void* buf, size_t count) {
if (VALID_FD (fd) && fd_table[fd].desock) {
#ifdef DEBUG
int r = syscall_cp (SYS_write, 1, buf, count);
int r = hook_output(buf, count);
DEBUG_LOG ("[%d] desock::write(%d, %p, %lu) = %d\n", gettid (), fd, buf, count, r);
return r;
#else
return count;
#endif
} else {
return syscall_cp (SYS_write, fd, buf, count);
}
}

visible ssize_t send (int fd, const void* buf, size_t len, int flags) {
if (VALID_FD (fd) && fd_table[fd].desock) {
#ifdef DEBUG
int r = syscall_cp (SYS_write, 1, buf, len);
int r = hook_output(buf, len);
DEBUG_LOG ("[%d] desock::send(%d, %p, %lu, %d) = %d\n", gettid (), fd, buf, len, flags, r);
return r;
#else
return len;
#endif
} else {
return sendto (fd, buf, len, flags, 0, 0);
}
}

visible ssize_t sendto (int fd, const void* buf, size_t len, int flags, const struct sockaddr* addr, socklen_t alen) {
if (VALID_FD (fd) && fd_table[fd].desock) {
#ifdef DEBUG
int r = syscall_cp (SYS_write, 1, buf, len);
int r = hook_output(buf, len);
DEBUG_LOG ("[%d] desock::sendto(%d, %p, %lu, %d, %p, %lu) = %d\n", gettid (), fd, buf, len, flags, addr, alen, r);
return r;
#else
return len;
#endif
} else {
return socketcall_cp (sendto, fd, buf, len, flags, addr, alen);
}
Expand Down
Loading

0 comments on commit 51f6cce

Please sign in to comment.