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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
/******************************************************************
* $Id: soap-nudp.c,v 1.10 2007/11/03 22:40:09 m0gg Exp $
*
* CSOAP Project: A SOAP client/server library in C
* Copyright (C) 2006 Heiko Ronsdorf
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Email: hero@persua.de
******************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <libxml/tree.h>
#include <libxml/uri.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
int inet_pton(int af, const char *src, void *dst)
{
/* struct sockaddr can't accomodate struct sockaddr_in6. */
union {
struct sockaddr_in6 sin6;
struct sockaddr_in sin;
} sa;
size_t srcsize;
switch(af)
{
case AF_INET:
sa.sin.sin_family = AF_INET;
srcsize = sizeof (sa.sin);
break;
case AF_INET6:
sa.sin6.sin6_family = AF_INET6;
srcsize = sizeof (sa.sin6);
break;
default:
errno = 76;
return -1;
}
if (WSAStringToAddress((LPSTR)src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0)
{
errno = WSAGetLastError();
return -1;
}
switch(af)
{
case AF_INET:
memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr));
break;
case AF_INET6:
memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr));
break;
}
return 1;
}
#endif
#include <nanohttp/nanohttp-error.h>
#include "soap-logging.h"
#include "soap-fault.h"
#include "soap-env.h"
#include "soap-ctx.h"
#include "soap-service.h"
#include "soap-router.h"
#include "soap-server.h"
#include "soap-transport.h"
#include "soap-addressing.h"
#include "soap-nudp.h"
static short _soap_nudp_port = NUDP_DEFAULT_PORT;
static int _soap_nudp_socket;
static pthread_t _soap_nudp_thread;
static pthread_attr_t _soap_nudp_attr;
static volatile int _soap_nudp_running = 0;
static short
_soap_nudp_server_set_port(void)
{
struct servent *entry;
if (!(entry = getservbyname("soap", "udp")))
{
log_warn("getservbyname(\"soap\", \"udp\") returned NULL, please edit services database");
_soap_nudp_port = NUDP_DEFAULT_PORT;
}
else
{
_soap_nudp_port = ntohs(entry->s_port);
}
return _soap_nudp_port;
}
static void
_soap_nudp_server_parse_arguments(int argc, char **argv)
{
int i;
for (i=1; i<argc; i++)
{
if (!strcmp(argv[i - 1], NUDP_ARG_PORT))
{
_soap_nudp_port = atoi(argv[i]);
}
}
log_verbose("socket bind to port \"%d\"", _soap_nudp_port);
return;
}
static herror_t
_soap_nudp_send_document(int socket, xmlDocPtr doc, const struct sockaddr *addr, socklen_t addr_len)
{
xmlChar *buf;
herror_t ret;
int size;
size_t sent;
ret = H_OK;
xmlDocDumpMemory(doc, &buf, &size);
if ((sent = sendto(socket, buf, size, 0, addr, addr_len)) == -1)
{
log_error("sendto failed (%s)", strerror(errno));
ret = herror_new("soap_nudp_client_invoke", 0, "Cannot send message");
}
xmlFree(buf);
return ret;
}
static herror_t
_soap_nudp_receive_document(int socket, xmlDocPtr *doc, struct sockaddr *addr, socklen_t *addr_len)
{
int cnt;
char buf[4096];
/** @todo: use a timeout ??? */
if ((cnt = recvfrom(socket, buf, 4095, 0, addr, addr_len)) < 0)
{
log_error("recvfrom failed (%s)", strerror(errno));
return herror_new("_soap_nudp_receive_document", 0, "Receiving document failed");
}
buf[cnt] = '\0';
if (!(*doc = xmlReadDoc(buf, NULL, NULL, XML_PARSE_NONET)))
{
log_error("xmlReadDoc failed");
return herror_new("_soap_nudp_receive_document", 0, "Cannot parse received data");
}
return H_OK;
}
static herror_t
_soap_nudp_client_invoke(void *unused, struct SoapCtx *request, struct SoapCtx **response)
{
xmlURI *to;
xmlDocPtr doc;
int sd;
herror_t status;
struct sockaddr_in addr;
struct sockaddr saddr;
socklen_t saddr_len;
if (!(to = soap_addressing_get_to_address(request->env)))
{
log_error("soap_addressing_get_to_address returned NULL");
return herror_new("soap_nudp_client_invoke", 0, "Destination address is missing");
}
memset(&addr, '\0', sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
if (to->port == 0)
addr.sin_port = htons(NUDP_DEFAULT_PORT);
else
addr.sin_port = htons(to->port);
if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) != 1)
{
log_error("inet_pton failed (%s)", strerror(errno));
return herror_new("soap_nudp_client_invoke", 0, "Cannot resolve destination address");
}
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
log_error("socket failed (%s)", strerror(errno));
return herror_new("soap_nudp_client_invoke", 0, "Cannot create socket");
}
_soap_nudp_send_document(sd, request->env->root->doc, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));
saddr_len = sizeof(struct sockaddr);
if ((status = _soap_nudp_receive_document(sd, &doc, &saddr, &saddr_len)) != H_OK)
{
log_error("_soap_nudp_receive_document failed (%s)", herror_message(status));
return status;
}
*response = soap_ctx_new(NULL);
if ((status = soap_env_new_from_doc(doc, &(*response)->env)) != H_OK)
{
log_error("soap_env_new_from_doc failed (%s)", herror_message(status));
return status;
}
/** @todo: set saddr in SOAP:Header */
return H_OK;
}
herror_t
soap_nudp_server_init_args(int argc, char **argv)
{
struct sockaddr_in addr;
_soap_nudp_server_set_port();
_soap_nudp_server_parse_arguments(argc, argv);
if ((_soap_nudp_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
log_error("socket failed (%s)", strerror(errno));
return herror_new("soap_nudp_server_init_args", 0, "Cannot create socket (%s)", strerror(errno));
}
memset(&addr, 0, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY);
addr.sin_port = htons(_soap_nudp_port);
if (bind(_soap_nudp_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0)
{
log_error("bind failed (%s)", strerror(errno));
return herror_new("soap_nudp_server_init_args", 0, "Cannot bind socket (%s)", strerror(errno));
}
return H_OK;
}
herror_t
soap_nudp_register(const void *data)
{
return H_OK;
}
void *
soap_nudp_server_run(void *unused)
{
xmlDocPtr doc;
struct sockaddr addr;
socklen_t addr_len;
struct SoapCtx *req;
struct SoapCtx *res;
xmlURI *to;
herror_t status;
while (_soap_nudp_running)
{
/* XXX: select with timeout */
addr_len = sizeof(struct sockaddr);
if (_soap_nudp_receive_document(_soap_nudp_socket, &doc, &addr, &addr_len) != H_OK)
{
log_error("_soap_nudp_receive_document failed (%s)", herror_message(status));
herror_release(status);
continue;
}
/* log_error(__FUNCTION__);
xmlDocFormatDump(stdout, doc, 1); */
req = soap_ctx_new(NULL);
soap_env_new_from_doc(doc, &(req->env));
/* only local part is interesting */
to = soap_addressing_get_to_address(req->env);
soap_addressing_set_to_address_string(req->env, to->path);
// xmlFreeDoc(doc);
soap_transport_process(req, &res);
xmlDocFormatDump(stderr, res->env->root->doc, 1);
_soap_nudp_send_document(_soap_nudp_socket, res->env->root->doc, &addr, addr_len);
soap_ctx_free(res);
soap_ctx_free(req);
}
close(_soap_nudp_socket);
return NULL;
}
herror_t
soap_nudp_server_run_threaded(void)
{
int err;
_soap_nudp_running = 1;
if ((err = pthread_create(&_soap_nudp_thread, &_soap_nudp_attr, soap_nudp_server_run, NULL)) < 0)
{
log_error("pthread_create failed (%s)", strerror(err));
return herror_new("soap_nudp_server_run_threaded", 0, "pthread_create failed (%s)", strerror(err));
}
return H_OK;
}
void
soap_nudp_server_destroy(void)
{
_soap_nudp_running = 0;
/* XXX: sleep for timeout, wait for select */
return;
}
herror_t
soap_nudp_client_init_args(int argc, char **argv)
{
soap_transport_add("soap.udp", NULL, _soap_nudp_client_invoke);
return H_OK;
}
void
soap_nudp_client_destroy(void)
{
return;
}
|