Path: blob/main/tests/sys/netinet/multicast-send.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2025 Gleb Smirnoff <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/socket.h>28#include <netinet/in.h>29#include <arpa/inet.h>30#include <net/if.h>31#include <assert.h>32#include <errno.h>33#include <limits.h>34#include <stdbool.h>35#include <stdlib.h>36#include <string.h>37#include <err.h>3839static in_port_t40atop(const char *c)41{42unsigned long ul;4344errno = 0;45if ((ul = strtol(c, NULL, 10)) < 1 || ul > IPPORT_MAX || errno != 0)46err(1, "can't parse %s", c);4748return ((in_port_t)ul);49}5051int52main(int argc, char *argv[])53{54struct sockaddr_in src = {55.sin_family = AF_INET,56.sin_len = sizeof(struct sockaddr_in),57}, dst = {58.sin_family = AF_INET,59.sin_len = sizeof(struct sockaddr_in),60};61struct ip_mreqn mreqn;62struct in_addr in;63int s;64bool index;6566if (argc < 7)67errx(1, "Usage: %s src-IPv4 src-port dst-IPv4 dst-port "68"interface payload", argv[0]);6970if (inet_pton(AF_INET, argv[1], &src.sin_addr) != 1)71err(1, "inet_pton(%s) failed", argv[1]);72src.sin_port = htons(atop(argv[2]));73if (inet_pton(AF_INET, argv[3], &dst.sin_addr) != 1)74err(1, "inet_pton(%s) failed", argv[3]);75dst.sin_port = htons(atop(argv[4]));76if (inet_pton(AF_INET, argv[5], &in) == 1)77index = false;78else if ((mreqn.imr_ifindex = if_nametoindex(argv[5])) > 0)79index = true;80else81err(1, "if_nametoindex(%s) failed", argv[5]);8283assert((s = socket(PF_INET, SOCK_DGRAM, 0)) > 0);84assert(bind(s, (struct sockaddr *)&src, sizeof(src)) == 0);85if (index)86assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &mreqn,87sizeof(mreqn)) == 0);88else89assert(setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &in,90sizeof(in)) == 0);91if (sendto(s, argv[6], strlen(argv[6]) + 1, 0, (struct sockaddr *)&dst,92sizeof(dst)) != (ssize_t)strlen(argv[6]) + 1)93err(1, "sendto failed");9495return (0);96}979899