Kannel: Open Source WAP and SMS gateway  svn-r5335
udpfeed.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /* udpfeed.c - blindly send UDP packets to a certain port
58  *
59  * This little tool reads a bunch of files and sends each of them
60  * to a given port as a single UDP packets. It's useful for running
61  * sets of test packets to see if any of them will crash the gateway.
62  * By default, it sends them at one-second intervals.
63  */
64 
65 #include <unistd.h>
66 
67 #include "gwlib/gwlib.h"
68 
69 #define UDP_MAXIMUM (65535 - 40)
70 
71 static unsigned char usage[] = "\
72 Usage: udpfeed [options] files...\n\
73 \n\
74 where options are:\n\
75 \n\
76 -h help\n\
77 -g hostname name of IP number of host to send to (default: localhost)\n\
78 -p port port number to send to (default: 9200)\n\
79 -i interval delay between packers (default: 1.0 seconds)\n\
80 \n\
81 Each file will be sent as a single packet.\n\
82 ";
83 
84 static Octstr *hostname;
85 static int port = 9200; /* By default, the sessionless WSP port */
86 static double interval = 1.0; /* Default interval (seconds) between packets */
87 static long maxsize = UDP_MAXIMUM; /* Maximum packet size in octets */
88 
89 static void help(void) {
90  info(0, "\n%s", usage);
91 }
92 
93 static void send_file(int udpsock, char *filename, Octstr *address) {
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 }
113 
114 int main(int argc, char **argv) {
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:648
void info(int err, const char *fmt,...)
Definition: log.c:672
Definition: http.c:2014
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
static unsigned char usage[]
Definition: udpfeed.c:71
void warning(int err, const char *fmt,...)
Definition: log.c:660
void octstr_destroy(Octstr *ostr)
Definition: octstr.c:324
char filename[FILENAME_MAX+1]
Definition: log.c:171
#define octstr_create(cstr)
Definition: octstr.h:125
void gwthread_sleep(double seconds)
Octstr * octstr_read_file(const char *filename)
Definition: octstr.c:1548
long octstr_len(const Octstr *ostr)
Definition: octstr.c:342
int main(int argc, char **argv)
Definition: udpfeed.c:114
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 octstr_truncate(Octstr *ostr, int new_len)
Definition: octstr.c:1327
int udp_sendto(int s, Octstr *datagram, Octstr *addr)
Definition: socket.c:567
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
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.