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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <poll.h>
#ifdef DEBUG
#include <time.h>
#endif
#include "isns_server.h"
#include "isns_cache.h"
#include "isns_pdu.h"
#include "isns_msgq.h"
#include "isns_func.h"
#include "isns_log.h"
#include "isns_provider.h"
/* external functions */
#ifdef DEBUG
extern void dump_pdu1(isns_pdu_t *);
extern int verbose_tc;
#endif
extern boolean_t time_to_exit;
void *
isns_connection(
void *arg
)
{
int status = 0;
conn_arg_t *conn;
isns_pdu_t *pdu, *combined_pdu, *new_combined_pdu;
uint8_t *payload_ptr;
size_t pdu_sz;
conn = (conn_arg_t *)arg;
conn->out_packet.pdu = NULL;
conn->out_packet.sz = 0;
combined_pdu = NULL;
pdu = NULL;
while (status == 0 &&
time_to_exit == B_FALSE &&
isns_rcv_pdu(conn->so, &pdu, &pdu_sz, ISNS_RCV_TIMEOUT) > 0) {
uint16_t flags = pdu->flags;
if (ISNS_MSG_RECEIVED_ENABLED()) {
char buf[INET6_ADDRSTRLEN];
struct sockaddr_storage *ssp = &conn->ss;
struct sockaddr_in *sinp = (struct sockaddr_in *)ssp;
if (ssp->ss_family == AF_INET) {
(void) inet_ntop(AF_INET,
(void *)&(sinp->sin_addr),
buf, sizeof (buf));
} else {
(void) inet_ntop(AF_INET6,
(void *)&(sinp->sin_addr),
buf, sizeof (buf));
}
ISNS_MSG_RECEIVED((uintptr_t)buf);
}
if ((flags & ISNS_FLAG_FIRST_PDU) == ISNS_FLAG_FIRST_PDU) {
if (combined_pdu != NULL || pdu->seq != 0) {
goto conn_done;
}
combined_pdu = pdu;
pdu = NULL;
} else {
if (combined_pdu == NULL ||
combined_pdu->func_id != pdu->func_id ||
combined_pdu->xid != pdu->xid ||
(combined_pdu->seq + 1) != pdu->seq) {
/* expect the first pdu, the same tranx id */
/* and the next sequence id */
goto conn_done;
}
new_combined_pdu = (isns_pdu_t *)malloc(
ISNSP_HEADER_SIZE +
combined_pdu->payload_len +
pdu->payload_len);
if (new_combined_pdu == NULL) {
goto conn_done;
}
(void) memcpy((void *)new_combined_pdu,
(void *)combined_pdu,
ISNSP_HEADER_SIZE + combined_pdu->payload_len);
payload_ptr = new_combined_pdu->payload +
combined_pdu->payload_len;
(void) memcpy((void *)payload_ptr,
(void *)pdu->payload,
pdu->payload_len);
new_combined_pdu->seq = pdu->seq;
free(combined_pdu);
combined_pdu = new_combined_pdu;
free(pdu);
pdu = NULL;
}
if ((flags & ISNS_FLAG_LAST_PDU) == ISNS_FLAG_LAST_PDU) {
#ifdef DEBUG
time_t t;
clock_t c;
dump_pdu1(combined_pdu);
if (verbose_tc != 0) {
t = time(NULL);
c = clock();
}
#endif
conn->in_packet.pdu = combined_pdu;
conn->out_packet.pl = 0;
conn->ec = 0;
if (packet_split_verify(conn) == 0) {
(void) cache_lock(conn->lock);
status = conn->handler(conn);
conn->ec = cache_unlock(conn->lock, conn->ec);
}
switch (status) {
case -1:
/* error */
break;
case 0:
status = isns_response(conn);
isnslog(LOG_DEBUG, "isns_connection",
"Response status: %d.", status);
if (ISNS_MSG_RESPONDED_ENABLED()) {
char buf[INET6_ADDRSTRLEN];
struct sockaddr_storage *ssp =
&conn->ss;
struct sockaddr_in *sinp =
(struct sockaddr_in *)ssp;
if (ssp->ss_family == AF_INET) {
(void) inet_ntop(AF_INET,
(void *)&(sinp->sin_addr),
buf, sizeof (buf));
} else {
(void) inet_ntop(AF_INET6,
(void *)&(sinp->sin_addr),
buf, sizeof (buf));
}
ISNS_MSG_RESPONDED((uintptr_t)buf);
}
break;
default:
/* no need to send response message */
status = 0;
break;
}
#ifdef DEBUG
if (verbose_tc != 0) {
t = time(NULL) - t;
c = clock() - c;
printf("time %d clock %.4lf -msg response\n",
t, c / (double)CLOCKS_PER_SEC);
}
#endif
free(combined_pdu);
combined_pdu = NULL;
}
}
conn_done:
if (pdu != NULL) {
free(pdu);
}
if (combined_pdu != NULL) {
free(combined_pdu);
}
(void) close(conn->so);
(void) free(conn->out_packet.pdu);
(void) free(conn);
/* decrease the thread ref count */
dec_thr_count();
return (NULL);
}
/* the iSNS server port watcher */
void *
isns_port_watcher(
/* LINTED E_FUNC_ARG_UNUSED */
void *arg
)
{
int s, f;
int opt = 1;
struct sockaddr_in sin;
struct sockaddr_in *sinp;
struct sockaddr_storage *ssp;
socklen_t sslen;
char buf[INET6_ADDRSTRLEN];
pthread_t tid;
struct pollfd fds;
int poll_ret;
conn_arg_t *conn;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) != -1) {
/* IPv4 */
isnslog(LOG_DEBUG, "isns_port_watcher", "IPv4 socket created.");
(void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&opt,
sizeof (opt));
sin.sin_family = AF_INET;
sin.sin_port = htons(ISNS_DEFAULT_SERVER_PORT);
sin.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
isnslog(LOG_DEBUG, "isns_port_watcher",
"binding on server port failed: %%m");
goto watch_failed;
}
isnslog(LOG_DEBUG, "isns_port_watcher",
"successful binding on server port.");
} else {
isnslog(LOG_DEBUG, "isns_port_watcher",
"cannot create socket: %%m.");
goto watch_failed;
}
if (listen(s, 5) < 0) {
isnslog(LOG_DEBUG, "isns_port_watcher",
"listening on server port failed: %%m.");
goto watch_failed;
}
isnslog(LOG_DEBUG, "isns_port_watcher", "listening on server port ok.");
fds.fd = s;
fds.events = (POLLIN | POLLRDNORM);
fds.revents = 0;
/* waiting for connections */
for (;;) {
if (time_to_exit) {
return (NULL);
}
poll_ret = poll(&fds, 1, 1000);
if (poll_ret <= 0) {
continue;
}
/* allocate a connection argument */
conn = (conn_arg_t *)malloc(sizeof (conn_arg_t));
if (conn == NULL) {
isnslog(LOG_DEBUG, "isns_port_watcher",
"malloc() failed.");
goto watch_failed;
}
ssp = &conn->ss;
sslen = sizeof (conn->ss);
f = accept(s, (struct sockaddr *)ssp, &sslen);
if (f < 0) {
isnslog(LOG_DEBUG, "isns_port_watcher",
"accepting connection failed: %%m.");
goto watch_failed;
}
sinp = (struct sockaddr_in *)ssp;
if (ssp->ss_family == AF_INET) {
(void) inet_ntop(AF_INET, (void *)&(sinp->sin_addr),
buf, sizeof (buf));
} else {
(void) inet_ntop(AF_INET6, (void *)&(sinp->sin_addr),
buf, sizeof (buf));
}
isnslog(LOG_DEBUG, "isns_port_watcher",
"connection from %s:%d.", buf,
sinp->sin_port);
if (ISNS_CONNECTION_ACCEPTED_ENABLED()) {
ISNS_CONNECTION_ACCEPTED((uintptr_t)buf);
}
conn->so = f;
/* create an isns connection */
if (pthread_create(&tid, NULL,
isns_connection, (void *)conn) != 0) {
(void) close(f);
(void) free(conn);
isnslog(LOG_DEBUG, "isns_port_watcher",
"pthread_create() failed.");
} else {
/* increase the thread ref count */
inc_thr_count();
}
}
watch_failed:
shutdown_server();
return (NULL);
}
static uint16_t xid = 0;
static pthread_mutex_t xid_mtx = PTHREAD_MUTEX_INITIALIZER;
uint16_t
get_server_xid(
)
{
uint16_t tmp;
(void) pthread_mutex_lock(&xid_mtx);
tmp = ++ xid;
(void) pthread_mutex_unlock(&xid_mtx);
return (tmp);
}
|