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>
20 * [ ] print usage, on -h and plain execution
21 * [ ] start/stop listening
25 #define CEGW_CMD_ADD 1
26 #define CEGW_CMD_LIST 2
27 #define CEGW_CMD_FLUSH 4
28 #define CEGW_CMD_LISTEN 8
42 struct in_addr eth_addr;
43 unsigned short eth_port;
44 struct in_addr eth_listen_addr;
45 unsigned short eth_listen_port;
52 char buf[768]; /* enough? */
63 unsigned int cegw_errno = 0;
78 char* cegw_errlist[] =
80 [ CEGW_ERR_UNKNOWN ] = "",
81 [ CEGW_ERR_IF_UNSPEC ] = "source or destination not specified",
82 [ CEGW_ERR_IF_SAME ] = "source and destination have same interface type",
83 [ CEGW_ERR_IF_TYPE ] = "unknown interface type",
84 [ CEGW_ERR_IF_CAN ] = "invalid can interface",
85 [ CEGW_ERR_IF_ETH ] = "invalid eth interface",
86 [ CEGW_ERR_COLON ] = "expected ':' (<ip>:<port>)",
87 [ CEGW_ERR_ATON ] = "ip address mismatch",
88 [ CEGW_ERR_PORT ] = "port number"
91 static void perr( char* s )
97 fprintf( stderr, "error: %s\n", s );
101 fprintf( stderr, "error: %s, %s\n", s,
102 cegw_errlist[ cegw_errno ] );
107 fprintf( stderr, "error: %s\n", cegw_errlist[ cegw_errno ] );
111 * read_addrport - parses @in for eth address.
112 * Valid input is e.g. udp@127.0.0.1:10502 or can@vcan0.
114 * @param[in] in string to search in
115 * @param[out] addr ip address
116 * @param[out] port transport layer port
117 * @return 0 on success, -1 otherwise
119 int read_addrport( char* in, struct in_addr* addr, unsigned short* port )
125 if( (delim = strchr( in, ':' )) == NULL )
127 cegw_errno = CEGW_ERR_COLON;
132 addrlen = delim - in;
133 memcpy( addrstr, in, addrlen );
134 addrstr[addrlen] = '\0';
135 if( inet_aton( addrstr, addr ) == 0 )
137 cegw_errno = CEGW_ERR_ATON;
142 if( sscanf( delim, ":%hu", port ) != 1 ) /* ToDo: handle overflow */
144 cegw_errno = CEGW_ERR_PORT;
152 * read_iftype - reads @in for iftype
153 * Iftype type is e.g. "can@" or "udp@".
155 * @param[in] in string to search in
156 * @param[out] iftype iftype detected
157 * @return pointer to @in after iftype on success, NULL otherwise
159 char* read_iftype( char* in, int* iftype )
163 if( strncmp( "udp@", optarg, 4 ) == 0 )
165 *iftype = IF_ETH_UDP;
169 if( strncmp( "tcp@", optarg, 4 ) == 0 )
174 if( strncmp( "can@", optarg, 4 ) == 0 )
180 cegw_errno = CEGW_ERR_IF_TYPE;
184 /* ToDo: move to common */
187 * read_if - reads interface from @in
188 * Function analyzes @in for interface specification in format
189 * <if>@<ip>:<port>, where <if> is can or udp, <ip> is address in dotted
192 * @param[in] in string to search in
193 * @param[out] iftype interface type (IF_CAN or IF_ETH_UDP)
194 * @param[out] d ip and port is stored to @d
196 int read_if( char* in, int* iftype, struct cegw_data* d )
200 if( (optstr = read_iftype( in, iftype )) == NULL )
208 d->can_ifidx = if_nametoindex( optstr );
209 if( d->can_ifidx == 0 )
211 cegw_errno = CEGW_ERR_IF_CAN;
216 if( read_addrport( optstr, &d->eth_addr, &d->eth_port ) != 0 )
229 inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
233 req->nh.nlmsg_type = RTM_NEWROUTE;
234 if( (d->src_if == 0 || d->dst_if == 0) )
236 cegw_errno = CEGW_ERR_IF_UNSPEC;
240 if( d->src_if == d->dst_if )
242 cegw_errno = CEGW_ERR_IF_SAME;
246 gwtype = (d->src_if == IF_CAN) ? CEGW_RULE_CAN_ETH : CEGW_RULE_ETH_CAN;
247 addattr_l( &req->nh, sizeof(*req), CEGW_CAN_IFINDEX, &d->can_ifidx, sizeof(d->can_ifidx) );
248 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
249 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
250 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, gwtype );
255 inline static int cegw_listen( struct cegw_nlmsg* req, struct cegw_data* d )
257 req->nh.nlmsg_type = RTM_NEWROUTE;
258 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_LISTEN );
259 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
260 addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
265 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
267 req->nh.nlmsg_type = RTM_GETROUTE;
268 req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
273 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
275 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
276 req->nh.nlmsg_type = RTM_DELROUTE;
280 void print_list_item( struct list_item* li )
282 char ifname[IF_NAMESIZE];
283 const char src_width = 21;
287 if( if_indextoname( li->can, ifname ) == NULL )
289 strncpy( ifname, "unknown", IF_NAMESIZE );
292 /* ToDo listening at */
295 case CEGW_RULE_CAN_ETH:
296 printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
297 inet_ntoa(li->ip), li->port );
299 case CEGW_RULE_ETH_CAN:
300 dotaddr = inet_ntoa(li->ip);
301 tmp = src_width - strlen(dotaddr) - 1;
302 printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
303 tmp, li->port, ifname );
308 int main( int argc, char* argv[] )
315 struct sockaddr_nl nladdr;
317 struct cegw_nlmsg req;
319 char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
321 struct nlmsghdr* nlh;
322 struct nlmsgerr* rte;
327 memset( &d, 0, sizeof(d) );
329 struct option long_opt[] =
331 { "add" , 0, NULL, 'A' },
332 { "flush" , 0, NULL, 'F' },
333 { "list" , 0, NULL, 'L' },
334 { "listen", 1, NULL, 'l' },
340 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
350 cmd |= CEGW_CMD_FLUSH;
353 cmd |= CEGW_CMD_LIST;
356 cmd |= CEGW_CMD_LISTEN;
357 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
359 perr( "'--listen'" );
362 if( tmp != IF_ETH_UDP )
364 perr( "'--listen' expects udp interface" );
367 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
369 perr( "'--listen'" );
374 if( read_if( optarg, &d.src_if, &d ) != 0 )
381 if( read_if( optarg, &d.dst_if, &d ) != 0 )
391 perr( "unknown option" );
397 /* prepare netlink message */
398 req.nh.nlmsg_len = NLMSG_LENGTH( sizeof(struct rtmsg) );
400 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
401 req.nh.nlmsg_seq = 0;
402 req.nh.nlmsg_pid = 0; /* ? */
404 memset( &req.rt, 0, sizeof(req.rt) );
405 req.rt.rtm_family = AF_CAN;
410 perr( "command not specified" );
414 if( cegw_add( &req, &d ) != 0 )
421 cegw_flush( &req, &d );
424 cegw_list( &req, &d );
426 case CEGW_CMD_LISTEN:
427 cegw_listen( &req, &d );
430 perr( "command mismatch" );
434 /* send over netlink socket */
435 s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
437 memset( &nladdr, 0, sizeof(nladdr) );
438 nladdr.nl_family = AF_NETLINK;
441 nladdr.nl_groups = 0;
443 err = sendto( s, &req, req.nh.nlmsg_len, 0,
444 (struct sockaddr*)&nladdr, sizeof(nladdr) );
447 perror( "netlink sendto" );
452 rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
458 nlh = (struct nlmsghdr*)rxbuf;
460 if( nlh->nlmsg_type == NLMSG_ERROR )
462 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
467 printf( "%s\n", strerror(abs(err)) );
471 printf( "netlink error: %s\n", strerror(abs(err)) );
476 if( cmd & CEGW_CMD_LIST )
478 /* ToDo recv while */
479 printf( "%10ssource%20sdestination\n", "", "" );
482 if( !NLMSG_OK( nlh, rsize ) )
486 if( nlh->nlmsg_type == NLMSG_DONE )
490 /* ToDo: NLMSG_ERR */
491 rta = NLMSG_DATA( nlh );
492 len = NLMSG_PAYLOAD( nlh, 0 );
493 for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
495 switch( rta->rta_type )
498 li.type = *(int*)RTA_DATA(rta);
500 case CEGW_CAN_IFINDEX:
501 li.can = *(int*)RTA_DATA(rta);
504 li.ip = *(struct in_addr*)RTA_DATA(rta);
507 li.port = *(unsigned short*)RTA_DATA(rta);
509 /* case CGW_ETH_PROTO */
513 print_list_item( &li );
515 nlh = NLMSG_NEXT( nlh, rsize );