return NULL;
}
+/* ToDo: move to common */
+
/**
* read_if - reads interface from @in
* Function analyzes @in for interface specification in format
return -cegw_errno;
}
- gwtype = (d->src_if == IF_CAN) ? CGW_TYPE_CAN_ETH_UDP : CGW_TYPE_ETH_CAN_UDP;
- addattr_l( &req->nh, sizeof(*req), CGW_CAN_IF, &d->can_ifidx, sizeof(d->can_ifidx) );
-
- switch( gwtype )
- {
- case CGW_TYPE_CAN_ETH_UDP:
- addattr_l( &req->nh, sizeof(*req), CGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
- addattr_l( &req->nh, sizeof(*req), CGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
- break;
- case CGW_TYPE_ETH_CAN_UDP:
- break;
- default:
- break;
- }
-
- addattr32( &req->nh, sizeof(*req), CGW_CMD_INFO, gwtype );
+ gwtype = (d->src_if == IF_CAN) ? CEGW_RULE_CAN_ETH : CEGW_RULE_ETH_CAN;
+ addattr_l( &req->nh, sizeof(*req), CEGW_CAN_IFINDEX, &d->can_ifidx, sizeof(d->can_ifidx) );
+ addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_addr, sizeof(d->eth_addr) );
+ addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_port, sizeof(d->eth_port) );
+ addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, gwtype );
return 0;
}
inline static int cegw_listen( struct cegw_nlmsg* req, struct cegw_data* d )
{
req->nh.nlmsg_type = RTM_NEWROUTE;
- addattr32( &req->nh, sizeof(*req), CGW_CMD_INFO, CEGW_LISTEN );
- addattr_l( &req->nh, sizeof(*req), CGW_LISTEN_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
- addattr_l( &req->nh, sizeof(*req), CGW_LISTEN_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
- printf( "listen at: %x, %hu\n", d->eth_listen_addr.s_addr, d->eth_listen_port );
+ addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_LISTEN );
+ addattr_l( &req->nh, sizeof(*req), CEGW_ETH_IP, &d->eth_listen_addr, sizeof(d->eth_listen_addr) );
+ addattr_l( &req->nh, sizeof(*req), CEGW_ETH_PORT, &d->eth_listen_port, sizeof(d->eth_listen_port) );
return 0;
}
inline static int cegw_flush( struct cegw_nlmsg* req, struct cegw_data* d )
{
- addattr32( &req->nh, sizeof(*req), CGW_CMD_INFO, CEGW_FLUSH );
+ addattr32( &req->nh, sizeof(*req), CEGW_CMD_INFO, CEGW_FLUSH );
req->nh.nlmsg_type = RTM_DELROUTE;
return 0;
}
{
char ifname[IF_NAMESIZE];
const char src_width = 21;
+ char* dotaddr;
+ int tmp;
if( if_indextoname( li->can, ifname ) == NULL )
{
/* ToDo listening at */
switch( li->type )
{
- case CGW_TYPE_CAN_ETH_UDP:
+ case CEGW_RULE_CAN_ETH:
printf( "can@%-*s -> udp@%s:%hu\n", src_width, ifname, \
inet_ntoa(li->ip), li->port );
break;
- case CGW_TYPE_ETH_CAN_UDP:
- printf( "udp@%-*s -> can@%s\n", src_width, "*:*", ifname );
+ case CEGW_RULE_ETH_CAN:
+ dotaddr = inet_ntoa(li->ip);
+ tmp = src_width - strlen(dotaddr) - 1;
+ printf( "udp@%s:%-*hu -> can@%s\n", inet_ntoa(li->ip), \
+ tmp, li->port, ifname );
break;
}
}
if( err < 0 )
{
perror( "netlink sendto" );
- return err;
+ return -1;
}
/* recv */
rsize = recv( s, &rxbuf, sizeof(rxbuf), 0 );
- printf( "recv size=%d\n", rsize );
if( rsize < 0 )
{
perr( "recv" );
}
nlh = (struct nlmsghdr*)rxbuf;
- if( cmd & CEGW_CMD_LIST )
+ if( nlh->nlmsg_type == NLMSG_ERROR )
{
- printf( "recv nlmsg_type=%d\n", nlh->nlmsg_type );
- if( nlh->nlmsg_type == NLMSG_ERROR )
+ rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
+ err = rte->error;
+
+ if( err == 0 )
+ {
+ printf( "%s\n", strerror(abs(err)) );
+ return 0;
+ } else
{
- struct nlmsgerr* nlerr = NLMSG_DATA( nlh );
- int err = nlerr->error;
- printf( "nlerror: %d,%s\n", err, strerror(abs(err)) );
+ printf( "netlink error: %s\n", strerror(abs(err)) );
+ return -1;
}
+ }
+
+ if( cmd & CEGW_CMD_LIST )
+ {
/* ToDo recv while */
printf( "%10ssource%20sdestination\n", "", "" );
while( 1 )
{
if( !NLMSG_OK( nlh, rsize ) )
{
- puts( "NLMSG_OK\n" );
break;
}
if( nlh->nlmsg_type == NLMSG_DONE )
{
- puts( "NLMSG_DONE" );
break;
}
/* ToDo: NLMSG_ERR */
-
rta = NLMSG_DATA( nlh );
- //rta = (struct rtattr*)( ((char *)rtm) + NLMSG_ALIGN(sizeof(struct rtmsg)) );
len = NLMSG_PAYLOAD( nlh, 0 );
for( ;RTA_OK(rta, len); rta = RTA_NEXT(rta,len) )
{
switch( rta->rta_type )
{
- case CGW_TYPE:
+ case CEGW_TYPE:
li.type = *(int*)RTA_DATA(rta);
break;
- case CGW_CAN_IF:
+ case CEGW_CAN_IFINDEX:
li.can = *(int*)RTA_DATA(rta);
break;
- case CGW_ETH_IP:
+ case CEGW_ETH_IP:
li.ip = *(struct in_addr*)RTA_DATA(rta);
break;
- case CGW_ETH_PORT:
+ case CEGW_ETH_PORT:
li.port = *(unsigned short*)RTA_DATA(rta);
break;
/* case CGW_ETH_PROTO */
nlh = NLMSG_NEXT( nlh, rsize );
}
- } else
- {
- if( nlh->nlmsg_type != NLMSG_ERROR )
- {
- fprintf( stderr, "error: unexpected netlink answer=%d\n", nlh->nlmsg_type );
- return -1;
- }
- rte = (struct nlmsgerr*)NLMSG_DATA( nlh );
- err = rte->error;
- if( err < 0 )
- fprintf( stderr, "error: netlink(%d); %s\n", err, strerror(abs(err)) );
}
-
+
return 0;
}