From: Radek Matějka Date: Wed, 19 Dec 2012 06:05:53 +0000 (-0600) Subject: source cleaning, improving and adding tests X-Git-Url: https://rtime.felk.cvut.cz/gitweb/can-eth-gw.git/commitdiff_plain/92baeb4738b79ee30851234f448f5da8056f75f4 source cleaning, improving and adding tests 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. --- diff --git a/distro/customroot/sbin/badinputtest b/distro/customroot/sbin/badinputtest new file mode 120000 index 0000000..cb8419a --- /dev/null +++ b/distro/customroot/sbin/badinputtest @@ -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 index 0000000..8c22931 --- /dev/null +++ b/distro/customroot/sbin/cesend @@ -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 index 0000000..080a390 --- /dev/null +++ b/distro/customroot/sbin/i0 @@ -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 index 0000000..ab7e25d --- /dev/null +++ b/distro/customroot/sbin/i1 @@ -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 index 0000000..bbcfdcb --- /dev/null +++ b/distro/customroot/sbin/i2 @@ -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 index 0000000..9d59a31 --- /dev/null +++ b/distro/customroot/sbin/mkcegwdev @@ -0,0 +1,4 @@ +#/bin/sh +modprobe canethgw +mknod /dev/cegw c 10 59 + diff --git a/distro/customroot/sbin/t3 b/distro/customroot/sbin/t3 index ec201a3..0617866 120000 --- a/distro/customroot/sbin/t3 +++ b/distro/customroot/sbin/t3 @@ -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 index fb867d8..0000000 --- a/distro/customroot/sbin/t4 +++ /dev/null @@ -1 +0,0 @@ -../../../test/t4 \ No newline at end of file diff --git a/kernel/canethgw.c b/kernel/canethgw.c index 90b892d..fc660d7 100644 --- a/kernel/canethgw.c +++ b/kernel/canethgw.c @@ -1,25 +1,10 @@ -#define DEBUG - #include #include #include -#include -#include -#include -#include #include -#include #include -#include -#include -#include -#include #include -#include -#include -#include #include -#include #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; } diff --git a/kernel/canethgw.h b/kernel/canethgw.h index f15b688..65d9cc5 100644 --- a/kernel/canethgw.h +++ b/kernel/canethgw.h @@ -2,47 +2,6 @@ #define CANETHGW_H #include -#include - -/* 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 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 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 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 index 0000000..a73f091 --- /dev/null +++ b/test/src/Makefile @@ -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 index 0000000..0a6136c --- /dev/null +++ b/test/src/badinputtest.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#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 26026d7..b5c6da2 100755 --- 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 060f5c4..476ef76 100755 --- 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 0fb8ffc..d15a54e 100755 --- 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 9e33374..4782adc 100755 --- 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 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 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 & - diff --git a/utils/cegw/cegw.c b/utils/cegw/cegw.c index c0cb885..a0b5259 100644 --- a/utils/cegw/cegw.c +++ b/utils/cegw/cegw.c @@ -1,18 +1,12 @@ #include #include -#include #include -#include -#include #include #include #include #include -#include #include #include -#include -#include #include #include #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 ':' (:)", + [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 : N x (:)\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 : :\n" + " [list of additional udp recipients ]\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 */