2 * Copyright: (c) 2012 Czech Technical University in Prague
5 * Radek Matějka <radek.matejka@gmail.com>
6 * Michal Sojka <sojkam1@fel.cvut.cz>
8 * Funded by: Volkswagen Group Research
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.
20 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <linux/can.h>
28 #include <sys/ioctl.h>
29 #include <sys/types.h>
30 #include <linux/can/raw.h>
31 #include <linux/can/canethgw.h>
33 unsigned int cegw_errno = 0;
35 static const char help_msg[] = "usage:\n"
36 " %s <can_if>[,filter]* <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
37 " [list of additional udp recipients <addr>:<port>]\n"
39 " %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
40 " Executing this command will set the gateway so that it will\n"
41 " listen for udp messages on 192.168.0.1:10501 and send them\n"
42 " to can0. Simultaneously, it will send all messages from can0\n"
43 " to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
44 " therefore cloned. Notice that there can be more udp recipients.\n"
45 " The can filter is specified in the same way as in candump utility.\n";
47 struct addrinfo hints = {
48 .ai_socktype = SOCK_DGRAM
52 * readsockaddr - parses @in for eth address.
53 * Valid input is e.g. 127.0.0.1:10502. If parsing fails
54 * the cause is stored in cegw_errno. Please note that
55 * the function modifies content of arg.
57 * @param[in] arg hostname:port string
58 * @param[out] addr filled sockaddr_in structure
59 * @return 0 on success, -1 otherwise
61 int readsockaddr(char *arg, struct sockaddr *addr, int ai_family)
67 delim = strchr(arg, ':');
70 fprintf(stderr, "expected ':' (<hostname>:<port>)");
77 hints.ai_family = ai_family;
79 ret = getaddrinfo(arg, delim, &hints, &res);
81 fprintf(stderr, "getaddrinfo failed: %s\n", gai_strerror(ret));
85 memcpy(addr, res->ai_addr, res->ai_addrlen);
92 * readfilter - reads can filter definition from nptr
94 int readfilter(char *nptr, struct can_filter **filter, int *out_numfilter, can_err_mask_t *err_mask)
98 struct can_filter *rfilter;
104 ptr++; /* hop behind the ',' */
105 ptr = strchr(ptr, ','); /* exit condition */
108 rfilter = malloc(numfilter * sizeof(*rfilter));
110 fprintf(stderr, "filter malloc failed");
119 ptr = nptr+1; /* hop behind the ',' */
120 nptr = strchr(ptr, ','); /* update exit condition */
122 if (sscanf(ptr, "%x:%x",
123 &rfilter[numfilter].can_id,
124 &rfilter[numfilter].can_mask) == 2) {
125 rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
127 } else if (sscanf(ptr, "%x~%x",
128 &rfilter[numfilter].can_id,
129 &rfilter[numfilter].can_mask) == 2) {
130 rfilter[numfilter].can_id |= CAN_INV_FILTER;
131 rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
133 } else if (sscanf(ptr, "#%x", err_mask) != 1) {
134 fprintf(stderr, "filter parsing failed");
140 *out_numfilter = numfilter;
144 int main(int argc, char *argv[])
152 int udp_sock, can_sock;
154 can_err_mask_t err_mask = 0;
155 struct sockaddr_can can_addr;
156 struct sockaddr *dst = NULL;
157 struct cegw_ioctl *gwctl = NULL;
158 struct can_filter *filter = NULL;
160 /* udp_addr can store both - in and in6 addresses */
161 struct sockaddr_in6 udp6_addr;
162 struct sockaddr *udp_addr = (struct sockaddr *) &udp6_addr;
165 if (argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
166 printf(help_msg, argv[0], argv[0]);
171 fprintf(stderr, "not enough arguments\n");
172 printf(help_msg, argv[0], argv[0]);
178 for (i=1; i<argc; i++) {
180 case 1: /* can ifindex */
181 nptr = strchr(argv[i], ',');
186 can_addr.can_family = AF_CAN;
187 tmpi = if_nametoindex(argv[i]);
189 fprintf(stderr, "given can interface not found\n");
193 can_addr.can_ifindex = tmpi;
195 case 2: /* listen addr */
196 readsockaddr(argv[i], udp_addr, AF_UNSPEC);
197 switch (udp_addr->sa_family) {
199 addrlen = sizeof(struct sockaddr_in);
202 addrlen = sizeof(struct sockaddr_in6);
205 fprintf(stderr, "unexpected sockaddr family");
208 gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + dstcnt*addrlen);
210 default: /* udp destination */
211 dst = (struct sockaddr *)(gwctl->udp_dst + (i-3)*addrlen);
212 readsockaddr(argv[i], dst, udp_addr->sa_family);
218 /* prepare udp socket */
219 udp_sock = socket(udp_addr->sa_family, SOCK_DGRAM, IPPROTO_UDP);
220 if (udp_sock == -1) {
221 perror("udp socket(..)");
225 if (bind(udp_sock, udp_addr, addrlen) != 0) {
230 /* prepare can socket */
231 can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
232 if (can_sock == -1) {
233 perror("can socket(..)");
237 if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
244 readfilter(nptr, &filter, &numfilter, &err_mask);
247 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
248 &err_mask, sizeof(err_mask));
251 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_FILTER,
252 filter, numfilter * sizeof(struct can_filter));
255 /* send it to kernel gateway */
256 fd = open("/dev/canethgw", O_RDONLY);
258 perror("/dev/canethgw");
262 gwctl->can_sock = can_sock;
263 gwctl->udp_sock = udp_sock;
264 gwctl->udp_dstcnt = dstcnt;
265 gwctl->udp_addrlen = addrlen;
267 if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
271 printf("gateway successfully set and running\n");
274 /* sleep until someone kills me */