[ADNS] src/event.c, src/setup.c: Don't use `getprotobyname'.

Mark Wooding mdw at distorted.org.uk
Tue May 22 01:59:53 BST 2018


The protocol constants for TCP and UDP are well-known, and provided as
constants in <netinet/in.h>.  Furthermore, weirder environments such as
Android don't have `getproto...' at all, and ADNS fails on startup there
with `ENOPROTOOPT'.

So don't bother calling `getprotobyname'; instead, just use the
`IPPROTO_...' constants.

Signed-off-by: Mark Wooding <mdw at distorted.org.uk>
---
 src/event.c | 8 +-------
 src/setup.c | 4 +---
 2 files changed, 2 insertions(+), 10 deletions(-)

diff --git a/src/event.c b/src/event.c
index dcc49e9cc72b0562d0ec42d8ca6dab57e11ede5e..6a607ede2847f082ef7afbb068bdedcb40e15849 100644
--- a/src/event.c
+++ b/src/event.c
@@ -97,7 +97,6 @@ static void tcp_broken_events(adns_state ads) {
 void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
   int r, fd, tries;
   adns_rr_addr *addr;
-  struct protoent *proto;
 
   for (tries=0; tries<ads->nservers; tries++) {
     switch (ads->tcpstate) {
@@ -115,13 +114,8 @@ void adns__tcp_tryconnect(adns_state ads, struct timeval now) {
     assert(!ads->tcprecv.used);
     assert(!ads->tcprecv_skip);
 
-    proto= getprotobyname("tcp");
-    if (!proto) {
-      adns__diag(ads,-1,0,"unable to find protocol no. for TCP !");
-      return;
-    }
     addr = &ads->servers[ads->tcpserver];
-    fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, proto->p_proto);
+    fd= socket(addr->addr.sa.sa_family, SOCK_STREAM, IPPROTO_TCP);
     if (fd<0) {
       adns__diag(ads,-1,0,"cannot create TCP socket: %s",strerror(errno));
       return;
diff --git a/src/setup.c b/src/setup.c
index 8ff92d284e5bf4a89955f336795479417d28ade1..24378b1fd7979fb065af9c98297b1b10295e1fae 100644
--- a/src/setup.c
+++ b/src/setup.c
@@ -646,7 +646,6 @@ static int init_begin(adns_state *ads_r, adns_initflags flags,
 
 static int init_finish(adns_state ads) {
   struct sockaddr_in sin;
-  struct protoent *proto;
   struct udpsocket *udp;
   int i;
   int r;
@@ -661,7 +660,6 @@ static int init_finish(adns_state ads) {
     addserver(ads,(struct sockaddr *)&sin, sizeof(sin));
   }
 
-  proto= getprotobyname("udp"); if (!proto) { r= ENOPROTOOPT; goto x_free; }
   ads->nudpsockets= 0;
   for (i=0; i<ads->nservers; i++) {
     if (adns__udpsocket_by_af(ads, ads->servers[i].addr.sa.sa_family))
@@ -669,7 +667,7 @@ static int init_finish(adns_state ads) {
     assert(ads->nudpsockets < MAXUDP);
     udp= &ads->udpsockets[ads->nudpsockets];
     udp->af= ads->servers[i].addr.sa.sa_family;
-    udp->fd= socket(udp->af,SOCK_DGRAM,proto->p_proto);
+    udp->fd= socket(udp->af,SOCK_DGRAM,IPPROTO_UDP);
     if (udp->fd < 0) { r= errno; goto x_free; }
     ads->nudpsockets++;
     r= adns__setnonblock(ads,udp->fd);
-- 
[mdw]




More information about the sgo-software-discuss mailing list