Kannel: Open Source WAP and SMS gateway  $Revision: 5037 $
udpfeed.c File Reference
#include <unistd.h>
#include "gwlib/gwlib.h"

Go to the source code of this file.

Macros

#define UDP_MAXIMUM   (65535 - 40)
 

Functions

static void help (void)
 
static void send_file (int udpsock, char *filename, Octstr *address)
 
int main (int argc, char **argv)
 

Variables

static unsigned char usage []
 
static Octstrhostname
 
static int port = 9200
 
static double interval = 1.0
 
static long maxsize = UDP_MAXIMUM
 

Macro Definition Documentation

#define UDP_MAXIMUM   (65535 - 40)

Definition at line 69 of file udpfeed.c.

Referenced by main().

Function Documentation

static void help ( void  )
static

Definition at line 89 of file udpfeed.c.

References info(), and usage.

Referenced by main().

89  {
90  info(0, "\n%s", usage);
91 }
void info(int err, const char *fmt,...)
Definition: log.c:636
static unsigned char usage[]
Definition: udpfeed.c:71
int main ( int  argc,
char **  argv 
)

Definition at line 114 of file udpfeed.c.

References error(), getopt(), gwlib_init(), gwlib_shutdown(), gwthread_sleep(), help(), interval, maxsize, octstr_create, octstr_destroy(), optarg, optind, send_file(), udp_client_socket(), udp_create_address(), UDP_MAXIMUM, and warning().

114  {
115  int opt;
116  Octstr *address;
117  int udpsock;
118 
119  gwlib_init();
120 
121  /* Set defaults that can't be set statically */
122  hostname = octstr_create("localhost");
123 
124  while ((opt = getopt(argc, argv, "hg:p:i:m:")) != EOF) {
125  switch(opt) {
126  case 'g':
129  break;
130 
131  case 'p':
132  port = atoi(optarg);
133  break;
134 
135  case 'i':
136  interval = atof(optarg);
137  break;
138 
139  case 'm':
140  maxsize = atol(optarg);
141  if (maxsize > UDP_MAXIMUM) {
143  warning(0, "-m: truncated to UDP maximum of"
144  "%ld bytes.", maxsize);
145  }
146  break;
147 
148  case 'h':
149  help();
150  exit(0);
151  break;
152 
153  case '?':
154  default:
155  error(0, "Unknown option '%c'", opt);
156  help();
157  exit(1);
158  break;
159  }
160  }
161 
162  address = udp_create_address(hostname, port);
163  udpsock = udp_client_socket();
164  if (udpsock < 0)
165  exit(1);
166 
167  for ( ; optind < argc; optind++) {
168  send_file(udpsock, argv[optind], address);
169  if (interval > 0 && optind + 1 < argc)
171  }
172 
173  octstr_destroy(address);
175  gwlib_shutdown();
176  return 0;
177 }
void error(int err, const char *fmt,...)
Definition: log.c:612
Definition: http.c:1998
static long maxsize
Definition: udpfeed.c:87
int optind
Definition: attgetopt.c:80
int getopt(int argc, char **argv, char *opts)
Definition: attgetopt.c:84
#define UDP_MAXIMUM
Definition: udpfeed.c:69
int udp_client_socket(void)
Definition: socket.c:464
static void help(void)
Definition: udpfeed.c:89
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
#define octstr_create(cstr)
Definition: octstr.h:125
void gwthread_sleep(double seconds)
Definition: octstr.c:118
static Octstr * hostname
Definition: udpfeed.c:84
char * optarg
Definition: attgetopt.c:82
static double interval
Definition: udpfeed.c:86
void gwlib_shutdown(void)
Definition: gwlib.c:94
void gwlib_init(void)
Definition: gwlib.c:78
static void send_file(int udpsock, char *filename, Octstr *address)
Definition: udpfeed.c:93
Octstr * udp_create_address(Octstr *host_or_ip, int port)
Definition: socket.c:517
static void send_file ( int  udpsock,
char *  filename,
Octstr address 
)
static

Definition at line 93 of file udpfeed.c.

References info(), maxsize, octstr_destroy(), octstr_len(), octstr_read_file(), octstr_truncate(), udp_sendto(), and warning().

Referenced by main().

93  {
94  Octstr *contents;
95 
96  contents = octstr_read_file(filename);
97  if (contents == NULL) {
98  info(0, "Skipping \"%s\".", filename);
99  return;
100  }
101 
102  info(0, "Sending \"%s\", %ld octets.", filename, octstr_len(contents));
103 
104  if (octstr_len(contents) > maxsize) {
105  octstr_truncate(contents, maxsize);
106  warning(0, "Truncating to %ld octets.", maxsize);
107  }
108 
109  udp_sendto(udpsock, contents, address);
110 
111  octstr_destroy(contents);
112 }
void info(int err, const char *fmt,...)
Definition: log.c:636
static long maxsize
Definition: udpfeed.c:87
void warning(int err, const char *fmt,...)
Definition: log.c:624
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:322
char filename[FILENAME_MAX+1]
Definition: log.c:135
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1546
long octstr_len(const Octstr *ostr)
Definition: octstr.c:340
Definition: octstr.c:118
void octstr_truncate(Octstr *ostr, int new_len)
Definition: octstr.c:1325
int udp_sendto(int s, Octstr *datagram, Octstr *addr)
Definition: socket.c:567

Variable Documentation

Octstr* hostname
static

Definition at line 84 of file udpfeed.c.

double interval = 1.0
static

Definition at line 86 of file udpfeed.c.

Referenced by main().

long maxsize = UDP_MAXIMUM
static

Definition at line 87 of file udpfeed.c.

Referenced by main(), and send_file().

int port = 9200
static

Definition at line 85 of file udpfeed.c.

unsigned char usage[]
static
Initial value:
= "\
Usage: udpfeed [options] files...\n\
\n\
where options are:\n\
\n\
-h help\n\
-g hostname name of IP number of host to send to (default: localhost)\n\
-p port port number to send to (default: 9200)\n\
-i interval delay between packers (default: 1.0 seconds)\n\
\n\
Each file will be sent as a single packet.\n\
"

Definition at line 71 of file udpfeed.c.

Referenced by help().

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