/
shaft-client.c
231 lines (184 loc) · 5.02 KB
/
shaft-client.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* $OpenBSD$ */
/*
* Copyright (c) 2010 Mathieu Sauve-Frankel <msf@openbsd.org>
* Copyright (c) 2001-2004 Damien Miller <djm@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/param.h>
#include <sys/statvfs.h>
#include <sys/uio.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "xmalloc.h"
#include "buffer.h"
#include "log.h"
#include "atomicio.h"
#include "progressmeter.h"
#include "misc.h"
#include "shaft.h"
#include "shaft-common.h"
#include "shaft-client.h"
/* Minimum amount of data to read at a time */
#define MIN_READ_SIZE 512
/* Maximum depth to descend in directory trees */
#define MAX_DIR_DEPTH 64
struct shaft_conn {
int fd_in;
int fd_out;
u_int num_requests;
u_int version;
u_int msg_id;
};
static void
send_msg(int fd, Buffer *m)
{
u_char mlen[4];
struct iovec iov[2];
if (buffer_len(m) > SHAFT_MAX_MSG_LENGTH)
fatal("Outbound message too long %u", buffer_len(m));
/* Send length first */
put_u32(mlen, buffer_len(m));
iov[0].iov_base = mlen;
iov[0].iov_len = sizeof(mlen);
iov[1].iov_base = buffer_ptr(m);
iov[1].iov_len = buffer_len(m);
if (atomiciov(writev, fd, iov, 2) != buffer_len(m) + sizeof(mlen))
fatal("Couldn't send packet: %s", strerror(errno));
buffer_clear(m);
}
static void
get_msg(int fd, Buffer *m)
{
u_int msg_len;
buffer_append_space(m, 4);
if (atomicio(read, fd, buffer_ptr(m), 4) != 4) {
if (errno == EPIPE)
fatal("Connection closed");
else
fatal("Couldn't read packet: %s", strerror(errno));
}
msg_len = buffer_get_int(m);
if (msg_len > SHAFT_MAX_MSG_LENGTH)
fatal("Received message too long %u", msg_len);
buffer_append_space(m, msg_len);
if (atomicio(read, fd, buffer_ptr(m), msg_len) != msg_len) {
if (errno == EPIPE)
fatal("Connection closed");
else
fatal("Read packet: %s", strerror(errno));
}
}
static u_int
get_status(int fd, u_int expected_id)
{
Buffer msg;
u_int type, id, status;
buffer_init(&msg);
get_msg(fd, &msg);
type = buffer_get_char(&msg);
id = buffer_get_int(&msg);
if (id != expected_id)
fatal("ID mismatch (%u != %u)", id, expected_id);
if (type != SHAFT_STATUS)
fatal("Expected SHAFT_STATUS(%u) packet, got %u",
SHAFT_STATUS, type);
status = buffer_get_int(&msg);
buffer_free(&msg);
debug3("SHAFT_STATUS %u", status);
return(status);
}
struct shaft_conn *
do_init(int fd_in, int fd_out, struct shaft_flow *flow)
{
u_int type, version;
Buffer msg;
struct shaft_conn *ret;
buffer_init(&msg);
buffer_put_char(&msg, SHAFT_INIT);
buffer_put_int(&msg, SHAFT_VERSION);
buffer_put_cstring(&msg, flow->local);
send_msg(fd_out, &msg);
buffer_clear(&msg);
get_msg(fd_in, &msg);
/* Expecting a VERSION reply */
if ((type = buffer_get_char(&msg)) != SHAFT_VERSION) {
error("Invalid packet back from SHAFT_INIT (type %u)",
version);
buffer_free(&msg);
return(NULL);
}
version = buffer_get_int(&msg);
flow->dst = buffer_get_cstring(&msg, NULL);
if (flow->dst == NULL) {
error("Invalid packet back from SHAFT_INIT remote addr is null");
buffer_free(&msg);
return(NULL);
}
debug2("Remote version: %u", version);
debug2("Remote Address: %s", flow->dst);
buffer_free(&msg);
ret = xmalloc(sizeof(*ret));
ret->fd_in = fd_in;
ret->fd_out = fd_out;
ret->version = version;
ret->msg_id = 1;
return(ret);
}
void
do_req_sa(struct shaft_conn *conn, struct shaft_sa *sa)
{
u_int id, type;
Buffer msg;
id = conn->msg_id++;
buffer_init(&msg);
buffer_put_char(&msg, SHAFT_REQUEST_SA);
buffer_put_int(&msg, id);
send_msg(conn->fd_out, &msg);
buffer_clear(&msg);
get_msg(conn->fd_in, &msg);
if ((type = buffer_get_char(&msg)) != SHAFT_REPLY_SA) {
error("Invalid packet back from SHAFT_REQUEST_SA");
buffer_free(&msg);
}
decode_sa(&msg, sa);
if (sa == NULL)
error("Invalide packet back from SHAFT_REPLY_SA");
buffer_free(&msg);
}
void
do_add_sa(struct shaft_conn *conn, char * rules)
{
u_int status, id, type;
Buffer msg;
id = conn->msg_id++;
buffer_init(&msg);
buffer_put_char(&msg, SHAFT_ADD_SA);
buffer_put_int(&msg, id);
send_msg(conn->fd_out, &msg);
add_rules(rules);
buffer_clear(&msg);
status = get_status(conn->fd_in, id);
if (status != SHAFT_OK)
fatal("Couldn't add SA");
}