]> rtime.felk.cvut.cz Git - can-eth-gw.git/blobdiff - utils/cegw/cegw.c
netdevice_notifier implemented; test script; cegwbench rxtimeo
[can-eth-gw.git] / utils / cegw / cegw.c
index dceb5a04e8f6c57ed06c478005a973147dbaf1bd..c5c27b8c7d27a81084034f389032577d2293054b 100644 (file)
@@ -181,6 +181,8 @@ char* read_iftype( char* in, int* iftype )
        return NULL;
 }
 
+/* ToDo: move to common */
+
 /**
  * read_if - reads interface from @in
  * Function analyzes @in for interface specification in format
@@ -241,22 +243,11 @@ inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
                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;
 }
@@ -264,10 +255,9 @@ inline static int cegw_add( struct cegw_nlmsg* req, struct cegw_data* d )
 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;
 }
@@ -282,7 +272,7 @@ inline static int cegw_list( struct cegw_nlmsg* req, struct cegw_data* d )
 
 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;
 }
@@ -291,6 +281,8 @@ void print_list_item( struct list_item* li )
 {
        char ifname[IF_NAMESIZE];
        const char src_width = 21;
+       char* dotaddr;
+       int tmp;
 
        if( if_indextoname( li->can, ifname ) == NULL )
        {
@@ -300,12 +292,15 @@ void print_list_item( struct list_item* li )
        /* 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;
        }
 }
@@ -450,12 +445,11 @@ int main( int argc, char* argv[] )
        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" );
@@ -463,48 +457,53 @@ int main( int argc, char* argv[] )
        }
        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 */
@@ -515,19 +514,8 @@ int main( int argc, char* argv[] )
 
                        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;
 }