Skip to content

Commit

Permalink
opsfclient: Convert to use getaddrinfo
Browse files Browse the repository at this point in the history
gethostbyname, rumor has it, is obsolete and should be removed.
Let's convert over to the successor.

Signed-off-by: Donald Sharp <[email protected]>
  • Loading branch information
donaldsharp committed Nov 23, 2023
1 parent 25f1b66 commit 66d1f52
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions ospfclient/ospf_apiclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct ospf_apiclient *ospf_apiclient_connect(char *host, int syncport)
struct sockaddr_in myaddr_sync;
struct sockaddr_in myaddr_async;
struct sockaddr_in peeraddr;
struct hostent *hp;
struct addrinfo hints, *res;
struct ospf_apiclient *new;
int size = 0;
unsigned int peeraddrlen;
Expand Down Expand Up @@ -155,8 +155,10 @@ struct ospf_apiclient *ospf_apiclient_connect(char *host, int syncport)

/* Make connection for synchronous requests and connect to server */
/* Resolve address of server */
hp = gethostbyname(host);
if (!hp) {
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(host, NULL, &hints, &res) != 0) {
fprintf(stderr, "ospf_apiclient_connect: no such host %s\n",
host);
close(async_server_sock);
Expand Down Expand Up @@ -217,7 +219,7 @@ struct ospf_apiclient *ospf_apiclient_connect(char *host, int syncport)
}

/* Prepare address structure for connect */
memcpy(&myaddr_sync.sin_addr, hp->h_addr, hp->h_length);
myaddr_sync = *(struct sockaddr_in *)res->ai_addr;
myaddr_sync.sin_family = AF_INET;
myaddr_sync.sin_port = htons(ospf_apiclient_getport());
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
Expand Down

0 comments on commit 66d1f52

Please sign in to comment.