This tests UDP.LADDR, UDP.LPORT, UDP.RADDR, and UDP.RPORT.
Signed-off-by: Masatake YAMATO <yamato@redhat.com>
--- /dev/null
+ 3 UDP SOCK state=close laddr=127.0.0.1:34567 close dgram 0 127.0.0.1 0.0.0.0 127.0.0.1:34567 34567 0.0.0.0:0 0
+ 4 UDP SOCK state=established laddr=127.0.0.1:23456 raddr=127.0.0.1:34567 established dgram 0 127.0.0.1 127.0.0.1 127.0.0.1:23456 23456 127.0.0.1:34567 34567
+ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
+ 3 UDP SOCK 0
+ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
+NAME pattern match: OK
+ 4 UDP SOCK established dgram 0 127.0.0.1 127.0.0.1 127.0.0.1:34567 34567
+ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT: 0
+LADDR/LPORT pattern match: OK
+ 3 UDP SOCK state=close laddr=127.0.0.1:34567 close dgram 0 127.0.0.1 0.0.0.0 127.0.0.1:34567 34567 0.0.0.0:0 0
+ 4 UDP SOCK state=close laddr=127.0.0.1:23456 close dgram 0 127.0.0.1 0.0.0.0 127.0.0.1:23456 23456 0.0.0.0:0 0
+ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT: 0
return NULL;
}
+static void *make_udp(const struct factory *factory, struct fdesc fdescs[],
+ int argc, char ** argv)
+{
+ struct arg server_port = decode_arg("server-port", factory->params, argc, argv);
+ unsigned short iserver_port = (unsigned short)ARG_INTEGER(server_port);
+ struct arg client_port = decode_arg("client-port", factory->params, argc, argv);
+ unsigned short iclient_port = (unsigned short)ARG_INTEGER(client_port);
+
+ struct arg server_do_bind = decode_arg("server-do-bind", factory->params, argc, argv);
+ bool bserver_do_bind = ARG_BOOLEAN(server_do_bind);
+ struct arg client_do_bind = decode_arg("client-do-bind", factory->params, argc, argv);
+ bool bclient_do_bind = ARG_BOOLEAN(client_do_bind);
+ struct arg client_do_connect = decode_arg("client-do-connect", factory->params, argc, argv);
+ bool bclient_do_connect = ARG_BOOLEAN(client_do_connect);
+
+ struct sockaddr_in sin, cin;
+ int ssd, csd;
+
+ const int y = 1;
+
+ free_arg(&client_do_connect);
+ free_arg(&client_do_bind);
+ free_arg(&server_do_bind);
+ free_arg(&server_port);
+ free_arg(&client_port);
+
+ ssd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (ssd < 0)
+ err(EXIT_FAILURE,
+ _("failed to make a udp socket for server"));
+
+ if (setsockopt(ssd, SOL_SOCKET,
+ SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) {
+ int e = errno;
+ close(ssd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)");
+ }
+
+ if (ssd != fdescs[0].fd) {
+ if (dup2(ssd, fdescs[0].fd) < 0) {
+ int e = errno;
+ close(ssd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to dup %d -> %d", ssd, fdescs[0].fd);
+ }
+ close(ssd);
+ ssd = fdescs[0].fd;
+ }
+
+ memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(iserver_port);
+ sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ if (bserver_do_bind) {
+ if (bind(ssd, &sin, sizeof(sin)) < 0) {
+ int e = errno;
+ close(ssd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to bind a srever socket");
+ }
+ }
+
+ csd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (csd < 0) {
+ int e = errno;
+ close(ssd);
+ errno = e;
+ err(EXIT_FAILURE,
+ _("failed to make a udp client socket"));
+ }
+
+ if (setsockopt(csd, SOL_SOCKET,
+ SO_REUSEADDR, (const char *)&y, sizeof(y)) < 0) {
+ int e = errno;
+ close(ssd);
+ close(csd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to setsockopt(SO_REUSEADDR)");
+ }
+
+ if (csd != fdescs[1].fd) {
+ if (dup2(csd, fdescs[1].fd) < 0) {
+ int e = errno;
+ close(ssd);
+ close(csd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to dup %d -> %d", csd, fdescs[1].fd);
+ }
+ close(csd);
+ csd = fdescs[1].fd;
+ }
+
+ if (bclient_do_bind) {
+ memset(&cin, 0, sizeof(cin));
+ cin.sin_family = AF_INET;
+ cin.sin_port = htons(iclient_port);
+ cin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ if (bind(csd, &cin, sizeof(cin)) < 0) {
+ int e = errno;
+ close(ssd);
+ close(csd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to bind a client socket");
+ }
+ }
+
+ if (bclient_do_connect) {
+ if (connect(csd, &sin, sizeof(sin)) < 0) {
+ int e = errno;
+ close(ssd);
+ close(csd);
+ errno = e;
+ err(EXIT_FAILURE, "failed to connect a client socket to the serer socket");
+ }
+ }
+
+ fdescs[0] = (struct fdesc) {
+ .fd = fdescs[0].fd,
+ .close = close_fdesc,
+ .data = NULL,
+ };
+ fdescs[1] = (struct fdesc) {
+ .fd = fdescs[1].fd,
+ .close = close_fdesc,
+ .data = NULL,
+ };
+
+ return NULL;
+}
+
#define PARAM_END { .name = NULL, }
static const struct factory factories[] = {
{
PARAM_END
}
},
+ {
+ .name = "udp",
+ .desc = "AF_INET+SOCK_DGRAM sockets",
+ .priv = false,
+ .N = 2,
+ .EX_N = 0,
+ .make = make_udp,
+ .params = (struct parameter []) {
+ {
+ .name = "server-port",
+ .type = PTYPE_INTEGER,
+ .desc = "TCP port the server may listen",
+ .defv.integer = 12345,
+ },
+ {
+ .name = "client-port",
+ .type = PTYPE_INTEGER,
+ .desc = "TCP port the client may bind",
+ .defv.integer = 23456,
+ },
+ {
+ .name = "server-do-bind",
+ .type = PTYPE_BOOLEAN,
+ .desc = "call bind with the server socket",
+ .defv.boolean = true,
+ },
+ {
+ .name = "client-do-bind",
+ .type = PTYPE_BOOLEAN,
+ .desc = "call bind with the client socket",
+ .defv.boolean = true,
+ },
+ {
+ .name = "client-do-connect",
+ .type = PTYPE_BOOLEAN,
+ .desc = "call connect with the client socket",
+ .defv.boolean = true,
+ },
+ PARAM_END
+ }
+ }
};
static int count_parameters(const struct factory *factory)
--- /dev/null
+#!/bin/bash
+#
+# Copyright (C) 2022 Masatake YAMATO <yamato@redhat.com>
+#
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="UDP sockets"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_LSFD"
+ts_check_test_command "$TS_HELPER_MKFDS"
+
+ts_cd "$TS_OUTDIR"
+
+PID=
+FDS=3
+FDC=4
+EXTRA_PARAMS=
+EXPR='(TYPE == "UDP") and (FD >= 3) and (FD <= 4)'
+EXPR_server='(TYPE == "UDP") and (FD == 3)'
+EXPR_client='(TYPE == "UDP") and (FD == 4)'
+NAME=
+LADDR=
+LPORT=
+
+{
+ coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+ server-port=34567 \
+ client-port=23456 ; }
+ if read -r -u "${MKFDS[0]}" PID; then
+ ${TS_CMD_LSFD} -n \
+ -o ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+ -p "${PID}" -Q "${EXPR}"
+ echo 'ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+ kill -CONT "${PID}"
+ wait "${MKFDS_PID}"
+ fi
+
+ coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+ server-port=34567 \
+ client-port=23456 \
+ server-do-bind=no; }
+ if read -r -u "${MKFDS[0]}" PID; then
+ ${TS_CMD_LSFD} -n \
+ -o ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+ -p "${PID}" -Q "${EXPR_server}"
+ echo 'ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+ NAME=$(${TS_CMD_LSFD} -n \
+ --raw \
+ -o NAME \
+ -p "${PID}" -Q "${EXPR_server}")
+ if [[ "$NAME" =~ ^socket:\[[[:digit:]]+\]$ ]]; then
+ echo "NAME pattern match: OK"
+ else
+ echo "NAME pattern match: FAILED (NAME=$NAME)"
+ fi
+
+ kill -CONT "${PID}"
+ wait "${MKFDS_PID}"
+ fi
+
+ coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+ server-port=34567 \
+ client-port=23456 \
+ client-do-bind=no; }
+ if read -r -u "${MKFDS[0]}" PID; then
+ ${TS_CMD_LSFD} -n \
+ -o ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT \
+ -p "${PID}" -Q "${EXPR_client}"
+ echo 'ASSOC,TYPE,STTYPE,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.RADDR,UDP.RPORT': $?
+
+ LADDR=$(${TS_CMD_LSFD} -n \
+ --raw \
+ -o UDP.LADDR \
+ -p "${PID}" -Q "${EXPR_client}")
+ LPORT=$(${TS_CMD_LSFD} -n \
+ --raw \
+ -o UDP.LPORT \
+ -p "${PID}" -Q "${EXPR_client}")
+
+ if [[ 127.0.0.1:$LPORT == ${LADDR} ]]; then
+ echo "LADDR/LPORT pattern match: OK"
+ else
+ echo "LADDR/LPORT pattern match: FAILED (LADDR=$LADDR, LPORT=$LPORT)"
+ fi
+
+ kill -CONT "${PID}"
+ wait "${MKFDS_PID}"
+ fi
+
+ coproc MKFDS { "$TS_HELPER_MKFDS" udp $FDS $FDC \
+ server-port=34567 \
+ client-port=23456 \
+ client-do-connect=no; }
+ if read -r -u "${MKFDS[0]}" PID; then
+ ${TS_CMD_LSFD} -n \
+ -o ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT \
+ -p "${PID}" -Q "${EXPR}"
+ echo 'ASSOC,TYPE,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,INET.LADDR,INET.RADDR,UDP.LADDR,UDP.LPORT,UDP.RADDR,UDP.RPORT': $?
+
+ kill -CONT "${PID}"
+ wait "${MKFDS_PID}"
+ fi
+} > "$TS_OUTPUT" 2>&1
+
+ts_finalize