--- /dev/null
+/home/monter/project/can-eth-gw/test/src/badinputtest
\ No newline at end of file
--- /dev/null
+../../../utils/cesend/cesend
\ No newline at end of file
--- /dev/null
+/home/monter/project/can-eth-gw/test/i0
\ No newline at end of file
--- /dev/null
+/home/monter/project/can-eth-gw/test/i1
\ No newline at end of file
--- /dev/null
+/home/monter/project/can-eth-gw/test/i2
\ No newline at end of file
--- /dev/null
+#/bin/sh
+modprobe canethgw
+mknod /dev/cegw c 10 59
+
-../../../test/t3
\ No newline at end of file
+/home/monter/project/can-eth-gw/test/t3
\ No newline at end of file
+++ /dev/null
-../../../test/t4
\ No newline at end of file
-#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");
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;
/**
* cegw_can2udp - performs can->udp routing
*
- * Runs as a thread.
+ * This function is run as a thread.
*/
static int cegw_can2udp(void* data)
{
}
}
- 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);
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;
}
static struct miscdevice cegw_device = {
.minor = MISC_DYNAMIC_MINOR,
- .name = "cegw",
+ .name = "canethgw",
.fops = &cegw_fops
};
{
misc_deregister(&cegw_device);
- /* ToDo: exit threads */
return;
}
#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
{
#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 */
--- /dev/null
+#!/bin/sh
+cegwprobe && mkcegwdev
+i1
+
--- /dev/null
+#!/bin/sh
+cegw vcan0 0.0.0.0:10501 127.0.0.1:10502
+
--- /dev/null
+#!/bin/sh
+cegw vcan0 0.0.0.0:10501 127.0.0.1:10502 127.0.0.1:10503
+
--- /dev/null
+PROGRAM=badinputtest
+
+all:
+ gcc -Wall -o${PROGRAM} -I../../kernel badinputtest.c
+debug:
+ gcc -Wall -ggdb -D_DEBUG -o${PROGRAM} -I../../kernel badinputtest.c
+
--- /dev/null
+#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;
+}
+
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: "
#!/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
#!/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
-#!/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 &
+++ /dev/null
-#!/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
-
+++ /dev/null
-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 &
-
#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"
{
CEGW_ERR_UNKNOWN,
CEGW_ERR_COLON,
+ CEGW_ERR_UNEXLEN,
CEGW_ERR_ATON,
CEGW_ERR_PORT
};
{
[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)
{
/**
* 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
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) {
/* 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) {
/* get port */
/* ToDo: handle overflow */
-
if (sscanf(delim, ":%hu", port) != 1) {
cegw_errno = CEGW_ERR_PORT;
return -1;
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 */
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);
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 */