-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadbp.c
171 lines (148 loc) · 4.58 KB
/
adbp.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <pthread.h>
#include "message.h"
#ifdef DEBUG
#define D if(1)
#else
#define D if(0)
#endif
// TODO: Refactor into library
// TODO: Better closing of FDs
// TODO: Make this multithreaded (for multiple clients)
// NOTE: 12332 is max, and +1 to null-terimate
// (allowing use of routines requiring strings)
// TODO: This was consistently largest packet len,
// but _not_ definitive
#define MAX_ADB_SERVER_PAYLOAD 16333
struct connection_args {
int inbound_fd;
int outbound_fd;
void (*handler)(adbp_forward_req);
};
// TODO: NARROW DOWN THE INCLUDES
int _bind(int port, struct sockaddr_in *address, int attrlen)
{
int fd;
int opt = 1;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address->sin_family = AF_INET;
address->sin_addr.s_addr = INADDR_ANY;
address->sin_port = htons(port);
if (bind(fd, (struct sockaddr *)address,
attrlen)<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
return fd;
}
int _connect(int port, struct sockaddr_in *address, int addrlen)
{
int fd;
if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}
address->sin_family = AF_INET;
address->sin_addr.s_addr = INADDR_ANY;
address->sin_port = htons(port);
if (connect(fd, (struct sockaddr *) address, addrlen) < 0) {
perror("socket connection failed");
exit(EXIT_FAILURE);
}
return fd;
}
int process_payload(int bytes_read, char *payload, int payload_len)
{
if (bytes_read == 0) {
D printf("Received TCP FIN.\n");
return 1;
}
if (bytes_read == -1) {
D printf("Received TCP error...Socket is likely closed.\n");
return 1;
}
D printf("TCP payload: '%.*s'\n", payload_len, payload);
return 0;
}
void *client_inbound(void *vargp)
{
int direct_transport = 0;
struct connection_args *args = (struct connection_args *)vargp;
char payload[MAX_ADB_SERVER_PAYLOAD];
while(1)
{
adbp_forward_req req;
ssize_t bytes_read = read(args->inbound_fd, &payload, sizeof(payload));
payload[bytes_read] = '\0'; // null terminate payload string
D printf("Received client payload.\n");
if (process_payload(bytes_read, &payload, bytes_read)) {
break;
}
parse_payload(&req, &payload, bytes_read);
args->handler(req);
send(args->outbound_fd, &payload, bytes_read, 0);
}
close(args->outbound_fd);
close(args->inbound_fd);
pthread_exit(NULL);
}
void adbp_start_server(int proxy_port, int adb_server_port, void (*handler)(adbp_forward_req))
{
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
int proxy_fd = _bind(proxy_port, &address, addrlen);
if (listen(proxy_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
D printf("Starting server on port %d...\n", proxy_port);
while (1) {
D printf("Accepting connections...\n");
// accept_inbound(proxy_fd, adb_server_port, (struct sockaddr *)&address, (socklen_t *) &addrlen);
int inbound_socket, valread;
if ((inbound_socket = accept(proxy_fd, &address, &addrlen)) < 0)
{
perror("accept");
exit(EXIT_FAILURE);
}
struct sockaddr_in adb_address;
int outbound_socket = _connect(adb_server_port, &adb_address, sizeof(adb_address));
struct connection_args args; // Can this pop from the heap too early? No
args.inbound_fd = inbound_socket;
args.outbound_fd = outbound_socket;
args.handler = handler;
pthread_t thread_id;
pthread_create(&thread_id, NULL, client_inbound, (void *) &args);
char payload[MAX_ADB_SERVER_PAYLOAD];
while(1)
{
ssize_t bytes_read = read(args.outbound_fd, &payload, sizeof(payload));
D printf("Received server payload.\n");
if (process_payload(bytes_read, &payload, sizeof(payload))) {
break;
}
send(args.inbound_fd, &payload, bytes_read, 0);
}
pthread_cancel(thread_id);
close(args.inbound_fd);
close(args.outbound_fd);
}
}