Skip to content

Commit

Permalink
[nasa/nos3#426] Default to localhost (0.0.0.0) if hostname lookup fails;
Browse files Browse the repository at this point in the history
  • Loading branch information
jlucas9 committed Dec 13, 2024
1 parent 26a4e99 commit 7953a38
Showing 1 changed file with 2 additions and 61 deletions.
63 changes: 2 additions & 61 deletions modules/protocol/sbn_tcp/fsw/src/sbn_tcp_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,16 @@ static SBN_Status_t Init(int Version, CFE_EVS_EventID_t EID, SBN_ProtocolOutlet_
SBN_TCP_FIRST_EID = EID;
if (Version != EXP_VERSION) /* TODO: define */
{
OS_printf("SBN_TCP version mismatch: expected %d, got %d\n", EXP_VERSION, Version);
return SBN_ERROR;
} /* end if */

if (Outlet == NULL)
{
OS_printf("SBN_TCP outlet is null!\n");
return SBN_ERROR;
} /* end if */

memcpy(&SBN, Outlet, sizeof(SBN));

OS_printf("SBN_TCP Lib Initialized.\n");
return SBN_SUCCESS;
} /* end Init() */

Expand All @@ -103,38 +100,27 @@ static SBN_Status_t ConfAddr(OS_SockAddr_t *Addr, const char *Address)
{
EVSSendErr(SBN_TCP_CONFIG_EID, "invalid net address");
return SBN_ERROR;
OS_printf("Failed Colon!\n");
} /* end if */

OS_printf("Passed Colon!\n");

strncpy(AddrHost, Address, AddrLen);
AddrHost[AddrLen] = '\0';
char *ValidatePtr = NULL;
OS_printf("Passed AddrHost!\n");

OS_SocketPort_t Port = strtol(Colon + 1, &ValidatePtr, 0);
OS_printf("Passed Port creation!\n");

if (!ValidatePtr || ValidatePtr == Colon + 1)
{
EVSSendErr(SBN_TCP_CONFIG_EID, "invalid port");
OS_printf("Failed Port Verification!\n");
return SBN_ERROR;
} /* end if */

OS_printf("Passed Port Verification!\n");

if (OS_SocketAddrInit(Addr, OS_SocketDomain_INET) != OS_SUCCESS)
{
EVSSendErr(SBN_TCP_SOCK_EID, "socket addr init failed");
OS_printf("Failed Addr Initialization!\n");
return SBN_ERROR;
} /* end if */

OS_printf("Passed Addr Initialization!\n");

char AddrV4[OS_MAX_API_NAME];
char AddrV4[OS_MAX_API_NAME] = "0.0.0.0";

/*
DNS Resolution for FSW Container
Expand All @@ -143,79 +129,34 @@ static SBN_Status_t ConfAddr(OS_SockAddr_t *Addr, const char *Address)
struct hostent *he;
struct in_addr **addr_list;
int i;

// Beginning attempt at switching over to use getaddrinfo
// int sockfd;
// struct addrinfo hints, *servinfo, *p;
// int rv;

// memset(&hints, 0, sizeof hints);
// hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
// hints.ai_socktype = SOCK_STREAM;

// OS_printf("Just before trying to getaddrinfo\n");

// if ((rv = getaddrinfo(AddrHost, NULL, &hints, &servinfo)) != 0) {
// fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
// exit(1);
// }
// else {
// addr_list = servinfo->ai_addr;
// OS_printf("New alternative working?\n");
// // addr_list = ((sockaddr_in)(servinfo->ai_addr))->sin_addr;
// }
// freeaddrinfo(servinfo); // all done with this structure
// End of attempts to use getaddrinfo

OS_printf("Starting Name Resolution on %s!\n", AddrHost);

he = gethostbyname(AddrHost);

OS_printf("Just after gethostbyname\n");
OS_printf("he Values:\n name = %s;\n addr_type = %d;\n len = %d;\nAddr List:\n", he->h_name, he->h_addrtype, he->h_length);

for(int j = 0; he->h_addr_list[j] != NULL; j++)
{
OS_printf(" %s\n", he->h_addr_list[j]);
}

if (he != NULL)
{
//OS_printf("sbn_tcp_if.c:ConfAddr:gethostbyname, he Values:\n name = %s;\n addr_type = %d;\n len = %d;\nAddr List:\n", he->h_name, he->h_addrtype, he->h_length);
addr_list = (struct in_addr **) he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++)
{
//Return the first one;
strcpy(AddrV4, inet_ntoa(*addr_list[i]));
OS_printf("Passed DNS Name Resolution!\n");
break;
}
}
else
{
OS_printf("Failed Name Resolution, gethostbyname(%s) returned NULL!\n", AddrHost);
}

/*
End hostname snippet from: https://stackoverflow.com/questions/38002016/problems-with-gethostbyname-c
*/

if (OS_SocketAddrFromString(Addr, AddrV4) != OS_SUCCESS)
{
OS_printf("Failed Set Address!\n");
EVSSendErr(SBN_TCP_SOCK_EID, "setting address host failed (AddrHost=%s)", AddrV4);
return SBN_ERROR;
} /* end if */
OS_printf("Passed Set Address!\n");

if (OS_SocketAddrSetPort(Addr, Port) != OS_SUCCESS)
{
EVSSendErr(SBN_TCP_SOCK_EID, "setting address port failed (Port=%d)", Port);
OS_printf("Failed Set Port!\n");
return SBN_ERROR;
} /* end if */

OS_printf("Passed Set Port!\n");

return SBN_SUCCESS;
} /* end ConfAddr() */

Expand Down

0 comments on commit 7953a38

Please sign in to comment.