1 // SPDX-License-Identifier: GPL-2.0
8 #include <netinet/in.h>
11 static void set_addr(struct sockaddr_storage *ss, char *ip, char *port, int *len)
13 if (ss->ss_family == AF_INET) {
14 struct sockaddr_in *a = (struct sockaddr_in *)ss;
16 a->sin_addr.s_addr = inet_addr(ip);
17 a->sin_port = htons(atoi(port));
20 struct sockaddr_in6 *a = (struct sockaddr_in6 *)ss;
22 a->sin6_family = AF_INET6;
23 inet_pton(AF_INET6, ip, &a->sin6_addr);
24 a->sin6_port = htons(atoi(port));
29 static int do_client(int argc, char *argv[])
31 struct sockaddr_storage ss;
36 printf("%s client -4|6 IP PORT [IP PORT]\n", argv[0]);
40 bzero((void *)&ss, sizeof(ss));
41 ss.ss_family = !strcmp(argv[2], "-4") ? AF_INET : AF_INET6;
42 csk = socket(ss.ss_family, SOCK_STREAM, IPPROTO_SCTP);
44 printf("failed to create socket\n");
49 set_addr(&ss, argv[5], argv[6], &len);
50 ret = bind(csk, (struct sockaddr *)&ss, len);
52 printf("failed to bind to address\n");
57 set_addr(&ss, argv[3], argv[4], &len);
58 ret = connect(csk, (struct sockaddr *)&ss, len);
60 printf("failed to connect to peer\n");
64 ret = send(csk, buf, strlen(buf) + 1, 0);
66 printf("failed to send msg %d\n", ret);
74 int main(int argc, char *argv[])
76 struct sockaddr_storage ss;
77 int lsk, csk, ret, len;
80 if (argc < 2 || (strcmp(argv[1], "server") && strcmp(argv[1], "client"))) {
81 printf("%s server|client ...\n", argv[0]);
85 if (!strcmp(argv[1], "client"))
86 return do_client(argc, argv);
89 printf("%s server -4|6 IP PORT [IFACE]\n", argv[0]);
93 ss.ss_family = !strcmp(argv[2], "-4") ? AF_INET : AF_INET6;
94 lsk = socket(ss.ss_family, SOCK_STREAM, IPPROTO_SCTP);
96 printf("failed to create lsk\n");
101 ret = setsockopt(lsk, SOL_SOCKET, SO_BINDTODEVICE,
102 argv[5], strlen(argv[5]) + 1);
104 printf("failed to bind to device\n");
109 set_addr(&ss, argv[3], argv[4], &len);
110 ret = bind(lsk, (struct sockaddr *)&ss, len);
112 printf("failed to bind to address\n");
116 ret = listen(lsk, 5);
118 printf("failed to listen on port\n");
122 csk = accept(lsk, (struct sockaddr *)NULL, (socklen_t *)NULL);
124 printf("failed to accept new client\n");
128 ret = recv(csk, buf, sizeof(buf), 0);
130 printf("failed to recv msg %d\n", ret);