-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Apply patch to use getaddrinfo when connecting to rfsim (#4)
- Loading branch information
1 parent
977c4a2
commit 33447e9
Showing
2 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
diff --git a/radio/rfsimulator/simulator.c b/radio/rfsimulator/simulator.c | ||
index 26feb13490..fcbc5ae4a9 100644 | ||
--- a/radio/rfsimulator/simulator.c | ||
+++ b/radio/rfsimulator/simulator.c | ||
@@ -29,6 +29,7 @@ | ||
*/ | ||
|
||
#include <sys/socket.h> | ||
+#include <sys/types.h> | ||
#include <netinet/in.h> | ||
#include <netinet/tcp.h> | ||
#include <arpa/inet.h> | ||
@@ -40,6 +41,7 @@ | ||
#include <errno.h> | ||
#include <sys/epoll.h> | ||
#include <string.h> | ||
+#include <netdb.h> | ||
|
||
#include <common/utils/assertions.h> | ||
#include <common/utils/LOG/log.h> | ||
@@ -585,25 +587,52 @@ static int startServer(openair0_device *device) { | ||
static int startClient(openair0_device *device) { | ||
rfsimulator_state_t *t = device->priv; | ||
t->role = SIMU_ROLE_CLIENT; | ||
- int sock; | ||
- if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { | ||
- LOG_E(HW, "socket(SOCK_STREAM) failed, errno(%d)\n", errno); | ||
- return -1; | ||
- } | ||
- struct sockaddr_in addr = {.sin_family = AF_INET, .sin_port = htons(t->port), .sin_addr = {.s_addr = INADDR_ANY}}; | ||
- addr.sin_addr.s_addr = inet_addr(t->ip); | ||
+ int sock, s; | ||
+ struct addrinfo hints; | ||
+ struct addrinfo *result, *rp; | ||
+ | ||
+ char port[6]; | ||
+ snprintf(port, sizeof(port), "%d", t->port); | ||
+ | ||
+ memset(&hints, 0, sizeof(hints)); | ||
+ hints.ai_family = AF_INET; | ||
+ hints.ai_socktype = SOCK_STREAM; | ||
+ hints.ai_flags = 0; | ||
+ hints.ai_protocol = 0; | ||
+ | ||
bool connected=false; | ||
|
||
while(!connected) { | ||
LOG_I(HW, "Trying to connect to %s:%d\n", t->ip, t->port); | ||
|
||
- if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) == 0) { | ||
- LOG_I(HW, "Connection to %s:%d established\n", t->ip, t->port); | ||
- connected=true; | ||
- } | ||
|
||
- LOG_I(HW, "connect() to %s:%d failed, errno(%d)\n", t->ip, t->port, errno); | ||
- sleep(1); | ||
+ s = getaddrinfo(t->ip, port, &hints, &result); | ||
+ if (s != 0) { | ||
+ LOG_I(HW, "getaddrinfo: %s\n", gai_strerror(s)); | ||
+ } else { | ||
+ for (rp = result; rp != NULL; rp = rp->ai_next) { | ||
+ sock = socket(rp->ai_family, rp->ai_socktype, | ||
+ rp->ai_protocol); | ||
+ if (sock == -1) | ||
+ continue; | ||
+ | ||
+ if (connect(sock, rp->ai_addr, rp->ai_addrlen) != -1) { | ||
+ LOG_I(HW, "Connection to %s:%d established\n", t->ip, t->port); | ||
+ connected=true; | ||
+ break; | ||
+ } | ||
+ | ||
+ close(sock); | ||
+ } | ||
+ | ||
+ freeaddrinfo(result); | ||
+ | ||
+ if (rp == NULL) { | ||
+ LOG_I(HW, "connect() to %s:%d failed, errno(%d)\n", t->ip, t->port, errno); | ||
+ fprintf(stderr, "Could not connect\n"); | ||
+ sleep(1); | ||
+ } | ||
+ } | ||
} | ||
|
||
if (setblocking(sock, notBlocking) == -1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters