]> rtime.felk.cvut.cz Git - can-eth-gw.git/blobdiff - utils/cegw/cegw.c
source cleaning, improving and adding tests
[can-eth-gw.git] / utils / cegw / cegw.c
index c0cb885313d6c8d6d7ffc098db923147f405bd9b..a0b5259ee6f89dbf7cf716c2efcb09989712cbcf 100644 (file)
@@ -1,18 +1,12 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <libgen.h>
 #include <string.h>
-#include <getopt.h>
-#include <errno.h>
 #include <sys/socket.h>
 #include <net/if.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"
@@ -23,6 +17,7 @@ enum
 {
        CEGW_ERR_UNKNOWN,
        CEGW_ERR_COLON,
+       CEGW_ERR_UNEXLEN,
        CEGW_ERR_ATON,
        CEGW_ERR_PORT
 };
@@ -31,14 +26,23 @@ char* cegw_errlist[] =
 {
        [CEGW_ERR_UNKNOWN] = "",
        [CEGW_ERR_COLON  ] = "expected ':' (<ip>:<port>)",
+       [CEGW_ERR_UNEXLEN] = "unexpected ip address length, please use dot notation"
+                            " (eg. 127.0.0.1)",
        [CEGW_ERR_ATON   ] = "ip address mismatch",
        [CEGW_ERR_PORT   ] = "port number"
 };
 
-static const char help_msg[] = "usage:\n\
-       %s <can_if> <udp_listen_addr>:<port> N x (<udp_dest_addr>:<port>)\n\
-example:\n\
-       %s can0 192.168.0.1:10501 192.168.0.4:980 192.168.0.7:1160\n";
+static const char help_msg[] = "usage:\n"
+                              "        %s <can_if> <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 any number\n"
+                              "        of udp recipients.\n";
 
 static void perr(char* s)
 {
@@ -57,7 +61,8 @@ static void perr(char* s)
 
 /**
  * read_addrport - parses @in for eth address.
- * Valid input is e.g. udp@127.0.0.1:10502 or can@vcan0.
+ * Valid input is e.g. 127.0.0.1:10502. If parsing fails
+ * the cause is stored in cegw_errno.
  *
  * @param[in]  in   string to search in
  * @param[out] addr ip address
@@ -67,7 +72,8 @@ static void perr(char* s)
 int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
 {
        char* delim = NULL;
-       char addrstr[16];
+       const int addrstr_len = 16;
+       char addrstr[addrstr_len];
        int addrlen;
 
        if ((delim = strchr(in, ':')) == NULL) {
@@ -77,6 +83,11 @@ int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
 
        /* get address */
        addrlen = delim - in;
+       if (addrlen > addrstr_len) {
+               cegw_errno = CEGW_ERR_UNEXLEN;
+               return -1;
+       }
+
        memcpy(addrstr, in, addrlen);
        addrstr[addrlen] = '\0';
        if (inet_aton(addrstr, addr) == 0) {
@@ -86,7 +97,6 @@ int read_addrport(char* in, struct in_addr* addr, unsigned short* port)
 
        /* get port */
        /* ToDo: handle overflow */
-
        if (sscanf(delim, ":%hu", port) != 1) {
                cegw_errno = CEGW_ERR_PORT;
                return -1;
@@ -108,7 +118,13 @@ int main(int argc, char* argv[])
        struct sockaddr_in* dst = NULL;
        struct cegw_ioctl* gwctl = NULL;
 
-       if (argc < 3) {
+       if (argc == 1)
+       {
+               printf(help_msg, argv[0], argv[0]);
+               return 0;
+       }
+
+       if (argc < 4) {
                perr("not enought arguments");
                printf(help_msg, argv[0], argv[0]);
                /* ToDo: print usage */
@@ -143,8 +159,8 @@ int main(int argc, char* argv[])
                                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" );
-                                       free( gwctl );
+                                       perr("udp destination mismatch");
+                                       free(gwctl);
                                        return -1;
                                }
                                dst->sin_port = htons(port);
@@ -198,7 +214,7 @@ int main(int argc, char* argv[])
                free(gwctl);
                return -1;
        }
-       printf("gateway sucessfully set-up and running\n");
+       printf("gateway successfully set and running\n");
        free(gwctl);
 
        /* sleep until someone kills me */