[PATCH] osmo-trx[master]: Add -j option to bind to specific address

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Pau Espin Pedrol gerrit-no-reply at lists.osmocom.org
Wed Aug 16 16:05:26 UTC 2017


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(&address,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*)&address,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<gNumToSend) {
@@ -82,7 +82,7 @@
   Thread readerThreadUnix;
   readerThreadUnix.start(testReaderUnix,NULL);
 
-  UDPSocket socket1(5061, "127.0.0.1",5934);
+  UDPSocket socket1("127.0.0.1", 5061, "127.0.0.1", 5934);
   UDDSocket socket1U("testSource","testDestination");
   
   COUT("socket1: " << socket1.port());
diff --git a/Transceiver52M/Transceiver.cpp b/Transceiver52M/Transceiver.cpp
index bf2dd6e..d9c231d 100644
--- a/Transceiver52M/Transceiver.cpp
+++ b/Transceiver52M/Transceiver.cpp
@@ -112,13 +112,14 @@
 }
 
 Transceiver::Transceiver(int wBasePort,
-                         const char *wTRXAddress,
+                         const char *TRXAddress,
+                         const char *GSMcoreAddress,
                          size_t tx_sps, size_t rx_sps, size_t chans,
                          GSM::Time wTransmitLatency,
                          RadioInterface *wRadioInterface,
                          double wRssiOffset)
-  : mBasePort(wBasePort), mAddr(wTRXAddress),
-    mClockSocket(wBasePort, wTRXAddress, mBasePort + 100),
+  : mBasePort(wBasePort), mLocalAddr(TRXAddress), mRemoteAddr(GSMcoreAddress),
+    mClockSocket(TRXAddress, wBasePort, GSMcoreAddress, wBasePort + 100),
     mTransmitLatency(wTransmitLatency), mRadioInterface(wRadioInterface),
     rssiOffset(wRssiOffset),
     mSPSTx(tx_sps), mSPSRx(rx_sps), mChans(chans), mEdge(false), mOn(false), mForceClockInterface(false),
@@ -197,8 +198,8 @@
     d_srcport = mBasePort + 2 * i + 2;
     d_dstport = mBasePort + 2 * i + 102;
 
-    mCtrlSockets[i] = new UDPSocket(c_srcport, mAddr.c_str(), c_dstport);
-    mDataSockets[i] = new UDPSocket(d_srcport, mAddr.c_str(), d_dstport);
+    mCtrlSockets[i] = new UDPSocket(mLocalAddr.c_str(), c_srcport, mRemoteAddr.c_str(), c_dstport);
+    mDataSockets[i] = new UDPSocket(mLocalAddr.c_str(), d_srcport, mRemoteAddr.c_str(), d_dstport);
   }
 
   /* Randomize the central clock */
diff --git a/Transceiver52M/Transceiver.h b/Transceiver52M/Transceiver.h
index 1eb1d1d..ad7a469 100644
--- a/Transceiver52M/Transceiver.h
+++ b/Transceiver52M/Transceiver.h
@@ -91,13 +91,15 @@
 public:
   /** Transceiver constructor
       @param wBasePort base port number of UDP sockets
-      @param TRXAddress IP address of the TRX manager, as a string
+      @param TRXAddress IP address of the TRX, as a string
+      @param GSMcoreAddress IP address of the GSM core, as a string
       @param wSPS number of samples per GSM symbol
       @param wTransmitLatency initial setting of transmit latency
       @param radioInterface associated radioInterface object
   */
   Transceiver(int wBasePort,
               const char *TRXAddress,
+              const char *GSMcoreAddress,
               size_t tx_sps, size_t rx_sps, size_t chans,
               GSM::Time wTransmitLatency,
               RadioInterface *wRadioInterface,
@@ -152,7 +154,8 @@
 
 private:
   int mBasePort;
-  std::string mAddr;
+  std::string mLocalAddr;
+  std::string mRemoteAddr;
 
   std::vector<UDPSocket *> mDataSockets;  ///< socket for writing to/reading from GSM core
   std::vector<UDPSocket *> mCtrlSockets;  ///< socket for writing/reading control commands from GSM core
diff --git a/Transceiver52M/osmo-trx.cpp b/Transceiver52M/osmo-trx.cpp
index 61b3098..3f72fb7 100644
--- a/Transceiver52M/osmo-trx.cpp
+++ b/Transceiver52M/osmo-trx.cpp
@@ -62,7 +62,8 @@
 
 struct trx_config {
 	std::string log_level;
-	std::string addr;
+	std::string local_addr;
+	std::string remote_addr;
 	std::string dev_args;
 	unsigned port;
 	unsigned tx_sps;
@@ -134,7 +135,8 @@
 	ost << "   Log Level............... " << config->log_level << std::endl;
 	ost << "   Device args............. " << config->dev_args << std::endl;
 	ost << "   TRX Base Port........... " << config->port << std::endl;
-	ost << "   TRX Address............. " << config->addr << std::endl;
+	ost << "   TRX Address............. " << config->local_addr << std::endl;
+	ost << "   GSM Core Address........." << config->remote_addr << std::endl;
 	ost << "   Channels................ " << config->chans << std::endl;
 	ost << "   Tx Samples-per-Symbol... " << config->tx_sps << std::endl;
 	ost << "   Rx Samples-per-Symbol... " << config->rx_sps << std::endl;
@@ -200,9 +202,10 @@
 	Transceiver *trx;
 	VectorFIFO *fifo;
 
-	trx = new Transceiver(config->port, config->addr.c_str(),
-			      config->tx_sps, config->rx_sps, config->chans,
-			      GSM::Time(3,0), radio, config->rssi_offset);
+	trx = new Transceiver(config->port, config->local_addr.c_str(),
+			      config->remote_addr.c_str(), config->tx_sps,
+			      config->rx_sps, config->chans, GSM::Time(3,0),
+			      radio, config->rssi_offset);
 	if (!trx->init(config->filler, config->rtsc,
 		       config->rach_delay, config->edge)) {
 		LOG(ALERT) << "Failed to initialize transceiver";
@@ -248,6 +251,7 @@
 		"  -a    UHD device args\n"
 		"  -l    Logging level (%s)\n"
 		"  -i    IP address of GSM core\n"
+		"  -j    IP address of osmo-trx\n"
 		"  -p    Base port number\n"
 		"  -e    Enable EDGE receiver\n"
 		"  -m    Enable multi-ARFCN transceiver (default=disabled)\n"
@@ -271,7 +275,8 @@
 	int option;
 
 	config->log_level = "NOTICE";
-	config->addr = DEFAULT_TRX_IP;
+	config->local_addr = DEFAULT_TRX_IP;
+	config->remote_addr = DEFAULT_TRX_IP;
 	config->port = DEFAULT_TRX_PORT;
 	config->tx_sps = DEFAULT_TX_SPS;
 	config->rx_sps = DEFAULT_RX_SPS;
@@ -288,7 +293,7 @@
 	config->edge = false;
 	config->sched_rr = -1;
 
-	while ((option = getopt(argc, argv, "ha:l:i:p:c:dmxgfo:s:b:r:A:R:Set:")) != -1) {
+	while ((option = getopt(argc, argv, "ha:l:i:j:p:c:dmxgfo:s:b:r:A:R:Set:")) != -1) {
 		switch (option) {
 		case 'h':
 			print_help();
@@ -301,7 +306,10 @@
 			config->log_level = optarg;
 			break;
 		case 'i':
-			config->addr = optarg;
+			config->remote_addr = optarg;
+			break;
+		case 'j':
+			config->local_addr = optarg;
 			break;
 		case 'p':
 			config->port = atoi(optarg);

-- 
To view, visit https://gerrit.osmocom.org/3539
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9ba184a1251c823e413a9230943ed263e52142ec
Gerrit-PatchSet: 1
Gerrit-Project: osmo-trx
Gerrit-Branch: master
Gerrit-Owner: Pau Espin Pedrol <pespin at sysmocom.de>



More information about the gerrit-log mailing list