[MERGED] osmo-trx[master]: Add -j option to bind to specific address
Tom Tsou has submitted this change and it was merged. Change subject: Add -j option to bind to specific address .. Add -j option to bind to specific address Before this patch, the binding of the listening sockets was hardcoded to a local IP. Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec --- M CommonLibs/Sockets.cpp M CommonLibs/Sockets.h M CommonLibs/SocketsTest.cpp M Transceiver52M/Transceiver.cpp M Transceiver52M/Transceiver.h M Transceiver52M/osmo-trx.cpp 6 files changed, 40 insertions(+), 28 deletions(-) Approvals: Tom Tsou: Looks good to me, approved Alexander Chemeris: Looks good to me, but someone else must approve Harald Welte: Looks good to me, but someone else must approve Jenkins Builder: Verified diff --git a/CommonLibs/Sockets.cpp b/CommonLibs/Sockets.cpp index 9030a86..a65d62b 100644 --- a/CommonLibs/Sockets.cpp +++ b/CommonLibs/Sockets.cpp @@ -223,18 +223,18 @@ -UDPSocket::UDPSocket(unsigned short wSrcPort) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); } -UDPSocket::UDPSocket(unsigned short wSrcPort, -const char * wDestIP, unsigned short wDestPort ) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort, +const char *wDestIP, unsigned short wDestPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); destination(wDestPort, wDestIP); } @@ -246,7 +246,7 @@ } -void UDPSocket::open(unsigned short localPort) +void UDPSocket::open(unsigned short localPort, const char *wlocalIP) { // create mSocketFD = socket(AF_INET,SOCK_DGRAM,0); @@ -265,7 +265,7 @@ size_t length = sizeof(address); bzero(,length); address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_addr.s_addr = inet_addr(wlocalIP); address.sin_port = htons(localPort); if (bind(mSocketFD,(struct sockaddr*),length)<0) { perror("bind() failed"); diff --git a/CommonLibs/Sockets.h b/CommonLibs/Sockets.h index 0a70269..8312843 100644 --- a/CommonLibs/Sockets.h +++ b/CommonLibs/Sockets.h @@ -144,11 +144,11 @@ public: /** Open a USP socket with an OS-assigned port and no default destination. */ - UDPSocket( unsigned short localPort=0); + UDPSocket(const char *localIP, unsigned short localPort); /** Given a full specification, open the socket and set the dest address. */ - UDPSocket( unsigned short localPort, - const char * remoteIP, unsigned short remotePort); + UDPSocket(const char *localIP, unsigned short localPort, + const char *remoteIP, unsigned short remotePort); /** Set the destination port. */ void destination( unsigned short wDestPort, const char * wDestIP ); @@ -157,7 +157,7 @@ unsigned short port() const; /** Open and bind the UDP socket to a local port. */ - void open(unsigned short localPort=0); + void open(unsigned short localPort=0, const char *wlocalIP="127.0.0.1"); /** Give the return address of the most recently received packet. */ const struct sockaddr_in* source() const { return (const struct sockaddr_in*)mSource; } diff --git a/CommonLibs/SocketsTest.cpp b/CommonLibs/SocketsTest.cpp index 1fa8bbd..c2849e0 100644 --- a/CommonLibs/SocketsTest.cpp +++ b/CommonLibs/SocketsTest.cpp @@ -37,7 +37,7 @@ void *testReaderIP(void *) { - UDPSocket readSocket(5934, "localhost", 5061); + UDPSocket readSocket("127.0.0.1", 5934, "localhost", 5061); readSocket.nonblocking(); int rc = 0; while (rc
osmo-trx[master]: Add -j option to bind to specific address
Patch Set 1: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/3539 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec Gerrit-PatchSet: 1 Gerrit-Project: osmo-trx Gerrit-Branch: master Gerrit-Owner: Pau Espin PedrolGerrit-Reviewer: Alexander Chemeris Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-Reviewer: Tom Tsou Gerrit-Reviewer: ttsou Gerrit-HasComments: No
osmo-trx[master]: Add -j option to bind to specific address
Patch Set 1: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/3539 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec Gerrit-PatchSet: 1 Gerrit-Project: osmo-trx Gerrit-Branch: master Gerrit-Owner: Pau Espin PedrolGerrit-Reviewer: Alexander Chemeris Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: No
osmo-trx[master]: Add -j option to bind to specific address
Patch Set 1: Code-Review+1 -- To view, visit https://gerrit.osmocom.org/3539 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec Gerrit-PatchSet: 1 Gerrit-Project: osmo-trx Gerrit-Branch: master Gerrit-Owner: Pau Espin PedrolGerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: No
[PATCH] osmo-trx[master]: Add -j option to bind to specific address
Review at https://gerrit.osmocom.org/3539 Add -j option to bind to specific address Before this patch, the binding of the listening sockets was hardcoded to a local IP. Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec --- M CommonLibs/Sockets.cpp M CommonLibs/Sockets.h M CommonLibs/SocketsTest.cpp M Transceiver52M/Transceiver.cpp M Transceiver52M/Transceiver.h M Transceiver52M/osmo-trx.cpp 6 files changed, 40 insertions(+), 28 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmo-trx refs/changes/39/3539/1 diff --git a/CommonLibs/Sockets.cpp b/CommonLibs/Sockets.cpp index 9030a86..a65d62b 100644 --- a/CommonLibs/Sockets.cpp +++ b/CommonLibs/Sockets.cpp @@ -223,18 +223,18 @@ -UDPSocket::UDPSocket(unsigned short wSrcPort) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); } -UDPSocket::UDPSocket(unsigned short wSrcPort, -const char * wDestIP, unsigned short wDestPort ) +UDPSocket::UDPSocket(const char *wSrcIP, unsigned short wSrcPort, +const char *wDestIP, unsigned short wDestPort) :DatagramSocket() { - open(wSrcPort); + open(wSrcPort, wSrcIP); destination(wDestPort, wDestIP); } @@ -246,7 +246,7 @@ } -void UDPSocket::open(unsigned short localPort) +void UDPSocket::open(unsigned short localPort, const char *wlocalIP) { // create mSocketFD = socket(AF_INET,SOCK_DGRAM,0); @@ -265,7 +265,7 @@ size_t length = sizeof(address); bzero(,length); address.sin_family = AF_INET; - address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_addr.s_addr = inet_addr(wlocalIP); address.sin_port = htons(localPort); if (bind(mSocketFD,(struct sockaddr*),length)<0) { perror("bind() failed"); diff --git a/CommonLibs/Sockets.h b/CommonLibs/Sockets.h index 0a70269..8312843 100644 --- a/CommonLibs/Sockets.h +++ b/CommonLibs/Sockets.h @@ -144,11 +144,11 @@ public: /** Open a USP socket with an OS-assigned port and no default destination. */ - UDPSocket( unsigned short localPort=0); + UDPSocket(const char *localIP, unsigned short localPort); /** Given a full specification, open the socket and set the dest address. */ - UDPSocket( unsigned short localPort, - const char * remoteIP, unsigned short remotePort); + UDPSocket(const char *localIP, unsigned short localPort, + const char *remoteIP, unsigned short remotePort); /** Set the destination port. */ void destination( unsigned short wDestPort, const char * wDestIP ); @@ -157,7 +157,7 @@ unsigned short port() const; /** Open and bind the UDP socket to a local port. */ - void open(unsigned short localPort=0); + void open(unsigned short localPort=0, const char *wlocalIP="127.0.0.1"); /** Give the return address of the most recently received packet. */ const struct sockaddr_in* source() const { return (const struct sockaddr_in*)mSource; } diff --git a/CommonLibs/SocketsTest.cpp b/CommonLibs/SocketsTest.cpp index 1fa8bbd..c2849e0 100644 --- a/CommonLibs/SocketsTest.cpp +++ b/CommonLibs/SocketsTest.cpp @@ -37,7 +37,7 @@ void *testReaderIP(void *) { - UDPSocket readSocket(5934, "localhost", 5061); + UDPSocket readSocket("127.0.0.1", 5934, "localhost", 5061); readSocket.nonblocking(); int rc = 0; while (rc