2016-03-29 06:24:54 +00:00
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "nest/locks.h"
|
|
|
|
#include "nest/route.h"
|
|
|
|
#include "lib/krt.h"
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2016-04-01 07:38:14 +00:00
|
|
|
static ip_addr
|
|
|
|
parse_addr(char *src)
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
2016-04-01 07:38:14 +00:00
|
|
|
ip_addr dst;
|
|
|
|
if (!ipa_pton(src, &dst))
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
|
|
|
printf("Invalid address %s\n", src);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2016-04-01 07:38:14 +00:00
|
|
|
return dst;
|
2016-03-29 06:24:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 07:38:14 +00:00
|
|
|
static uint
|
|
|
|
parse_uint(const char *src)
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
|
|
|
errno = 0;
|
2016-04-01 07:38:14 +00:00
|
|
|
uint dst = strtoul(src, NULL, 10);
|
2016-03-29 06:24:54 +00:00
|
|
|
if (errno)
|
|
|
|
{
|
|
|
|
printf("Invalid number %s\n", src);
|
|
|
|
exit(-1);
|
|
|
|
}
|
2016-04-01 07:38:14 +00:00
|
|
|
return dst;
|
2016-03-29 06:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
err_hook(sock *s, int err)
|
|
|
|
{
|
|
|
|
if (!err)
|
|
|
|
{
|
|
|
|
printf("Sock EOF \n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:46:02 +00:00
|
|
|
perror("err_hook: ");
|
2016-03-29 06:24:54 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-04-06 13:46:02 +00:00
|
|
|
socktest_open(sock *s)
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
|
|
|
if (sk_open(s) < 0)
|
2016-04-05 07:09:57 +00:00
|
|
|
{
|
|
|
|
perror(s->err);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-29 06:24:54 +00:00
|
|
|
|
|
|
|
if (cf_mcast)
|
2016-04-01 07:38:14 +00:00
|
|
|
{
|
|
|
|
sk_setup_multicast(s); /* transmission */
|
|
|
|
sk_join_group(s, s->daddr); /* reception */
|
|
|
|
}
|
|
|
|
|
2016-03-29 06:24:54 +00:00
|
|
|
if (cf_bcast)
|
|
|
|
sk_setup_broadcast(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
sock *
|
2016-04-06 13:46:02 +00:00
|
|
|
socktest_parse_args(int argc, char *argv[], int is_send)
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
|
|
|
int is_recv = !is_send;
|
2016-04-05 07:10:28 +00:00
|
|
|
const char *opt_list = is_send ? "bumi:l:p:v:t:c:B:" : "bum:i:l:p:v:t:c:B:";
|
2016-03-29 06:24:54 +00:00
|
|
|
int c;
|
|
|
|
|
2016-04-01 07:38:14 +00:00
|
|
|
/* Set defaults */
|
|
|
|
uint port = PKT_PORT;
|
2016-03-29 06:24:54 +00:00
|
|
|
cf_value = PKT_VALUE;
|
2016-04-01 07:38:14 +00:00
|
|
|
cf_mcast = cf_bcast = cf_bind = cf_count = counter = 0;
|
2016-03-29 06:24:54 +00:00
|
|
|
|
2016-04-01 07:38:14 +00:00
|
|
|
/* Create socket */
|
2016-03-29 06:24:54 +00:00
|
|
|
sock *s = sk_new(&root_pool);
|
|
|
|
s->type = SK_IP;
|
|
|
|
s->err_hook = err_hook;
|
|
|
|
|
|
|
|
while ((c = getopt(argc, argv, opt_list)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'u':
|
|
|
|
s->type = SK_UDP;
|
|
|
|
break;
|
2016-04-01 07:38:14 +00:00
|
|
|
case 'c':
|
|
|
|
cf_count = parse_uint(optarg);
|
|
|
|
break;
|
2016-03-29 06:24:54 +00:00
|
|
|
case 'm':
|
|
|
|
cf_mcast = 1;
|
|
|
|
if (is_recv)
|
2016-04-01 07:38:14 +00:00
|
|
|
s->daddr = parse_addr(optarg);
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
cf_bcast = 1;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
s->iface = if_get_by_name(optarg);
|
2016-04-01 07:38:14 +00:00
|
|
|
s->iface->index = if_nametoindex(optarg);
|
2016-04-05 07:12:59 +00:00
|
|
|
if (s->iface->index == 0)
|
|
|
|
{
|
|
|
|
printf("No interface exists with the name %s \n", optarg);
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
cf_bind = 1;
|
2016-04-01 07:38:14 +00:00
|
|
|
s->flags |= SKF_BIND;
|
|
|
|
/* fall through */
|
|
|
|
case 'l':
|
|
|
|
if (ipa_nonzero(s->saddr))
|
|
|
|
printf("Redefine source address, don't use -l and -B together \n");
|
|
|
|
s->saddr = parse_addr(optarg);
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2016-04-01 07:38:14 +00:00
|
|
|
port = parse_uint(optarg);
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
2016-04-01 07:38:14 +00:00
|
|
|
cf_value = parse_uint(optarg);
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
2016-04-06 13:23:15 +00:00
|
|
|
s->ttl = parse_uint(optarg);
|
2016-03-29 06:24:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto usage;
|
|
|
|
}
|
|
|
|
|
2016-04-06 13:46:02 +00:00
|
|
|
if (is_recv && s->type == SK_UDP)
|
2016-03-29 06:24:54 +00:00
|
|
|
s->sport = port;
|
|
|
|
else
|
|
|
|
s->dport = port;
|
|
|
|
|
|
|
|
if (optind + is_send != argc)
|
|
|
|
goto usage;
|
|
|
|
|
|
|
|
if (is_send)
|
2016-04-01 07:38:14 +00:00
|
|
|
s->daddr = parse_addr(argv[optind]);
|
2016-03-29 06:24:54 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
|
|
|
|
usage:
|
2016-04-01 07:38:14 +00:00
|
|
|
printf("Usage: %s [-u] [-c count] [-m%s|-b] [-B bind_addr] [-i iface] [-l fake_local_addr] [-p port] [-v value] [-t ttl]%s\n",
|
2016-03-29 06:24:54 +00:00
|
|
|
argv[0], is_recv ? " maddr" : "", is_send ? " daddr" : "");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-04-01 07:38:14 +00:00
|
|
|
static void
|
2016-04-06 13:46:02 +00:00
|
|
|
scan_interfaces(void)
|
2016-04-01 07:38:14 +00:00
|
|
|
{
|
|
|
|
/* create mockup config */
|
|
|
|
struct config *c = config_alloc("mockup");
|
|
|
|
init_list(&c->protos);
|
|
|
|
cfg_mem = c->mem;
|
|
|
|
new_config = c;
|
|
|
|
new_config->master_rtc = mb_allocz(&root_pool, sizeof(struct rtable_config));
|
|
|
|
|
|
|
|
/* create mockup device protocol */
|
|
|
|
protos_build();
|
|
|
|
proto_build(&proto_unix_iface);
|
|
|
|
struct proto_config *kif_config = kif_init_config(SYM_PROTO);
|
|
|
|
kif_config->table = new_config->master_rtc;
|
|
|
|
struct proto *krt = proto_unix_iface.init(kif_config);
|
|
|
|
|
|
|
|
/* scan interfaces */
|
|
|
|
proto_unix_iface.start(krt);
|
|
|
|
}
|
|
|
|
|
2016-03-29 06:24:54 +00:00
|
|
|
void
|
2016-04-06 13:46:02 +00:00
|
|
|
socktest_bird_init(void)
|
2016-03-29 06:24:54 +00:00
|
|
|
{
|
2016-04-05 07:12:28 +00:00
|
|
|
log_switch(1, NULL, NULL);
|
2016-03-29 06:24:54 +00:00
|
|
|
resource_init();
|
|
|
|
io_init();
|
|
|
|
if_init();
|
2016-04-06 13:46:02 +00:00
|
|
|
scan_interfaces();
|
2016-03-29 06:24:54 +00:00
|
|
|
}
|