From bca4ec2c2e724d59daf41882cec4e5c209661209 Mon Sep 17 00:00:00 2001 From: Artem Prilutskiy Date: Sun, 10 Jul 2016 09:56:15 +0300 Subject: [PATCH] External Server port is allocated dynamically starting now --- CronosAgent.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/CronosAgent.c b/CronosAgent.c index 1236e47..b763a76 100644 --- a/CronosAgent.c +++ b/CronosAgent.c @@ -125,14 +125,14 @@ int main(int argc, const char* argv[]) print("Copyright 2016 Artem Prilutskiy (R3ABM, cyanide.burnout@gmail.com)\n"); print("\n"); - // Parameters of server + // Parameters for server const char* serverPort = STRING(REWIND_DEFAULT_PORT); const char* serverLocation = NULL; const char* serverPassword = NULL; struct addrinfo* serverAddress = NULL; - // Parameters of repeater + // Parameters for repeater int proxyTrapPort = 162; int proxyMediaPort = KAIROS_HAM_DEFAULT_PORT; @@ -154,14 +154,13 @@ int main(int argc, const char* argv[]) { "server-password", required_argument, NULL, 'w' }, { "server-address", required_argument, NULL, 's' }, { "server-port", required_argument, NULL, 'p' }, - { "media-port", required_argument, NULL, 'b' }, { "trap-port", required_argument, NULL, 't' }, { "service-mode", required_argument, NULL, 'm' }, { NULL, 0, NULL, 0 } }; int selection = 0; - while ((selection = getopt_long(argc, CAST(char* const*, argv), "n:r:c:w:s:p:b:t:m:", options, NULL)) != EOF) + while ((selection = getopt_long(argc, CAST(char* const*, argv), "n:r:c:w:s:p:t:m:", options, NULL)) != EOF) switch (selection) { case 'n': @@ -188,10 +187,6 @@ int main(int argc, const char* argv[]) serverPort = optarg; break; - case 'b': - proxyMediaPort = strtol(optarg, NULL, 10); - break; - case 't': proxyTrapPort = strtol(optarg, NULL, 10); break; @@ -215,7 +210,6 @@ int main(int argc, const char* argv[]) " --server-password \n" " --server-address \n" " --server-port \n" - " --media-port \n" " --trap-port \n" " --service-mode \n" " bit 0 - print to standard output\n" @@ -289,6 +283,7 @@ int main(int argc, const char* argv[]) int mediaHandle; int remoteHandle; struct sockaddr_in proxySocketAddress; + socklen_t proxySocketLength = sizeof(proxySocketAddress); proxySocketAddress.sin_family = AF_INET; proxySocketAddress.sin_addr.s_addr = htonl(INADDR_ANY); @@ -296,7 +291,7 @@ int main(int argc, const char* argv[]) proxySocketAddress.sin_port = 0; remoteHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if((remoteHandle < 0) || - (bind(remoteHandle, (struct sockaddr*)&proxySocketAddress, sizeof(proxySocketAddress)) < 0)) + (bind(remoteHandle, (struct sockaddr*)&proxySocketAddress, proxySocketLength) < 0)) { print("Error opening port for Remote Control\n"); return EXIT_FAILURE; @@ -305,18 +300,19 @@ int main(int argc, const char* argv[]) proxySocketAddress.sin_port = htons(proxyTrapPort); trapHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if((trapHandle < 0) || - (bind(trapHandle, (struct sockaddr*)&proxySocketAddress, sizeof(proxySocketAddress)) < 0)) + (bind(trapHandle, (struct sockaddr*)&proxySocketAddress, proxySocketLength) < 0)) { print("Error opening port for SNMP Traps\n"); return EXIT_FAILURE; } - proxySocketAddress.sin_port = htons(proxyMediaPort); + proxySocketAddress.sin_port = 0; mediaHandle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if((mediaHandle < 0) || - (bind(mediaHandle, (struct sockaddr*)&proxySocketAddress, sizeof(proxySocketAddress)) < 0)) + (bind(mediaHandle, (struct sockaddr*)&proxySocketAddress, proxySocketLength) < 0) || + (getsockname(mediaHandle, (struct sockaddr*)&proxySocketAddress, &proxySocketLength) < 0)) { - print("Error opening port for KAIROS External Server\n"); + print("Error opening port for External Server\n"); return EXIT_FAILURE; } @@ -662,7 +658,7 @@ int main(int argc, const char* argv[]) size_t length = sizeof(struct RewindAddressData); struct RewindAddressData* data = (struct RewindAddressData*)outgoingBuffer->data; data->address = information->ipi_addr; - data->port = htons(proxyMediaPort); + data->port = proxySocketAddress.sin_port; outgoingBuffer->type = htole16(REWIND_TYPE_ADDRESS_NOTICE); outgoingBuffer->number = htole32(++ sequenceNumbers[0]);