Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
wapproxy.c File Reference
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <assert.h>
#include "gwlib/gwlib.h"
#include "msg.h"
#include "shared.h"
#include "wap/wap.h"
#include "wap/wtp.h"
#include "wap/wtp_pdu.h"

Go to the source code of this file.

Data Structures

struct  _udpc
 

Typedefs

typedef struct _udpc Udpc
 

Enumerations

enum  { CONNECTIONLESS_PORT = 9200, CONNECTION_ORIENTED_PORT = 9201, WTLS_CONNECTIONLESS_PORT = 9202, WTLS_CONNECTION_ORIENTED_PORT = 9203 }
 

Functions

static void udpc_destroy (Udpc *udpc)
 
static WAPEventwdp_msg2event (Msg *msg)
 
static void wdp_event_dump (Msg *msg)
 
static void wtp_event_dump (Msg *msg)
 
static void dump (Msg *msg)
 
static void udp_receiver (void *arg)
 
static int send_udp (int fd, Msg *msg)
 
static void udp_sender (void *arg)
 
static Udpcudpc_create (int port, char *interface_name, Octstr *map_addr)
 
static int add_service (int port, char *interface_name, Octstr *map_addr)
 
static int udp_start (Cfg *cfg)
 
static Udpcudpc_find_mapping (Msg *msg, int inbound)
 
static int udp_addwdp_from_server (Msg *msg)
 
static int udp_addwdp_from_client (Msg *msg)
 
static int udp_shutdown (void)
 
static int udp_die (void)
 
static void wdp_router (void *arg)
 
static void service_router (void *arg)
 
static void help (void)
 
int main (int argc, char **argv)
 

Variables

static volatile sig_atomic_t udp_running
 
static Listudpc_list
 
static Octstrinterface_name = NULL
 
static Octstrwapgw
 
static int verbose = 0
 
static int server_port = 0
 
Listincoming_wdp
 
Listoutgoing_wdp
 
Listflow_threads
 
Counterincoming_wdp_counter
 
Counteroutgoing_wdp_counter
 

Typedef Documentation

typedef struct _udpc Udpc

Enumeration Type Documentation

anonymous enum
Enumerator
CONNECTIONLESS_PORT 
CONNECTION_ORIENTED_PORT 
WTLS_CONNECTIONLESS_PORT 
WTLS_CONNECTION_ORIENTED_PORT 

Definition at line 123 of file wapproxy.c.

Function Documentation

static int add_service ( int  port,
char *  interface_name,
Octstr map_addr 
)
static

Definition at line 455 of file wapproxy.c.

References error(), gwlist_add_producer(), gwlist_append(), gwthread_create, _udpc::outgoing_list, _udpc::receiver, udp_receiver(), udp_sender(), udpc_create(), and udpc_destroy().

Referenced by udp_addwdp_from_client(), and udp_start().

456 {
457  Udpc *udpc;
458 
459  if ((udpc = udpc_create(port, interface_name, map_addr)) == NULL)
460  goto error;
462 
463  udpc->receiver = gwthread_create(udp_receiver, udpc);
464  if (udpc->receiver == -1)
465  goto error;
466 
467  if (gwthread_create(udp_sender, udpc) == -1)
468  goto error;
469 
470  gwlist_append(udpc_list, udpc);
471  return 0;
472 
473 error:
474  error(0, "Failed to start UDP receiver/sender thread");
475  udpc_destroy(udpc);
476  return -1;
477 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
void gwlist_append(List *list, void *item)
Definition: list.c:179
Definition: bb_udp.c:100
static void udpc_destroy(Udpc *udpc)
Definition: wapproxy.c:440
static List * udpc_list
Definition: wapproxy.c:110
List * outgoing_list
Definition: bb_udp.c:103
static void udp_receiver(void *arg)
Definition: wapproxy.c:285
#define gwthread_create(func, arg)
Definition: gwthread.h:90
long receiver
Definition: bb_udp.c:104
static void udp_sender(void *arg)
Definition: wapproxy.c:367
static Octstr * interface_name
Definition: wapproxy.c:111
void gwlist_add_producer(List *list)
Definition: list.c:383
static Udpc * udpc_create(int port, char *interface_name, Octstr *map_addr)
Definition: wapproxy.c:404
static void dump ( Msg msg)
static

Definition at line 246 of file wapproxy.c.

References msg_dump(), verbose, wdp_event_dump(), and wtp_event_dump().

Referenced by udp_receiver(), and udp_sender().

247 {
248  switch (verbose) {
249  case 0:
250  break;
251  case 1:
252  msg_dump(msg, 0);
253  break;
254  case 2:
255  wdp_event_dump(msg);
256  break;
257  case 3:
258  msg_dump(msg, 0);
259  wdp_event_dump(msg);
260  break;
261  case 4:
262  wtp_event_dump(msg);
263  break;
264  case 5:
265  msg_dump(msg, 0);
266  wtp_event_dump(msg);
267  break;
268  case 6:
269  wdp_event_dump(msg);
270  wtp_event_dump(msg);
271  break;
272  case 7:
273  msg_dump(msg, 0);
274  wdp_event_dump(msg);
275  wtp_event_dump(msg);
276  break;
277  }
278 }
void msg_dump(Msg *msg, int level)
Definition: msg.c:152
static void wtp_event_dump(Msg *msg)
Definition: wapproxy.c:184
static void wdp_event_dump(Msg *msg)
Definition: wapproxy.c:172
static int verbose
Definition: wapproxy.c:113
static void help ( void  )
static

Definition at line 717 of file wapproxy.c.

References info().

Referenced by main().

718 {
719  info(0, "Usage: wapproxy [options] host ...");
720  info(0, "where host is the real wap gw to forward to and options are:");
721  info(0, "-v number");
722  info(0, " set log level for stderr logging");
723  info(0, "-i interface");
724  info(0, " bind to the given interface for UDP server port (default: 0.0.0.0)");
725  info(0, "-p port");
726  info(0, " bind to the given port for UDP server port (default: 9201)");
727  info(0, "-m");
728  info(0, " dump WDP/UDP packets, msg_dump()");
729  info(0, "-e");
730  info(0, " dump WAP event packets, wap_event_dump()");
731  info(0, "-t");
732  info(0, " dump WTP PDUs, wtp_pdu_dump()");
733 }
void info(int err, const char *fmt,...)
Definition: log.c:636
int main ( int  argc,
char **  argv 
)

Definition at line 736 of file wapproxy.c.

References cfg, CONNECTION_ORIENTED_PORT, counter_create(), counter_destroy(), debug(), error(), getopt(), gwlib_init(), gwlib_shutdown(), gwlist_add_producer(), gwlist_consume(), gwlist_create, gwlist_destroy(), gwlist_remove_producer(), gwthread_create, gwthread_sleep(), help(), log_set_output_level(), octstr_create, octstr_destroy(), optarg, optind, panic, report_versions(), server_port, service_router(), udp_shutdown(), udp_start(), verbose, and wdp_router().

737 {
738  int opt;
739  Cfg *cfg = NULL;
740 
741  gwlib_init();
742 
744 
745  while ((opt = getopt(argc, argv, "v:meti:p:")) != EOF) {
746 
747  switch (opt) {
748  case 'v':
750  break;
751 
752  case 'm':
753  verbose += 1;
754  break;
755 
756  case 'e':
757  verbose += 2;
758  break;
759 
760  case 't':
761  verbose += 4;
762  break;
763 
764  case 'h':
765  help();
766  exit(0);
767 
768  case 'i':
770  break;
771 
772  case 'p':
773  server_port = atoi(optarg);
774  break;
775 
776  case '?':
777  default:
778  error(0, "Invalid option %c", opt);
779  help();
780  panic(0, "Stopping.");
781  }
782  }
783 
784  if (optind == argc) {
785  help();
786  exit(0);
787  }
788 
789  /* get the host or IP of the real wap gw to forward the WDP packets */
790  wapgw = octstr_create(argv[optind]);
791 
792  /* if no interface was given use 0.0.0.0 */
793  if (!interface_name)
795 
796  report_versions("wapproxy");
797 
798  /* initialize main inbound and outbound queues */
802 
805 
806  /* start the main UDP listening threads */
807  udp_start(cfg);
808 
810 
811  debug("bb", 0, "starting WDP routers");
812  if (gwthread_create(service_router, NULL) == -1)
813  panic(0, "Failed to start a new thread for inbound WDP routing");
814  if (gwthread_create(wdp_router, NULL) == -1)
815  panic(0, "Failed to start a new thread for outbound WDP routing");
816 
817  gwthread_sleep(5.0); /* give time to threads to register themselves */
818 
819  while (gwlist_consume(flow_threads) != NULL)
820  ;
821 
822  udp_shutdown();
823 
825 
829 
834 
835  gwlib_shutdown();
836 
837  return 0;
838 }
void error(int err, const char *fmt,...)
Definition: log.c:612
static int server_port
Definition: wapproxy.c:114
static int udp_start(Cfg *cfg)
Definition: wapproxy.c:485
void counter_destroy(Counter *counter)
Definition: counter.c:110
static Octstr * wapgw
Definition: wapproxy.c:112
int optind
Definition: attgetopt.c:80
List * incoming_wdp
Definition: wapproxy.c:116
List * outgoing_wdp
Definition: wapproxy.c:117
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
static int udp_shutdown(void)
Definition: wapproxy.c:634
Definition: cfg.c:164
Counter * counter_create(void)
Definition: counter.c:94
void log_set_output_level(enum output_level level)
Definition: log.c:217
static void help(void)
Definition: wapproxy.c:717
List * flow_threads
Definition: wapproxy.c:118
void gwlist_remove_producer(List *list)
Definition: list.c:401
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define gwthread_create(func, arg)
Definition: gwthread.h:90
#define octstr_create(cstr)
Definition: octstr.h:125
void gwthread_sleep(double seconds)
void report_versions(const char *boxname)
Definition: utils.c:539
static Cfg * cfg
Definition: smsbox.c:115
static int verbose
Definition: wapproxy.c:113
void * gwlist_consume(List *list)
Definition: list.c:427
Counter * outgoing_wdp_counter
Definition: wapproxy.c:121
static void wdp_router(void *arg)
Definition: wapproxy.c:670
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
char * optarg
Definition: attgetopt.c:82
#define panic
Definition: log.h:87
void gwlib_shutdown(void)
Definition: gwlib.c:94
#define gwlist_create()
Definition: list.h:136
void gwlib_init(void)
Definition: gwlib.c:78
Counter * incoming_wdp_counter
Definition: wapproxy.c:120
static Octstr * interface_name
Definition: wapproxy.c:111
static void service_router(void *arg)
Definition: wapproxy.c:691
void gwlist_add_producer(List *list)
Definition: list.c:383
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static int send_udp ( int  fd,
Msg msg 
)
static

Definition at line 352 of file wapproxy.c.

References error(), octstr_destroy(), udp_create_address(), and udp_sendto().

Referenced by udp_sender().

353 {
354  Octstr *cliaddr;
355  int ret;
356 
357  cliaddr = udp_create_address(msg->wdp_datagram.destination_address,
358  msg->wdp_datagram.destination_port);
359  ret = udp_sendto(fd, msg->wdp_datagram.user_data, cliaddr);
360  if (ret == -1)
361  error(0, "could not send UDP datagram");
362  octstr_destroy(cliaddr);
363  return ret;
364 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
int udp_sendto(int s, Octstr *datagram, Octstr *addr)
Definition: socket.c:567
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
static void service_router ( void *  arg)
static

Definition at line 691 of file wapproxy.c.

References gw_assert(), gwlist_add_producer(), gwlist_consume(), gwlist_remove_producer(), msg, udp_addwdp_from_client(), and udp_die().

Referenced by main().

692 {
693  Msg *msg;
694 
696 
697  while (1) {
698 
699  if ((msg = gwlist_consume(incoming_wdp)) == NULL)
700  break;
701 
702  gw_assert(msg_type(msg) == wdp_datagram);
703 
705  }
706  udp_die();
707 
709 }
msg_type
Definition: msg.h:73
List * incoming_wdp
Definition: wapproxy.c:116
Definition: msg.h:79
static int udp_addwdp_from_client(Msg *msg)
Definition: wapproxy.c:583
List * flow_threads
Definition: wapproxy.c:118
void gwlist_remove_producer(List *list)
Definition: list.c:401
static int udp_die(void)
Definition: wapproxy.c:644
gw_assert(wtls_machine->packet_to_send!=NULL)
void * gwlist_consume(List *list)
Definition: list.c:427
void gwlist_add_producer(List *list)
Definition: list.c:383
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
static int udp_addwdp_from_client ( Msg msg)
static

Definition at line 583 of file wapproxy.c.

References add_service(), CONNECTION_ORIENTED_PORT, gwlist_produce(), info(), octstr_destroy(), octstr_duplicate, octstr_get_cstr, _udpc::outgoing_list, panic, udp_create_address(), udp_get_ip(), udp_get_port(), udp_running, and udpc_find_mapping().

Referenced by service_router().

584 {
585  Udpc *udpc;
586  Octstr *map_addr;
587  Octstr *os;
588  Octstr *source;
589 
590  if (!udp_running) return -1;
591  assert(msg != NULL);
592  assert(msg_type(msg) == wdp_datagram);
593 
594  /*
595  * Check if there is allready a bound UDP port for this mapping.
596  * If not create a mapping and bind the mapped UDP port
597  * The mapped port is simply 2x of the client port.
598  */
599  if ((udpc = udpc_find_mapping(msg, 1)) == NULL) {
600  info(0, "Creating UDP mapping <%s:%ld> <-> <%s:%ld>",
601  octstr_get_cstr(msg->wdp_datagram.source_address),
602  msg->wdp_datagram.source_port,
603  octstr_get_cstr(msg->wdp_datagram.destination_address),
604  msg->wdp_datagram.source_port*2);
605 
606  map_addr = udp_create_address(msg->wdp_datagram.source_address,
607  msg->wdp_datagram.source_port);
608  add_service(msg->wdp_datagram.source_port * 2,
609  octstr_get_cstr(interface_name), map_addr);
610  /* now we should find it in the udpc_list */
611  if ((udpc = udpc_find_mapping(msg, 1)) == NULL)
612  panic(0,"Could not find UDP mapping, internal error");
613  }
614 
615  /* now swap the message addressing */
616  octstr_destroy(msg->wdp_datagram.source_address);
617  octstr_destroy(msg->wdp_datagram.destination_address);
618 
620  source = udp_create_address(os, msg->wdp_datagram.source_port * 2);
621  msg->wdp_datagram.source_address = udp_get_ip(source);
622  msg->wdp_datagram.source_port = udp_get_port(source);
623  msg->wdp_datagram.destination_address = octstr_duplicate(wapgw);
624  msg->wdp_datagram.destination_port = CONNECTION_ORIENTED_PORT;
625 
626  octstr_destroy(os);
627 
628  gwlist_produce(udpc->outgoing_list, msg);
629 
630  return -1;
631 }
void info(int err, const char *fmt,...)
Definition: log.c:636
static Udpc * udpc_find_mapping(Msg *msg, int inbound)
Definition: wapproxy.c:502
void gwlist_produce(List *list, void *item)
Definition: list.c:411
static Octstr * wapgw
Definition: wapproxy.c:112
int udp_get_port(Octstr *addr)
Definition: socket.c:547
msg_type
Definition: msg.h:73
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Definition: bb_udp.c:100
List * outgoing_list
Definition: bb_udp.c:103
static volatile sig_atomic_t udp_running
Definition: wapproxy.c:109
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Definition: octstr.c:118
#define panic
Definition: log.h:87
static Octstr * interface_name
Definition: wapproxy.c:111
static int add_service(int port, char *interface_name, Octstr *map_addr)
Definition: wapproxy.c:455
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
static int udp_addwdp_from_server ( Msg msg)
static

Definition at line 532 of file wapproxy.c.

References gwlist_produce(), _udpc::map_addr, octstr_create, octstr_destroy(), octstr_duplicate, octstr_get_cstr, _udpc::outgoing_list, panic, server_port, udp_create_address(), udp_get_ip(), udp_get_port(), udp_running, and udpc_find_mapping().

Referenced by wdp_router().

533 {
534  Udpc *udpc;
535  Octstr *os;
536  Octstr *source;
537 
538  if (!udp_running) return -1;
539  assert(msg != NULL);
540  assert(msg_type(msg) == wdp_datagram);
541 
542  octstr_destroy(msg->wdp_datagram.source_address);
543  msg->wdp_datagram.source_address =
544  octstr_create(octstr_get_cstr(msg->wdp_datagram.destination_address));
545  msg->wdp_datagram.source_port = msg->wdp_datagram.destination_port;
546 
547  if ((udpc = udpc_find_mapping(msg, 0)) == NULL)
548  /* there should have been one */
549  panic(0,"Could not find UDP mapping, internal error");
550 
551  /* insert the found mapped destination */
552  octstr_destroy(msg->wdp_datagram.source_address);
553  octstr_destroy(msg->wdp_datagram.destination_address);
554 
555  msg->wdp_datagram.destination_address = udp_get_ip(udpc->map_addr);
556  msg->wdp_datagram.destination_port = udp_get_port(udpc->map_addr);
557 
558  /* now search for our inbound UDP socket */
560  source = udp_create_address(os, server_port);
561 
562  msg->wdp_datagram.source_address = udp_get_ip(source);
563  msg->wdp_datagram.source_port = udp_get_port(source);
564  if ((udpc = udpc_find_mapping(msg, 0)) == NULL)
565  panic(0,"Could not find main inbound UDP socket, internal error");
566 
567  /*
568  * ok, got the destination, got the socket,
569  * now put it on the outbound queue
570  */
571  gwlist_produce(udpc->outgoing_list, msg);
572 
573  octstr_destroy(os);
574 
575  return 0;
576 }
static int server_port
Definition: wapproxy.c:114
static Udpc * udpc_find_mapping(Msg *msg, int inbound)
Definition: wapproxy.c:502
void gwlist_produce(List *list, void *item)
Definition: list.c:411
int udp_get_port(Octstr *addr)
Definition: socket.c:547
msg_type
Definition: msg.h:73
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Definition: bb_udp.c:100
List * outgoing_list
Definition: bb_udp.c:103
static volatile sig_atomic_t udp_running
Definition: wapproxy.c:109
#define octstr_duplicate(ostr)
Definition: octstr.h:187
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr * map_addr
Definition: wapproxy.c:134
Definition: octstr.c:118
#define panic
Definition: log.h:87
static Octstr * interface_name
Definition: wapproxy.c:111
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
static int udp_die ( void  )
static

Definition at line 644 of file wapproxy.c.

References debug(), gwlist_consume(), gwlist_destroy(), gwlist_remove_producer(), _udpc::outgoing_list, and udp_running.

Referenced by service_router(), and wdp_router().

645 {
646  Udpc *udpc;
647 
648  if (!udp_running) return -1;
649 
650  /*
651  * remove producers from all outgoing lists.
652  */
653  debug("bb.udp", 0, "udp_die: removing producers from udp-lists");
654 
655  while ((udpc = gwlist_consume(udpc_list)) != NULL) {
657  }
658  gwlist_destroy(udpc_list, NULL);
659  udp_running = 0;
660 
661  return 0;
662 }
Definition: bb_udp.c:100
static List * udpc_list
Definition: wapproxy.c:110
void gwlist_remove_producer(List *list)
Definition: list.c:401
List * outgoing_list
Definition: bb_udp.c:103
static volatile sig_atomic_t udp_running
Definition: wapproxy.c:109
void * gwlist_consume(List *list)
Definition: list.c:427
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static void udp_receiver ( void *  arg)
static

Definition at line 285 of file wapproxy.c.

References _udpc::addr, counter_increase(), dump(), error(), _udpc::fd, gwlist_add_producer(), gwlist_produce(), gwlist_remove_producer(), gwthread_wakeup(), info(), MAIN_THREAD_ID, _udpc::map_addr, msg, msg_create, octstr_compare(), octstr_destroy(), octstr_get_cstr, read_available(), udp_get_ip(), udp_get_port(), and udp_recvfrom().

Referenced by add_service().

286 {
287  Octstr *datagram, *cliaddr;
288  int ret;
289  Msg *msg;
290  Udpc *conn = arg;
291  Octstr *ip;
292 
296 
297  /* remove messages from socket until it is closed */
298  while (1) {
299 
300  if (read_available(conn->fd, 100000) < 1)
301  continue;
302 
303  ret = udp_recvfrom(conn->fd, &datagram, &cliaddr);
304  if (ret == -1) {
305  if (errno == EAGAIN)
306  /* No datagram available, don't block. */
307  continue;
308 
309  error(errno, "Failed to receive an UDP");
310  continue;
311  }
312 
313  ip = udp_get_ip(cliaddr);
314  msg = msg_create(wdp_datagram);
315 
316  msg->wdp_datagram.source_address = udp_get_ip(cliaddr);
317  msg->wdp_datagram.source_port = udp_get_port(cliaddr);
318  msg->wdp_datagram.destination_address = udp_get_ip(conn->addr);
319  msg->wdp_datagram.destination_port = udp_get_port(conn->addr);
320  msg->wdp_datagram.user_data = datagram;
321 
322  info(0, "datagram received <%s:%d> -> <%s:%d>",
323  octstr_get_cstr(udp_get_ip(cliaddr)), udp_get_port(cliaddr),
325 
326  dump(msg);
327 
328  /*
329  * Descide if this is (a) or (b) UDP packet and add them to the
330  * corresponding queues
331  */
332  if (octstr_compare(conn->addr, conn->map_addr) == 0) {
335  } else {
338  }
339 
340  octstr_destroy(cliaddr);
341  octstr_destroy(ip);
342  }
345 }
void error(int err, const char *fmt,...)
Definition: log.c:612
void info(int err, const char *fmt,...)
Definition: log.c:636
static void dump(Msg *msg)
Definition: wapproxy.c:246
void gwlist_produce(List *list, void *item)
Definition: list.c:411
int udp_get_port(Octstr *addr)
Definition: socket.c:547
#define msg_create(type)
Definition: msg.h:136
List * incoming_wdp
Definition: wapproxy.c:116
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
unsigned long counter_increase(Counter *counter)
Definition: counter.c:123
List * outgoing_wdp
Definition: wapproxy.c:117
Definition: bb_udp.c:100
Definition: msg.h:79
List * flow_threads
Definition: wapproxy.c:118
void gwlist_remove_producer(List *list)
Definition: list.c:401
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Octstr * addr
Definition: bb_udp.c:102
Octstr * map_addr
Definition: wapproxy.c:134
Definition: octstr.c:118
int read_available(int fd, long wait_usec)
Definition: socket.c:406
Counter * outgoing_wdp_counter
Definition: wapproxy.c:121
void gwthread_wakeup(long thread)
Counter * incoming_wdp_counter
Definition: wapproxy.c:120
#define MAIN_THREAD_ID
Definition: gwthread.h:77
void gwlist_add_producer(List *list)
Definition: list.c:383
int fd
Definition: bb_udp.c:101
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
int udp_recvfrom(int s, Octstr **datagram, Octstr **addr)
Definition: socket.c:582
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static void udp_sender ( void *  arg)
static

Definition at line 367 of file wapproxy.c.

References counter_increase(), dump(), _udpc::fd, gwlist_add_producer(), gwlist_consume(), gwlist_remove_producer(), gwthread_join(), info(), msg, msg_destroy(), octstr_get_cstr, _udpc::outgoing_list, _udpc::receiver, send_udp(), and udpc_destroy().

Referenced by add_service().

368 {
369  Msg *msg;
370  Udpc *conn = arg;
371 
373  while (1) {
374 
375  if ((msg = gwlist_consume(conn->outgoing_list)) == NULL)
376  break;
377 
378  info(0, "sending datagram <%s:%ld> -> <%s:%ld>",
379  octstr_get_cstr(msg->wdp_datagram.source_address),
380  msg->wdp_datagram.source_port,
381  octstr_get_cstr(msg->wdp_datagram.destination_address),
382  msg->wdp_datagram.destination_port);
383 
384  dump(msg);
385 
386  if (send_udp(conn->fd, msg) == -1) {
387  msg_destroy(msg);
388  continue;
389  }
391  msg_destroy(msg);
392  }
393  gwthread_join(conn->receiver);
394 
395  udpc_destroy(conn);
397 }
void info(int err, const char *fmt,...)
Definition: log.c:636
static void dump(Msg *msg)
Definition: wapproxy.c:246
void gwthread_join(long thread)
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
unsigned long counter_increase(Counter *counter)
Definition: counter.c:123
Definition: bb_udp.c:100
static void udpc_destroy(Udpc *udpc)
Definition: wapproxy.c:440
Definition: msg.h:79
List * flow_threads
Definition: wapproxy.c:118
void gwlist_remove_producer(List *list)
Definition: list.c:401
List * outgoing_list
Definition: bb_udp.c:103
void msg_destroy(Msg *msg)
Definition: msg.c:132
long receiver
Definition: bb_udp.c:104
void * gwlist_consume(List *list)
Definition: list.c:427
Counter * outgoing_wdp_counter
Definition: wapproxy.c:121
static int send_udp(int fd, Msg *msg)
Definition: wapproxy.c:352
void gwlist_add_producer(List *list)
Definition: list.c:383
int fd
Definition: bb_udp.c:101
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
static int udp_shutdown ( void  )
static

Definition at line 634 of file wapproxy.c.

References debug(), gwlist_remove_producer(), and udp_running.

Referenced by main().

635 {
636  if (!udp_running) return -1;
637 
638  debug("bb.thread", 0, "udp_shutdown: Starting avalanche");
640  return 0;
641 }
List * incoming_wdp
Definition: wapproxy.c:116
void gwlist_remove_producer(List *list)
Definition: list.c:401
static volatile sig_atomic_t udp_running
Definition: wapproxy.c:109
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
static int udp_start ( Cfg cfg)
static

Definition at line 485 of file wapproxy.c.

References add_service(), debug(), gwlist_add_producer(), gwlist_create, octstr_get_cstr, server_port, and udp_running.

Referenced by main().

486 {
487 
488  if (udp_running) return -1;
489 
490  debug("wap.proxy", 0, "starting UDP sender/receiver module");
491 
492  udpc_list = gwlist_create(); /* have a list of running systems */
493 
494  add_service(server_port, octstr_get_cstr(interface_name), NULL); /* wsp/wtp */
495 
497  udp_running = 1;
498  return 0;
499 }
static int server_port
Definition: wapproxy.c:114
List * incoming_wdp
Definition: wapproxy.c:116
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
static List * udpc_list
Definition: wapproxy.c:110
static volatile sig_atomic_t udp_running
Definition: wapproxy.c:109
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
#define gwlist_create()
Definition: list.h:136
static Octstr * interface_name
Definition: wapproxy.c:111
void gwlist_add_producer(List *list)
Definition: list.c:383
static int add_service(int port, char *interface_name, Octstr *map_addr)
Definition: wapproxy.c:455
static Udpc* udpc_create ( int  port,
char *  interface_name,
Octstr map_addr 
)
static

Definition at line 404 of file wapproxy.c.

References _udpc::addr, debug(), error(), _udpc::fd, gwlist_create, _udpc::map_addr, octstr_create, octstr_destroy(), octstr_get_cstr, _udpc::outgoing_list, udp_bind(), udp_create_address(), udp_get_ip(), and udp_get_port().

Referenced by add_service().

405 {
406  Udpc *udpc;
407  Octstr *os;
408  int fl;
409 
410  udpc = gw_malloc(sizeof(Udpc));
411  udpc->fd = udp_bind(port, interface_name);
412 
414  udpc->addr = udp_create_address(os, port);
415  udpc->map_addr = map_addr ? map_addr : udpc->addr;
416 
417  octstr_destroy(os);
418  if (udpc->addr == NULL) {
419  error(0, "updc_create: could not resolve interface <%s>", interface_name);
420  close(udpc->fd);
421  gw_free(udpc);
422  return NULL;
423  }
424 
425  fl = fcntl(udpc->fd, F_GETFL);
426  fcntl(udpc->fd, F_SETFL, fl | O_NONBLOCK);
427 
428  os = udp_get_ip(udpc->addr);
429  debug("wap.proxy",0, "bound to UDP <%s:%d>",
430  octstr_get_cstr(os), udp_get_port(udpc->addr));
431 
432  octstr_destroy(os);
433 
434  udpc->outgoing_list = gwlist_create();
435 
436  return udpc;
437 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
int udp_get_port(Octstr *addr)
Definition: socket.c:547
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
Definition: bb_udp.c:100
int udp_bind(int port, const char *source_addr)
Definition: socket.c:478
List * outgoing_list
Definition: bb_udp.c:103
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
Octstr * addr
Definition: bb_udp.c:102
Octstr * map_addr
Definition: wapproxy.c:134
Definition: octstr.c:118
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
#define gwlist_create()
Definition: list.h:136
static Octstr * interface_name
Definition: wapproxy.c:111
int fd
Definition: bb_udp.c:101
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
static void udpc_destroy ( Udpc udpc)
static

Definition at line 440 of file wapproxy.c.

References _udpc::addr, _udpc::fd, gw_assert(), gwlist_destroy(), gwlist_len(), octstr_destroy(), and _udpc::outgoing_list.

Referenced by add_service(), and udp_sender().

441 {
442  if (udpc == NULL)
443  return;
444 
445  if (udpc->fd >= 0)
446  close(udpc->fd);
447  octstr_destroy(udpc->addr);
448  gw_assert(gwlist_len(udpc->outgoing_list) == 0);
449  gwlist_destroy(udpc->outgoing_list, NULL);
450 
451  gw_free(udpc);
452 }
long gwlist_len(List *list)
Definition: list.c:166
List * outgoing_list
Definition: bb_udp.c:103
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
Octstr * addr
Definition: bb_udp.c:102
gw_assert(wtls_machine->packet_to_send!=NULL)
int fd
Definition: bb_udp.c:101
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145
static Udpc* udpc_find_mapping ( Msg msg,
int  inbound 
)
static

Definition at line 502 of file wapproxy.c.

References _udpc::addr, gwlist_get(), gwlist_len(), gwlist_lock(), gwlist_unlock(), _udpc::map_addr, octstr_compare(), udp_get_ip(), and udp_get_port().

Referenced by udp_addwdp_from_client(), and udp_addwdp_from_server().

503 {
504  int i;
505  Udpc *udpc;
506  Octstr *addr;
507 
508  /* check if there is allready a bound UDP port */
510  for (i=0; i < gwlist_len(udpc_list); i++) {
511  udpc = gwlist_get(udpc_list, i);
512 
513  /* decide if we compare against inbound or outbound traffic mapping */
514  addr = inbound ? udpc->map_addr : udpc->addr;
515 
516  if (msg->wdp_datagram.source_port == udp_get_port(addr) &&
517  octstr_compare(msg->wdp_datagram.source_address,
518  udp_get_ip(addr)) == 0) {
520  return udpc;
521  }
522  }
524  return NULL;
525 }
long gwlist_len(List *list)
Definition: list.c:166
int udp_get_port(Octstr *addr)
Definition: socket.c:547
void * gwlist_get(List *list, long pos)
Definition: list.c:292
void gwlist_unlock(List *list)
Definition: list.c:354
Definition: bb_udp.c:100
static List * udpc_list
Definition: wapproxy.c:110
Octstr * addr
Definition: bb_udp.c:102
void gwlist_lock(List *list)
Definition: list.c:347
Octstr * map_addr
Definition: wapproxy.c:134
Definition: octstr.c:118
Octstr * udp_get_ip(Octstr *addr)
Definition: socket.c:557
int octstr_compare(const Octstr *ostr1, const Octstr *ostr2)
Definition: octstr.c:869
static void wdp_event_dump ( Msg msg)
static

Definition at line 172 of file wapproxy.c.

References wap_event_destroy(), wap_event_dump(), and wdp_msg2event().

Referenced by dump().

173 {
174  WAPEvent *dgram;
175 
176  if ((dgram = wdp_msg2event(msg)) != NULL)
177  /* wap_dispatch_datagram(dgram); */
178  wap_event_dump(dgram);
179 
180  wap_event_destroy(dgram);
181 }
void wap_event_dump(WAPEvent *event)
Definition: wap_events.c:181
static WAPEvent * wdp_msg2event(Msg *msg)
Definition: wapproxy.c:148
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102
static WAPEvent* wdp_msg2event ( Msg msg)
static

Definition at line 148 of file wapproxy.c.

References CONNECTION_ORIENTED_PORT, gw_assert(), octstr_duplicate, server_port, WAPEvent::u, wap_addr_tuple_create(), and wap_event_create.

Referenced by wdp_event_dump(), and wtp_event_dump().

149 {
150  WAPEvent *dgram = NULL;
151 
152  gw_assert(msg_type(msg) == wdp_datagram);
153 
154  if (msg->wdp_datagram.destination_port == server_port ||
155  msg->wdp_datagram.source_port == server_port ||
156  msg->wdp_datagram.destination_port == CONNECTION_ORIENTED_PORT ||
157  msg->wdp_datagram.source_port == CONNECTION_ORIENTED_PORT) {
158 
159  dgram = wap_event_create(T_DUnitdata_Ind);
160  dgram->u.T_DUnitdata_Ind.addr_tuple = wap_addr_tuple_create(
161  msg->wdp_datagram.source_address,
162  msg->wdp_datagram.source_port,
163  msg->wdp_datagram.destination_address,
164  msg->wdp_datagram.destination_port);
165  dgram->u.T_DUnitdata_Ind.user_data =
166  octstr_duplicate(msg->wdp_datagram.user_data);
167  }
168  return dgram;
169 }
static int server_port
Definition: wapproxy.c:114
msg_type
Definition: msg.h:73
#define octstr_duplicate(ostr)
Definition: octstr.h:187
#define wap_event_create(type)
Definition: wap_events.h:107
gw_assert(wtls_machine->packet_to_send!=NULL)
WAPAddrTuple * wap_addr_tuple_create(Octstr *rmt_addr, long rmt_port, Octstr *lcl_addr, long lcl_port)
Definition: wap_addr.c:96
union WAPEvent::@87 u
static void wdp_router ( void *  arg)
static

Definition at line 670 of file wapproxy.c.

References gw_assert(), gwlist_add_producer(), gwlist_consume(), gwlist_remove_producer(), msg, udp_addwdp_from_server(), and udp_die().

Referenced by main().

671 {
672  Msg *msg;
673 
675 
676  while (1) {
677 
678  if ((msg = gwlist_consume(outgoing_wdp)) == NULL)
679  break;
680 
681  gw_assert(msg_type(msg) == wdp_datagram);
682 
684  }
685  udp_die();
686 
688 }
static int udp_addwdp_from_server(Msg *msg)
Definition: wapproxy.c:532
msg_type
Definition: msg.h:73
List * outgoing_wdp
Definition: wapproxy.c:117
Definition: msg.h:79
List * flow_threads
Definition: wapproxy.c:118
void gwlist_remove_producer(List *list)
Definition: list.c:401
static int udp_die(void)
Definition: wapproxy.c:644
gw_assert(wtls_machine->packet_to_send!=NULL)
void * gwlist_consume(List *list)
Definition: list.c:427
void gwlist_add_producer(List *list)
Definition: list.c:383
static XMLRPCDocument * msg
Definition: test_xmlrpc.c:86
static void wtp_event_dump ( Msg msg)
static

Definition at line 184 of file wapproxy.c.

References debug(), error(), gwlist_destroy(), gwlist_extract_first(), gwlist_len(), info(), WAPEvent::type, wap_event_destroy(), wap_event_dump(), wap_event_name(), wdp_msg2event(), wtp_event_is_for_responder(), and wtp_unpack_wdp_datagram().

Referenced by dump().

185 {
186  WAPEvent *dgram;
187  List *events;
188  long i, n;
189 
190  dgram = wdp_msg2event(msg);
191  if (dgram == NULL)
192  error(0, "dgram is null");
193 
194  /*
195  pdu = wtp_pdu_unpack(dgram->u.T_DUnitdata_Ind.user_data);
196  if (pdu == NULL) {
197  error(0, "WTP PDU unpacking failed, WAP event is:");
198  wap_event_dump(dgram);
199  } else {
200  wtp_pdu_dump(pdu, 0);
201  wtp_pdu_destroy(pdu);
202  }
203  */
204 
205  events = wtp_unpack_wdp_datagram(dgram);
206  n = gwlist_len(events);
207  debug("wap.proxy",0,"datagram contains %ld events", n);
208 
209  i = 1;
210  while (gwlist_len(events) > 0) {
211  WAPEvent *event;
212 
213  event = gwlist_extract_first(events);
214 
215  info(0, "WTP: %ld/%ld event %s.", i, n, wap_event_name(event->type));
216 
217  if (wtp_event_is_for_responder(event))
218  /* wtp_resp_dispatch_event(event); */
219  debug("",0,"datagram is for WTP responder");
220  else
221  /* wtp_initiator_dispatch_event(event); */
222  debug("",0,"datagram is for WTP initiator");
223 
224  wap_event_dump(event);
225  /*
226  switch (event->type) {
227  RcvInvoke:
228  debug("",0,"XXX invoke");
229  break;
230  RcvResult:
231  debug("",0,"XXX result");
232  break;
233  default:
234  error(0,"unkown WTP event type while unpacking");
235  break;
236  }
237  */
238  i++;
239  }
240 
241  wap_event_destroy(dgram);
242  gwlist_destroy(events, NULL);
243 }
void error(int err, const char *fmt,...)
Definition: log.c:612
List * wtp_unpack_wdp_datagram(WAPEvent *datagram)
Definition: wtp.c:98
void info(int err, const char *fmt,...)
Definition: log.c:636
long gwlist_len(List *list)
Definition: list.c:166
void wap_event_dump(WAPEvent *event)
Definition: wap_events.c:181
void * gwlist_extract_first(List *list)
Definition: list.c:305
int wtp_event_is_for_responder(WAPEvent *event)
Definition: wtp.c:156
const char * wap_event_name(WAPEventName type)
Definition: wap_events.c:169
static WAPEvent * wdp_msg2event(Msg *msg)
Definition: wapproxy.c:148
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:690
WAPEventName type
Definition: wap_events.h:88
Definition: list.c:102
void wap_event_destroy(WAPEvent *event)
Definition: wap_events.c:102
void gwlist_destroy(List *list, gwlist_item_destructor_t *destructor)
Definition: list.c:145

Variable Documentation

List* flow_threads

Definition at line 118 of file wapproxy.c.

List* incoming_wdp

Definition at line 116 of file wapproxy.c.

Referenced by run_wapbox().

Counter* incoming_wdp_counter

Definition at line 120 of file wapproxy.c.

Octstr* interface_name = NULL
static

Definition at line 111 of file wapproxy.c.

List* outgoing_wdp

Definition at line 117 of file wapproxy.c.

Referenced by run_wapbox().

Counter* outgoing_wdp_counter

Definition at line 121 of file wapproxy.c.

int server_port = 0
static

Definition at line 114 of file wapproxy.c.

Referenced by main(), udp_addwdp_from_server(), udp_start(), and wdp_msg2event().

volatile sig_atomic_t udp_running
static
List* udpc_list
static

Definition at line 110 of file wapproxy.c.

int verbose = 0
static

Definition at line 113 of file wapproxy.c.

Referenced by dump(), and main().

Octstr* wapgw
static

Definition at line 112 of file wapproxy.c.

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.