File: acd-client.c

package info (click to toggle)
ell 0.81-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,344 kB
  • sloc: ansic: 61,546; sh: 4,896; makefile: 574
file content (151 lines) | stat: -rw-r--r-- 2,896 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Embedded Linux library
 * Copyright (C) 2020  Intel Corporation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <signal.h>
#include <sys/socket.h>
#include <linux/if_arp.h>
#include <getopt.h>

#include <ell/ell.h>

static enum l_acd_defend_policy policy = L_ACD_DEFEND_POLICY_DEFEND;

static void do_debug(const char *str, void *user_data)
{
	const char *prefix = user_data;

	l_info("%s%s", prefix, str);
}

static void signal_handler(uint32_t signo, void *user_data)
{
	switch (signo) {
	case SIGINT:
	case SIGTERM:
		l_info("Terminate");
		l_main_quit();
		break;
	}
}

static void event_handler(enum l_acd_event event, void *user_data)
{
	char *ip = user_data;

	switch (event) {
	case L_ACD_EVENT_CONFLICT:
		l_info("IP %s conflicts with another host", ip);
		break;
	case L_ACD_EVENT_AVAILABLE:
		l_info("IP %s is available", ip);
		break;
	case L_ACD_EVENT_LOST:
		l_info("IP %s has been lost", ip);
		l_main_quit();
		break;
	}

	if (policy == L_ACD_DEFEND_POLICY_NONE)
		l_main_quit();
}

static void usage(void)
{
	printf("acd-client usage\n"
		"Usage:\n");
	printf("\tacd-client <ifindex> <ip> [options]\n");
	printf("Options:\n"
		"\t-D, --defend [=policy] Keep running to defend address, "
			"optionally by policy:\n"
			"\t\tnone: never defend\n"
			"\t\tdefend: defend once (default)\n"
			"\t\tinfinite: defend infinitely\n"
		"\t-n, --no-probes       Disable initial probe stage\n"
		"\t-d, --debug           Run with debugging on\n");
}

static const struct option main_options[] = {
	{ "defend",	 optional_argument,	NULL, 'D' },
	{ "no-probes",	 no_argument,		NULL, 'n' },
	{ "debug",	 no_argument,		NULL, 'd' },
	{ }
};

int main(int argc, char *argv[])
{
	struct l_acd *acd;
	int ifindex;
	bool debug = false;
	bool no_probe = false;

	l_log_set_stderr();
	l_debug_enable("*");

	if (argc < 3) {
		usage();
		exit(0);
	}

	for (;;) {
		int opt;

		opt = getopt_long(argc - 2, argv + 2, "ndD::",
					main_options, NULL);
		if (opt < 0)
			break;

		switch (opt) {
		case 'D':
			if (optarg) {
				if (!strcmp(optarg, "infinite"))
					policy = L_ACD_DEFEND_POLICY_INFINITE;
				else if (!strcmp(optarg, "none"))
					policy = L_ACD_DEFEND_POLICY_NONE;
			}

			break;
		case 'n':
			no_probe = true;
			break;
		case 'd':
			debug = true;
			break;
		}
	}

	ifindex = atoi(argv[1]);

	if (!l_main_init())
		return -1;

	acd = l_acd_new(ifindex);
	l_acd_set_skip_probes(acd, no_probe);
	l_acd_set_defend_policy(acd, policy);

	if (debug) {
		l_acd_set_debug(acd, do_debug, "[ACD] ", NULL);
		l_info("Set debug");
	}
	l_acd_set_event_handler(acd, event_handler, argv[2], NULL);
	l_acd_start(acd, argv[2]);

	l_main_run_with_signal(signal_handler, NULL);

	l_acd_destroy(acd);
	l_main_exit();

	return 0;
}