summaryrefslogtreecommitdiff
path: root/accel-pppd/ppp/ipcp_opt_wins.c
blob: c9a3bb5cb8e6fb30e60f62aa3978a7bf7a8bde96 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#include "ppp.h"
#include "ppp_ipcp.h"
#include "log.h"
#include "ipdb.h"
#include "events.h"

#include "memdebug.h"

static in_addr_t conf_wins1;
static in_addr_t conf_wins2;

static struct ipcp_option_t *wins1_init(struct ppp_ipcp_t *ipcp);
static struct ipcp_option_t *wins2_init(struct ppp_ipcp_t *ipcp);
static void wins_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt);
static int wins_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr);
static int wins_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr);
static int wins_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr);
static void wins1_print(void (*print)(const char *fmt, ...), struct ipcp_option_t *, uint8_t *ptr);
static void wins2_print(void (*print)(const char *fmt, ...), struct ipcp_option_t *, uint8_t *ptr);

struct wins_option_t
{
	struct ipcp_option_t opt;
	in_addr_t addr;
};

static struct ipcp_option_handler_t wins1_opt_hnd =
{
	.init = wins1_init,
	.send_conf_req = wins_send_conf_req,
	.send_conf_nak = wins_send_conf_nak,
	.recv_conf_req = wins_recv_conf_req,
	.free = wins_free,
	.print = wins1_print,
};

static struct ipcp_option_handler_t wins2_opt_hnd =
{
	.init = wins2_init,
	.send_conf_req = wins_send_conf_req,
	.send_conf_nak = wins_send_conf_nak,
	.recv_conf_req = wins_recv_conf_req,
	.free = wins_free,
	.print = wins2_print,
};

static struct ipcp_option_t *wins1_init(struct ppp_ipcp_t *ipcp)
{
	struct wins_option_t *wins_opt = _malloc(sizeof(*wins_opt));

	memset(wins_opt, 0, sizeof(*wins_opt));
	wins_opt->opt.id = CI_WINS1;
	wins_opt->opt.len = 6;
	wins_opt->addr = conf_wins1;

	return &wins_opt->opt;
}

static struct ipcp_option_t *wins2_init(struct ppp_ipcp_t *ipcp)
{
	struct wins_option_t *wins_opt = _malloc(sizeof(*wins_opt));

	memset(wins_opt, 0, sizeof(*wins_opt));
	wins_opt->opt.id = CI_WINS2;
	wins_opt->opt.len = 6;
	wins_opt->addr = conf_wins2;

	return &wins_opt->opt;
}

static void wins_free(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt)
{
	struct wins_option_t *wins_opt = container_of(opt, typeof(*wins_opt), opt);

	_free(wins_opt);
}

static int wins_send_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr)
{
	return 0;
}

static int wins_send_conf_nak(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr)
{
	struct wins_option_t *wins_opt = container_of(opt, typeof(*wins_opt), opt);
	struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t *)ptr;
	opt32->hdr.id = wins_opt->opt.id;
	opt32->hdr.len = 6;
	opt32->val = wins_opt->addr;
	return 6;
}

static int wins_recv_conf_req(struct ppp_ipcp_t *ipcp, struct ipcp_option_t *opt, uint8_t *ptr)
{
	struct wins_option_t *wins_opt = container_of(opt, typeof(*wins_opt), opt);
	struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t *)ptr;

	if (opt32->hdr.len != 6)
		return IPCP_OPT_REJ;

	if (!wins_opt->addr) {
		wins_opt->addr = opt32->val;
		return IPCP_OPT_ACK;
	}

	if (wins_opt->addr == opt32->val)
		return IPCP_OPT_ACK;

	return IPCP_OPT_NAK;
}

static void wins1_print(void (*print)(const char *fmt, ...), struct ipcp_option_t *opt, uint8_t *ptr)
{
	struct wins_option_t *wins_opt = container_of(opt, typeof(*wins_opt), opt);
	struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t *)ptr;
	struct in_addr in;

	if (ptr)
		in.s_addr = opt32->val;
	else
		in.s_addr = wins_opt->addr;

	print("<wins1 %s>", inet_ntoa(in));
}

static void wins2_print(void (*print)(const char *fmt, ...), struct ipcp_option_t *opt, uint8_t *ptr)
{
	struct wins_option_t *wins_opt = container_of(opt, typeof(*wins_opt), opt);
	struct ipcp_opt32_t *opt32 = (struct ipcp_opt32_t *)ptr;
	struct in_addr in;

	if (ptr)
		in.s_addr = opt32->val;
	else
		in.s_addr = wins_opt->addr;

	print("<wins2 %s>", inet_ntoa(in));
}

static void ev_wins(struct ev_wins_t *ev)
{
	struct wins_option_t *wins_opt;
	struct ppp_t *ppp;

	if (!ev->ses->ctrl->ppp)
		return;

	ppp = container_of(ev->ses, typeof(*ppp), ses);

	wins_opt = container_of(ipcp_find_option(ppp, &wins1_opt_hnd), typeof(*wins_opt), opt);
	wins_opt->addr = ev->wins1;

	wins_opt = container_of(ipcp_find_option(ppp, &wins2_opt_hnd), typeof(*wins_opt), opt);
	wins_opt->addr = ev->wins2;
}

static void load_config(void)
{
	char *opt;

	opt = conf_get_opt("wins", "wins1");
	if (opt)
		conf_wins1 = inet_addr(opt);

	opt = conf_get_opt("wins", "wins2");
	if (opt)
		conf_wins2 = inet_addr(opt);
}

static void wins_opt_init()
{
	ipcp_option_register(&wins1_opt_hnd);
	ipcp_option_register(&wins2_opt_hnd);

	load_config();
	triton_event_register_handler(EV_CONFIG_RELOAD, (triton_event_func)load_config);

	triton_event_register_handler(EV_WINS, (triton_event_func)ev_wins);
}

DEFINE_INIT(4, wins_opt_init);