]> rtime.felk.cvut.cz Git - can-eth-gw.git/blobdiff - utils/cegw/cegw.c
Remove todo
[can-eth-gw.git] / utils / cegw / cegw.c
index 566a09f8ba909675e151a58cf8fe35093fef42be..c0e67f0f89e61ff5cf56fedd4ff0f87f0fa2e83b 100644 (file)
+/*
+ * Copyright: (c) 2012 Czech Technical University in Prague
+ *
+ * Authors:
+ *     Radek Matějka <radek.matejka@gmail.com>
+ *     Michal Sojka  <sojkam1@fel.cvut.cz>
+ *
+ * Funded by: Volkswagen Group Research
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+
 #include <stdio.h>
 #include <stdlib.h>
-#include <libgen.h>
 #include <string.h>
-#include <getopt.h>
-#include <errno.h>
 #include <sys/socket.h>
+#include <netdb.h>
 #include <net/if.h>
-#include <libnetlink.h>
-#include <linux/netlink.h>
-#include <linux/rtnetlink.h>
 #include <arpa/inet.h>
 #include <linux/can.h>
-#include <linux/types.h>
 #include <limits.h>
 #include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
-#include "canethgw.h"
-
-/**
- * ToDo:
- * [ ] print usage, on -h and plain execution
- * [ ] start/stop listening
- * [ ] split to files
- */
-
-#define CEGW_CMD_ADD     1
-#define CEGW_CMD_LIST    2
-#define CEGW_CMD_FLUSH   4
-#define CEGW_CMD_LISTEN  8
-
-enum
-{
-       IF_UNDEF,
-       IF_CAN,
-       IF_ETH_UDP
-};
-
-struct cegw_data
-{
-       int content;
-       int src_if, dst_if;
-       int can_ifidx;
-       struct in_addr eth_addr;
-       unsigned short eth_port;
-       struct in_addr eth_listen_addr;
-       unsigned short eth_listen_port;
-};
-
-struct cegw_nlmsg
-{
-       struct nlmsghdr nh;
-       struct rtmsg rt;
-       char buf[768]; /* enough? */
-};
-
-struct list_item
-{
-       int type;
-       int can;
-       struct in_addr ip;
-       unsigned short port;
-};
+#include <sys/types.h>
+#include <linux/can/raw.h>
+#include <linux/can/canethgw.h>
 
 unsigned int cegw_errno = 0;
 
-enum
-{
+enum {
        CEGW_ERR_UNKNOWN,
-       CEGW_ERR_IF_UNSPEC,
-       CEGW_ERR_IF_SAME,
-       CEGW_ERR_IF_TYPE,
-       CEGW_ERR_IF_CAN,
-       CEGW_ERR_IF_ETH,
        CEGW_ERR_COLON,
-       CEGW_ERR_ATON,
-       CEGW_ERR_PORT
+       CEGW_ERR_GETADDRI,
+       CEGW_ERR_FLTALCK,
+       CEGW_ERR_FLTPARSE,
+       CEGW_ERR_LAST
 };
 
-char* cegw_errlist[] =
-{
-       [ CEGW_ERR_UNKNOWN   ] = "",
-       [ CEGW_ERR_IF_UNSPEC ] = "source or destination not specified",
-       [ CEGW_ERR_IF_SAME   ] = "source and destination have same interface type",
-       [ CEGW_ERR_IF_TYPE   ] = "unknown interface type",
-       [ CEGW_ERR_IF_CAN    ] = "invalid can interface",
-       [ CEGW_ERR_IF_ETH    ] = "invalid eth interface",
-       [ CEGW_ERR_COLON     ] = "expected ':' (<ip>:<port>)",
-       [ CEGW_ERR_ATON      ] = "ip address mismatch",
-       [ CEGW_ERR_PORT      ] = "port number"
+char *cegw_errlist[] = {
+       [CEGW_ERR_COLON   ] = "expected ':' (<hostname>:<port>)",
+       [CEGW_ERR_GETADDRI] = "getaddrinfo failed",
+       [CEGW_ERR_FLTALCK ] = "filter alloc failed",
+       [CEGW_ERR_FLTPARSE] = "filter parsing failed"
 };
 
-static void perr( char* s )
+static const char help_msg[] = "usage:\n"
+                              "        %s <can_if>[,filter]* <udp_listen_addr>:<port> <udp_dest_addr>:<port>\n"
+                              "                [list of additional udp recipients <addr>:<port>]\n"
+                              "example:\n"
+                              "        %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n\n"
+                              "        Executing this command will set the gateway so that it will\n"
+                              "        listen for udp messages on 192.168.0.1:10501 and send them\n"
+                              "        to can0. Simultaneously, it will send all messages from can0\n"
+                              "        to 192.168.0.4:980 and 192.168.0.7:1160 via udp. The message is\n"
+                              "        therefore cloned. Notice that there can be more udp recipients.\n"
+                              "        The can filter is specified in the same way as in candump utility.\n";
+
+const struct addrinfo hints = {
+       .ai_family = AF_INET,
+       .ai_socktype = SOCK_DGRAM
+};
+
+static void perr(char *s)
 {
-       if( s )
-       {
-               if( cegw_errno == 0 )
-               {
-                       fprintf( stderr, "error: %s\n", s );
-
-               } else
-               {
-                       fprintf( stderr, "error: %s, %s\n", s,
-                                cegw_errlist[ cegw_errno ] );
+       if (s) {
+               if (cegw_errno == 0 || cegw_errno >= CEGW_ERR_LAST) {
+                       fprintf(stderr, "error: %s\n", s);
+
+               } else {
+                       fprintf(stderr, "error: %s, %s\n", s, cegw_errlist[cegw_errno]);
                }
                return;
        }
 
-       fprintf( stderr, "error: %s\n", cegw_errlist[ cegw_errno ] );
+       fprintf(stderr, "error: %s\n", cegw_errlist[cegw_errno]);
 }
 
 /**
- * read_addrport - parses @in for eth address.
- * Valid input is e.g. udp@127.0.0.1:10502 or can@vcan0.
+ * readsockaddr - parses @in for eth address.
+ * Valid input is e.g. 127.0.0.1:10502. If parsing fails
+ * the cause is stored in cegw_errno. Please note that
+ * the function modifies content of arg.
  *
- * @param[in]  in   string to search in
- * @param[out] addr ip address
- * @param[out] port transport layer port
+ * @param[in]  arg   hostname:port string
+ * @param[out] addr  filled sockaddr_in structure
  * @return 0 on success, -1 otherwise
  */
-int read_addrport( char* in, struct in_addr* addr, unsigned short* port )
+int readsockaddr(char *arg, struct sockaddr_in *addr)
 {
-       char* delim = NULL;
-       char addrstr[16];
-       int addrlen;
+       int ret;
+       char *delim;
+       struct addrinfo *res;
 
-       if( (delim = strchr( in, ':' )) == NULL )
-       {
+       delim = strchr(arg, ':');
+
+       if (delim == NULL) {
                cegw_errno = CEGW_ERR_COLON;
                return -1;
        }
 
-       /* get address */
-       addrlen = delim - in;
-       memcpy( addrstr, in, addrlen );
-       addrstr[addrlen] = '\0';
-       if( inet_aton( addrstr, addr ) == 0 )
-       {
-               cegw_errno = CEGW_ERR_ATON;
-               return -1;
-       }
+       *delim = '\0';
+       delim++;
 
-       /* get port */
-       if( sscanf( delim, ":%hu", port ) != 1 ) /* ToDo: handle overflow */
-       {
-               cegw_errno = CEGW_ERR_PORT;
+       ret = getaddrinfo(arg, delim, &hints, &res);
+       if (ret != 0) {
+               fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
+               cegw_errno = CEGW_ERR_GETADDRI;
                return -1;
        }
 
+       memcpy(addr, res->ai_addr, sizeof(*addr));
+
+       freeaddrinfo(res);
        return 0;
 }
 
 /**
- * read_iftype - reads @in for iftype
- * Iftype type is e.g. "can@" or "udp@".
- *
- * @param[in] in string to search in
- * @param[out] iftype iftype detected
- * @return pointer to @in after iftype on success, NULL otherwise
+ * readfilter - reads can filter definition from nptr
  */
-char* read_iftype( char* in, int* iftype )
+int readfilter(char *nptr, struct can_filter **filter, int *out_numfilter, can_err_mask_t *err_mask)
 {
-       char* ret = in+4;
-
-       if( strncmp( "udp@", optarg, 4 ) == 0 )
-       {
-               *iftype = IF_ETH_UDP;
-               return ret;
+       char *ptr;
+       int numfilter;
+       struct can_filter *rfilter;
+
+       numfilter = 0;
+       ptr = nptr;
+       while (ptr) {
+               numfilter++;
+               ptr++; /* hop behind the ',' */
+               ptr = strchr(ptr, ','); /* exit condition */
        }
-       /*
-       if( strncmp( "tcp@", optarg, 4 ) == 0 )
-       {
-               return NULL;
-       }
-       */
-       if( strncmp( "can@", optarg, 4 ) == 0 )
-       {
-               *iftype = IF_CAN;
-               return ret;
-       }
-
-       cegw_errno = CEGW_ERR_IF_TYPE;
-       return NULL;
-}
-
-/* ToDo: move to common */
-
-/**
- * read_if - reads interface from @in
- * Function analyzes @in for interface specification in format
- * <if>@<ip>:<port>, where <if> is can or udp, <ip> is address in dotted
- * format
- *
- * @param[in]  in string to search in
- * @param[out] iftype interface type (IF_CAN or IF_ETH_UDP)
- * @param[out] d ip and port is stored to @d
- */
-int read_if( char* in, int* iftype, struct cegw_data* d )
-{
-       char* optstr = NULL;
 
-       if( (optstr = read_iftype( in, iftype )) == NULL )
-       {
+       rfilter = malloc(numfilter * sizeof(*rfilter));
+       if (!rfilter) {
+               cegw_errno = CEGW_ERR_FLTALCK;
                return -1;
        }
 
-       switch( *iftype )
-       {
-               case IF_CAN:
-                       d->can_ifidx = if_nametoindex( optstr );
-                       if( d->can_ifidx == 0 )
-                       {
-                               cegw_errno = CEGW_ERR_IF_CAN;
-                               return -1;
-                       }
-                       break;
-               case IF_ETH_UDP:
-                       if( read_addrport( optstr, &d->eth_addr, &d->eth_port ) != 0 )
-                       {
-                               return -1;
-                       }
-                       break;
-               default:
+       numfilter = 0;
+       *err_mask = 0;
+
+       while (nptr) {
+
+               ptr = nptr+1; /* hop behind the ',' */
+               nptr = strchr(ptr, ','); /* update exit condition */
+
+               if (sscanf(ptr, "%x:%x",
+                                       &rfilter[numfilter].can_id,
+                                       &rfilter[numfilter].can_mask) == 2) {
+                       rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
+                       numfilter++;
+               } else if (sscanf(ptr, "%x~%x",
+                                       &rfilter[numfilter].can_id,
+                                       &rfilter[numfilter].can_mask) == 2) {
+                       rfilter[numfilter].can_id |= CAN_INV_FILTER;
+                       rfilter[numfilter].can_mask &= ~CAN_ERR_FLAG;
+                       numfilter++;
+               } else if (sscanf(ptr, "#%x", err_mask) != 1) {
+                       cegw_errno = CEGW_ERR_FLTPARSE;
+                       free(rfilter);
                        return -1;
-                       break;
+               }
        }
 
+       *filter = rfilter;
+       *out_numfilter = numfilter;
        return 0;
 }
 
-int main( int argc, char* argv[] )
+int main(int argc, char *argv[])
 {
-       int s;
-       int tmp = 0;
-       int cmd = 0;
-       char* optstr;
-       char opt;
-       struct sockaddr_nl nladdr;
-       int err = 0;
-       struct cegw_nlmsg req;
-       struct cegw_data d;
-       char rxbuf[8192]; /* ToDo: /linux/netlink.h? */
-       int rsize = 0;
-       struct nlmsghdr* nlh;
-       struct nlmsgerr* rte;
-       struct rtattr* rta;
-       int len;
-       struct list_item li;
+       int i;
+       int fd;
+       int tmpi;
+       int dstcnt;
+       char *nptr;
+       int numfilter = 0;
        int udp_sock, can_sock;
+       can_err_mask_t err_mask = 0;
        struct sockaddr_in udp_addr;
        struct sockaddr_can can_addr;
-       struct in_addr ipaddr;
-       unsigned short port;
-       int i;
-       struct sockaddr_in* dst = NULL;
+       struct sockaddr_in *dst = NULL;
+       struct cegw_ioctl *gwctl = NULL;
+       struct can_filter *filter = NULL;
 
-       if( argc < 3 )
-       {
-               perr( "not enought arguments" );
-               /* ToDo: print usage */
-               return;
+       if (argc == 1 || (argc == 2 && strcmp(argv[1], "-h") == 0)) {
+               printf(help_msg, argv[0], argv[0]);
+               return 0;
+       }
+
+       if (argc < 4) {
+               perr("not enough arguments");
+               printf(help_msg, argv[0], argv[0]);
+               return 1;
        }
 
-       int dstcnt = argc-3;
-#ifdef _DEBUG
-       printf( "dstcnt=%i\n", dstcnt );
-#endif
-       struct cegw_ioctl* gwctl = (struct cegw_ioctl*)malloc( sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in) );
-
-       for( i=1; i<argc; i++ )
-       {
-               printf( "%s\n", argv[i] );
-               switch( i )
-               {
-                       case 1: /* can */
+       dstcnt = argc-3;
+       gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + (dstcnt)*sizeof(struct sockaddr_in));
+
+       for (i=1; i<argc; i++) {
+               switch (i) {
+                       case 1: /* can ifindex */
+                               nptr = strchr(argv[i], ',');
+                               if (nptr) {
+                                       *nptr = '\0';
+                               }
+
                                can_addr.can_family = AF_CAN;
-                               can_addr.can_ifindex = if_nametoindex( argv[i] ); /* ToDo */
+                               tmpi = if_nametoindex(argv[i]);
+                               if (tmpi == 0) {
+                                       perr("given can interface not found");
+                                       free(gwctl);
+                                       return 1;
+                               }
+                               can_addr.can_ifindex = tmpi;
                                break;
                        case 2: /* listen addr */
-                               udp_addr.sin_family = AF_INET;
-                               if( read_addrport(argv[i], &udp_addr.sin_addr, &port) != 0 )
-                               {
-                                       perr( "listening address mismatch" );
-                                       /* ToDo: free gwctl, all branches */
+                               if (readsockaddr(argv[i], &udp_addr) != 0) {
+                                       perr("reading listening address failed");
+                                       free(gwctl);
+                                       return 1;
                                }
-                               udp_addr.sin_port = htons(port);
                                break;
                        default: /* udp destination */
                                dst = &gwctl->udp_dst[i-3];
-                               dst->sin_family = AF_INET;
-                               if( read_addrport(argv[i], &dst->sin_addr, &port) != 0 )
-                               {
-                                       perr( "udp destination mismatch" );
+                               if (readsockaddr(argv[i], dst) != 0) {
+                                       perr("reading udp destination failed");
+                                       free(gwctl);
+                                       return 1;
                                }
-                               dst->sin_port = htons(port);
                                break;
                }
        }
 
-       /* udp socket */
-       udp_sock = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP );
-       if( udp_sock == -1 )
-       {
-               fprintf( stderr, "error: udp socket(..) failed\n" );
-               return -1;
+       /* prepare udp socket */
+       udp_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
+       if (udp_sock == -1) {
+               perror("udp socket()");
+               free(gwctl);
+               return 1;
        }
 
-       printf( "port:%x\n", udp_addr.sin_port );
-       if(bind( udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in) ) != 0)
-       {
-               fprintf( stderr, "error: udp bind(..) failed\n" );
-               return -1;
+       if (bind(udp_sock, (struct sockaddr *)&udp_addr, sizeof(struct sockaddr_in)) != 0) {
+               perror("bind(udp)");
+               free(gwctl);
+               return 1;
        }
 
-       /* can socket */
-       /* ToDo: name lookup error */
-       can_sock = socket( PF_CAN, SOCK_RAW, CAN_RAW );
-       if( can_sock == -1 )
-       {
-               fprintf( stderr, "error: can socket(..) failed\n" );
-               return -1;
+       /* prepare can socket */
+       can_sock = socket(PF_CAN, SOCK_RAW, CAN_RAW);
+       if (can_sock == -1) {
+               perror("can socket()");
+               free(gwctl);
+               return 1;
        }
 
-       if( bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0 )
-       {
-               fprintf( stderr, "error: can bind(..) failed\n" );
-               return -1;
+       if (bind(can_sock, (struct sockaddr *)&can_addr, sizeof(struct sockaddr_can)) != 0) {
+               perror("bind(can)");
+               free(gwctl);
+               return 1;
        }
 
-       /* send it to kernel gateway */
-       int fd;
+       /* can filter */
+       if (nptr && (readfilter(nptr, &filter, &numfilter, &err_mask) != 0)) {
+               perr("can filter");
+               free(gwctl);
+               return 1;
+       }
 
-       fd = open( "/dev/cegw", O_RDONLY );
-       if( fd == -1 )
-       {
-               fprintf( stderr, "error: could not open device file\n" );
-               return -1;
+       if (err_mask)
+               setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_ERR_FILTER,
+                               &err_mask, sizeof(err_mask));
+
+       if (numfilter)
+               setsockopt(can_sock, SOL_CAN_RAW, CAN_RAW_FILTER,
+                               filter, numfilter * sizeof(struct can_filter));
+       free(filter);
+
+       /* send it to kernel gateway */
+       fd = open("/dev/canethgw", O_RDONLY);
+       if (fd == -1) {
+               perror("/dev/canethgw");
+               free(gwctl);
+               return 1;
        }
 
        gwctl->can_sock = can_sock;
@@ -345,18 +295,16 @@ int main( int argc, char* argv[] )
        gwctl->udp_dstcnt = dstcnt;
        gwctl->udp_addrlen = sizeof(struct sockaddr_in);
 
-       if( ioctl( fd, CEGW_IOCTL_START, gwctl ) != 0 )
-       {
-               perror( NULL );
+       if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
+               perror("ioctl");
+               free(gwctl);
+               return 1;
        }
+       printf("gateway successfully set and running\n");
+       free(gwctl);
 
-       //close( fd );
-
-       while(1)
-       {
-               sleep( UINT_MAX );
-       }
+       /* sleep until someone kills me */
+       pause();
 
        return 0;
 }
-