Skip to content
Snippets Groups Projects
Commit ffe6faff authored by Denis Filatov's avatar Denis Filatov
Browse files

test generator, openWRT package

parent a5573d2b
No related branches found
No related tags found
No related merge requests found
Makefile 100644 → 100755
......@@ -34,9 +34,12 @@ MAKE_FLAGS += CFLAGS="$(TARGET_CFLAGS) -Wall -I include"
define Package/its_bridge/install
$(INSTALL_DIR) $(1)/usr/bin/
$(INSTALL_DIR) $(1)/etc/
$(INSTALL_DIR) $(1)/etc/ssl/{certs,private}
$(CP) $(PKG_BUILD_DIR)/{its_bridge_client,its_bridge_server,its_web_server_config} $(1)/usr/bin
$(CP) -r ./etc $(1)/
$(CP) -r ./etc/openwrt/* $(1)/etc/
$(CP) ./certs/*.pem $(1)/etc/ssl/certs
$(CP) ./certs/*.key $(1)/etc/ssl/private
endef
......
config general 'nic'
option its 'eth0.1'
option udp 'eth0.2'
config service 'udp'
option port '5000'
option send '239.0.102.102'
list group '239.0.101.101'
#!/bin/sh /etc/rc.common
# Copyright (C) 2006-2011 OpenWrt.org
SERVICE_USE_PID=1
START=49
open_its_port() {
port=`uci get its_bridge.udp.port`
[ -z "$port" ] && port=5000
for p in 0 1 2 3 4 5 6 7 8 9; do
v=`uci get glfw.@opening[$p].port 2>/dev/null`
if [ "$v" == "$port" ]; then
echo "Port $port already opened"
break
fi
done
if [ "$v" != "$port" ]; then
v=`uci add glfw opening`
uci set "glfw.${v}.port=$port"
uci set "glfw.${v}.name=ITS Bridge"
uci set "glfw.${v}.proto=UDP"
uci set "glfw.${v}.status=Enabled"
/usr/bin/glfw.sh
fi
}
start() {
mkdir -m 0755 -p /var/log/its_bridge
# open port
open_its_port
service_start /usr/bin/its_bridge_client -c /etc/its_bridge/client.conf >> /var/log/its_bridge/client.log 2>&1
service_start /usr/bin/its_bridge_server -c /etc/its_bridge/server.conf >> /var/log/its_bridge/server.log 2>&1
service_start /usr/bin/its_web_server_config -c /etc/its_bridge/webserver.conf >> /var/log/its_bridge/web.log 2>&1
}
stop() {
service_stop /usr/bin/its_bridge_client
service_stop /usr/bin/its_bridge_server
service_stop /usr/bin/its_web_server_config
}
# client.conf sample
daemon_mode=1
mac_address=080027d6c900
its_nic=eth0.1
udp_nic=eth0.2
udp_address=239.0.102.102
udp_protocol=multicast
udp_port=5000
# server.conf sample
daemon_mode=0
mac_address=f8cab8083918
its_nic=eth0.1
udp_nic=eth0.2
udp_address=239.0.101.101
udp_protocol=multicast
udp_port=5000
# webserver.conf sample
daemon_mode=0
realm=vendors
login=guest
password=password
https_port=8888
cert_pem=/etc/ssl/certs/its_bridge.pem
cert_key=/etc/ssl/private/its_bridge.key
conf_path=/etc/its_bridge
......@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
......@@ -46,6 +47,7 @@ int32_t parse_config_file(const char* p_config_file);
void free_config_file_resources(void);
char* bin2hex(char* p_hex, size_t p_hlen, const uint8_t* p_bin, size_t p_blen);
char* hex2bin(char* p_bin, size_t p_blen, const char* p_hex, size_t p_hlen);
void usage(const char* p_progname, const uint8_t p_role);
......
......@@ -15,6 +15,7 @@ extern uint16_t udp_port;
extern bool daemonized;
extern char* pid_file;
extern state_t state;
extern int test_generate;
pcap_t* device = NULL;
int32_t socket_hd = -1;
......@@ -174,56 +175,78 @@ int main(const int32_t p_argc, char* const p_argv[]) {
inet_pton(AF_INET, udp_address, &remote_addr.sin_addr);
remote_addr.sin_port = htons(udp_port);
/* Prepare ITS traffic capture */
char error_buffer[PCAP_ERRBUF_SIZE];
bpf_u_int32 net, mask;
if (pcap_lookupnet(its_nic, &net, &mask, error_buffer) != 0) {
fprintf(stderr, "Failed to fetch newtork address for device %s.\n", its_nic);
close(socket_hd);
goto error;
}
printf("Device %s Network address: %d.\n", its_nic, net);
device = pcap_open_live(its_nic, 65535/*64*1024*/, 1, 100, error_buffer);
if (device == NULL) {
fprintf(stderr, "Failed to open device %s.\n", its_nic);
close(socket_hd);
goto error;
}
/* Setup filter */
char filter[128] = {0};
char* mac_bc = "ffffffffffff";
/* Accept ITS broadcasted messages */
printf("mac_bc: %s.\n", mac_bc);
printf("mac_address: %s.\n", mac_address);
strcpy(filter, "ether dst ");
strcat(filter, mac_bc);
if (strlen(mac_address) != 0) {
/* Accept ITS messages sent by this component */
strcat(filter, " and ether src ");
strcat(filter, mac_address);
}
strcat(filter, " and ether proto 0x8947");
/* Log final PCAP filter */
printf("Filter: %s.\n", filter);
{
struct bpf_program f = {0};
if (pcap_compile(device, &f, filter, 1, PCAP_NETMASK_UNKNOWN) != 0) {
fprintf(stderr, "Failed to compile PCAP filter.\n");
pcap_close(device);
if (test_generate) {
u_char buf[32];
memset(buf, 0, sizeof(buf));
struct ether_header* eth_header = (struct ether_header*)buf;
hex2bin ((char*)eth_header->ether_shost, sizeof(eth_header->ether_shost), (const char*)mac_address, 12);
memset (eth_header->ether_dhost, 0, sizeof(eth_header->ether_dhost));
eth_header->ether_type = htons(0x8947);
struct pcap_pkthdr pkthdr = {
{0,0},
sizeof(buf),
sizeof(buf)
};
printf("Generate test packets and send to %s:%d\n", inet_ntoa(remote_addr.sin_addr), htons(remote_addr.sin_port));
while (state != _exiting) {
sleep(1);
pcap_message_callback(NULL, &pkthdr, buf);
}
printf("Stop packet generation\n");
} else {
/* Prepare ITS traffic capture */
char error_buffer[PCAP_ERRBUF_SIZE];
bpf_u_int32 net, mask;
if (pcap_lookupnet(its_nic, &net, &mask, error_buffer) != 0) {
fprintf(stderr, "Failed to fetch newtork address for device %s.\n", its_nic);
close(socket_hd);
goto error;
} else {
if (pcap_setfilter(device, &f) != 0) {
fprintf(stderr, "Failed to set PCAP filter.\n");
}
printf("Device %s Network address: %d.\n", its_nic, net);
device = pcap_open_live(its_nic, 65535/*64*1024*/, 1, 100, error_buffer);
if (device == NULL) {
fprintf(stderr, "Failed to open device %s.\n", its_nic);
close(socket_hd);
goto error;
}
/* Setup filter */
char filter[128] = {0};
char* mac_bc = "ffffffffffff";
/* Accept ITS broadcasted messages */
printf("mac_bc: %s.\n", mac_bc);
printf("mac_address: %s.\n", mac_address);
strcpy(filter, "ether dst ");
strcat(filter, mac_bc);
if (strlen(mac_address) != 0) {
/* Accept ITS messages sent by this component */
strcat(filter, " and ether src ");
strcat(filter, mac_address);
}
strcat(filter, " and ether proto 0x8947");
/* Log final PCAP filter */
printf("Filter: %s.\n", filter);
{
struct bpf_program f = {0};
if (pcap_compile(device, &f, filter, 1, PCAP_NETMASK_UNKNOWN) != 0) {
fprintf(stderr, "Failed to compile PCAP filter.\n");
pcap_close(device);
goto error;
} else {
if (pcap_setfilter(device, &f) != 0) {
fprintf(stderr, "Failed to set PCAP filter.\n");
pcap_close(device);
goto error;
}
}
pcap_freecode(&f);
}
/* Loop on incoming ITS traffic */
pcap_loop(device, -1, pcap_message_callback, NULL);
if (device != NULL) {
pcap_close(device);
}
pcap_freecode(&f);
}
/* Loop on incoming ITS traffic */
pcap_loop(device, -1, pcap_message_callback, NULL);
if (device != NULL) {
pcap_close(device);
}
close(socket_hd);
......
src/utils.c 100644 → 100755
......@@ -20,10 +20,12 @@ uint16_t https_port = 8888;
char* pid_file = NULL;
state_t state = _starting;
int test_generate = 0;
int32_t parse_params(const int p_argc, char* const p_argv[]) {
int option;
int args_left;
while ((option = getopt(p_argc, p_argv, "a:c:dhi:m:n:p:u:v")) != -1) {
while ((option = getopt(p_argc, p_argv, "a:c:dhi:m:n:p:u:vg")) != -1) {
switch (option) {
case 'a':
udp_protocol = optarg;
......@@ -49,6 +51,9 @@ int32_t parse_params(const int p_argc, char* const p_argv[]) {
case 'u':
udp_address = optarg;
break;
case 'g':
test_generate = 1;
break;
case 'h':
/*No break;*/
case 'v':
......@@ -199,6 +204,46 @@ char* bin2hex(char* p_hex, size_t p_hlen, const uint8_t* p_bin, size_t p_blen) {
return p_hex + p_blen * 2;
}
char * hex2bin(char * bin, size_t blen, const char * hex, size_t hlen)
{
// check
const unsigned char * h = (const unsigned char*)hex;
const unsigned char * e = h+hlen;
unsigned char * b = (unsigned char*)bin;
int n = 0;
while (h<e){
unsigned char ch = *h++;
if (isspace((int)(ch))) continue;
if (ch >= '0' && ch <= '9') continue;
if (ch >= 'A' && ch <= 'F') continue;
if (ch >= 'a' && ch <= 'f') continue;
return NULL;
}
h = hex;
while (h < e){
unsigned char ch = *h++;
if (ch >= '0' && ch <= '9') ch -= '0';
else if (ch >= 'A' && ch <= 'F') ch -= 'A' - 0x0A;
else if (ch >= 'a' && ch <= 'f') ch -= 'a' - 0x0A;
else continue;
if (!n){
*b = ch;
}
else{
unsigned char ch1 = *b;
*b++ = (ch1 << 4) | ch;
}
n = !n;
}
if (n){
unsigned char ch1 = *b;
*b++ = (ch1 << 4);
n = 0;
}
return b;
}
void usage(const char* p_progname, const uint8_t p_role) {
if ((p_role == 0) || (p_role == 1)) {
fprintf(stderr, "Usage: %s -a<UDP protocol> -i<NIC ITS> -m<mac_address> -n<NIC ITS> -p<udp_port> -u<udp_address> [-d] [-hv]\n", p_progname);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment