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;
43 char *cegw_errlist[] = {
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"
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"
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";
64 const struct addrinfo hints = {
66 .ai_socktype = SOCK_DGRAM
69 static void perr(char *s)
72 if (cegw_errno == 0) {
73 fprintf(stderr, "error: %s\n", s);
76 fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
81 fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
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.
90 * @param[in] arg hostname:port string
91 * @param[out] addr filled sockaddr_in structure
92 * @return 0 on success, -1 otherwise
94 int readsockaddr(char *arg, struct sockaddr_in *addr)
100 delim = strchr(arg, ':');
103 cegw_errno = CEGW_ERR_COLON;
110 ret = getaddrinfo(arg, delim, &hints, &res);
112 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
113 cegw_errno = CEGW_ERR_GETADDRI;
117 memcpy(addr, res->ai_addr, sizeof(*addr));
124 * readfilter - reads can filter definition from nptr
126 int readfilter(char *nptr, struct can_filter **filter, int *out_numfilter, can_err_mask_t *err_mask)
130 struct can_filter *rfilter;
136 ptr++; /* hop behind the ',' */
137 ptr = strchr(ptr, ','); /* exit condition */
140 rfilter = malloc(numfilter * sizeof(*rfilter));
142 cegw_errno = CEGW_ERR_FLTALCK;
151 ptr = nptr+1; /* hop behind the ',' */
152 nptr = strchr(ptr, ','); /* update exit condition */
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;
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;
165 } else if (sscanf(ptr, "#%x", err_mask) != 1) {
166 cegw_errno = CEGW_ERR_FLTPARSE;
173 *out_numfilter = numfilter;
177 int main(int argc, char *argv[])
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;
194 printf(help_msg, argv[0], argv[0]);
199 perr("not enough arguments");
200 printf(help_msg, argv[0], argv[0]);
201 /* ToDo: print usage */
206 gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
208 for (i=1; i<argc; i++) {
210 case 1: /* can ifindex */
211 nptr = strchr(argv[i], ',');
216 can_addr.can_family = AF_CAN;
217 tmpi = if_nametoindex(argv[i]);
219 perr("given can interface not found");
223 can_addr.can_ifindex = tmpi;
225 case 2: /* listen addr */
226 if (readsockaddr(argv[i], &udp_addr) != 0) {
227 perr("reading listening address failed");
232 default: /* udp destination */
233 dst = &gwctl->udp_dst[i-3];
234 if (readsockaddr(argv[i], dst) != 0) {
235 perr("reading udp destination failed");
243 /* prepare udp socket */
244 udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
245 if (udp_sock == -1) {
246 perror("udp socket()");
251 if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
257 /* prepare can socket */
258 can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
259 if (can_sock == -1) {
260 perror("can socket()");
265 if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
272 if (nptr && (readfilter(nptr, &filter, &numfilter, &err_mask) != 0)) {
279 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
280 &err_mask, sizeof(err_mask));
283 setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_FILTER,
284 filter, numfilter * sizeof(struct can_filter));
287 /* send it to kernel gateway */
288 fd = open("/dev/canethgw", O_RDONLY);
290 perror("/dev/canethgw");
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);
300 if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
305 printf("gateway successfully set and running\n");
308 /* sleep until someone kills me */