2021-10-29 08:51:35 -07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
#include <errno.h>
|
2022-02-09 16:36:43 -08:00
|
|
|
#include <error.h>
|
2021-10-29 08:51:35 -07:00
|
|
|
#include <netdb.h>
|
2022-02-09 16:36:43 -08:00
|
|
|
#include <stdbool.h>
|
2021-10-29 08:51:35 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2022-02-09 16:36:47 -08:00
|
|
|
#include <time.h>
|
2021-10-29 08:51:35 -07:00
|
|
|
#include <unistd.h>
|
2022-02-09 16:36:44 -08:00
|
|
|
#include <linux/icmp.h>
|
|
|
|
#include <linux/icmpv6.h>
|
2022-02-09 16:36:47 -08:00
|
|
|
#include <linux/net_tstamp.h>
|
2021-10-29 08:51:35 -07:00
|
|
|
#include <linux/types.h>
|
2022-02-09 16:36:44 -08:00
|
|
|
#include <linux/udp.h>
|
2021-10-29 08:51:35 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2022-02-09 16:36:43 -08:00
|
|
|
enum {
|
|
|
|
ERN_SUCCESS = 0,
|
|
|
|
/* Well defined errors, callers may depend on these */
|
|
|
|
ERN_SEND = 1,
|
|
|
|
/* Informational, can reorder */
|
|
|
|
ERN_HELP,
|
|
|
|
ERN_SEND_SHORT,
|
|
|
|
ERN_SOCK_CREATE,
|
|
|
|
ERN_RESOLVE,
|
|
|
|
ERN_CMSG_WR,
|
2022-02-09 16:36:47 -08:00
|
|
|
ERN_SOCKOPT,
|
|
|
|
ERN_GETTIME,
|
2022-02-09 16:36:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct options {
|
|
|
|
bool silent_send;
|
|
|
|
const char *host;
|
|
|
|
const char *service;
|
2022-02-09 16:36:46 -08:00
|
|
|
struct {
|
|
|
|
unsigned int mark;
|
|
|
|
} sockopt;
|
2022-02-09 16:36:43 -08:00
|
|
|
struct {
|
2022-02-09 16:36:44 -08:00
|
|
|
unsigned int family;
|
2022-02-09 16:36:43 -08:00
|
|
|
unsigned int type;
|
2022-02-09 16:36:44 -08:00
|
|
|
unsigned int proto;
|
2022-02-09 16:36:43 -08:00
|
|
|
} sock;
|
|
|
|
struct {
|
|
|
|
bool ena;
|
|
|
|
unsigned int val;
|
|
|
|
} mark;
|
2022-02-09 16:36:47 -08:00
|
|
|
struct {
|
|
|
|
bool ena;
|
|
|
|
unsigned int delay;
|
|
|
|
} txtime;
|
2022-02-09 16:36:43 -08:00
|
|
|
} opt = {
|
|
|
|
.sock = {
|
2022-02-09 16:36:44 -08:00
|
|
|
.family = AF_UNSPEC,
|
2022-02-09 16:36:43 -08:00
|
|
|
.type = SOCK_DGRAM,
|
2022-02-09 16:36:44 -08:00
|
|
|
.proto = IPPROTO_UDP,
|
2022-02-09 16:36:43 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-02-09 16:36:47 -08:00
|
|
|
static struct timespec time_start_mono;
|
|
|
|
|
2022-02-09 16:36:43 -08:00
|
|
|
static void __attribute__((noreturn)) cs_usage(const char *bin)
|
|
|
|
{
|
|
|
|
printf("Usage: %s [opts] <dst host> <dst port / service>\n", bin);
|
|
|
|
printf("Options:\n"
|
|
|
|
"\t\t-s Silent send() failures\n"
|
2022-02-09 16:36:44 -08:00
|
|
|
"\t\t-4/-6 Force IPv4 / IPv6 only\n"
|
|
|
|
"\t\t-p prot Socket protocol\n"
|
|
|
|
"\t\t (u = UDP (default); i = ICMP; r = RAW)\n"
|
|
|
|
"\n"
|
2022-02-09 16:36:43 -08:00
|
|
|
"\t\t-m val Set SO_MARK with given value\n"
|
2022-02-09 16:36:46 -08:00
|
|
|
"\t\t-M val Set SO_MARK via setsockopt\n"
|
2022-02-09 16:36:47 -08:00
|
|
|
"\t\t-d val Set SO_TXTIME with given delay (usec)\n"
|
2022-02-09 16:36:43 -08:00
|
|
|
"");
|
|
|
|
exit(ERN_HELP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cs_parse_args(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char o;
|
|
|
|
|
2022-02-09 16:36:47 -08:00
|
|
|
while ((o = getopt(argc, argv, "46sp:m:M:d:")) != -1) {
|
2022-02-09 16:36:43 -08:00
|
|
|
switch (o) {
|
|
|
|
case 's':
|
|
|
|
opt.silent_send = true;
|
|
|
|
break;
|
2022-02-09 16:36:44 -08:00
|
|
|
case '4':
|
|
|
|
opt.sock.family = AF_INET;
|
|
|
|
break;
|
|
|
|
case '6':
|
|
|
|
opt.sock.family = AF_INET6;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
if (*optarg == 'u' || *optarg == 'U') {
|
|
|
|
opt.sock.proto = IPPROTO_UDP;
|
|
|
|
} else if (*optarg == 'i' || *optarg == 'I') {
|
|
|
|
opt.sock.proto = IPPROTO_ICMP;
|
|
|
|
} else if (*optarg == 'r') {
|
|
|
|
opt.sock.type = SOCK_RAW;
|
|
|
|
} else {
|
|
|
|
printf("Error: unknown protocol: %s\n", optarg);
|
|
|
|
cs_usage(argv[0]);
|
|
|
|
}
|
|
|
|
break;
|
2022-02-09 16:36:47 -08:00
|
|
|
|
2022-02-09 16:36:43 -08:00
|
|
|
case 'm':
|
|
|
|
opt.mark.ena = true;
|
|
|
|
opt.mark.val = atoi(optarg);
|
|
|
|
break;
|
2022-02-09 16:36:46 -08:00
|
|
|
case 'M':
|
|
|
|
opt.sockopt.mark = atoi(optarg);
|
|
|
|
break;
|
2022-02-09 16:36:47 -08:00
|
|
|
case 'd':
|
|
|
|
opt.txtime.ena = true;
|
|
|
|
opt.txtime.delay = atoi(optarg);
|
|
|
|
break;
|
2022-02-09 16:36:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind != argc - 2)
|
|
|
|
cs_usage(argv[0]);
|
|
|
|
|
|
|
|
opt.host = argv[optind];
|
|
|
|
opt.service = argv[optind + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-02-09 16:36:47 -08:00
|
|
|
cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
|
2021-10-29 08:51:35 -07:00
|
|
|
{
|
|
|
|
struct cmsghdr *cmsg;
|
2022-02-09 16:36:43 -08:00
|
|
|
size_t cmsg_len;
|
|
|
|
|
|
|
|
msg->msg_control = cbuf;
|
|
|
|
cmsg_len = 0;
|
|
|
|
|
|
|
|
if (opt.mark.ena) {
|
|
|
|
cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
|
|
|
|
cmsg_len += CMSG_SPACE(sizeof(__u32));
|
|
|
|
if (cbuf_sz < cmsg_len)
|
|
|
|
error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
|
|
|
|
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SO_MARK;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
|
|
|
|
*(__u32 *)CMSG_DATA(cmsg) = opt.mark.val;
|
|
|
|
}
|
2022-02-09 16:36:47 -08:00
|
|
|
if (opt.txtime.ena) {
|
|
|
|
struct sock_txtime so_txtime = {
|
|
|
|
.clockid = CLOCK_MONOTONIC,
|
|
|
|
};
|
|
|
|
__u64 txtime;
|
|
|
|
|
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
|
|
|
|
&so_txtime, sizeof(so_txtime)))
|
|
|
|
error(ERN_SOCKOPT, errno, "setsockopt TXTIME");
|
|
|
|
|
|
|
|
txtime = time_start_mono.tv_sec * (1000ULL * 1000 * 1000) +
|
|
|
|
time_start_mono.tv_nsec +
|
|
|
|
opt.txtime.delay * 1000;
|
|
|
|
|
|
|
|
cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
|
|
|
|
cmsg_len += CMSG_SPACE(sizeof(txtime));
|
|
|
|
if (cbuf_sz < cmsg_len)
|
|
|
|
error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
|
|
|
|
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_TXTIME;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(txtime));
|
|
|
|
memcpy(CMSG_DATA(cmsg), &txtime, sizeof(txtime));
|
|
|
|
}
|
2022-02-09 16:36:43 -08:00
|
|
|
|
|
|
|
if (cmsg_len)
|
|
|
|
msg->msg_controllen = cmsg_len;
|
|
|
|
else
|
|
|
|
msg->msg_control = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-02-09 16:36:44 -08:00
|
|
|
char buf[] = "blablablabla";
|
2022-02-09 16:36:43 -08:00
|
|
|
struct addrinfo hints, *ai;
|
2021-10-29 08:51:35 -07:00
|
|
|
struct iovec iov[1];
|
|
|
|
struct msghdr msg;
|
2022-02-09 16:36:43 -08:00
|
|
|
char cbuf[1024];
|
2021-10-29 08:51:35 -07:00
|
|
|
int err;
|
|
|
|
int fd;
|
|
|
|
|
2022-02-09 16:36:43 -08:00
|
|
|
cs_parse_args(argc, argv);
|
2021-10-29 08:51:35 -07:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2022-02-09 16:36:44 -08:00
|
|
|
hints.ai_family = opt.sock.family;
|
2021-10-29 08:51:35 -07:00
|
|
|
|
|
|
|
ai = NULL;
|
2022-02-09 16:36:43 -08:00
|
|
|
err = getaddrinfo(opt.host, opt.service, &hints, &ai);
|
2021-10-29 08:51:35 -07:00
|
|
|
if (err) {
|
2022-02-09 16:36:44 -08:00
|
|
|
fprintf(stderr, "Can't resolve address [%s]:%s\n",
|
|
|
|
opt.host, opt.service);
|
2022-02-09 16:36:43 -08:00
|
|
|
return ERN_SOCK_CREATE;
|
2021-10-29 08:51:35 -07:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:36:44 -08:00
|
|
|
if (ai->ai_family == AF_INET6 && opt.sock.proto == IPPROTO_ICMP)
|
|
|
|
opt.sock.proto = IPPROTO_ICMPV6;
|
|
|
|
|
|
|
|
fd = socket(ai->ai_family, opt.sock.type, opt.sock.proto);
|
2021-10-29 08:51:35 -07:00
|
|
|
if (fd < 0) {
|
|
|
|
fprintf(stderr, "Can't open socket: %s\n", strerror(errno));
|
|
|
|
freeaddrinfo(ai);
|
2022-02-09 16:36:43 -08:00
|
|
|
return ERN_RESOLVE;
|
2021-10-29 08:51:35 -07:00
|
|
|
}
|
|
|
|
|
2022-02-09 16:36:44 -08:00
|
|
|
if (opt.sock.proto == IPPROTO_ICMP) {
|
|
|
|
buf[0] = ICMP_ECHO;
|
|
|
|
buf[1] = 0;
|
|
|
|
} else if (opt.sock.proto == IPPROTO_ICMPV6) {
|
|
|
|
buf[0] = ICMPV6_ECHO_REQUEST;
|
|
|
|
buf[1] = 0;
|
|
|
|
} else if (opt.sock.type == SOCK_RAW) {
|
|
|
|
struct udphdr hdr = { 1, 2, htons(sizeof(buf)), 0 };
|
|
|
|
struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;;
|
|
|
|
|
|
|
|
memcpy(buf, &hdr, sizeof(hdr));
|
|
|
|
sin6->sin6_port = htons(opt.sock.proto);
|
|
|
|
}
|
|
|
|
|
2022-02-09 16:36:46 -08:00
|
|
|
if (opt.sockopt.mark &&
|
|
|
|
setsockopt(fd, SOL_SOCKET, SO_MARK,
|
|
|
|
&opt.sockopt.mark, sizeof(opt.sockopt.mark)))
|
|
|
|
error(ERN_SOCKOPT, errno, "setsockopt SO_MARK");
|
|
|
|
|
2022-02-09 16:36:47 -08:00
|
|
|
if (clock_gettime(CLOCK_MONOTONIC, &time_start_mono))
|
|
|
|
error(ERN_GETTIME, errno, "gettime MONOTINIC");
|
|
|
|
|
2022-02-09 16:36:44 -08:00
|
|
|
iov[0].iov_base = buf;
|
|
|
|
iov[0].iov_len = sizeof(buf);
|
2021-10-29 08:51:35 -07:00
|
|
|
|
2022-02-09 16:36:43 -08:00
|
|
|
memset(&msg, 0, sizeof(msg));
|
2021-10-29 08:51:35 -07:00
|
|
|
msg.msg_name = ai->ai_addr;
|
|
|
|
msg.msg_namelen = ai->ai_addrlen;
|
|
|
|
msg.msg_iov = iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
|
2022-02-09 16:36:47 -08:00
|
|
|
cs_write_cmsg(fd, &msg, cbuf, sizeof(cbuf));
|
2021-10-29 08:51:35 -07:00
|
|
|
|
|
|
|
err = sendmsg(fd, &msg, 0);
|
2022-02-09 16:36:43 -08:00
|
|
|
if (err < 0) {
|
|
|
|
if (!opt.silent_send)
|
|
|
|
fprintf(stderr, "send failed: %s\n", strerror(errno));
|
|
|
|
err = ERN_SEND;
|
2022-02-09 16:36:44 -08:00
|
|
|
} else if (err != sizeof(buf)) {
|
2022-02-09 16:36:43 -08:00
|
|
|
fprintf(stderr, "short send\n");
|
|
|
|
err = ERN_SEND_SHORT;
|
|
|
|
} else {
|
|
|
|
err = ERN_SUCCESS;
|
|
|
|
}
|
2021-10-29 08:51:35 -07:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
freeaddrinfo(ai);
|
2022-02-09 16:36:43 -08:00
|
|
|
return err;
|
2021-10-29 08:51:35 -07:00
|
|
|
}
|