2008-01-28 21:37:16 +00:00
|
|
|
/* source: xio-udp.c */
|
2016-07-22 06:54:31 +00:00
|
|
|
/* Copyright Gerhard Rieger and contributors (see file CHANGES) */
|
2008-01-27 12:00:08 +00:00
|
|
|
/* Published under the GNU General Public License V.2, see file COPYING */
|
|
|
|
|
|
|
|
/* this file contains the source for handling UDP addresses */
|
|
|
|
|
|
|
|
#include "xiosysincludes.h"
|
|
|
|
|
|
|
|
#if WITH_UDP && (WITH_IP4 || WITH_IP6)
|
|
|
|
|
|
|
|
#include "xioopen.h"
|
|
|
|
#include "xio-socket.h"
|
|
|
|
#include "xio-ip4.h"
|
|
|
|
#include "xio-ip6.h"
|
|
|
|
#include "xio-ip.h"
|
|
|
|
#include "xio-ipapp.h"
|
|
|
|
#include "xio-tcpwrap.h"
|
|
|
|
|
|
|
|
#include "xio-udp.h"
|
|
|
|
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
static int xioopen_udp_sendto(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, const struct addrdesc *addrdesc);
|
|
|
|
static int xioopen_udp_datagram(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, const struct addrdesc *addrdesc);
|
|
|
|
static int xioopen_udp_recvfrom(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, const struct addrdesc *addrdesc);
|
|
|
|
static int xioopen_udp_recv(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *xfd, const struct addrdesc *addrdesc);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
int _xioopen_udp_sendto(const char *hostname, const char *servname,
|
|
|
|
struct opt *opts,
|
2023-06-14 09:41:44 +00:00
|
|
|
int xioflags, xiofile_t *xxfd, groups_t groups,
|
2008-01-27 12:00:08 +00:00
|
|
|
int pf, int socktype, int ipproto);
|
|
|
|
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp_connect = { "UDP-CONNECT", 1+XIO_RDWR, xioopen_ipapp_connect, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP, SOCK_DGRAM, IPPROTO_UDP, PF_UNSPEC HELP(":<host>:<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_LISTEN
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp_listen = { "UDP-LISTEN", 1+XIO_RDWR, xioopen_ipdgram_listen, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_LISTEN|GROUP_CHILD|GROUP_RANGE, PF_UNSPEC, IPPROTO_UDP, PF_UNSPEC HELP(":<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_LISTEN */
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp_sendto = { "UDP-SENDTO", 1+XIO_RDWR, xioopen_udp_sendto, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP, PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp_recvfrom = { "UDP-RECVFROM", 1+XIO_RDWR, xioopen_udp_recvfrom, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_CHILD|GROUP_RANGE, PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp_recv = { "UDP-RECV", 1+XIO_RDONLY, xioopen_udp_recv, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_RANGE, PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp_datagram = { "UDP-DATAGRAM", 1+XIO_RDWR, xioopen_udp_datagram, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_RANGE, PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
|
|
|
|
#if WITH_IP4
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp4_connect = { "UDP4-CONNECT", 1+XIO_RDWR, xioopen_ipapp_connect, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP, SOCK_DGRAM, IPPROTO_UDP, PF_INET HELP(":<host>:<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_LISTEN
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp4_listen = { "UDP4-LISTEN", 1+XIO_RDWR, xioopen_ipdgram_listen, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP|GROUP_LISTEN|GROUP_CHILD|GROUP_RANGE, PF_INET, IPPROTO_UDP, PF_INET HELP(":<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_LISTEN */
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp4_sendto = { "UDP4-SENDTO", 1+XIO_RDWR, xioopen_udp_sendto, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP, PF_INET, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp4_datagram = { "UDP4-DATAGRAM", 1+XIO_RDWR, xioopen_udp_datagram, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP|GROUP_RANGE, PF_INET, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp4_recvfrom= { "UDP4-RECVFROM", 1+XIO_RDWR, xioopen_udp_recvfrom, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP|GROUP_CHILD|GROUP_RANGE, PF_INET, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp4_recv = { "UDP4-RECV", 1+XIO_RDONLY, xioopen_udp_recv, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP4|GROUP_IP_UDP|GROUP_RANGE, PF_INET, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_IP4 */
|
|
|
|
|
|
|
|
#if WITH_IP6
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp6_connect = { "UDP6-CONNECT", 1+XIO_RDWR, xioopen_ipapp_connect, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP, SOCK_DGRAM, IPPROTO_UDP, PF_INET6 HELP(":<host>:<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_LISTEN
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp6_listen = { "UDP6-LISTEN", 1+XIO_RDWR, xioopen_ipdgram_listen, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_LISTEN|GROUP_CHILD|GROUP_RANGE, PF_INET6, IPPROTO_UDP, 0 HELP(":<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_LISTEN */
|
2023-06-21 18:44:29 +00:00
|
|
|
const struct addrdesc xioaddr_udp6_sendto = { "UDP6-SENDTO", 1+XIO_RDWR, xioopen_udp_sendto, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP, PF_INET6, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp6_datagram= { "UDP6-DATAGRAM", 1+XIO_RDWR, xioopen_udp_datagram,GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_RANGE, PF_INET6, SOCK_DGRAM, IPPROTO_UDP HELP(":<host>:<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp6_recvfrom= { "UDP6-RECVFROM", 1+XIO_RDWR, xioopen_udp_recvfrom, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_CHILD|GROUP_RANGE, PF_INET6, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
|
|
|
const struct addrdesc xioaddr_udp6_recv = { "UDP6-RECV", 1+XIO_RDONLY, xioopen_udp_recv, GROUP_FD|GROUP_SOCKET|GROUP_SOCK_IP6|GROUP_IP_UDP|GROUP_RANGE, PF_INET6, SOCK_DGRAM, IPPROTO_UDP HELP(":<port>") };
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_IP6 */
|
|
|
|
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
int _xioopen_ipdgram_listen(struct single *sfd,
|
|
|
|
int xioflags, union sockaddr_union *us, socklen_t uslen,
|
|
|
|
struct opt *opts, int pf, int socktype, int ipproto) {
|
2008-01-27 12:00:08 +00:00
|
|
|
union sockaddr_union themunion;
|
|
|
|
union sockaddr_union *them = &themunion;
|
2008-07-24 19:51:38 +00:00
|
|
|
struct pollfd readfd;
|
2008-01-27 12:00:08 +00:00
|
|
|
bool dofork = false;
|
2017-01-07 09:46:01 +00:00
|
|
|
int maxchildren = 0;
|
2008-01-27 12:00:08 +00:00
|
|
|
pid_t pid;
|
|
|
|
char *rangename;
|
|
|
|
char infobuff[256];
|
|
|
|
unsigned char buff1[1];
|
|
|
|
socklen_t themlen;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
retropt_bool(opts, OPT_FORK, &dofork);
|
|
|
|
|
|
|
|
if (dofork) {
|
|
|
|
if (!(xioflags & XIO_MAYFORK)) {
|
|
|
|
Error("option fork not allowed here");
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-07 09:46:01 +00:00
|
|
|
retropt_int(opts, OPT_MAX_CHILDREN, &maxchildren);
|
|
|
|
|
|
|
|
if (! dofork && maxchildren) {
|
|
|
|
Error("option max-children not allowed without option fork");
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
#if WITH_IP4 /*|| WITH_IP6*/
|
|
|
|
if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
|
2023-11-05 12:56:58 +00:00
|
|
|
if (xioparserange(rangename, pf, &sfd->para.socket.range,
|
2023-11-05 18:39:01 +00:00
|
|
|
sfd->para.socket.ip.ai_flags)
|
2023-11-05 12:56:58 +00:00
|
|
|
< 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
free(rangename);
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
free(rangename);
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->para.socket.dorange = true;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WITH_LIBWRAP
|
2020-12-30 18:46:42 +00:00
|
|
|
xio_retropt_tcpwrap(sfd, opts);
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_LIBWRAP */
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT, &sfd->para.socket.ip.sourceport)
|
2008-01-27 12:00:08 +00:00
|
|
|
>= 0) {
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->para.socket.ip.dosourceport = true;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
2020-12-30 18:46:42 +00:00
|
|
|
retropt_bool(opts, OPT_LOWPORT, &sfd->para.socket.ip.lowport);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2008-02-01 22:15:14 +00:00
|
|
|
if (dofork) {
|
|
|
|
xiosetchilddied(); /* set SIGCHLD handler */
|
|
|
|
}
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
while (true) { /* we loop with fork or prohibited packets */
|
|
|
|
/* now wait for some packet on this datagram socket, get its sender
|
|
|
|
address, connect there, and return */
|
2023-10-26 17:45:01 +00:00
|
|
|
union integral notnull;
|
|
|
|
union integral reuseaddr;
|
2010-10-03 14:38:04 +00:00
|
|
|
int doreuseaddr = (dofork != 0);
|
2008-01-27 12:00:08 +00:00
|
|
|
char infobuff[256];
|
|
|
|
union sockaddr_union _sockname;
|
|
|
|
union sockaddr_union *la = &_sockname; /* local address */
|
|
|
|
|
2023-10-26 17:45:01 +00:00
|
|
|
reuseaddr.u_int = dofork;
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
if ((sfd->fd = xiosocket(opts, pf, socktype, ipproto, E_ERROR)) < 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
2023-10-26 17:45:01 +00:00
|
|
|
doreuseaddr |= (retropt_2integrals(opts, OPT_SO_REUSEADDR,
|
|
|
|
&reuseaddr, ¬null) >= 0);
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(sfd, -1, opts, PH_PASTSOCKET);
|
2023-10-26 17:45:01 +00:00
|
|
|
|
|
|
|
/* SO_REUSEADDR handling of UDP sockets is helpful on Solaris */
|
2010-10-03 14:38:04 +00:00
|
|
|
if (doreuseaddr) {
|
2020-12-30 18:46:42 +00:00
|
|
|
if (Setsockopt(sfd->fd, opt_so_reuseaddr.major,
|
2023-10-26 17:45:01 +00:00
|
|
|
opt_so_reuseaddr.minor, &reuseaddr.u_int, sizeof(reuseaddr.u_int))
|
2010-10-03 14:38:04 +00:00
|
|
|
< 0) {
|
|
|
|
Warn6("setsockopt(%d, %d, %d, {%d}, "F_Zd"): %s",
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->fd, opt_so_reuseaddr.major,
|
2023-10-26 17:45:01 +00:00
|
|
|
opt_so_reuseaddr.minor, reuseaddr.u_int, sizeof(reuseaddr.u_int),
|
2010-10-03 14:38:04 +00:00
|
|
|
strerror(errno));
|
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
2020-12-30 18:46:42 +00:00
|
|
|
applyopts_cloexec(sfd->fd, opts);
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(sfd, -1, opts, PH_PREBIND);
|
|
|
|
applyopts(sfd, -1, opts, PH_BIND);
|
2020-12-30 18:46:42 +00:00
|
|
|
if (Bind(sfd->fd, &us->soa, uslen) < 0) {
|
|
|
|
Error4("bind(%d, {%s}, "F_socklen"): %s", sfd->fd,
|
|
|
|
sockaddr_info(&us->soa, uslen, infobuff, sizeof(infobuff)),
|
2008-01-27 12:00:08 +00:00
|
|
|
uslen, strerror(errno));
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
/* under some circumstances bind() fills sockaddr with interesting info. */
|
2020-12-30 18:46:42 +00:00
|
|
|
if (Getsockname(sfd->fd, &us->soa, &uslen) < 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
Error4("getsockname(%d, %p, {%d}): %s",
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->fd, &us->soa, uslen, strerror(errno));
|
|
|
|
}
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(sfd, -1, opts, PH_PASTBIND);
|
2020-12-30 18:46:42 +00:00
|
|
|
|
|
|
|
if (ipproto == IPPROTO_UDP) {
|
|
|
|
Notice1("listening on UDP %s",
|
|
|
|
sockaddr_info(&us->soa, uslen, infobuff, sizeof(infobuff)));
|
|
|
|
} else {
|
|
|
|
Notice2("listening on PROTO%d %s", ipproto,
|
|
|
|
sockaddr_info(&us->soa, uslen, infobuff, sizeof(infobuff)));
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
readfd.fd = sfd->fd;
|
2008-07-24 19:51:38 +00:00
|
|
|
readfd.events = POLLIN|POLLERR;
|
2008-09-29 19:18:58 +00:00
|
|
|
while (xiopoll(&readfd, 1, NULL) < 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
if (errno != EINTR) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
themlen = socket_init(pf, them);
|
|
|
|
do {
|
2020-12-30 18:46:42 +00:00
|
|
|
result = Recvfrom(sfd->fd, buff1, 1, MSG_PEEK,
|
2008-01-27 12:00:08 +00:00
|
|
|
&them->soa, &themlen);
|
|
|
|
} while (result < 0 && errno == EINTR);
|
|
|
|
if (result < 0) {
|
2014-03-09 21:08:19 +00:00
|
|
|
Error5("recvfrom(%d, %p, 1, MSG_PEEK, {%s}, {"F_socklen"}): %s",
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->fd, buff1,
|
2008-01-27 12:00:08 +00:00
|
|
|
sockaddr_info(&them->soa, themlen, infobuff, sizeof(infobuff)),
|
|
|
|
themlen, strerror(errno));
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
|
|
|
|
Notice1("accepting UDP connection from %s",
|
|
|
|
sockaddr_info(&them->soa, themlen, infobuff, sizeof(infobuff)));
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
if (xiocheckpeer(sfd, them, la) < 0) {
|
|
|
|
Notice1("forbidding UDP connection from %s",
|
|
|
|
sockaddr_info(&them->soa, themlen,
|
|
|
|
infobuff, sizeof(infobuff)));
|
2008-01-27 12:00:08 +00:00
|
|
|
/* drop packet */
|
|
|
|
char buff[512];
|
2020-12-30 18:46:42 +00:00
|
|
|
Recv(sfd->fd, buff, sizeof(buff), 0); /* drop packet */
|
|
|
|
Close(sfd->fd);
|
2008-01-27 12:00:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Info1("permitting UDP connection from %s",
|
|
|
|
sockaddr_info(&them->soa, themlen, infobuff, sizeof(infobuff)));
|
|
|
|
|
|
|
|
if (dofork) {
|
2023-09-30 13:18:39 +00:00
|
|
|
pid = xio_fork(false, E_ERROR, sfd->shutup);
|
2008-01-27 12:00:08 +00:00
|
|
|
if (pid < 0) {
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:17:55 +00:00
|
|
|
if (pid == 0) { /* child */
|
2017-01-07 09:46:01 +00:00
|
|
|
pid_t cpid = Getpid();
|
|
|
|
xiosetenvulong("PID", cpid, 1);
|
2008-01-27 12:00:08 +00:00
|
|
|
break;
|
|
|
|
}
|
2008-09-22 20:17:55 +00:00
|
|
|
|
2008-09-04 21:42:52 +00:00
|
|
|
/* server: continue loop with socket()+recvfrom() */
|
2023-06-23 20:48:26 +00:00
|
|
|
/* This avoids the requirement of a sync (trigger) mechanism as with
|
|
|
|
RECVFROM addresses */
|
|
|
|
/* And when we dont close this we got awkward behaviour on Linux 2.4:
|
2008-01-27 12:00:08 +00:00
|
|
|
recvfrom gives 0 bytes with invalid socket address */
|
2020-12-30 18:46:42 +00:00
|
|
|
if (Close(sfd->fd) < 0) {
|
|
|
|
Info2("close(%d): %s", sfd->fd, strerror(errno));
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
|
2017-01-07 09:46:01 +00:00
|
|
|
while (maxchildren) {
|
|
|
|
if (num_child < maxchildren) break;
|
|
|
|
Notice("maxchildren are active, waiting");
|
|
|
|
/* UINT_MAX would even be nicer, but Openindiana works only
|
|
|
|
with 31 bits */
|
|
|
|
while (!Sleep(INT_MAX)) ; /* any signal lets us continue */
|
|
|
|
}
|
|
|
|
Info("still listening");
|
2008-01-27 12:00:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2020-12-30 18:46:42 +00:00
|
|
|
} /* end of the big while loop */
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(sfd, -1, opts, PH_CONNECT);
|
2020-12-30 18:46:42 +00:00
|
|
|
if ((result = Connect(sfd->fd, &them->soa, themlen)) < 0) {
|
2014-03-09 21:08:19 +00:00
|
|
|
Error4("connect(%d, {%s}, "F_socklen"): %s",
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->fd,
|
2008-01-27 12:00:08 +00:00
|
|
|
sockaddr_info(&them->soa, themlen, infobuff, sizeof(infobuff)),
|
|
|
|
themlen, strerror(errno));
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
|
2008-09-22 20:17:55 +00:00
|
|
|
/* set the env vars describing the local and remote sockets */
|
2020-12-30 18:46:42 +00:00
|
|
|
if (Getsockname(sfd->fd, &us->soa, &uslen) < 0) {
|
2008-09-22 20:17:55 +00:00
|
|
|
Warn4("getsockname(%d, %p, {%d}): %s",
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->fd, &us->soa, uslen, strerror(errno));
|
2008-09-22 20:17:55 +00:00
|
|
|
}
|
2020-12-30 18:46:42 +00:00
|
|
|
xiosetsockaddrenv("SOCK", us, uslen, IPPROTO_UDP);
|
2008-09-22 20:17:55 +00:00
|
|
|
xiosetsockaddrenv("PEER", them, themlen, IPPROTO_UDP);
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
sfd->howtoend = END_SHUTDOWN;
|
|
|
|
applyopts_fchown(sfd->fd, opts);
|
2023-07-13 07:06:08 +00:00
|
|
|
applyopts(sfd, -1, opts, PH_LATE);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
if ((result = _xio_openlate(sfd, opts)) < 0)
|
2008-01-27 12:00:08 +00:00
|
|
|
return result;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-30 18:46:42 +00:00
|
|
|
/* we expect the form: port */
|
2023-07-13 07:06:35 +00:00
|
|
|
int xioopen_ipdgram_listen(
|
|
|
|
int argc,
|
|
|
|
const char *argv[],
|
|
|
|
struct opt *opts,
|
|
|
|
int xioflags,
|
|
|
|
xiofile_t *xfd,
|
|
|
|
const struct addrdesc *addrdesc)
|
|
|
|
{
|
2023-07-13 07:06:08 +00:00
|
|
|
struct single *sfd = &xfd->stream;
|
2020-12-30 18:46:42 +00:00
|
|
|
const char *portname = argv[1];
|
2023-07-13 07:06:35 +00:00
|
|
|
int pf = addrdesc->arg1;
|
|
|
|
int ipproto = addrdesc->arg2;
|
2020-12-30 18:46:42 +00:00
|
|
|
union sockaddr_union us;
|
|
|
|
int socktype = SOCK_DGRAM;
|
|
|
|
socklen_t uslen;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_syntax(argv[0], 1, argc-1, addrdesc->syntax);
|
|
|
|
return STAT_NORETRY;
|
2020-12-30 18:46:42 +00:00
|
|
|
}
|
|
|
|
|
2023-11-05 12:59:14 +00:00
|
|
|
xioinit_ip(&pf, xioparms.default_ip);
|
2020-12-30 18:46:42 +00:00
|
|
|
if (pf == PF_UNSPEC) {
|
|
|
|
#if WITH_IP4 && WITH_IP6
|
2023-10-26 20:16:21 +00:00
|
|
|
switch (xioparms.default_ip) {
|
|
|
|
case '4': pf = PF_INET; break;
|
|
|
|
case '6': pf = PF_INET6; break;
|
|
|
|
default: break; /* includes \0 */
|
|
|
|
}
|
2020-12-30 18:46:42 +00:00
|
|
|
#elif WITH_IP6
|
|
|
|
pf = PF_INET6;
|
|
|
|
#else
|
|
|
|
pf = PF_INET;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
retropt_socket_pf(opts, &pf);
|
|
|
|
retropt_int(opts, OPT_SO_PROTOTYPE, &ipproto);
|
|
|
|
|
2023-07-13 07:06:08 +00:00
|
|
|
if (applyopts_single(sfd, opts, PH_INIT) < 0)
|
|
|
|
return -1;
|
|
|
|
applyopts(sfd, -1, opts, PH_INIT);
|
2020-12-30 18:46:42 +00:00
|
|
|
|
|
|
|
uslen = socket_init(pf, &us);
|
|
|
|
retropt_bind(opts, pf, socktype, ipproto,
|
|
|
|
(struct sockaddr *)&us, &uslen, 1,
|
2023-11-05 18:39:01 +00:00
|
|
|
xfd->stream.para.socket.ip.ai_flags);
|
2020-12-30 18:46:42 +00:00
|
|
|
|
|
|
|
if (false) {
|
|
|
|
;
|
|
|
|
#if WITH_IP4
|
|
|
|
} else if (pf == PF_INET) {
|
|
|
|
us.ip4.sin_port = parseport(portname, ipproto);
|
|
|
|
#endif
|
|
|
|
#if WITH_IP6
|
|
|
|
} else if (pf == PF_INET6) {
|
|
|
|
us.ip6.sin6_port = parseport(portname, ipproto);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
Error1("xioopen_ipdgram_listen(): unknown address family %d", pf);
|
|
|
|
}
|
|
|
|
|
2023-11-05 12:56:58 +00:00
|
|
|
return _xioopen_ipdgram_listen(&xfd->stream, xioflags, &us, uslen,
|
2020-12-30 18:46:42 +00:00
|
|
|
opts, pf, socktype, ipproto);
|
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
static int xioopen_udp_sendto(
|
|
|
|
int argc,
|
|
|
|
const char *argv[],
|
|
|
|
struct opt *opts,
|
|
|
|
int xioflags,
|
|
|
|
xiofile_t *xfd,
|
|
|
|
const struct addrdesc *addrdesc)
|
|
|
|
{
|
|
|
|
int pf = addrdesc->arg1;
|
|
|
|
int socktype = addrdesc->arg2;
|
|
|
|
int ipproto = addrdesc->arg3;
|
2008-01-27 12:00:08 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_syntax(argv[0], 2, argc-1, addrdesc->syntax);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
2008-09-20 21:47:06 +00:00
|
|
|
|
|
|
|
retropt_socket_pf(opts, &pf);
|
2023-11-05 12:56:58 +00:00
|
|
|
if ((result = _xioopen_udp_sendto(argv[1], argv[2], opts, xioflags, xfd,
|
2023-07-13 07:06:35 +00:00
|
|
|
addrdesc->groups, pf, socktype, ipproto))
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
2023-11-05 12:56:58 +00:00
|
|
|
_xio_openlate(&xfd->stream, opts);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-20 21:47:06 +00:00
|
|
|
/*
|
|
|
|
applies and consumes the following option:
|
|
|
|
PH_INIT, PH_PASTSOCKET, PH_FD, PH_PREBIND, PH_BIND, PH_PASTBIND, PH_CONNECTED, PH_LATE
|
|
|
|
OFUNC_OFFSET
|
|
|
|
OPT_BIND, OPT_SOURCEPORT, OPT_LOWPORT, OPT_SO_TYPE, OPT_SO_PROTOTYPE, OPT_USER, OPT_GROUP, OPT_CLOEXEC
|
|
|
|
*/
|
2008-01-27 12:00:08 +00:00
|
|
|
static
|
|
|
|
int _xioopen_udp_sendto(const char *hostname, const char *servname,
|
|
|
|
struct opt *opts,
|
2023-06-14 09:41:44 +00:00
|
|
|
int xioflags, xiofile_t *xxfd, groups_t groups,
|
2008-01-27 12:00:08 +00:00
|
|
|
int pf, int socktype, int ipproto) {
|
2023-07-13 07:06:35 +00:00
|
|
|
struct single *sfd = &xxfd->stream;
|
2008-01-27 12:00:08 +00:00
|
|
|
union sockaddr_union us;
|
|
|
|
socklen_t uslen;
|
|
|
|
int feats = 3; /* option bind supports address and port */
|
|
|
|
bool needbind = false;
|
|
|
|
int result;
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->howtoend = END_SHUTDOWN;
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
/* ...res_opts[] */
|
|
|
|
if (applyopts_single(sfd, opts, PH_INIT) < 0)
|
|
|
|
return -1;
|
|
|
|
applyopts(sfd, -1, opts, PH_INIT);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->salen = sizeof(sfd->peersa);
|
2008-01-27 12:00:08 +00:00
|
|
|
if ((result =
|
2023-10-22 21:15:49 +00:00
|
|
|
xioresolve(hostname, servname, pf, socktype, ipproto,
|
2023-07-13 07:06:35 +00:00
|
|
|
&sfd->peersa, &sfd->salen,
|
|
|
|
sfd->para.socket.ip.ai_flags))
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (pf == PF_UNSPEC) {
|
2023-07-13 07:06:35 +00:00
|
|
|
pf = sfd->peersa.soa.sa_family;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
uslen = socket_init(pf, &us);
|
|
|
|
if (retropt_bind(opts, pf, socktype, ipproto, &us.soa, &uslen, feats,
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.ip.ai_flags)
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_NOACTION) {
|
|
|
|
needbind = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT,
|
2023-07-13 07:06:35 +00:00
|
|
|
&sfd->para.socket.ip.sourceport) >= 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
switch (pf) {
|
|
|
|
#if WITH_IP4
|
|
|
|
case PF_INET:
|
2023-07-13 07:06:35 +00:00
|
|
|
us.ip4.sin_port = htons(sfd->para.socket.ip.sourceport);
|
2008-01-27 12:00:08 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if WITH_IP6
|
|
|
|
case PF_INET6:
|
2023-07-13 07:06:35 +00:00
|
|
|
us.ip6.sin6_port = htons(sfd->para.socket.ip.sourceport);
|
2008-01-27 12:00:08 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
needbind = true;
|
|
|
|
}
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
retropt_bool(opts, OPT_LOWPORT, &sfd->para.socket.ip.lowport);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->dtype = XIODATA_RECVFROM;
|
2008-01-27 12:00:08 +00:00
|
|
|
return _xioopen_dgram_sendto(needbind?&us:NULL, uslen,
|
2023-07-13 07:06:35 +00:00
|
|
|
opts, xioflags, sfd, groups,
|
2022-07-27 07:17:04 +00:00
|
|
|
pf, socktype, ipproto,
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.ip.lowport);
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
static int xioopen_udp_datagram(
|
|
|
|
int argc,
|
|
|
|
const char *argv[],
|
|
|
|
struct opt *opts,
|
|
|
|
int xioflags,
|
|
|
|
xiofile_t *xxfd,
|
|
|
|
const struct addrdesc *addrdesc)
|
|
|
|
{
|
|
|
|
struct single *sfd = &xxfd->stream;
|
|
|
|
int pf = addrdesc->arg1;
|
|
|
|
int socktype = addrdesc->arg2;
|
|
|
|
int ipproto = addrdesc->arg3;
|
2008-01-27 12:00:08 +00:00
|
|
|
char *rangename;
|
|
|
|
char *hostname;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_syntax(argv[0], 2, argc-1, addrdesc->syntax);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((hostname = strdup(argv[1])) == NULL) {
|
|
|
|
Error1("strdup(\"%s\"): out of memory", argv[1]);
|
|
|
|
return STAT_RETRYLATER;
|
|
|
|
}
|
|
|
|
|
2020-12-27 19:43:08 +00:00
|
|
|
/* only accept packets with correct remote ports */
|
2023-07-13 07:06:35 +00:00
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT, &sfd->para.socket.ip.sourceport)
|
2020-12-27 19:43:08 +00:00
|
|
|
>= 0) {
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.ip.dosourceport = true;
|
2020-12-27 19:43:08 +00:00
|
|
|
}
|
|
|
|
|
2008-09-20 21:47:06 +00:00
|
|
|
retropt_socket_pf(opts, &pf);
|
2023-11-06 20:22:49 +00:00
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
result =
|
2023-07-13 07:06:35 +00:00
|
|
|
_xioopen_udp_sendto(hostname, argv[2], opts, xioflags, xxfd,
|
|
|
|
addrdesc->groups, pf, socktype, ipproto);
|
2008-01-27 12:00:08 +00:00
|
|
|
free(hostname);
|
|
|
|
if (result != STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-11-06 20:22:49 +00:00
|
|
|
if (sfd->para.socket.ip.dosourceport) {
|
|
|
|
switch (sfd->peersa.soa.sa_family) {
|
|
|
|
case PF_INET:
|
|
|
|
default:
|
|
|
|
sfd->para.socket.ip.sourceport = ntohs(sfd->peersa.ip4.sin_port);
|
|
|
|
break;
|
|
|
|
case PF_INET6:
|
|
|
|
sfd->para.socket.ip.sourceport = ntohs(sfd->peersa.ip6.sin6_port);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->dtype = XIOREAD_RECV|XIOWRITE_SENDTO;
|
2008-01-27 12:00:08 +00:00
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.la.soa.sa_family = sfd->peersa.soa.sa_family;
|
2008-01-27 12:00:08 +00:00
|
|
|
|
|
|
|
/* which reply packets will be accepted - determine by range option */
|
2023-07-13 07:06:35 +00:00
|
|
|
if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
|
|
|
|
if (xioparserange(rangename, pf, &sfd->para.socket.range,
|
|
|
|
sfd->para.socket.ip.ai_flags)
|
2023-11-05 12:56:58 +00:00
|
|
|
< 0) {
|
2008-01-27 12:00:08 +00:00
|
|
|
free(rangename);
|
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.dorange = true;
|
|
|
|
sfd->dtype |= XIOREAD_RECV_CHECKRANGE;
|
2008-01-27 12:00:08 +00:00
|
|
|
free(rangename);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if WITH_LIBWRAP
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_retropt_tcpwrap(sfd, opts);
|
2008-01-27 12:00:08 +00:00
|
|
|
#endif /* WITH_LIBWRAP */
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
_xio_openlate(sfd, opts);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
static int xioopen_udp_recvfrom(
|
|
|
|
int argc,
|
|
|
|
const char *argv[],
|
|
|
|
struct opt *opts,
|
|
|
|
int xioflags,
|
|
|
|
xiofile_t *xfd,
|
|
|
|
const struct addrdesc *addrdesc)
|
|
|
|
{
|
|
|
|
struct single *sfd = &xfd->stream;
|
|
|
|
int pf = addrdesc->arg1;
|
|
|
|
int socktype = addrdesc->arg2;
|
|
|
|
int ipproto = addrdesc->arg3;
|
2008-01-27 12:00:08 +00:00
|
|
|
union sockaddr_union us;
|
|
|
|
socklen_t uslen = sizeof(us);
|
2023-11-05 12:58:07 +00:00
|
|
|
int ai_flags2[2];
|
2008-01-27 12:00:08 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_syntax(argv[0], 1, argc-1, addrdesc->syntax);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
|
2023-11-05 12:59:14 +00:00
|
|
|
xioinit_ip(&pf, xioparms.default_ip);
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->howtoend = END_NONE;
|
2008-01-27 12:00:08 +00:00
|
|
|
retropt_socket_pf(opts, &pf);
|
|
|
|
if (pf == PF_UNSPEC) {
|
|
|
|
#if WITH_IP4 && WITH_IP6
|
2023-10-26 20:16:21 +00:00
|
|
|
switch (xioparms.default_ip) {
|
|
|
|
case '4': pf = PF_INET; break;
|
|
|
|
case '6': pf = PF_INET6; break;
|
|
|
|
default: break; /* includes \0 */
|
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
#elif WITH_IP6
|
|
|
|
pf = PF_INET6;
|
|
|
|
#else
|
|
|
|
pf = PF_INET;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-11-05 12:58:07 +00:00
|
|
|
/* Set AI_PASSIVE, except when it is explicitely disabled */
|
|
|
|
ai_flags2[0] = xfd->stream.para.socket.ip.ai_flags[0];
|
|
|
|
ai_flags2[1] = xfd->stream.para.socket.ip.ai_flags[1];
|
|
|
|
if (!(ai_flags2[1] & AI_PASSIVE))
|
|
|
|
ai_flags2[0] |= AI_PASSIVE;
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if ((result =
|
2023-11-05 12:56:58 +00:00
|
|
|
xioresolve(NULL, argv[1], pf, socktype, ipproto, &us, &uslen,
|
2023-11-05 18:39:01 +00:00
|
|
|
ai_flags2))
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (pf == PF_UNSPEC) {
|
|
|
|
pf = us.soa.sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
union sockaddr_union la;
|
|
|
|
socklen_t lalen = sizeof(la);
|
2023-06-12 21:01:54 +00:00
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if (retropt_bind(opts, pf, socktype, ipproto, &la.soa, &lalen, 1,
|
2023-07-13 07:06:35 +00:00
|
|
|
sfd->para.socket.ip.ai_flags)
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_NOACTION) {
|
|
|
|
switch (pf) {
|
|
|
|
#if WITH_IP4
|
|
|
|
case PF_INET: us.ip4.sin_addr = la.ip4.sin_addr; break;
|
|
|
|
#endif
|
|
|
|
#if WITH_IP6
|
|
|
|
case PF_INET6: us.ip6.sin6_addr = la.ip6.sin6_addr; break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT, &sfd->para.socket.ip.sourceport) >= 0) {
|
|
|
|
sfd->para.socket.ip.dosourceport = true;
|
2008-01-27 12:00:08 +00:00
|
|
|
}
|
2023-07-13 07:06:35 +00:00
|
|
|
retropt_bool(opts, OPT_LOWPORT, &sfd->para.socket.ip.lowport);
|
2008-01-27 12:00:08 +00:00
|
|
|
|
|
|
|
xfd->stream.dtype = XIODATA_RECVFROM_ONE;
|
|
|
|
if ((result =
|
2023-07-13 07:06:35 +00:00
|
|
|
_xioopen_dgram_recvfrom(sfd, xioflags, &us.soa, uslen,
|
2008-01-27 12:00:08 +00:00
|
|
|
opts, pf, socktype, ipproto, E_ERROR))
|
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
_xio_openlate(&xfd->stream, opts);
|
|
|
|
return STAT_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-13 07:06:35 +00:00
|
|
|
static int xioopen_udp_recv(
|
|
|
|
int argc,
|
|
|
|
const char *argv[],
|
|
|
|
struct opt *opts,
|
|
|
|
int xioflags,
|
|
|
|
xiofile_t *xfd,
|
|
|
|
const struct addrdesc *addrdesc)
|
|
|
|
{
|
|
|
|
int pf = addrdesc->arg1;
|
|
|
|
int socktype = addrdesc->arg2;
|
|
|
|
int ipproto = addrdesc->arg3;
|
2008-01-27 12:00:08 +00:00
|
|
|
union sockaddr_union us;
|
|
|
|
socklen_t uslen = sizeof(us);
|
|
|
|
char *rangename;
|
2023-11-05 12:58:07 +00:00
|
|
|
int ai_flags2[2];
|
2008-01-27 12:00:08 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (argc != 2) {
|
2023-07-13 07:06:35 +00:00
|
|
|
xio_syntax(argv[0], 1, argc-1, addrdesc->syntax);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
|
|
|
|
retropt_socket_pf(opts, &pf);
|
|
|
|
if (pf == PF_UNSPEC) {
|
|
|
|
#if WITH_IP4 && WITH_IP6
|
2023-10-26 20:16:21 +00:00
|
|
|
switch (xioparms.default_ip) {
|
|
|
|
case '4': pf = PF_INET; break;
|
|
|
|
case '6': pf = PF_INET6; break;
|
|
|
|
default: break; /* includes \0 */
|
|
|
|
}
|
2008-01-27 12:00:08 +00:00
|
|
|
#elif WITH_IP6
|
|
|
|
pf = PF_INET6;
|
|
|
|
#else
|
|
|
|
pf = PF_INET;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-11-05 12:58:07 +00:00
|
|
|
/* Set AI_PASSIVE, except when it is explicitely disabled */
|
|
|
|
ai_flags2[0] = xfd->stream.para.socket.ip.ai_flags[0];
|
|
|
|
ai_flags2[1] = xfd->stream.para.socket.ip.ai_flags[1];
|
|
|
|
if (!(ai_flags2[1] & AI_PASSIVE))
|
|
|
|
ai_flags2[0] |= AI_PASSIVE;
|
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if ((result =
|
2023-11-05 12:56:58 +00:00
|
|
|
xioresolve(NULL, argv[1], pf, socktype, ipproto, &us, &uslen,
|
2023-11-05 18:39:01 +00:00
|
|
|
ai_flags2))
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (pf == PF_UNSPEC) {
|
|
|
|
pf = us.soa.sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
{
|
|
|
|
union sockaddr_union la;
|
|
|
|
socklen_t lalen = sizeof(la);
|
2023-06-12 21:01:54 +00:00
|
|
|
|
2008-01-27 12:00:08 +00:00
|
|
|
if (retropt_bind(opts, pf, socktype, ipproto,
|
|
|
|
&xfd->stream.para.socket.la.soa, &lalen, 1,
|
2023-11-05 18:39:01 +00:00
|
|
|
ai_flags2)
|
2008-01-27 12:00:08 +00:00
|
|
|
!= STAT_NOACTION) {
|
|
|
|
switch (pf) {
|
|
|
|
#if WITH_IP4
|
|
|
|
case PF_INET:
|
|
|
|
us.ip4.sin_addr = xfd->stream.para.socket.la.ip4.sin_addr; break;
|
|
|
|
#endif
|
|
|
|
#if WITH_IP6
|
|
|
|
case PF_INET6:
|
|
|
|
us.ip6.sin6_addr = xfd->stream.para.socket.la.ip6.sin6_addr; break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xfd->stream.para.socket.la.soa.sa_family = pf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WITH_IP4 /*|| WITH_IP6*/
|
|
|
|
if (retropt_string(opts, OPT_RANGE, &rangename) >= 0) {
|
2023-11-05 12:56:58 +00:00
|
|
|
if (xioparserange(rangename, pf, &xfd->stream.para.socket.range,
|
2023-11-05 18:39:01 +00:00
|
|
|
xfd->stream.para.socket.ip.ai_flags)
|
2023-11-05 12:56:58 +00:00
|
|
|
< 0) {
|
2023-10-22 21:15:49 +00:00
|
|
|
free(rangename);
|
2008-01-27 12:00:08 +00:00
|
|
|
return STAT_NORETRY;
|
|
|
|
}
|
|
|
|
xfd->stream.para.socket.dorange = true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if WITH_LIBWRAP
|
|
|
|
xio_retropt_tcpwrap(&xfd->stream, opts);
|
|
|
|
#endif /* WITH_LIBWRAP */
|
|
|
|
|
|
|
|
if (retropt_ushort(opts, OPT_SOURCEPORT,
|
|
|
|
&xfd->stream.para.socket.ip.sourceport)
|
|
|
|
>= 0) {
|
|
|
|
xfd->stream.para.socket.ip.dosourceport = true;
|
|
|
|
}
|
|
|
|
retropt_bool(opts, OPT_LOWPORT, &xfd->stream.para.socket.ip.lowport);
|
|
|
|
|
|
|
|
xfd->stream.dtype = XIODATA_RECV;
|
|
|
|
if ((result = _xioopen_dgram_recv(&xfd->stream, xioflags, &us.soa, uslen,
|
|
|
|
opts, pf, socktype, ipproto, E_ERROR))
|
|
|
|
!= STAT_OK) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
_xio_openlate(&xfd->stream, opts);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_UDP && (WITH_IP4 || WITH_IP6) */
|