4 #include <sys/socket.h>
11 #include <sys/ioctl.h>
14 unsigned int cegw_errno = 0;
25 char* cegw_errlist[] =
27 [CEGW_ERR_UNKNOWN] = "",
28 [CEGW_ERR_COLON ] = "expected ':' (<ip>:<port>)",
29 [CEGW_ERR_UNEXLEN] = "unexpected ip address length, please use dot notation"
31 [CEGW_ERR_ATON ] = "ip address mismatch",
32 [CEGW_ERR_PORT ] = "port number"
35 static const char help_msg[] = "usage:\n"
36 " %s <can_if> <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 any number\n"
45 " of udp recipients.\n";
47 static void perr(char* s)
50 if (cegw_errno == 0) {
51 fprintf(stderr, "error: %s\n", s);
54 fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
59 fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
63 * read_addrport - parses @in for eth address.
64 * Valid input is e.g. 127.0.0.1:10502. If parsing fails
65 * the cause is stored in cegw_errno.
67 * @param[in] in string to search in
68 * @param[out] addr ip address
69 * @param[out] port transport layer port
70 * @return 0 on success, -1 otherwise
72 int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
75 const int addrstr_len = 16;
76 char addrstr[addrstr_len];
79 if ((delim = strchr(in, ':')) == NULL) {
80 cegw_errno = CEGW_ERR_COLON;
86 if (addrlen > addrstr_len) {
87 cegw_errno = CEGW_ERR_UNEXLEN;
91 memcpy(addrstr, in, addrlen);
92 addrstr[addrlen] = '\0';
93 if (inet_aton(addrstr, addr) == 0) {
94 cegw_errno = CEGW_ERR_ATON;
99 /* ToDo: handle overflow */
100 if (sscanf(delim, ":%hu", port) != 1) {
101 cegw_errno = CEGW_ERR_PORT;
108 int main(int argc, char* argv[])
115 int udp_sock, can_sock;
116 struct sockaddr_in udp_addr;
117 struct sockaddr_can can_addr;
118 struct sockaddr_in* dst = NULL;
119 struct cegw_ioctl* gwctl = NULL;
123 printf(help_msg, argv[0], argv[0]);
128 perr("not enought arguments");
129 printf(help_msg, argv[0], argv[0]);
130 /* ToDo: print usage */
135 gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
137 for (i=1; i<argc; i++) {
139 case 1: /* can ifindex */
140 can_addr.can_family = AF_CAN;
141 tmpi = if_nametoindex(argv[i]);
143 perr("given can interface not found");
147 can_addr.can_ifindex = tmpi;
149 case 2: /* listen addr */
150 udp_addr.sin_family = AF_INET;
151 if (read_addrport(argv[i], &udp_addr.sin_addr, &port) != 0) {
152 perr("listening address mismatch");
156 udp_addr.sin_port = htons(port);
158 default: /* udp destination */
159 dst = &gwctl->udp_dst[i-3];
160 dst->sin_family = AF_INET;
161 if (read_addrport(argv[i], &dst->sin_addr, &port) != 0) {
162 perr("udp destination mismatch");
166 dst->sin_port = htons(port);
171 /* prepare udp socket */
172 udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
173 if (udp_sock == -1) {
174 fprintf(stderr, "error: udp socket(..) failed\n");
179 if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
180 fprintf(stderr, "error: udp bind(..) failed\n");
185 /* prepare can socket */
186 can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
187 if (can_sock == -1) {
188 fprintf(stderr, "error: can socket(..) failed\n");
193 if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
194 fprintf(stderr, "error: can bind(..) failed\n");
199 /* send it to kernel gateway */
200 fd = open("/dev/cegw", O_RDONLY);
202 fprintf(stderr, "error: could not open device file\n");
207 gwctl->can_sock = can_sock;
208 gwctl->udp_sock = udp_sock;
209 gwctl->udp_dstcnt = dstcnt;
210 gwctl->udp_addrlen = sizeof(struct sockaddr_in);
212 if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
217 printf("gateway successfully set and running\n");
220 /* sleep until someone kills me */