7 #include <sys/socket.h>
9 #include <libnetlink.h>
10 #include <linux/netlink.h>
11 #include <linux/rtnetlink.h>
12 #include <arpa/inet.h>
13 #include <linux/can.h>
14 #include <linux/types.h>
16 #include <sys/types.h>
19 #include <sys/ioctl.h>
24 * [ ] print usage, on -h and plain execution
25 * [ ] start/stop listening
29 #define CEGW_CMD_ADD 1
30 #define CEGW_CMD_LIST 2
31 #define CEGW_CMD_FLUSH 4
32 #define CEGW_CMD_LISTEN 8
46 struct in_addr eth_addr;
47 unsigned short eth_port;
48 struct in_addr eth_listen_addr;
49 unsigned short eth_listen_port;
56 char buf[768]; /* enough? */
67 unsigned int cegw_errno = 0;
82 char* cegw_errlist[] =
84 [ CEGW_ERR_UNKNOWN ] = "",
85 [ CEGW_ERR_IF_UNSPEC ] = "source or destination not specified",
86 [ CEGW_ERR_IF_SAME ] = "source and destination have same interface type",
87 [ CEGW_ERR_IF_TYPE ] = "unknown interface type",
88 [ CEGW_ERR_IF_CAN ] = "invalid can interface",
89 [ CEGW_ERR_IF_ETH ] = "invalid eth interface",
90 [ CEGW_ERR_COLON ] = "expected ':' (<ip>:<port>)",
91 [ CEGW_ERR_ATON ] = "ip address mismatch",
92 [ CEGW_ERR_PORT ] = "port number"
95 static void perr( char* s )
101 fprintf( stderr, "error: %s\n", s );
105 fprintf( stderr, "error: %s, %s\n", s,
106 cegw_errlist[ cegw_errno ] );
111 fprintf( stderr, "error: %s\n", cegw_errlist[ cegw_errno ] );
115 * read_addrport - parses @in for eth address.
116 * Valid input is e.g. udp@127.0.0.1:10502 or can@vcan0.
118 * @param[in] in string to search in
119 * @param[out] addr ip address
120 * @param[out] port transport layer port
121 * @return 0 on success, -1 otherwise
123 int read_addrport( char* in, struct in_addr* addr, unsigned short* port )
129 if( (delim = strchr( in, ':' )) == NULL )
131 cegw_errno = CEGW_ERR_COLON;
136 addrlen = delim - in;
137 memcpy( addrstr, in, addrlen );
138 addrstr[addrlen] = '\0';
139 if( inet_aton( addrstr, addr ) == 0 )
141 cegw_errno = CEGW_ERR_ATON;
146 if( sscanf( delim, ":%hu", port ) != 1 ) /* ToDo: handle overflow */
148 cegw_errno = CEGW_ERR_PORT;
156 * read_iftype - reads @in for iftype
157 * Iftype type is e.g. "can@" or "udp@".
159 * @param[in] in string to search in
160 * @param[out] iftype iftype detected
161 * @return pointer to @in after iftype on success, NULL otherwise
163 char* read_iftype( char* in, int* iftype )
167 if( strncmp( "udp@", optarg, 4 ) == 0 )
169 *iftype = IF_ETH_UDP;
173 if( strncmp( "tcp@", optarg, 4 ) == 0 )
178 if( strncmp( "can@", optarg, 4 ) == 0 )
184 cegw_errno = CEGW_ERR_IF_TYPE;
188 /* ToDo: move to common */
191 * read_if - reads interface from @in
192 * Function analyzes @in for interface specification in format
193 * <if>@<ip>:<port>, where <if> is can or udp, <ip> is address in dotted
196 * @param[in] in string to search in
197 * @param[out] iftype interface type (IF_CAN or IF_ETH_UDP)
198 * @param[out] d ip and port is stored to @d
200 int read_if( char* in, int* iftype, struct cegw_data* d )
204 if( (optstr = read_iftype( in, iftype )) == NULL )
212 d->can_ifidx = if_nametoindex( optstr );
213 if( d->can_ifidx == 0 )
215 cegw_errno = CEGW_ERR_IF_CAN;
220 if( read_addrport( optstr, &d->eth_addr, &d->eth_port ) != 0 )
233 inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
237 req->nh.nlmsg_type = RTM_NEWROUTE;
238 if( (d->src_if == 0 || d->dst_if == 0) )
240 cegw_errno = CEGW_ERR_IF_UNSPEC;
244 if( d->src_if == d->dst_if )
246 cegw_errno = CEGW_ERR_IF_SAME;
250 gwtype = (d->src_if == IF_CAN) ? CEGW_RULE_CAN_ETH : CEGW_RULE_ETH_CAN;
251 addattr_l( &req->nh, sizeof(*req), CEGW_CAN_IFINDEX, &d->can_ifidx, sizeof(d->can_ifidx) );
252 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
253 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
254 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, gwtype );
259 inline static int cegw_listen( struct cegw_nlmsg* req, struct cegw_data* d )
261 req->nh.nlmsg_type = RTM_NEWROUTE;
262 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_LISTEN );
263 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
264 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
269 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
271 req->nh.nlmsg_type = RTM_GETROUTE;
272 req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
277 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
279 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
280 req->nh.nlmsg_type = RTM_DELROUTE;
284 void print_list_item( struct list_item* li )
286 char ifname[IF_NAMESIZE];
287 const char src_width = 21;
291 if( if_indextoname( li->can, ifname ) == NULL )
293 strncpy( ifname, "unknown", IF_NAMESIZE );
296 /* ToDo listening at */
299 case CEGW_RULE_CAN_ETH:
300 printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
301 inet_ntoa(li->ip), li->port );
303 case CEGW_RULE_ETH_CAN:
304 dotaddr = inet_ntoa(li->ip);
305 tmp = src_width - strlen(dotaddr) - 1;
306 printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
307 tmp, li->port, ifname );
312 int main( int argc, char* argv[] )
319 struct sockaddr_nl nladdr;
321 struct cegw_nlmsg req;
323 char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
325 struct nlmsghdr* nlh;
326 struct nlmsgerr* rte;
334 fd = open( "/dev/cegw", O_RDONLY );
337 fprintf( stderr, "error: could not open device file\n" );
341 ioctl( fd, CEGW_IOCTL_CAN_SOCK, 8 );
345 memset( &d, 0, sizeof(d) );
347 struct option long_opt[] =
349 { "add" , 0, NULL, 'A' },
350 { "flush" , 0, NULL, 'F' },
351 { "list" , 0, NULL, 'L' },
352 { "listen", 1, NULL, 'l' },
358 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
368 cmd |= CEGW_CMD_FLUSH;
371 cmd |= CEGW_CMD_LIST;
374 cmd |= CEGW_CMD_LISTEN;
375 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
377 perr( "'--listen'" );
380 if( tmp != IF_ETH_UDP )
382 perr( "'--listen' expects udp interface" );
385 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
387 perr( "'--listen'" );
392 if( read_if( optarg, &d.src_if, &d ) != 0 )
399 if( read_if( optarg, &d.dst_if, &d ) != 0 )
409 perr( "unknown option" );
415 /* prepare netlink message */
416 req.nh.nlmsg_len = NLMSG_LENGTH( sizeof(struct rtmsg) );
418 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
419 req.nh.nlmsg_seq = 0;
420 req.nh.nlmsg_pid = 0; /* ? */
422 memset( &req.rt, 0, sizeof(req.rt) );
423 req.rt.rtm_family = AF_CAN;
428 perr( "command not specified" );
432 if( cegw_add( &req, &d ) != 0 )
439 cegw_flush( &req, &d );
442 cegw_list( &req, &d );
444 case CEGW_CMD_LISTEN:
445 cegw_listen( &req, &d );
448 perr( "command mismatch" );
452 /* send over netlink socket */
453 s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
455 memset( &nladdr, 0, sizeof(nladdr) );
456 nladdr.nl_family = AF_NETLINK;
459 nladdr.nl_groups = 0;
461 err = sendto( s, &req, req.nh.nlmsg_len, 0,
462 (struct sockaddr*)&nladdr, sizeof(nladdr) );
465 perror( "netlink sendto" );
470 rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
476 nlh = (struct nlmsghdr*)rxbuf;
478 if( nlh->nlmsg_type == NLMSG_ERROR )
480 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
485 printf( "%s\n", strerror(abs(err)) );
489 printf( "netlink error: %s\n", strerror(abs(err)) );
494 if( cmd & CEGW_CMD_LIST )
496 /* ToDo recv while */
497 printf( "%10ssource%20sdestination\n", "", "" );
500 if( !NLMSG_OK( nlh, rsize ) )
504 if( nlh->nlmsg_type == NLMSG_DONE )
508 /* ToDo: NLMSG_ERR */
509 rta = NLMSG_DATA( nlh );
510 len = NLMSG_PAYLOAD( nlh, 0 );
511 for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
513 switch( rta->rta_type )
516 li.type = *(int*)RTA_DATA(rta);
518 case CEGW_CAN_IFINDEX:
519 li.can = *(int*)RTA_DATA(rta);
522 li.ip = *(struct in_addr*)RTA_DATA(rta);
525 li.port = *(unsigned short*)RTA_DATA(rta);
527 /* case CGW_ETH_PROTO */
531 print_list_item( &li );
533 nlh = NLMSG_NEXT( nlh, rsize );