-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathotherport.c
85 lines (69 loc) · 2.28 KB
/
otherport.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dlfcn.h>
#include <string.h>
ssize_t (*real_sendto)(int socket, const void *buffer, size_t length, int flags,
const struct sockaddr *dest_addr, socklen_t dest_len);
ssize_t (*real_recvfrom)(int socket, void *buffer, size_t length, int flags,
struct sockaddr *address, socklen_t *address_len);
unsigned short old_port, new_port;
void _init(void)
{
char *old_port_env;
if (!(old_port_env = getenv("OLD_PORT"))) {
fprintf(stderr, "getenv(OLD_PORT) failed\n");
exit(42);
}
old_port = htons(atoi(old_port_env));
char *new_port_env;
if (!(new_port_env = getenv("NEW_PORT"))) {
fprintf(stderr, "getenv(NEW_PORT) failed\n");
exit(42);
}
new_port = htons(atoi(new_port_env));
const char *err;
real_sendto = dlsym(RTLD_NEXT, "sendto");
if ((err = dlerror()) != NULL) {
fprintf(stderr, "dlsym(sendto) failed: %s\n", err);
exit(42);
}
real_recvfrom = dlsym(RTLD_NEXT, "recvfrom");
if ((err = dlerror()) != NULL) {
fprintf(stderr, "dlsym(recvfrom): %s\n", err);
exit(42);
}
}
ssize_t sendto(int socket, const void *buffer, size_t length, int flags,
const struct sockaddr *dest_addr, socklen_t dest_len)
{
static struct sockaddr_in *dest_addr_in;
dest_addr_in = (struct sockaddr_in *)dest_addr;
if (dest_addr_in->sin_port == old_port) {
static struct sockaddr_storage new_dest_addr;
memcpy(&new_dest_addr, dest_addr, dest_len);
static struct sockaddr_in *new_dest_addr_in;
new_dest_addr_in = (struct sockaddr_in *)&new_dest_addr;
new_dest_addr_in->sin_port = new_port;
return real_sendto(socket, buffer, length, flags,
(struct sockaddr *)&new_dest_addr, dest_len);
}
return real_sendto(socket, buffer, length, flags, dest_addr, dest_len);
}
ssize_t recvfrom(int socket, void *buffer, size_t length, int flags,
struct sockaddr *address, socklen_t *address_len)
{
static struct sockaddr_in *address_in;
address_in = (struct sockaddr_in *)address;
if (address_in != NULL && address_in->sin_port == old_port) {
address_in->sin_port = new_port;
}
ssize_t res;
res = real_recvfrom(socket, buffer, length, flags, address,
address_len);
if (address_in != NULL && address_in->sin_port == new_port) {
address_in->sin_port = old_port;
}
return res;
}