]> rtime.felk.cvut.cz Git - can-eth-gw.git/commitdiff
source cleaning, improving and adding tests
authorRadek Matějka <radek.matejka@gmail.com>
Wed, 19 Dec 2012 06:05:53 +0000 (00:05 -0600)
committerRadek Matějka <radek.matejka@gmail.com>
Wed, 19 Dec 2012 06:05:53 +0000 (00:05 -0600)
Source of kernel module and cegw utility were cleaned from unused
code.
A new test focused on incorrect input handling was introduced.
The older test scripts were modified to work with the new version.

22 files changed:
distro/customroot/sbin/badinputtest [new symlink]
distro/customroot/sbin/cesend [new symlink]
distro/customroot/sbin/i0 [new symlink]
distro/customroot/sbin/i1 [new symlink]
distro/customroot/sbin/i2 [new symlink]
distro/customroot/sbin/mkcegwdev [new file with mode: 0755]
distro/customroot/sbin/t3
distro/customroot/sbin/t4 [deleted symlink]
kernel/canethgw.c
kernel/canethgw.h
test/i0 [new file with mode: 0755]
test/i1 [new file with mode: 0755]
test/i2 [new file with mode: 0755]
test/src/Makefile [new file with mode: 0644]
test/src/badinputtest.c [new file with mode: 0644]
test/t0
test/t1
test/t2
test/t3
test/t4 [deleted file]
test/t7 [deleted file]
utils/cegw/cegw.c

diff --git a/distro/customroot/sbin/badinputtest b/distro/customroot/sbin/badinputtest
new file mode 120000 (symlink)
index 0000000..cb8419a
--- /dev/null
@@ -0,0 +1 @@
+/home/monter/project/can-eth-gw/test/src/badinputtest
\ No newline at end of file
diff --git a/distro/customroot/sbin/cesend b/distro/customroot/sbin/cesend
new file mode 120000 (symlink)
index 0000000..8c22931
--- /dev/null
@@ -0,0 +1 @@
+../../../utils/cesend/cesend
\ No newline at end of file
diff --git a/distro/customroot/sbin/i0 b/distro/customroot/sbin/i0
new file mode 120000 (symlink)
index 0000000..080a390
--- /dev/null
@@ -0,0 +1 @@
+/home/monter/project/can-eth-gw/test/i0
\ No newline at end of file
diff --git a/distro/customroot/sbin/i1 b/distro/customroot/sbin/i1
new file mode 120000 (symlink)
index 0000000..ab7e25d
--- /dev/null
@@ -0,0 +1 @@
+/home/monter/project/can-eth-gw/test/i1
\ No newline at end of file
diff --git a/distro/customroot/sbin/i2 b/distro/customroot/sbin/i2
new file mode 120000 (symlink)
index 0000000..bbcfdcb
--- /dev/null
@@ -0,0 +1 @@
+/home/monter/project/can-eth-gw/test/i2
\ No newline at end of file
diff --git a/distro/customroot/sbin/mkcegwdev b/distro/customroot/sbin/mkcegwdev
new file mode 100755 (executable)
index 0000000..9d59a31
--- /dev/null
@@ -0,0 +1,4 @@
+#/bin/sh
+modprobe canethgw
+mknod /dev/cegw c 10 59
+
index ec201a31f56b8594d0b2a7e031fa47dc7f81a1a0..06178668b4449288a6d69c3ac3444d5b6494536e 120000 (symlink)
@@ -1 +1 @@
-../../../test/t3
\ No newline at end of file
+/home/monter/project/can-eth-gw/test/t3
\ No newline at end of file
diff --git a/distro/customroot/sbin/t4 b/distro/customroot/sbin/t4
deleted file mode 120000 (symlink)
index fb867d8..0000000
+++ /dev/null
@@ -1 +0,0 @@
-../../../test/t4
\ No newline at end of file
index 90b892d1949361595429e1d9f4aec71f1542383f..fc660d7438d26e143402cc885ea922d32d882343 100644 (file)
@@ -1,25 +1,10 @@
-#define DEBUG
-
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/kthread.h>
-#include <linux/sched.h>
-#include <linux/delay.h>
-#include <linux/wait.h>
-#include <linux/netdevice.h>
 #include <linux/file.h>
-#include <linux/socket.h>
 #include <net/sock.h>
-#include <linux/if_arp.h>
-#include <linux/net.h>
-#include <linux/netdevice.h>
-#include <linux/can/core.h>
 #include <linux/can.h>
-#include <net/rtnetlink.h>
-#include <linux/completion.h>
-#include <linux/mutex.h>
 #include <linux/miscdevice.h>
-#include <net/inet_common.h>
 #include "canethgw.h"
 
 MODULE_LICENSE("GPL");
@@ -33,24 +18,6 @@ static int cegw_thread_start(void *data);
 static int cegw_thread_stop(struct cegw_job *job);
 static void cegw_job_release(struct kref *ref);
 
-enum __cegw_state {
-       CEGW_RUN,
-       CEGW_STOP,
-       CEGW_EXIT
-};
-
-struct cegw_rule {
-       int can_ifindex;
-       struct in_addr eth_ip;
-       unsigned short eth_port;
-       struct hlist_node list;
-};
-
-struct cegw_setting {
-       struct in_addr eth_ip;
-       unsigned short eth_port;
-};
-
 static int cegw_udp_send(struct socket *udp_sock, struct can_frame *cf, struct sockaddr_in* addr)
 {
        struct msghdr mh;
@@ -130,7 +97,7 @@ static int cegw_udp2can(void *data)
 /**
  * cegw_can2udp - performs can->udp routing
  *
- * Runs as a thread.
+ * This function is run as a thread.
  */
 static int cegw_can2udp(void* data)
 {
@@ -294,13 +261,13 @@ static long cegw_ioctl_start(struct file *file, unsigned long arg)
                }
        }
 
-       job->udp_sock = sockfd_lookup( gwctl.udp_sock, &err );
+       job->udp_sock = sockfd_lookup(gwctl.udp_sock, &err);
        if (job->udp_sock == NULL) {
                kfree(job);
                return err;
        }
 
-       job->can_sock = sockfd_lookup( gwctl.can_sock, &err );
+       job->can_sock = sockfd_lookup(gwctl.can_sock, &err);
        if (job->can_sock == NULL) {
                fput(job->udp_sock->file);
                kfree(job);
@@ -319,13 +286,14 @@ static long cegw_ioctl_start(struct file *file, unsigned long arg)
 
 static long cegw_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
-       int err = 0;
+       int err;
 
        switch (cmd) {
                case CEGW_IOCTL_START:
                        err = cegw_ioctl_start(file, arg);
                        break;
                default:
+                       err = -EOPNOTSUPP;
                        break;
        }
 
@@ -341,7 +309,7 @@ static const struct file_operations cegw_fops = {
 
 static struct miscdevice cegw_device = {
        .minor = MISC_DYNAMIC_MINOR,
-       .name = "cegw",
+       .name = "canethgw",
        .fops = &cegw_fops
 };
 
@@ -356,7 +324,6 @@ static void __exit cegw_exit(void)
 {
        misc_deregister(&cegw_device);
 
-       /* ToDo: exit threads */
        return;
 }
 
index f15b688229dad52899044ea882a76295898a0265..65d9cc51b8e9dd36042e978b385918c129bf141d 100644 (file)
@@ -2,47 +2,6 @@
 #define CANETHGW_H
 
 #include <linux/types.h>
-#include <linux/can.h>
-
-/* these are from gw.h */
-struct rtcanmsg {
-       __u8  can_family;
-       __u8  gwtype;
-       __u16 flags;
-};
-
-/* CEGW_CMD_INFO values */
-enum {
-       CEGW_RULE_CAN_ETH,
-       CEGW_RULE_ETH_CAN,
-       CEGW_LISTEN,
-       CEGW_FLUSH,
-       __CEGW_CMD_MAX
-};
-#define CEGW_CMD_MAX (__CEGW_CMD_MAX - 1)
-
-/* rtnetlink attributes */
-enum {
-       CEGW_TYPE,
-       CEGW_CAN_IFINDEX,
-       CEGW_ETH_IP,
-       CEGW_ETH_PORT,
-       CEGW_CMD_INFO,
-       __CEGW_MAX
-};
-#define CEGW_MAX (__CEGW_MAX - 1)
-
-struct cegw_udp_dst
-{
-       __u32 len;
-       struct sockaddr addr;
-};
-
-struct cegw_udp_dst4
-{
-       __u32 len;
-       struct sockaddr_in addr;
-};
 
 struct cegw_ioctl
 {
@@ -67,11 +26,5 @@ struct cegw_job
 #define CEGW_IOCTL_BASE 'c'
 #define CEGW_IOCTL_START _IOW(CEGW_IOCTL_BASE, 0, struct cegw_ioctl)
 
-/* ToDo:
- *     CEGW_HANDLED
- *     CEGW_DROPPED
- *     CEGW_ETH_PROTO
- */
-
 #endif /* CANETHGW_H */
 
diff --git a/test/i0 b/test/i0
new file mode 100755 (executable)
index 0000000..2f39807
--- /dev/null
+++ b/test/i0
@@ -0,0 +1,4 @@
+#!/bin/sh
+cegwprobe && mkcegwdev
+i1
+
diff --git a/test/i1 b/test/i1
new file mode 100755 (executable)
index 0000000..48d2437
--- /dev/null
+++ b/test/i1
@@ -0,0 +1,3 @@
+#!/bin/sh
+cegw vcan0 0.0.0.0:10501 127.0.0.1:10502
+
diff --git a/test/i2 b/test/i2
new file mode 100755 (executable)
index 0000000..a97d226
--- /dev/null
+++ b/test/i2
@@ -0,0 +1,3 @@
+#!/bin/sh
+cegw vcan0 0.0.0.0:10501 127.0.0.1:10502 127.0.0.1:10503
+
diff --git a/test/src/Makefile b/test/src/Makefile
new file mode 100644 (file)
index 0000000..a73f091
--- /dev/null
@@ -0,0 +1,7 @@
+PROGRAM=badinputtest
+
+all:
+       gcc -Wall -o${PROGRAM} -I../../kernel badinputtest.c
+debug:
+       gcc -Wall -ggdb -D_DEBUG -o${PROGRAM} -I../../kernel badinputtest.c
+
diff --git a/test/src/badinputtest.c b/test/src/badinputtest.c
new file mode 100644 (file)
index 0000000..0a6136c
--- /dev/null
@@ -0,0 +1,65 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <net/if.h>
+#include <arpa/inet.h>
+#include <linux/can.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include "canethgw.h"
+
+int main(int argc, char* argv[])
+{
+       int fd;
+       struct cegw_ioctl* gwctl = NULL;
+
+       gwctl = (struct cegw_ioctl*)malloc(sizeof(*gwctl) + sizeof(struct sockaddr_in));
+
+       /**/
+       fd = open("/dev/cegw", O_RDONLY);
+       if (fd == -1) {
+               fprintf(stderr, "error: could not open device file\n");
+               free(gwctl);
+               return -1;
+       }
+
+       gwctl->can_sock = 0;
+       gwctl->udp_sock = 0;
+       gwctl->udp_dstcnt = 0;
+       gwctl->udp_addrlen = sizeof(struct sockaddr_in);
+
+       if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
+               fprintf(stderr, "error: ioctl call 1 failed\n");
+       } else
+       {
+               printf("ioctl call 1 OK\n");
+       }
+       close(fd);
+
+       /**/
+       fd = open("/dev/cegw", O_RDWR);
+       if (fd == -1) {
+               fprintf(stderr, "error: could not open device file\n");
+               free(gwctl);
+               return -1;
+       }
+
+       gwctl->can_sock = -1;
+       gwctl->udp_sock = -1;
+       gwctl->udp_dstcnt = 0;
+       gwctl->udp_addrlen = sizeof(struct sockaddr_in);
+
+       if (ioctl(fd, CEGW_IOCTL_START, gwctl) != 0) {
+               fprintf(stderr, "error: ioctl call 2 failed\n");
+       } else {
+               printf("ioctl call 2 OK\n");
+       }
+       close(fd);
+
+       free(gwctl);
+       return 0;
+}
+
diff --git a/test/t0 b/test/t0
index 26026d72c71b23661ea49ea3485b907690f42184..b5c6da21cfa69631ba41a5c76f6ed8308914e65c 100755 (executable)
--- a/test/t0
+++ b/test/t0
@@ -15,15 +15,19 @@ evalmsg()
                echo -ne "\033[0m"
 }
 
+if test $# -ne 0; then
 if test $# -eq 1; then
        N=$1;
-fi
-
-if test $# -eq 4; then
+else if test $# -eq 4; then
        CAN=$1
        UDP_SRC=$2
        UDP_DST=$3
        N=$4
+else
+       echo "unexpected number of arguments"
+       return;
+fi
+fi
 fi
 
 echo -n "Testing eth->can: "
diff --git a/test/t1 b/test/t1
index 060f5c462601d8605cb2c622937e96b3e25b1c72..476ef76ce92e1ee99f76b316f02aa203f5430757 100755 (executable)
--- a/test/t1
+++ b/test/t1
@@ -1,7 +1,6 @@
 #!/bin/sh
-downmod
-cegwprobe
-cegw --listen udp@127.0.0.1:10501
-cegw --add -s can@vcan0 -d udp@127.0.0.1:10502
-#cegw --listen udp@127.0.0.1:10501
+cegw vcan0 127.0.0.1:10501 127.0.0.1:10502 &
+JOB=$!
+t0
+kill $JOB
 
diff --git a/test/t2 b/test/t2
index 0fb8ffc44199d24be7a090982a41ea5cfc3cf93e..d15a54e7b7db2d57c763dcc0caa70f2d33d54eab 100755 (executable)
--- a/test/t2
+++ b/test/t2
@@ -1,7 +1,9 @@
 #!/bin/sh
 downmod
 cegwprobe
-cegw --listen udp@127.0.0.1:10501
-cegw --add -s can@vcan0 -d udp@127.0.0.1:10502
-cegw --add -s udp@127.0.0.1:10502 -d can@vcan0
+cegw vcan0 127.0.0.1:10501 127.0.0.1:10502 127.0.0.1:10503 &
+JOB=$!
+t0
+t0 vcan0 127.0.0.1:10501 127.0.0.1:10503 10
+kill $JOB
 
diff --git a/test/t3 b/test/t3
index 9e333740d9cbc53451b4f8b234d6305bc5f9f01e..4782adcc987f7d5abe11650ce290096982034d26 100755 (executable)
--- a/test/t3
+++ b/test/t3
@@ -1,9 +1,11 @@
-#!/bin/sh
-downmod
-cegwprobe
-cegw --listen udp@127.0.0.1:10501
-cegw --listen udp@127.0.0.1:10501
-cegw --listen udp@127.0.0.1:10501
-cegw --listen udp@127.0.0.1:10501
-modprobe -r canethgw
+cegw vcan0 0.0.0.0:10501 127.0.0.1:10502 127.0.0.1:10503&
+JOBA=$!
+cegw vcan0 0.0.0.0:10601 127.0.0.1:10602 127.0.0.1:10503&
+JOBB=$!
+cegw vcan0 0.0.0.0:10701 127.0.0.1:10702 127.0.0.1:10503&
+JOBC=$!
+
+t0 vcan0 127.0.0.1:10501 127.0.0.1:10502 10 && kill $JOBA &
+t0 vcan0 127.0.0.1:10601 127.0.0.1:10602 10 && kill $JOBB &
+t0 vcan0 127.0.0.1:10701 127.0.0.1:10702 10 && kill $JOBC &
 
diff --git a/test/t4 b/test/t4
deleted file mode 100755 (executable)
index b7d2b40..0000000
--- a/test/t4
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/sh
-downmod
-cegwprobe
-cegw --listen udp@127.0.0.1:10501
-cegw --add -s can@vcan0 -d udp@127.0.0.1:10502
-cegw --add -s udp@127.0.0.1:10502 -d can@vcan0
-cegw --listen udp@127.0.0.1:10501
-
diff --git a/test/t7 b/test/t7
deleted file mode 100755 (executable)
index 4782adc..0000000
--- a/test/t7
+++ /dev/null
@@ -1,11 +0,0 @@
-cegw vcan0 0.0.0.0:10501 127.0.0.1:10502 127.0.0.1:10503&
-JOBA=$!
-cegw vcan0 0.0.0.0:10601 127.0.0.1:10602 127.0.0.1:10503&
-JOBB=$!
-cegw vcan0 0.0.0.0:10701 127.0.0.1:10702 127.0.0.1:10503&
-JOBC=$!
-
-t0 vcan0 127.0.0.1:10501 127.0.0.1:10502 10 && kill $JOBA &
-t0 vcan0 127.0.0.1:10601 127.0.0.1:10602 10 && kill $JOBB &
-t0 vcan0 127.0.0.1:10701 127.0.0.1:10702 10 && kill $JOBC &
-
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 */