Author: bapt
Date: Thu Dec  6 08:27:56 2018
New Revision: 341622
URL: https://svnweb.freebsd.org/changeset/base/341622

Log:
  MFC r340111:
  
  nfsd: Factorize code
  
  Factorize code by using struct sockaddr_storage to handle both ipv6 and ipv4
  
  Discussed with: rmacklem
  Reviewed by: manu
  Sponsored by: Gandi.net
  Differential Revision: https://reviews.freebsd.org/D13223

Modified:
  stable/11/usr.sbin/nfsd/nfsd.c
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.sbin/nfsd/nfsd.c
==============================================================================
--- stable/11/usr.sbin/nfsd/nfsd.c      Thu Dec  6 08:27:30 2018        
(r341621)
+++ stable/11/usr.sbin/nfsd/nfsd.c      Thu Dec  6 08:27:56 2018        
(r341622)
@@ -149,10 +149,8 @@ main(int argc, char **argv)
        struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints;
        struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6;
        struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6;
-       struct sockaddr_in inetpeer;
-       struct sockaddr_in6 inet6peer;
+       struct sockaddr_storage peer;
        fd_set ready, sockbits;
-       fd_set v4bits, v6bits;
        int ch, connect_type_cnt, i, maxsock, msgsock;
        socklen_t len;
        int on = 1, unregister, reregister, sock;
@@ -450,8 +448,6 @@ main(int argc, char **argv)
        }
 
        (void)signal(SIGUSR1, cleanup);
-       FD_ZERO(&v4bits);
-       FD_ZERO(&v6bits);
        FD_ZERO(&sockbits);
  
        rpcbregcnt = 0;
@@ -633,7 +629,6 @@ main(int argc, char **argv)
                                }
                                freeaddrinfo(ai_tcp);
                                FD_SET(tcpsock, &sockbits);
-                               FD_SET(tcpsock, &v4bits); 
                                maxsock = tcpsock;
                                connect_type_cnt++;
                        }
@@ -712,7 +707,6 @@ main(int argc, char **argv)
                                }
                                freeaddrinfo(ai_tcp6);
                                FD_SET(tcp6sock, &sockbits);
-                               FD_SET(tcp6sock, &v6bits);
                                if (maxsock < tcp6sock)
                                        maxsock = tcp6sock;
                                connect_type_cnt++;
@@ -786,52 +780,25 @@ main(int argc, char **argv)
                }
                for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) {
                        if (FD_ISSET(tcpsock, &ready)) {
-                               if (FD_ISSET(tcpsock, &v4bits)) {
-                                       len = sizeof(inetpeer);
-                                       if ((msgsock = accept(tcpsock,
-                                           (struct sockaddr *)&inetpeer, 
&len)) < 0) {
-                                               error = errno;
-                                               syslog(LOG_ERR, "accept failed: 
%m");
-                                               if (error == ECONNABORTED ||
-                                                   error == EINTR)
-                                                       continue;
-                                               nfsd_exit(1);
-                                       }
-                                       memset(inetpeer.sin_zero, 0,
-                                               sizeof(inetpeer.sin_zero));
-                                       if (setsockopt(msgsock, SOL_SOCKET,
-                                           SO_KEEPALIVE, (char *)&on, 
sizeof(on)) < 0)
-                                               syslog(LOG_ERR,
-                                                   "setsockopt SO_KEEPALIVE: 
%m");
-                                       addsockargs.sock = msgsock;
-                                       addsockargs.name = (caddr_t)&inetpeer;
-                                       addsockargs.namelen = len;
-                                       nfssvc(nfssvc_addsock, &addsockargs);
-                                       (void)close(msgsock);
-                               } else if (FD_ISSET(tcpsock, &v6bits)) {
-                                       len = sizeof(inet6peer);
-                                       if ((msgsock = accept(tcpsock,
-                                           (struct sockaddr *)&inet6peer,
-                                           &len)) < 0) {
-                                               error = errno;
-                                               syslog(LOG_ERR,
-                                                    "accept failed: %m");
-                                               if (error == ECONNABORTED ||
-                                                   error == EINTR)
-                                                       continue;
-                                               nfsd_exit(1);
-                                       }
-                                       if (setsockopt(msgsock, SOL_SOCKET,
-                                           SO_KEEPALIVE, (char *)&on,
-                                           sizeof(on)) < 0)
-                                               syslog(LOG_ERR, "setsockopt "
-                                                   "SO_KEEPALIVE: %m");
-                                       addsockargs.sock = msgsock;
-                                       addsockargs.name = (caddr_t)&inet6peer;
-                                       addsockargs.namelen = len;
-                                       nfssvc(nfssvc_addsock, &addsockargs);
-                                       (void)close(msgsock);
+                               len = sizeof(peer);
+                               if ((msgsock = accept(tcpsock,
+                                   (struct sockaddr *)&peer, &len)) < 0) {
+                                       error = errno;
+                                       syslog(LOG_ERR, "accept failed: %m");
+                                       if (error == ECONNABORTED ||
+                                           error == EINTR)
+                                               continue;
+                                       nfsd_exit(1);
                                }
+                               if (setsockopt(msgsock, SOL_SOCKET,
+                                   SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0)
+                                       syslog(LOG_ERR,
+                                           "setsockopt SO_KEEPALIVE: %m");
+                               addsockargs.sock = msgsock;
+                               addsockargs.name = (caddr_t)&peer;
+                               addsockargs.namelen = len;
+                               nfssvc(nfssvc_addsock, &addsockargs);
+                               (void)close(msgsock);
                        }
                }
        }
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to