1 /* accept - listen for and accept a remote network connection on a given port */
4 Copyright (C) 2020 Free Software Foundation, Inc.
6 This file is part of GNU Bush.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (HAVE_UNISTD_H)
30 #include "bushtypes.h"
35 #include <sys/socket.h>
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
39 #include "loadables.h"
41 static int accept_bind_variable (char *, int);
51 char *tmoutarg
, *fdvar
, *rhostvar
, *rhost
;
53 int servsock
, clisock
;
54 struct sockaddr_in server
, client
;
56 struct timeval timeval
;
57 struct linger linger
= { 0, 0 };
59 rhostvar
= tmoutarg
= fdvar
= rhost
= (char *)NULL
;
61 reset_internal_getopt ();
62 while ((opt
= internal_getopt (list
, "r:t:v:")) != -1)
67 rhostvar
= list_optarg
;
70 tmoutarg
= list_optarg
;
84 /* Validate input and variables */
88 opt
= uconvert (tmoutarg
, &ival
, &uval
, (char **)0);
89 if (opt
== 0 || ival
< 0 || uval
< 0)
91 builtin_error ("%s: invalid timeout specification", tmoutarg
);
92 return (EXECUTION_FAILURE
);
94 timeval
.tv_sec
= ival
;
95 timeval
.tv_usec
= uval
;
96 /* XXX - should we warn if ival == uval == 0 ? */
105 if (legal_number (list
->word
->word
, &iport
) == 0 || iport
< 0 || iport
> TYPE_MAXIMUM (unsigned short))
107 builtin_error ("%s: invalid port number", list
->word
->word
);
108 return (EXECUTION_FAILURE
);
110 uport
= (unsigned short)iport
;
115 unbind_variable (fdvar
);
117 unbind_variable (rhostvar
);
119 if ((servsock
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_IP
)) < 0)
121 builtin_error ("cannot create socket: %s", strerror (errno
));
122 return (EXECUTION_FAILURE
);
125 memset ((char *)&server
, 0, sizeof (server
));
126 server
.sin_family
= AF_INET
;
127 server
.sin_port
= htons(uport
);
128 server
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
130 if (bind (servsock
, (struct sockaddr
*)&server
, sizeof (server
)) < 0)
132 builtin_error ("socket bind failure: %s", strerror (errno
));
134 return (EXECUTION_FAILURE
);
138 setsockopt (servsock
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&opt
, sizeof (opt
));
139 setsockopt (servsock
, SOL_SOCKET
, SO_LINGER
, (void *)&linger
, sizeof (linger
));
141 if (listen (servsock
, 1) < 0)
143 builtin_error ("listen failure: %s", strerror (errno
));
145 return (EXECUTION_FAILURE
);
153 FD_SET(servsock
, &iofds
);
155 opt
= select (servsock
+1, &iofds
, 0, 0, &timeval
);
157 builtin_error ("select failure: %s", strerror (errno
));
161 return (EXECUTION_FAILURE
);
165 clientlen
= sizeof (client
);
166 if ((clisock
= accept (servsock
, (struct sockaddr
*)&client
, &clientlen
)) < 0)
168 builtin_error ("client accept failure: %s", strerror (errno
));
170 return (EXECUTION_FAILURE
);
175 accept_bind_variable (fdvar
, clisock
);
178 rhost
= inet_ntoa (client
.sin_addr
);
179 v
= builtin_bind_variable (rhostvar
, rhost
, 0);
180 if (v
== 0 || readonly_p (v
) || noassign_p (v
))
181 builtin_error ("%s: cannot set variable", rhostvar
);
184 return (EXECUTION_SUCCESS
);
188 accept_bind_variable (varname
, intval
)
193 char ibuf
[INT_STRLEN_BOUND (int) + 1], *p
;
195 p
= fmtulong (intval
, 10, ibuf
, sizeof (ibuf
), 0);
196 v
= builtin_bind_variable (varname
, p
, 0);
197 if (v
== 0 || readonly_p (v
) || noassign_p (v
))
198 builtin_error ("%s: cannot set variable", varname
);
202 char *accept_doc
[] = {
203 "Accept a network connection on a specified port.",
205 "This builtin allows a bush script to act as a TCP/IP server.",
207 "Options, if supplied, have the following meanings:",
208 " -t timeout wait TIMEOUT seconds for a connection. TIMEOUT may",
209 " be a decimal number including a fractional portion",
210 " -v varname store the numeric file descriptor of the connected",
211 " socket into VARNAME. The default VARNAME is ACCEPT_FD",
212 " -r rhost store the IP address of the remote host into the shell",
213 " variable RHOST, in dotted-decimal notation",
215 "If successful, the shell variable ACCEPT_FD, or the variable named by the",
216 "-v option, will be set to the fd of the connected socket, suitable for",
217 "use as 'read -u$ACCEPT_FD'. RHOST, if supplied, will hold the IP address",
218 "of the remote client. The return status is 0.",
220 "On failure, the return status is 1 and ACCEPT_FD (or VARNAME) and RHOST,",
221 "if supplied, will be unset.",
223 "The server socket fd will be closed before accept returns.",
227 struct builtin accept_struct
= {
228 "accept", /* builtin name */
229 accept_builtin
, /* function implementing the builtin */
230 BUILTIN_ENABLED
, /* initial flags for builtin */
231 accept_doc
, /* array of long documentation strings. */
232 "accept [-t timeout] [-v varname] [-r addrvar ] port", /* usage synopsis; becomes short_doc */
233 0 /* reserved for internal use */