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 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
18 #include <sys/socket.h>
20 #include <arpa/inet.h>
21 #include <linux/can.h>
25 #include <sys/ioctl.h>
28 unsigned int cegw_errno = 0;
39 char* cegw_errlist[] =
41 [CEGW_ERR_UNKNOWN] = "",
42 [CEGW_ERR_COLON ] = "expected ':' (<ip>:<port>)",
43 [CEGW_ERR_UNEXLEN] = "unexpected ip address length, please use dot notation"
45 [CEGW_ERR_ATON ] = "ip address mismatch",
46 [CEGW_ERR_PORT ] = "port number"
49 static const char help_msg[] = "usage:\n"
50 " %s <can_if> <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
51 " [list of additional udp recipients <addr:port>]\n"
53 " %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
54 " Executing this command will set the gateway so that it will\n"
55 " listen for udp messages on 192.168.0.1:10501 and send them\n"
56 " to can0. Simultaneously, it will send all messages from can0\n"
57 " to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
58 " therefore cloned. Notice that there can be any number\n"
59 " of udp recipients.\n";
61 static void perr(char* s)
64 if (cegw_errno == 0) {
65 fprintf(stderr, "error: %s\n", s);
68 fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
73 fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
77 * read_addrport - parses @in for eth address.
78 * Valid input is e.g. 127.0.0.1:10502. If parsing fails
79 * the cause is stored in cegw_errno.
81 * @param[in] in string to search in
82 * @param[out] addr ip address
83 * @param[out] port transport layer port
84 * @return 0 on success, -1 otherwise
86 int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
89 const int addrstr_len = 16;
90 char addrstr[addrstr_len];
93 if ((delim = strchr(in, ':')) == NULL) {
94 cegw_errno = CEGW_ERR_COLON;
100 if (addrlen > addrstr_len) {
101 cegw_errno = CEGW_ERR_UNEXLEN;
105 memcpy(addrstr, in, addrlen);
106 addrstr[addrlen] = '\0';
107 if (inet_aton(addrstr, addr) == 0) {
108 cegw_errno = CEGW_ERR_ATON;
113 /* ToDo: handle overflow */
114 if (sscanf(delim, ":%hu", port) != 1) {
115 cegw_errno = CEGW_ERR_PORT;
122 int main(int argc, char* argv[])
129 int udp_sock, can_sock;
130 struct sockaddr_in udp_addr;
131 struct sockaddr_can can_addr;
132 struct sockaddr_in* dst = NULL;
133 struct cegw_ioctl* gwctl = NULL;
137 printf(help_msg, argv[0], argv[0]);
142 perr("not enought arguments");
143 printf(help_msg, argv[0], argv[0]);
144 /* ToDo: print usage */
149 gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
151 for (i=1; i<argc; i++) {
153 case 1: /* can ifindex */
154 can_addr.can_family = AF_CAN;
155 tmpi = if_nametoindex(argv[i]);
157 perr("given can interface not found");
161 can_addr.can_ifindex = tmpi;
163 case 2: /* listen addr */
164 udp_addr.sin_family = AF_INET;
165 if (read_addrport(argv[i], &udp_addr.sin_addr, &port) != 0) {
166 perr("listening address mismatch");
170 udp_addr.sin_port = htons(port);
172 default: /* udp destination */
173 dst = &gwctl->udp_dst[i-3];
174 dst->sin_family = AF_INET;
175 if (read_addrport(argv[i], &dst->sin_addr, &port) != 0) {
176 perr("udp destination mismatch");
180 dst->sin_port = htons(port);
185 /* prepare udp socket */
186 udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
187 if (udp_sock == -1) {
188 fprintf(stderr, "error: udp socket(..) failed\n");
193 if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
194 fprintf(stderr, "error: udp bind(..) failed\n");
199 /* prepare can socket */
200 can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
201 if (can_sock == -1) {
202 fprintf(stderr, "error: can socket(..) failed\n");
207 if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
208 fprintf(stderr, "error: can bind(..) failed\n");
213 /* send it to kernel gateway */
214 fd = open("/dev/cegw", O_RDONLY);
216 fprintf(stderr, "error: could not open device file\n");
221 gwctl->can_sock = can_sock;
222 gwctl->udp_sock = udp_sock;
223 gwctl->udp_dstcnt = dstcnt;
224 gwctl->udp_addrlen = sizeof(struct sockaddr_in);
226 if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
231 printf("gateway successfully set and running\n");
234 /* sleep until someone kills me */