hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * RTnet test server
 *
 * Copyright (C) 2015 Gilles Chanteperdrix <gch@xenomai.org>
 *
 * SPDX-License-Identifier: MIT
 */
 
#include <stdio.h>
#include <stdlib.h>
 
#include <unistd.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
#include <rtcfg_chrdev.h>
#include <sys/cobalt.h>
#include <smokey/smokey.h>
#include <xenomai/init.h>
#include "smokey_net.h"
#include "smokey_net_server.h"
 
static const char *intf = "rteth0";
 
int smokey_net_server_check_inner(const char *file, int line,
                     const char *msg, int status)
{
       if (status >= 0)
          return status;
 
       fprintf(stderr, "FAILED %s: returned error %d - %s\n",
          msg, -status, strerror(-status));
       exit(EXIT_FAILURE);
}
 
static int rtnet_rtcfg_setup_server(void)
{
   struct rtcfg_cmd cmd;
   int fd;
 
   memset(&cmd, 0, sizeof(cmd));
 
   cmd.args.server.period    = 1000;
   cmd.args.server.burstrate = 4;
   cmd.args.server.heartbeat = 1000;
   cmd.args.server.threshold = 2;
   cmd.args.server.flags     = 0;
 
   check_unix(snprintf(cmd.head.if_name, sizeof(cmd.head.if_name),
               intf, sizeof(cmd.head.if_name)));
 
   fd = check_unix(open("/dev/rtnet", O_RDWR));
 
   check_unix(ioctl(fd, RTCFG_IOC_SERVER, &cmd));
 
   return fd;
}
 
static void
rtnet_rtcfg_add_client(int fd, const char *hwaddr, const char *ipaddr)
{
   struct rtcfg_cmd cmd;
   struct ether_addr mac;
   struct in_addr ip;
 
   fprintf(stderr, "add client %s, mac %s\n", ipaddr, hwaddr);
 
   memset(&cmd, 0, sizeof(cmd));
 
   check_unix(snprintf(cmd.head.if_name, sizeof(cmd.head.if_name),
               intf, sizeof(cmd.head.if_name)));
 
   if (ether_aton_r(hwaddr, &mac) == 0) {
       fprintf(stderr, "%s is an invalid mac address\n", hwaddr);
       exit(EXIT_FAILURE);
   }
 
   if (check_unix(inet_aton(ipaddr, &ip)) == 0) {
       fprintf(stderr, "%s is an invalid ip address\n", ipaddr);
       exit(EXIT_FAILURE);
   }
 
   cmd.args.add.addr_type = RTCFG_ADDR_IP | FLAG_ASSIGN_ADDR_BY_MAC;
   cmd.args.add.ip_addr = ip.s_addr;
   cmd.args.add.timeout = 3000;
   memcpy(cmd.args.add.mac_addr, mac.ether_addr_octet,
       sizeof(mac.ether_addr_octet));
 
   check_unix(ioctl(fd, RTCFG_IOC_ADD, &cmd));
}
 
static void cleanup(int sig)
{
   struct rtcfg_cmd cmd;
   int fd;
 
   memset(&cmd, 0, sizeof(cmd));
 
   check_unix(snprintf(cmd.head.if_name, sizeof(cmd.head.if_name),
               intf, sizeof(cmd.head.if_name)));
 
   fd = check_unix(open("/dev/rtnet", O_RDWR));
 
   check_unix(ioctl(fd, RTCFG_IOC_DETACH, &cmd));
 
   close(fd);
 
   signal(sig, SIG_DFL);
   raise(sig);
}
 
void application_usage(void)
{
   fprintf(stderr, "%s options [ <interface> ]:\n\n"
       "Runs server for smokey network tests, on interface named "
       "<interface>\n"
       "(rtlo if unspecified)\n\n"
       "Available options:\n"
       "-f | --file <file>\t\tAnswers clients from file named <file>"
       "\n\t(uses standard input if unspecified)\n"
       "\tWhere every line contains a mac address and an IP address\n",
       get_program_name());
}
 
int main(int argc, char *argv[])
{
   int net_config, c, fd, err;
   FILE *input = stdin;
 
   check_native(cobalt_corectl(_CC_COBALT_GET_NET_CONFIG,
                   &net_config, sizeof(net_config)));
 
   for (;;) {
       int option_index = 0;
 
       static struct option long_options[] = {
           { "help", no_argument,       0, 'h', },
           { "file", required_argument, 0, 'f', },
           { 0,      0,                 0, 0,   },
       };
 
       c = getopt_long(argc, argv, "hf:", long_options, &option_index);
       if (c == -1)
           break;
 
       switch(c) {
       case 'h':
           application_usage();
           exit(EXIT_SUCCESS);
 
       case 'f':
           input = fopen(optarg, "r");
           if (input == NULL) {
               fprintf(stderr, "fopen(%s): %m\n", optarg);
               exit(EXIT_FAILURE);
           }
           break;
 
       case '?':
           application_usage();
           exit(EXIT_FAILURE);
       }
   }
 
   if (optind < argc) {
       if (argc - optind > 1) {
           application_usage();
           printf("\nOnly one interface argument expected\n");
           exit(EXIT_FAILURE);
       }
 
       intf = argv[optind];
       if (strcmp(intf, "rtlo") == 0) {
           application_usage();
           printf("\nRunning smokey_net_server on rtlo makes no sense\n");
           exit(EXIT_FAILURE);
       }
   }
 
   if ((net_config & _CC_COBALT_NET_CFG) == 0) {
       fprintf(stderr, "RTcfg not enabled, aborting\n");
       exit(EXIT_FAILURE);
   }
 
   fprintf(stderr, "Smokey network tests server, using interface %s\n",
       intf);
 
   signal(SIGINT, cleanup);
   signal(SIGTERM, cleanup);
   signal(SIGHUP, cleanup);
 
   fd = rtnet_rtcfg_setup_server();
 
   do {
       char *mac, *ip;
 
       err = fscanf(input, "%ms %ms\n", &mac, &ip);
       if (err == 2) {
           rtnet_rtcfg_add_client(fd, mac, ip);
           free(mac);
           free(ip);
       }
   } while (err != EOF);
 
   close(fd);
 
   smokey_net_server_loop(net_config);
   exit(EXIT_SUCCESS);
}