00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #include <unistd.h>
00066
00067 #include "gwlib/gwlib.h"
00068
00069 #define UDP_MAXIMUM (65535 - 40)
00070
00071 static unsigned char usage[] = "\
00072 Usage: udpfeed [options] files...\n\
00073 \n\
00074 where options are:\n\
00075 \n\
00076 -h help\n\
00077 -g hostname name of IP number of host to send to (default: localhost)\n\
00078 -p port port number to send to (default: 9200)\n\
00079 -i interval delay between packers (default: 1.0 seconds)\n\
00080 \n\
00081 Each file will be sent as a single packet.\n\
00082 ";
00083
00084 static Octstr *hostname;
00085 static int port = 9200;
00086 static double interval = 1.0;
00087 static long maxsize = UDP_MAXIMUM;
00088
00089 static void help(void) {
00090 info(0, "\n%s", usage);
00091 }
00092
00093 static void send_file(int udpsock, char *filename, Octstr *address) {
00094 Octstr *contents;
00095
00096 contents = octstr_read_file(filename);
00097 if (contents == NULL) {
00098 info(0, "Skipping \"%s\".", filename);
00099 return;
00100 }
00101
00102 info(0, "Sending \"%s\", %ld octets.", filename, octstr_len(contents));
00103
00104 if (octstr_len(contents) > maxsize) {
00105 octstr_truncate(contents, maxsize);
00106 warning(0, "Truncating to %ld octets.", maxsize);
00107 }
00108
00109 udp_sendto(udpsock, contents, address);
00110
00111 octstr_destroy(contents);
00112 }
00113
00114 int main(int argc, char **argv) {
00115 int opt;
00116 Octstr *address;
00117 int udpsock;
00118
00119 gwlib_init();
00120
00121
00122 hostname = octstr_create("localhost");
00123
00124 while ((opt = getopt(argc, argv, "hg:p:i:m:")) != EOF) {
00125 switch(opt) {
00126 case 'g':
00127 octstr_destroy(hostname);
00128 hostname = octstr_create(optarg);
00129 break;
00130
00131 case 'p':
00132 port = atoi(optarg);
00133 break;
00134
00135 case 'i':
00136 interval = atof(optarg);
00137 break;
00138
00139 case 'm':
00140 maxsize = atol(optarg);
00141 if (maxsize > UDP_MAXIMUM) {
00142 maxsize = UDP_MAXIMUM;
00143 warning(0, "-m: truncated to UDP maximum of"
00144 "%ld bytes.", maxsize);
00145 }
00146 break;
00147
00148 case 'h':
00149 help();
00150 exit(0);
00151 break;
00152
00153 case '?':
00154 default:
00155 error(0, "Unknown option '%c'", opt);
00156 help();
00157 exit(1);
00158 break;
00159 }
00160 }
00161
00162 address = udp_create_address(hostname, port);
00163 udpsock = udp_client_socket();
00164 if (udpsock < 0)
00165 exit(1);
00166
00167 for ( ; optind < argc; optind++) {
00168 send_file(udpsock, argv[optind], address);
00169 if (interval > 0 && optind + 1 < argc)
00170 gwthread_sleep(interval);
00171 }
00172
00173 octstr_destroy(address);
00174 octstr_destroy(hostname);
00175 gwlib_shutdown();
00176 return 0;
00177 }
See file LICENSE for details about the license agreement for using,
modifying, copying or deriving work from this software.