]> rtime.felk.cvut.cz Git - can-eth-gw.git/blob - utils/cegw/cegw.c
feb083af9ee61cb38d9edc95878d7cf40e9698b5
[can-eth-gw.git] / utils / cegw / cegw.c
1 /*
2  * Copyright: (c) 2012 Czech Technical University in Prague
3  *
4  * Authors:
5  *      Radek Matějka <radek.matejka@gmail.com>
6  *      Michal Sojka  <sojkam1@fel.cvut.cz>
7  *
8  * Funded by: Volkswagen Group Research
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version
13  * 2 of the License, or (at your option) any later version.
14  */
15
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/socket.h>
21 #include <netdb.h>
22 #include <net/if.h>
23 #include <arpa/inet.h>
24 #include <linux/can.h>
25 #include <limits.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <linux/can/raw.h>
31 #include <linux/can/canethgw.h>
32
33 unsigned int cegw_errno = 0;
34
35 enum {
36         CEGW_ERR_UNKNOWN,
37         CEGW_ERR_COLON,
38         CEGW_ERR_GETADDRI,
39         CEGW_ERR_FLTALCK,
40         CEGW_ERR_FLTPARSE
41 };
42
43 char *cegw_errlist[] = {
44
45         [CEGW_ERR_UNKNOWN ] = "",
46         [CEGW_ERR_COLON   ] = "expected ':' (<hostname>:<port>)",
47         [CEGW_ERR_GETADDRI] = "getaddrinfo failed",
48         [CEGW_ERR_FLTALCK ] = "filter alloc failed",
49         [CEGW_ERR_FLTPARSE] = "filter parsing failed"
50 };
51
52 static const char help_msg[] = "usage:\n"
53                                "        %s <can_if>[,filter]* <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
54                                "                [list of additional udp recipients <addr>:<port>]\n"
55                                "example:\n"
56                                "        %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
57                                "        Executing this command will set the gateway so that it will\n"
58                                "        listen for udp messages on 192.168.0.1:10501 and send them\n"
59                                "        to can0. Simultaneously, it will send all messages from can0\n"
60                                "        to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
61                                "        therefore cloned. Notice that there can be more udp recipients.\n"
62                                "        The can filter is specified in the same way as in candump utility.\n";
63
64 const struct addrinfo hints = {
65         .ai_family = AF_INET,
66         .ai_socktype = SOCK_DGRAM
67 };
68
69 static void perr(char *s)
70 {
71         if (s) {
72                 if (cegw_errno == 0) {
73                         fprintf(stderr, "error: %s\n", s);
74
75                 } else {
76                         fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
77                 }
78                 return;
79         }
80
81         fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
82 }
83
84 /**
85  * readsockaddr - parses @in for eth address.
86  * Valid input is e.g. 127.0.0.1:10502. If parsing fails
87  * the cause is stored in cegw_errno. Please note that
88  * the function modifies content of arg.
89  *
90  * @param[in]  arg   hostname:port string
91  * @param[out] addr  filled sockaddr_in structure
92  * @return 0 on success, -1 otherwise
93  */
94 int readsockaddr(char *arg, struct sockaddr_in *addr)
95 {
96         int ret;
97         char *delim;
98         struct addrinfo *res;
99
100         delim = strchr(arg, ':');
101
102         if (delim == NULL) {
103                 cegw_errno = CEGW_ERR_COLON;
104                 return -1;
105         }
106
107         *delim = '\0';
108         delim++;
109
110         ret = getaddrinfo(arg, delim, &hints, &res);
111         if (ret != 0) {
112                 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
113                 cegw_errno = CEGW_ERR_GETADDRI;
114                 return -1;
115         }
116
117         memcpy(addr, res->ai_addr, sizeof(*addr));
118
119         freeaddrinfo(res);
120         return 0;
121 }
122
123 /**
124  * readfilter - reads can filter definition from nptr
125  */
126 int readfilter(char *nptr, struct can_filter **filter, int *out_numfilter, can_err_mask_t *err_mask)
127 {
128         char *ptr;
129         int numfilter;
130         struct can_filter *rfilter;
131
132         numfilter = 0;
133         ptr = nptr;
134         while (ptr) {
135                 numfilter++;
136                 ptr++; /* hop behind the ',' */
137                 ptr = strchr(ptr, ','); /* exit condition */
138         }
139
140         rfilter = malloc(numfilter * sizeof(*rfilter));
141         if (!rfilter) {
142                 cegw_errno = CEGW_ERR_FLTALCK;
143                 return -1;
144         }
145
146         numfilter = 0;
147         *err_mask = 0;
148
149         while (nptr) {
150
151                 ptr = nptr+1; /* hop behind the ',' */
152                 nptr = strchr(ptr, ','); /* update exit condition */
153
154                 if (sscanf(ptr, "%x:%x",
155                                         &rfilter[numfilter].can_id,
156                                         &rfilter[numfilter].can_mask) == 2) {
157                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
158                         numfilter++;
159                 } else if (sscanf(ptr, "%x~%x",
160                                         &rfilter[numfilter].can_id,
161                                         &rfilter[numfilter].can_mask) == 2) {
162                         rfilter[numfilter].can_id |= CAN_INV_FILTER;
163                         rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
164                         numfilter++;
165                 } else if (sscanf(ptr, "#%x", err_mask) != 1) {
166                         cegw_errno = CEGW_ERR_FLTPARSE;
167                         free(rfilter);
168                         return -1;
169                 }
170         }
171
172         *filter = rfilter;
173         *out_numfilter = numfilter;
174         return 0;
175 }
176
177 int main(int argc, char *argv[])
178 {
179         int i;
180         int fd;
181         int tmpi;
182         int dstcnt;
183         char *nptr;
184         int numfilter = 0;
185         int udp_sock, can_sock;
186         can_err_mask_t err_mask = 0;
187         struct sockaddr_in udp_addr;
188         struct sockaddr_can can_addr;
189         struct sockaddr_in *dst = NULL;
190         struct cegw_ioctl *gwctl = NULL;
191         struct can_filter *filter = NULL;
192
193         if (argc == 1) {
194                 printf(help_msg, argv[0], argv[0]);
195                 return 0;
196         }
197
198         if (argc < 4) {
199                 perr("not enough arguments");
200                 printf(help_msg, argv[0], argv[0]);
201                 /* ToDo: print usage */
202                 return 1;
203         }
204
205         dstcnt = argc-3;
206         gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
207
208         for (i=1; i<argc; i++) {
209                 switch (i) {
210                         case 1: /* can ifindex */
211                                 nptr = strchr(argv[i], ',');
212                                 if (nptr) {
213                                         *nptr = '\0';
214                                 }
215
216                                 can_addr.can_family = AF_CAN;
217                                 tmpi = if_nametoindex(argv[i]);
218                                 if (tmpi == 0) {
219                                         perr("given can interface not found");
220                                         free(gwctl);
221                                         return 1;
222                                 }
223                                 can_addr.can_ifindex = tmpi;
224                                 break;
225                         case 2: /* listen addr */
226                                 if (readsockaddr(argv[i], &udp_addr) != 0) {
227                                         perr("reading listening address failed");
228                                         free(gwctl);
229                                         return 1;
230                                 }
231                                 break;
232                         default: /* udp destination */
233                                 dst = &gwctl->udp_dst[i-3];
234                                 if (readsockaddr(argv[i], dst) != 0) {
235                                         perr("reading udp destination failed");
236                                         free(gwctl);
237                                         return 1;
238                                 }
239                                 break;
240                 }
241         }
242
243         /* prepare udp socket */
244         udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
245         if (udp_sock == -1) {
246                 perror("udp socket()");
247                 free(gwctl);
248                 return 1;
249         }
250
251         if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
252                 perror("bind(udp)");
253                 free(gwctl);
254                 return 1;
255         }
256
257         /* prepare can socket */
258         can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
259         if (can_sock == -1) {
260                 perror("can socket()");
261                 free(gwctl);
262                 return 1;
263         }
264
265         if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
266                 perror("bind(can)");
267                 free(gwctl);
268                 return 1;
269         }
270
271         /* can filter */
272         if (nptr && (readfilter(nptr, &filter, &numfilter, &err_mask) != 0)) {
273                 perr("can filter");
274                 free(gwctl);
275                 return 1;
276         }
277
278         if (err_mask)
279                 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
280                                 &err_mask, sizeof(err_mask));
281
282         if (numfilter)
283                 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_FILTER,
284                                 filter, numfilter * sizeof(struct can_filter));
285         free(filter);
286
287         /* send it to kernel gateway */
288         fd = open("/dev/canethgw", O_RDONLY);
289         if (fd == -1) {
290                 perror("/dev/canethgw");
291                 free(gwctl);
292                 return 1;
293         }
294
295         gwctl->can_sock = can_sock;
296         gwctl->udp_sock = udp_sock;
297         gwctl->udp_dstcnt = dstcnt;
298         gwctl->udp_addrlen = sizeof(struct sockaddr_in);
299
300         if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
301                 perror("ioctl");
302                 free(gwctl);
303                 return 1;
304         }
305         printf("gateway successfully set and running\n");
306         free(gwctl);
307
308         /* sleep until someone kills me */
309         pause();
310
311         return 0;
312 }
313