]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/svc_socket.c
multiple threads for mountd
[nfs-utils.git] / support / nfs / svc_socket.c
1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <netdb.h>
23 #include <rpc/rpc.h>
24 #include <sys/socket.h>
25 #include <sys/fcntl.h>
26 #include <errno.h>
27
28 #ifdef _LIBC
29 # include <libintl.h>
30 #else
31 # ifndef _
32 #  define _(s)                  (s)
33 # endif
34 # define __socket(d, t, p)      socket ((d), (t), (p))
35 # define __close(f)             close ((f))
36 #endif
37
38 static int
39 svc_socket (u_long number, int type, int protocol, int reuse)
40 {
41   struct sockaddr_in addr;
42   socklen_t len = sizeof (struct sockaddr_in);
43   char rpcdata [1024], servdata [1024];
44   struct rpcent rpcbuf, *rpcp;
45   struct servent servbuf, *servp;
46   int sock, ret;
47   const char *proto = protocol == IPPROTO_TCP ? "tcp" : "udp";
48
49   if ((sock = __socket (AF_INET, type, protocol)) < 0)
50     {
51       perror (_("svc_socket: socket creation problem"));
52       return sock;
53     }
54
55   if (reuse)
56     {
57       ret = 1;
58       ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &ret,
59                         sizeof (ret));
60       if (ret < 0)
61         {
62           perror (_("svc_socket: socket reuse problem"));
63           return ret;
64         }
65     }
66
67   __bzero ((char *) &addr, sizeof (addr));
68   addr.sin_family = AF_INET;
69
70   ret = getrpcbynumber_r (number, &rpcbuf, rpcdata, sizeof rpcdata,
71                           &rpcp);
72   if (ret == 0 && rpcp != NULL)
73     {
74       /* First try name.  */
75       ret = getservbyname_r (rpcp->r_name, proto, &servbuf, servdata,
76                              sizeof servdata, &servp);
77       if ((ret != 0 || servp == NULL) && rpcp->r_aliases)
78         {
79           const char **a;
80
81           /* Then we try aliases.  */
82           for (a = (const char **) rpcp->r_aliases; *a != NULL; a++) 
83             {
84               ret = getservbyname_r (*a, proto, &servbuf, servdata,
85                                      sizeof servdata, &servp);
86               if (ret == 0 && servp != NULL)
87                 break;
88             }
89         }
90     }
91
92   if (ret == 0 && servp != NULL)
93     {
94       addr.sin_port = servp->s_port;
95       if (bind (sock, (struct sockaddr *) &addr, len) < 0)
96         {
97           perror (_("svc_socket: bind problem"));
98           (void) __close (sock);
99           sock = -1;
100         }
101     }
102   else
103     {
104       if (bindresvport (sock, &addr))
105         {
106           addr.sin_port = 0;
107           if (bind (sock, (struct sockaddr *) &addr, len) < 0)
108             {
109               perror (_("svc_socket: bind problem"));
110               (void) __close (sock);
111               sock = -1;
112             }
113         }
114     }
115
116   if (sock >= 0 && protocol == IPPROTO_TCP)
117     {
118         /* Make the TCP rendezvous socket non-block to avoid
119          * problems with blocking in accept() after a spurious
120          * wakeup from the kernel */
121         int flags;
122         if ((flags = fcntl(sock, F_GETFL)) < 0)
123           {
124               perror (_("svc_socket: can't get socket flags"));
125               (void) __close (sock);
126               sock = -1;
127           }
128         else if (fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0)
129           {
130               perror (_("svc_socket: can't set socket flags"));
131               (void) __close (sock);
132               sock = -1;
133           }
134     }
135
136   return sock;
137 }
138
139 /*
140  * Create and bind a TCP socket based on program number
141  */
142 int
143 svctcp_socket (u_long number, int reuse)
144 {
145   return svc_socket (number, SOCK_STREAM, IPPROTO_TCP, reuse);
146 }
147
148 /*
149  * Create and bind a UDP socket based on program number
150  */
151 int
152 svcudp_socket (u_long number, int reuse)
153 {
154   return svc_socket (number, SOCK_DGRAM, IPPROTO_UDP, reuse);
155 }
156
157 #ifdef TEST
158 static int
159 check (u_long number, u_short port, int protocol, int reuse)
160 {
161   int socket;
162   int result;
163   struct sockaddr_in addr;
164   socklen_t len = sizeof (struct sockaddr_in);
165
166   if (protocol == IPPROTO_TCP)
167     socket = svctcp_socket (number, reuse);
168   else
169     socket = svcudp_socket (number, reuse);
170
171   if (socket < 0)
172     return 1;
173
174   result = getsockname (socket, (struct sockaddr *) &addr, &len);
175   if (result == 0)
176     {
177       if (port != 0 && ntohs (addr.sin_port) != port)
178         printf ("Program: %ld, expect port: %d, got: %d\n",
179                 number, port, ntohs (addr.sin_port)); 
180       else
181         printf ("Program: %ld, port: %d\n",
182                 number, ntohs (addr.sin_port)); 
183     }
184
185   close (socket);
186   return result;
187 }
188
189 int
190 main (void)
191 {
192   int result = 0;
193
194   result += check (100001, 0, IPPROTO_TCP, 0);
195   result += check (100001, 0, IPPROTO_UDP, 0);
196   result += check (100003, 2049, IPPROTO_TCP, 1);
197   result += check (100003, 2049, IPPROTO_UDP, 1);
198
199   return result;
200 }
201 #endif