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) );
261 printf( "listen at: %x, %hu\n", d->eth_listen_addr.s_addr, d->eth_listen_port );
266 inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
268 req->nh.nlmsg_type = RTM_GETROUTE;
269 req->nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
274 inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
276 addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
277 req->nh.nlmsg_type = RTM_DELROUTE;
281 void print_list_item( struct list_item* li )
283 char ifname[IF_NAMESIZE];
284 const char src_width = 21;
288 if( if_indextoname( li->can, ifname ) == NULL )
290 strncpy( ifname, "unknown", IF_NAMESIZE );
293 /* ToDo listening at */
296 case CEGW_RULE_CAN_ETH:
297 printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
298 inet_ntoa(li->ip), li->port );
300 case CEGW_RULE_ETH_CAN:
301 dotaddr = inet_ntoa(li->ip);
302 tmp = src_width - strlen(dotaddr) - 1;
303 printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
304 tmp, li->port, ifname );
309 int main( int argc, char* argv[] )
316 struct sockaddr_nl nladdr;
318 struct cegw_nlmsg req;
320 char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
322 struct nlmsghdr* nlh;
323 struct nlmsgerr* rte;
328 memset( &d, 0, sizeof(d) );
330 struct option long_opt[] =
332 { "add" , 0, NULL, 'A' },
333 { "flush" , 0, NULL, 'F' },
334 { "list" , 0, NULL, 'L' },
335 { "listen", 1, NULL, 'l' },
341 opt = getopt_long( argc, argv, "AFLl:s:d:", long_opt, NULL );
351 cmd |= CEGW_CMD_FLUSH;
354 cmd |= CEGW_CMD_LIST;
357 cmd |= CEGW_CMD_LISTEN;
358 if( (optstr = read_iftype( optarg, &tmp )) == NULL )
360 perr( "'--listen'" );
363 if( tmp != IF_ETH_UDP )
365 perr( "'--listen' expects udp interface" );
368 if( read_addrport( optstr, &d.eth_listen_addr, &d.eth_listen_port ) )
370 perr( "'--listen'" );
375 if( read_if( optarg, &d.src_if, &d ) != 0 )
382 if( read_if( optarg, &d.dst_if, &d ) != 0 )
392 perr( "unknown option" );
398 /* prepare netlink message */
399 req.nh.nlmsg_len = NLMSG_LENGTH( sizeof(struct rtmsg) );
401 req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
402 req.nh.nlmsg_seq = 0;
403 req.nh.nlmsg_pid = 0; /* ? */
405 memset( &req.rt, 0, sizeof(req.rt) );
406 req.rt.rtm_family = AF_CAN;
411 perr( "command not specified" );
415 if( cegw_add( &req, &d ) != 0 )
422 cegw_flush( &req, &d );
425 cegw_list( &req, &d );
427 case CEGW_CMD_LISTEN:
428 cegw_listen( &req, &d );
431 perr( "command mismatch" );
435 /* send over netlink socket */
436 s = socket( PF_NETLINK, SOCK_RAW, NETLINK_ROUTE ); /* chck */
438 memset( &nladdr, 0, sizeof(nladdr) );
439 nladdr.nl_family = AF_NETLINK;
442 nladdr.nl_groups = 0;
444 err = sendto( s, &req, req.nh.nlmsg_len, 0,
445 (struct sockaddr*)&nladdr, sizeof(nladdr) );
448 perror( "netlink sendto" );
453 rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
454 printf( "recv size=%d\n", rsize );
460 nlh = (struct nlmsghdr*)rxbuf;
462 if( cmd & CEGW_CMD_LIST )
464 printf( "recv nlmsg_type=%d\n", nlh->nlmsg_type );
465 if( nlh->nlmsg_type == NLMSG_ERROR )
467 struct nlmsgerr* nlerr = NLMSG_DATA( nlh );
468 int err = nlerr->error;
469 printf( "nlerror: %d,%s\n", err, strerror(abs(err)) );
471 /* ToDo recv while */
472 printf( "%10ssource%20sdestination\n", "", "" );
475 if( !NLMSG_OK( nlh, rsize ) )
477 puts( "NLMSG_OK\n" );
480 if( nlh->nlmsg_type == NLMSG_DONE )
482 puts( "NLMSG_DONE" );
485 /* ToDo: NLMSG_ERR */
487 rta = NLMSG_DATA( nlh );
488 //rta = (struct rtattr*)( ((char *)rtm) + NLMSG_ALIGN(sizeof(struct rtmsg)) );
489 len = NLMSG_PAYLOAD( nlh, 0 );
490 for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
492 switch( rta->rta_type )
495 li.type = *(int*)RTA_DATA(rta);
497 case CEGW_CAN_IFINDEX:
498 li.can = *(int*)RTA_DATA(rta);
501 li.ip = *(struct in_addr*)RTA_DATA(rta);
504 li.port = *(unsigned short*)RTA_DATA(rta);
506 /* case CGW_ETH_PROTO */
510 print_list_item( &li );
512 nlh = NLMSG_NEXT( nlh, rsize );
516 if( nlh->nlmsg_type != NLMSG_ERROR )
518 fprintf( stderr, "error: unexpected netlink answer=%d\n", nlh->nlmsg_type );
521 rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
524 fprintf( stderr, "error: netlink(%d); %s\n", err, strerror(abs(err)) );