1*a61bd7b9SXin Long // SPDX-License-Identifier: GPL-2.0
2*a61bd7b9SXin Long
3*a61bd7b9SXin Long #include <stdio.h>
4*a61bd7b9SXin Long #include <stdlib.h>
5*a61bd7b9SXin Long #include <string.h>
6*a61bd7b9SXin Long #include <unistd.h>
7*a61bd7b9SXin Long #include <sys/types.h>
8*a61bd7b9SXin Long #include <netinet/in.h>
9*a61bd7b9SXin Long #include <arpa/inet.h>
10*a61bd7b9SXin Long
set_addr(struct sockaddr_storage * ss,char * ip,char * port,int * len)11*a61bd7b9SXin Long static void set_addr(struct sockaddr_storage *ss, char *ip, char *port, int *len)
12*a61bd7b9SXin Long {
13*a61bd7b9SXin Long if (ss->ss_family == AF_INET) {
14*a61bd7b9SXin Long struct sockaddr_in *a = (struct sockaddr_in *)ss;
15*a61bd7b9SXin Long
16*a61bd7b9SXin Long a->sin_addr.s_addr = inet_addr(ip);
17*a61bd7b9SXin Long a->sin_port = htons(atoi(port));
18*a61bd7b9SXin Long *len = sizeof(*a);
19*a61bd7b9SXin Long } else {
20*a61bd7b9SXin Long struct sockaddr_in6 *a = (struct sockaddr_in6 *)ss;
21*a61bd7b9SXin Long
22*a61bd7b9SXin Long a->sin6_family = AF_INET6;
23*a61bd7b9SXin Long inet_pton(AF_INET6, ip, &a->sin6_addr);
24*a61bd7b9SXin Long a->sin6_port = htons(atoi(port));
25*a61bd7b9SXin Long *len = sizeof(*a);
26*a61bd7b9SXin Long }
27*a61bd7b9SXin Long }
28*a61bd7b9SXin Long
do_client(int argc,char * argv[])29*a61bd7b9SXin Long static int do_client(int argc, char *argv[])
30*a61bd7b9SXin Long {
31*a61bd7b9SXin Long struct sockaddr_storage ss;
32*a61bd7b9SXin Long char buf[] = "hello";
33*a61bd7b9SXin Long int csk, ret, len;
34*a61bd7b9SXin Long
35*a61bd7b9SXin Long if (argc < 5) {
36*a61bd7b9SXin Long printf("%s client -4|6 IP PORT [IP PORT]\n", argv[0]);
37*a61bd7b9SXin Long return -1;
38*a61bd7b9SXin Long }
39*a61bd7b9SXin Long
40*a61bd7b9SXin Long bzero((void *)&ss, sizeof(ss));
41*a61bd7b9SXin Long ss.ss_family = !strcmp(argv[2], "-4") ? AF_INET : AF_INET6;
42*a61bd7b9SXin Long csk = socket(ss.ss_family, SOCK_STREAM, IPPROTO_SCTP);
43*a61bd7b9SXin Long if (csk < 0) {
44*a61bd7b9SXin Long printf("failed to create socket\n");
45*a61bd7b9SXin Long return -1;
46*a61bd7b9SXin Long }
47*a61bd7b9SXin Long
48*a61bd7b9SXin Long if (argc >= 7) {
49*a61bd7b9SXin Long set_addr(&ss, argv[5], argv[6], &len);
50*a61bd7b9SXin Long ret = bind(csk, (struct sockaddr *)&ss, len);
51*a61bd7b9SXin Long if (ret < 0) {
52*a61bd7b9SXin Long printf("failed to bind to address\n");
53*a61bd7b9SXin Long return -1;
54*a61bd7b9SXin Long }
55*a61bd7b9SXin Long }
56*a61bd7b9SXin Long
57*a61bd7b9SXin Long set_addr(&ss, argv[3], argv[4], &len);
58*a61bd7b9SXin Long ret = connect(csk, (struct sockaddr *)&ss, len);
59*a61bd7b9SXin Long if (ret < 0) {
60*a61bd7b9SXin Long printf("failed to connect to peer\n");
61*a61bd7b9SXin Long return -1;
62*a61bd7b9SXin Long }
63*a61bd7b9SXin Long
64*a61bd7b9SXin Long ret = send(csk, buf, strlen(buf) + 1, 0);
65*a61bd7b9SXin Long if (ret < 0) {
66*a61bd7b9SXin Long printf("failed to send msg %d\n", ret);
67*a61bd7b9SXin Long return -1;
68*a61bd7b9SXin Long }
69*a61bd7b9SXin Long close(csk);
70*a61bd7b9SXin Long
71*a61bd7b9SXin Long return 0;
72*a61bd7b9SXin Long }
73*a61bd7b9SXin Long
main(int argc,char * argv[])74*a61bd7b9SXin Long int main(int argc, char *argv[])
75*a61bd7b9SXin Long {
76*a61bd7b9SXin Long struct sockaddr_storage ss;
77*a61bd7b9SXin Long int lsk, csk, ret, len;
78*a61bd7b9SXin Long char buf[20];
79*a61bd7b9SXin Long
80*a61bd7b9SXin Long if (argc < 2 || (strcmp(argv[1], "server") && strcmp(argv[1], "client"))) {
81*a61bd7b9SXin Long printf("%s server|client ...\n", argv[0]);
82*a61bd7b9SXin Long return -1;
83*a61bd7b9SXin Long }
84*a61bd7b9SXin Long
85*a61bd7b9SXin Long if (!strcmp(argv[1], "client"))
86*a61bd7b9SXin Long return do_client(argc, argv);
87*a61bd7b9SXin Long
88*a61bd7b9SXin Long if (argc < 5) {
89*a61bd7b9SXin Long printf("%s server -4|6 IP PORT [IFACE]\n", argv[0]);
90*a61bd7b9SXin Long return -1;
91*a61bd7b9SXin Long }
92*a61bd7b9SXin Long
93*a61bd7b9SXin Long ss.ss_family = !strcmp(argv[2], "-4") ? AF_INET : AF_INET6;
94*a61bd7b9SXin Long lsk = socket(ss.ss_family, SOCK_STREAM, IPPROTO_SCTP);
95*a61bd7b9SXin Long if (lsk < 0) {
96*a61bd7b9SXin Long printf("failed to create lsk\n");
97*a61bd7b9SXin Long return -1;
98*a61bd7b9SXin Long }
99*a61bd7b9SXin Long
100*a61bd7b9SXin Long if (argc >= 6) {
101*a61bd7b9SXin Long ret = setsockopt(lsk, SOL_SOCKET, SO_BINDTODEVICE,
102*a61bd7b9SXin Long argv[5], strlen(argv[5]) + 1);
103*a61bd7b9SXin Long if (ret < 0) {
104*a61bd7b9SXin Long printf("failed to bind to device\n");
105*a61bd7b9SXin Long return -1;
106*a61bd7b9SXin Long }
107*a61bd7b9SXin Long }
108*a61bd7b9SXin Long
109*a61bd7b9SXin Long set_addr(&ss, argv[3], argv[4], &len);
110*a61bd7b9SXin Long ret = bind(lsk, (struct sockaddr *)&ss, len);
111*a61bd7b9SXin Long if (ret < 0) {
112*a61bd7b9SXin Long printf("failed to bind to address\n");
113*a61bd7b9SXin Long return -1;
114*a61bd7b9SXin Long }
115*a61bd7b9SXin Long
116*a61bd7b9SXin Long ret = listen(lsk, 5);
117*a61bd7b9SXin Long if (ret < 0) {
118*a61bd7b9SXin Long printf("failed to listen on port\n");
119*a61bd7b9SXin Long return -1;
120*a61bd7b9SXin Long }
121*a61bd7b9SXin Long
122*a61bd7b9SXin Long csk = accept(lsk, (struct sockaddr *)NULL, (socklen_t *)NULL);
123*a61bd7b9SXin Long if (csk < 0) {
124*a61bd7b9SXin Long printf("failed to accept new client\n");
125*a61bd7b9SXin Long return -1;
126*a61bd7b9SXin Long }
127*a61bd7b9SXin Long
128*a61bd7b9SXin Long ret = recv(csk, buf, sizeof(buf), 0);
129*a61bd7b9SXin Long if (ret <= 0) {
130*a61bd7b9SXin Long printf("failed to recv msg %d\n", ret);
131*a61bd7b9SXin Long return -1;
132*a61bd7b9SXin Long }
133*a61bd7b9SXin Long close(csk);
134*a61bd7b9SXin Long close(lsk);
135*a61bd7b9SXin Long
136*a61bd7b9SXin Long return 0;
137*a61bd7b9SXin Long }
138