-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreload.c
More file actions
61 lines (54 loc) · 1.19 KB
/
preload.c
File metadata and controls
61 lines (54 loc) · 1.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define _GNU_SOURCE 1
#include <nsocker/client.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <dlfcn.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
typedef int (*fn_socket)(int domain, int type, int protocol);
static fn_socket prev_socket;
static char* path;
static void init() __attribute__((constructor));
static void fini() __attribute__((destructor));
static void init()
{
prev_socket = (fn_socket) dlsym(RTLD_NEXT, "socket");
path = getenv("NSOCKER_SERVER");
if (path)
path = strdup(path);
}
static void fini()
{
free(path);
}
int socket(int domain, int type, int protocol)
{
if (domain == AF_UNIX) {
return prev_socket(domain, type, protocol);
} else {
ns_context *c = ns_get();
if (!c) {
if (path) {
if(!ns_context_push_new(path)) {
perror("connecting to NSOCKET_SERVER");
errno = EPROTO;
return -1;
}
c = ns_get();
} else {
return prev_socket(domain, type, protocol);
}
}
if (!c)
abort();
if (c->socket_client) {
return ns_client_socket(c->socket_client, domain, type, protocol);
} else {
return prev_socket(domain, type, protocol);
}
}
}