1 /* Copyright (C) 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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
24 #include <sys/socket.h>
25 #include <sys/fcntl.h>
34 # define __socket(d, t, p) socket ((d), (t), (p))
35 # define __close(f) close ((f))
39 svc_socket (u_long number, int type, int protocol, int reuse)
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 = NULL;
47 const char *proto = protocol == IPPROTO_TCP ? "tcp" : "udp";
49 if ((sock = __socket (AF_INET, type, protocol)) < 0)
51 perror (_("svc_socket: socket creation problem"));
58 ret = setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, &ret,
62 perror (_("svc_socket: socket reuse problem"));
67 memset (&addr, 0, sizeof (addr));
68 addr.sin_family = AF_INET;
70 ret = getrpcbynumber_r (number, &rpcbuf, rpcdata, sizeof rpcdata,
72 if (ret == 0 && rpcp != NULL)
75 ret = getservbyname_r (rpcp->r_name, proto, &servbuf, servdata,
76 sizeof servdata, &servp);
77 if ((ret != 0 || servp == NULL) && rpcp->r_aliases)
81 /* Then we try aliases. */
82 for (a = (const char **) rpcp->r_aliases; *a != NULL; a++)
84 ret = getservbyname_r (*a, proto, &servbuf, servdata,
85 sizeof servdata, &servp);
86 if (ret == 0 && servp != NULL)
92 if (ret == 0 && servp != NULL)
94 addr.sin_port = servp->s_port;
95 if (bind (sock, (struct sockaddr *) &addr, len) < 0)
97 perror (_("svc_socket: bind problem"));
98 (void) __close (sock);
104 if (bindresvport (sock, &addr))
107 if (bind (sock, (struct sockaddr *) &addr, len) < 0)
109 perror (_("svc_socket: bind problem"));
110 (void) __close (sock);
118 /* This socket might be shared among multiple processes
119 * if mountd is run multi-threaded. So it is safest to
120 * make it non-blocking, else all threads might wake
121 * one will get the data, and the others will block
123 * In all cases, transaction on this socket are atomic
124 * (accept for TCP, packet-read and packet-write for UDP)
125 * so O_NONBLOCK will not confuse unprepared code causing
126 * it to corrupt messages.
127 * It generally safest to have O_NONBLOCK when doing an accept
128 * as if we get a RST after the SYN and before accept runs,
129 * we can block despite being told there was an acceptable
133 if ((flags = fcntl(sock, F_GETFL)) < 0)
135 perror (_("svc_socket: can't get socket flags"));
136 (void) __close (sock);
139 else if (fcntl(sock, F_SETFL, flags|O_NONBLOCK) < 0)
141 perror (_("svc_socket: can't set socket flags"));
142 (void) __close (sock);
151 * Create and bind a TCP socket based on program number
154 svctcp_socket (u_long number, int reuse)
156 return svc_socket (number, SOCK_STREAM, IPPROTO_TCP, reuse);
160 * Create and bind a UDP socket based on program number
163 svcudp_socket (u_long number, int reuse)
165 return svc_socket (number, SOCK_DGRAM, IPPROTO_UDP, 0);
170 check (u_long number, u_short port, int protocol, int reuse)
174 struct sockaddr_in addr;
175 socklen_t len = sizeof (struct sockaddr_in);
177 if (protocol == IPPROTO_TCP)
178 socket = svctcp_socket (number, reuse);
180 socket = svcudp_socket (number, reuse);
185 result = getsockname (socket, (struct sockaddr *) &addr, &len);
188 if (port != 0 && ntohs (addr.sin_port) != port)
189 printf ("Program: %ld, expect port: %d, got: %d\n",
190 number, port, ntohs (addr.sin_port));
192 printf ("Program: %ld, port: %d\n",
193 number, ntohs (addr.sin_port));
205 result += check (100001, 0, IPPROTO_TCP, 0);
206 result += check (100001, 0, IPPROTO_UDP, 0);
207 result += check (100003, 2049, IPPROTO_TCP, 1);
208 result += check (100003, 2049, IPPROTO_UDP, 1);