Skip to content

Commit

Permalink
cleaned up messages
Browse files Browse the repository at this point in the history
  • Loading branch information
reinhrst committed Dec 31, 2013
1 parent e4145ad commit 673d9b5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
15 changes: 10 additions & 5 deletions notify-forwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <unistd.h>
#include "str_replace.h"
#include <errno.h>
#include <syslog.h>


#define NOTIFY_PORT 3400
#define MAX_HEADERS_SIZE 10000
Expand Down Expand Up @@ -107,6 +109,8 @@ char* receive_http(int conn) {
}

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);
Expand All @@ -125,12 +129,13 @@ int main(int argc, char** argv) {

listen(notify_socket, LISTEN_BACKLOG);

printf("waiting for connection\n");
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) {
printf("strange, no request.... %s\n", strerror(errno));
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);
Expand All @@ -156,16 +161,16 @@ int main(int argc, char** argv) {
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));
printf("connection %s --> %s(%d): forwarding %ld bytes...", inet_ntoa(sonos_box_address.sin_addr), host, port, strlen(send_buffer));
send(forward_socket, send_buffer, strlen(send_buffer), 0);
char* response = receive_http(forward_socket);
if (!response) {
printf("no response (%s)\n", strerror(errno));
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;
}
printf("replying %ld bytes\n", strlen(response));
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);
Expand Down
10 changes: 6 additions & 4 deletions sonos-forwarder.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <time.h>
#include <unistd.h>
#include "str_replace.h"
#include <syslog.h>

#define RECEIVEBUFFER_SIZE 1E3
#define SSDP_PORT 1900
Expand Down Expand Up @@ -48,6 +49,7 @@ int create_bounded_socket(struct sockaddr_in sock_address) {
}

int main(int argc, char** argv) {
openlog("sonos-f", LOG_PID|LOG_CONS, LOG_DAEMON);

int ssdp_socket, forward_reply_socket;
struct sockaddr_in ssdp_socket_address = {0}, forward_reply_socket_address = {0};
Expand Down Expand Up @@ -85,26 +87,26 @@ int main(int argc, char** argv) {
int nr_bytes_received;
nr_bytes_received = recvfrom(ssdp_socket, receive_buffer, RECEIVEBUFFER_SIZE, 0, (struct sockaddr*)&remote_address, &remote_address_length);
if(strstr(receive_buffer, "ZonePlayer") != NULL) {
printf("Message %s --> internal\n", inet_ntoa(remote_address.sin_addr));
struct sockaddr_in passthrough_socket_address = {0};
fill_socketaddr_in(&passthrough_socket_address, 0, inside_nat_ip_address);
int passthrough_socket = create_bounded_socket(passthrough_socket_address);
sendto(passthrough_socket, receive_buffer, nr_bytes_received, 0, (struct sockaddr*)&ssdp_socket_address, sizeof(ssdp_socket_address));
if (setsockopt(passthrough_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
perror("Error");
exit(1);
}
nr_bytes_received = recv(passthrough_socket, receive_buffer, RECEIVEBUFFER_SIZE, 0);
if (nr_bytes_received <= 0) {
printf("no reply\n");
syslog(LOG_DEBUG, "Message %s --> internal", inet_ntoa(remote_address.sin_addr));
} else {
int send_buffer_size = nr_bytes_received - strlen(to_replace) + strlen(replace_with) + 1;
char* send_buffer = str_replace(receive_buffer, to_replace, replace_with, STR_REPLACE_REPLACE_ALL);
sendto(forward_reply_socket, send_buffer, send_buffer_size, 0, (struct sockaddr*) &remote_address, remote_address_length);
free(send_buffer);
printf("Message %s <-- internal\n", inet_ntoa(remote_address.sin_addr));
syslog(LOG_DEBUG, "Message %s <-> internal", inet_ntoa(remote_address.sin_addr));
}
} else {
printf("Message %s not routed\n", inet_ntoa(remote_address.sin_addr));
syslog(LOG_DEBUG, "Message %s not routed", inet_ntoa(remote_address.sin_addr));
}
}
}

0 comments on commit 673d9b5

Please sign in to comment.