summaryrefslogtreecommitdiffstats
path: root/libcsoap/soap-nudp.c
blob: fb4475d977793791a74286fa62138fed5f73f905 (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
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
/******************************************************************
*  $Id: soap-nudp.c,v 1.6 2006/11/29 11:04:25 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>

#include <nanohttp/nanohttp-error.h>
#include <nanohttp/nanohttp-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_warn1("getservbyname(\"soap\", \"udp\") returned NULL, please edit services database");
    _soap_nudp_port = NUDP_DEFAULT_PORT;
  }
  else
  {
    _soap_nudp_port = 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_verbose2("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;
  int size;

  xmlDocDumpMemory(doc, &buf, &size);
  if (sendto(socket, buf, size, 0, addr, addr_len) < 0)
  {
    log_error2("sendto failed (%s)", strerror(errno));
    return herror_new("soap_nudp_client_invoke", 0, "Cannot send message");
  }
  xmlFree(buf);

  return H_OK;
}

static herror_t
_soap_nudp_receive_document(int socket, xmlDocPtr *doc, struct sockaddr *addr, socklen_t *addr_len)
{
  int cnt;
  char buf[4096];

  if ((cnt = recvfrom(socket, buf, 4096, 0, addr, addr_len)) < 0)
  {
    log_error2("recvfrom failed (%s)", strerror(errno));
    return herror_new("_soap_nudp_receive_document", 0, "Receiving document failed");
  }

  if (!(*doc = xmlReadDoc(buf, NULL, NULL, XML_PARSE_NONET)))
  {
    log_error1("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 *req, struct SoapCtx **res)
{
  xmlURI *to;
  xmlDocPtr doc;
  int sd;
  herror_t status;
  struct sockaddr_in addr;

  if (!(to = soap_addressing_get_to_address(req->env)))
  {
    log_error1("soap_addressing_get_to_address returned NULL");
    return herror_new("soap_nudp_client_invoke", 0, "Destination address is missing");
  }

  bzero(&addr, 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_error2("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_error2("socket failed (%s)", strerror(errno));
    return herror_new("soap_nudp_client_invoke", 0, "Cannot create socket");
  }

  _soap_nudp_send_document(sd, req->env->root->doc, (struct sockaddr *)&addr, sizeof(struct sockaddr_in));

  if ((status = _soap_nudp_receive_document(sd, &doc, NULL, NULL)) != H_OK)
  {
    log_error2("_soap_nudp_receive_document failed (%s)", herror_message(status));
    return status;
  }

  if ((status = soap_env_new_from_doc(doc, &(*res)->env)) != H_OK)
  {
    log_error2("soap_env_new_from_doc failed (%s)", herror_message(status));
    return status;
  }

  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_error2("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_error2("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;

  while (_soap_nudp_running)
  {
    /* XXX: select with timeout */

    _soap_nudp_receive_document(_soap_nudp_socket, &doc, &addr, &addr_len);

    // log_error1(__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);

    _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_error2("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;
}