-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify-forwarder.c
182 lines (161 loc) · 6.88 KB
/
notify-forwarder.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
172
173
174
175
176
177
178
179
180
181
182
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "str_replace.h"
#include <errno.h>
#include <syslog.h>
#define NOTIFY_PORT 3400
#define MAX_HEADERS_SIZE 10000
#define REPLY_TIMEOUT_USEC 5E5
#define LISTEN_BACKLOG 5
#define HOST_LINE "\r\nHOST: "
#define CONTENT_LENGTH_LINE "\r\nCONTENT-LENGTH:"
void fill_socketaddr_in(struct sockaddr_in* sock_address, uint16_t port, const char* ip_address) {
sock_address->sin_family = AF_INET;
sock_address->sin_port = htons(port);
if (inet_pton(AF_INET, ip_address, &sock_address->sin_addr) != 1) {
fprintf(stderr, "Not a valid address: %s\n", ip_address);
exit(1);
}
}
int create_bounded_tcp_socket(struct sockaddr_in sock_address) {
int sock;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("cannot create socket");
exit(1);
}
if (bind(sock, (struct sockaddr *)&sock_address, sizeof(sock_address)) < 0) {
perror("bind failed");
exit(1);
}
return sock;
}
char* get_host(const char* buffer) {
char* host_start = strstr(buffer, HOST_LINE) + strlen(HOST_LINE);
char* host_end = strstr(host_start, ":");
char* host = malloc((host_end - host_start + 1));
strncpy(host, host_start, host_end - host_start);
host[host_end - host_start] = '\0';
return host;
}
unsigned short get_content_length(const char* buffer) {
char* cl_start = strstr(buffer, CONTENT_LENGTH_LINE);
if (!cl_start) {
return 0;
}
return (unsigned short) strtol(cl_start + strlen(CONTENT_LENGTH_LINE), NULL, 10);
}
unsigned short get_port(const char* buffer) {
char* host_start = strstr(buffer, HOST_LINE) + strlen(HOST_LINE);
char* host_end = strstr(host_start, ":");
return (unsigned short) strtol(host_end + 1, NULL, 10);
}
char* receive_http(int conn) {
int nr_bytes_received=0;
char* headers = malloc(MAX_HEADERS_SIZE * sizeof(char));
while(1) {
if (nr_bytes_received == MAX_HEADERS_SIZE) {
headers[MAX_HEADERS_SIZE - 1] = '\0';
fprintf(stderr, "No header: %s\n", headers);
exit(1);
}
int nr_bytes = recvfrom(conn, headers + nr_bytes_received, MAX_HEADERS_SIZE - nr_bytes_received - 1, 0, NULL, NULL);
if (nr_bytes < 0) {
free(headers);
return NULL;
}
nr_bytes_received += nr_bytes;
headers[nr_bytes_received] = '\0';
if (strstr(headers, "\r\n\r\n")) {
break;
}
usleep(5E4);
}
int header_length = strstr(headers, "\r\n\r\n") - headers + 4;
int body_length = get_content_length(headers);
int alloc_size = header_length + body_length + 1;
char* data = realloc(headers, alloc_size * sizeof(char));
data[alloc_size-1] = '\0';
while (nr_bytes_received != header_length + body_length) {
int nr_bytes = recvfrom(conn, data+nr_bytes_received, alloc_size-nr_bytes_received, 0, NULL, NULL);
usleep(5E4);
if (nr_bytes < 0) {
free(headers);
return NULL;
}
nr_bytes_received += nr_bytes;
}
return data;
}
int main(int argc, char** argv) {
openlog("sonos-n", LOG_PID|LOG_CONS, LOG_DAEMON);
int notify_socket, forward_socket, sonos_connection;
struct sockaddr_in notify_socket_address = {0}, forward_destination_address = {0}, sonos_box_address;
socklen_t sonos_box_address_length=sizeof(sonos_box_address);
struct timeval timeout = {0, REPLY_TIMEOUT_USEC};
if (argc != 4) {
fprintf(stderr, "Call with 3 arguments:\n%s external-interface-ip-address internal-interface-ip-address sonos-box-ip-address", argv[0]);
}
char* outside_nat_ip_address = argv[1];
char* inside_nat_ip_address = argv[2];
char* sonos_box_ip_address = argv[3];
fill_socketaddr_in(¬ify_socket_address, NOTIFY_PORT, inside_nat_ip_address);
notify_socket = create_bounded_tcp_socket(notify_socket_address);
listen(notify_socket, LISTEN_BACKLOG);
syslog(LOG_DEBUG, "waiting for connection");
while (1) {
sonos_connection = accept(notify_socket, (struct sockaddr *)&sonos_box_address, &sonos_box_address_length);
char* request = receive_http(sonos_connection);
if (!request) {
syslog(LOG_ERR, "strange, no request.... %s", strerror(errno));
sleep(10); // sleep a while to avoid flooding the log with these errors
continue;
}
char* send_buffer = str_replace(request, sonos_box_ip_address, outside_nat_ip_address, STR_REPLACE_REPLACE_ALL);
long lengthdifference = strlen(send_buffer) - strlen(request);
if (lengthdifference) {
//update the content-length header
int oldlength = get_content_length(send_buffer);
char* cl_start = strstr(send_buffer, CONTENT_LENGTH_LINE);
char* cl_end = strstr(cl_start + strlen(CONTENT_LENGTH_LINE), "\r\n");
char* replace_from = calloc(cl_end - cl_start + 1, sizeof(char));
strncpy(replace_from, cl_start, cl_end-cl_start);
char* replace_with;
asprintf(&replace_with, "%s %ld", CONTENT_LENGTH_LINE, oldlength + lengthdifference);
char* new_send_buffer = str_replace(send_buffer, replace_from, replace_with, 1);
free(replace_from);
free(replace_with);
free(send_buffer);
send_buffer = new_send_buffer;
}
char* host = get_host(send_buffer);
unsigned short port = get_port(send_buffer);
fill_socketaddr_in(&forward_destination_address, port, host);
forward_socket = socket(AF_INET, SOCK_STREAM, 0);
if (setsockopt(forward_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
perror("Error");
exit(1);
}
connect(forward_socket, (struct sockaddr*) &forward_destination_address, sizeof(forward_destination_address));
send(forward_socket, send_buffer, strlen(send_buffer), 0);
char* response = receive_http(forward_socket);
if (!response) {
syslog(LOG_NOTICE, "connection %s --> %s(%d): forwarding %ld bytes... no response (%s)", inet_ntoa(sonos_box_address.sin_addr), host, port, strlen(send_buffer), strerror(errno));
continue;
}
syslog(LOG_DEBUG, "connection %s --> %s(%d): forwarding %ld bytes... replying %ld bytes", inet_ntoa(sonos_box_address.sin_addr), host, port, strlen(send_buffer), strlen(response));
send(sonos_connection, response, strlen(response), 0);
close(forward_socket);
close(sonos_connection);
free(host);
free(send_buffer);
free(request);
free(response);
}
}