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>
22 #include <arpa/inet.h>
23 #include <linux/can.h>
27 #include <sys/ioctl.h>
30 unsigned int cegw_errno = 0;
41 char* cegw_errlist[] =
43 [CEGW_ERR_UNKNOWN] = "",
44 [CEGW_ERR_COLON ] = "expected ':' (<ip>:<port>)",
45 [CEGW_ERR_UNEXLEN] = "unexpected ip address length, please use dot notation"
47 [CEGW_ERR_ATON ] = "ip address mismatch",
48 [CEGW_ERR_PORT ] = "port number"
51 static const char help_msg[] = "usage:\n"
52 " %s <can_if> <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
53 " [list of additional udp recipients <addr:port>]\n"
55 " %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
56 " Executing this command will set the gateway so that it will\n"
57 " listen for udp messages on 192.168.0.1:10501 and send them\n"
58 " to can0. Simultaneously, it will send all messages from can0\n"
59 " to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
60 " therefore cloned. Notice that there can be any number\n"
61 " of udp recipients.\n";
63 static void perr(char* s)
66 if (cegw_errno == 0) {
67 fprintf(stderr, "error: %s\n", s);
70 fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
75 fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
79 * read_addrport - parses @in for eth address.
80 * Valid input is e.g. 127.0.0.1:10502. If parsing fails
81 * the cause is stored in cegw_errno.
83 * @param[in] in string to search in
84 * @param[out] addr ip address
85 * @param[out] port transport layer port
86 * @return 0 on success, -1 otherwise
88 int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
91 const int addrstr_len = 16;
92 char addrstr[addrstr_len];
95 if ((delim = strchr(in, ':')) == NULL) {
96 cegw_errno = CEGW_ERR_COLON;
101 addrlen = delim - in;
102 if (addrlen > addrstr_len) {
103 cegw_errno = CEGW_ERR_UNEXLEN;
107 memcpy(addrstr, in, addrlen);
108 addrstr[addrlen] = '\0';
109 if (inet_aton(addrstr, addr) == 0) {
110 cegw_errno = CEGW_ERR_ATON;
115 /* ToDo: handle overflow */
116 if (sscanf(delim, ":%hu", port) != 1) {
117 cegw_errno = CEGW_ERR_PORT;
124 int main(int argc, char* argv[])
131 int udp_sock, can_sock;
132 struct sockaddr_in udp_addr;
133 struct sockaddr_can can_addr;
134 struct sockaddr_in* dst = NULL;
135 struct cegw_ioctl* gwctl = NULL;
139 printf(help_msg, argv[0], argv[0]);
144 perr("not enough arguments");
145 printf(help_msg, argv[0], argv[0]);
146 /* ToDo: print usage */
151 gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
153 for (i=1; i<argc; i++) {
155 case 1: /* can ifindex */
156 can_addr.can_family = AF_CAN;
157 tmpi = if_nametoindex(argv[i]);
159 perr("given can interface not found");
163 can_addr.can_ifindex = tmpi;
165 case 2: /* listen addr */
166 udp_addr.sin_family = AF_INET;
167 if (read_addrport(argv[i], &udp_addr.sin_addr, &port) != 0) {
168 perr("listening address mismatch");
172 udp_addr.sin_port = htons(port);
174 default: /* udp destination */
175 dst = &gwctl->udp_dst[i-3];
176 dst->sin_family = AF_INET;
177 if (read_addrport(argv[i], &dst->sin_addr, &port) != 0) {
178 perr("udp destination mismatch");
182 dst->sin_port = htons(port);
187 /* prepare udp socket */
188 udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
189 if (udp_sock == -1) {
190 fprintf(stderr, "error: udp socket(..) failed\n");
195 if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
196 fprintf(stderr, "error: udp bind(..) failed\n");
201 /* prepare can socket */
202 can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
203 if (can_sock == -1) {
204 fprintf(stderr, "error: can socket(..) failed\n");
209 if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
210 fprintf(stderr, "error: can bind(..) failed\n");
215 /* send it to kernel gateway */
216 fd = open("/dev/canethgw", O_RDONLY);
218 fprintf(stderr, "error: could not open device file\n");
223 gwctl->can_sock = can_sock;
224 gwctl->udp_sock = udp_sock;
225 gwctl->udp_dstcnt = dstcnt;
226 gwctl->udp_addrlen = sizeof(struct sockaddr_in);
228 if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
233 printf("gateway successfully set and running\n");
236 /* sleep until someone kills me */