Udpxy v1.0-Chipmunk-build21
[tomato.git] / release / src / router / udpxy / uopt.c
blob531a20ce9f3cc35f58d905c38255c052acd20fe7
1 /* @(#) option-associated functions for udpxy
3 * Copyright 2008-2011 Pavel V. Cherenkov (pcherenkov@gmail.com)
5 * This file is part of udpxy.
7 * udpxy is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * udpxy is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with udpxy. If not, see <http://www.gnu.org/licenses/>.
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
26 #include "udpxy.h"
27 #include "uopt.h"
28 #include "util.h"
31 static int
32 read_http_footer (char* buf, size_t len)
34 const char* ev = getenv ("UDPXY_HTTP200_FTR_LN");
35 ssize_t n = 0;
37 assert (buf && len);
39 /* if 1-line footer is specified */
40 if (ev) {
41 (void) strncpy (buf, ev, len-1);
42 buf[len-1] = '\0';
43 return 0;
46 if (NULL != (ev = getenv ("UDPXY_HTTP200_FTR_FILE"))) {
47 n = txtf_read (ev, buf, len, stderr);
50 return (n<0) ? 1 : 0;
54 /* populate options with default/initial values
56 int
57 init_uopt( struct udpxy_opt* uo )
59 int rc = 0;
60 assert( uo );
62 uo->is_verbose = uf_FALSE;
63 uo->cl_tpstat = uf_FALSE;
64 uo->nice_incr = 0;
65 uo->rbuf_len = DEFAULT_CACHE_LEN;
66 uo->rbuf_msgs = 1;
67 uo->max_clients = DEFAULT_CLIENT_COUNT;
68 uo->rcv_tmout = get_timeval( "UDPXY_RCV_TMOUT",
69 RLY_SOCK_TIMEOUT );
70 uo->dhold_tmout = get_timeval( "UDPXY_DHOLD_TMOUT",
71 DHOLD_TIMEOUT );
72 uo->sr_tmout = get_timeval ( "UDPXY_SREAD_TMOUT", SRVSOCK_TIMEOUT );
73 uo->sw_tmout = get_timeval ( "UDPXY_SWRITE_TMOUT", SRVSOCK_TIMEOUT );
74 uo->ssel_tmout = get_timeval ( "UDPXY_SSEL_TMOUT", SSEL_TIMEOUT );
75 uo->lq_backlog = (int) get_sizeval ("UDPXY_LQ_BACKLOG", LQ_BACKLOG);
76 uo->ss_rlwmark = (int) get_sizeval ("UDPXY_SRV_RLWMARK", SRV_RLWMARK);
78 uo->nosync_sbuf = (u_short)get_flagval( "UDPXY_SSOCKBUF_NOSYNC", 0 );
79 uo->nosync_dbuf = (u_short)get_flagval( "UDPXY_DSOCKBUF_NOSYNC", 0 );
81 uo->srcfile = NULL;
82 uo->dstfile = NULL;
83 uo->mcast_refresh = DEFAULT_MCAST_REFRESH;
85 if (-1 == read_http_footer (uo->h200_ftr, sizeof(uo->h200_ftr))) {
86 rc = -1; /* modify rc only if there is an error */
88 return rc;
92 /* release resources allocated for udpxy options
94 void
95 free_uopt( struct udpxy_opt* uo )
97 assert( uo );
99 if( uo->srcfile ) {
100 free( uo->srcfile ); uo->srcfile = NULL;
103 if( uo->dstfile ) {
104 free( uo->dstfile ); uo->dstfile = NULL;
109 #ifdef UDPXREC_MOD
111 /* populate udpxrec options with default/initial values
114 init_recopt( struct udpxrec_opt* ro )
116 int rc = 0;
118 assert( ro );
120 ro->is_verbose = uf_FALSE;
121 ro->nice_incr = 0;
123 ro->bg_time = ro->end_time = 0;
124 ro->max_fsize = 0;
125 ro->bufsize = DEFAULT_CACHE_LEN;
126 ro->rbuf_msgs = 1;
128 ro->mcast_addr[0] = '\0';
130 ro->dstfile = NULL;
131 ro->pidfile = NULL;
132 ro->rec_channel[0] = '\0';
133 ro->rec_port = 0;
134 ro->waitupd_sec = -1;
136 ro->nosync_sbuf =
137 (flag_t)get_flagval( "UDPXY_SSOCKBUF_NOSYNC", 0 );
138 ro->nosync_dbuf =
139 (flag_t)get_flagval( "UDPXY_DSOCKBUF_NOSYNC", 0 );
141 ro->rcv_tmout = 0;
143 return rc;
147 /* release resources allocated for udpxy options
149 void
150 free_recopt( struct udpxrec_opt* ro )
152 assert( ro );
154 if( ro->dstfile ) {
155 free( ro->dstfile ); ro->dstfile = NULL;
157 if( ro->pidfile ) {
158 free( ro->pidfile ); ro->pidfile = NULL;
161 ro->rec_channel[0] = '\0';
165 /* print udpxrec options to stream
167 void
168 fprint_recopt( FILE* stream, struct udpxrec_opt* ro )
170 assert( stream && ro );
172 if( ro->is_verbose ) {
173 (void)fprintf( stream, "verbose=[ON] " );
175 if( ro->nice_incr ) {
176 (void)fprintf( stream, "nice_incr=[%d] ", ro->nice_incr );
178 if( ro->bg_time > 0 ) {
179 (void)fprintf( stream, "begin_time=[%s] ",
180 Zasctime( localtime(&(ro->bg_time) )) );
182 if( ro->end_time > 0 ) {
183 (void)fprintf( stream, "end_time=[%s] ",
184 Zasctime( localtime(&(ro->end_time) )) );
186 if( ro->max_fsize > 0 ) {
187 (void)fprintf( stream, "Max filesize=[%.0f] bytes ",
188 (double)ro->max_fsize );
191 (void)fprintf( stream, "Buffer size=[%ld] bytes ",
192 (long)ro->bufsize );
194 if( ro->rbuf_msgs > 0 ) {
195 (void)fprintf( stream, "Max messages=[%ld] ",
196 (long)ro->rbuf_msgs );
198 if( ro->mcast_addr[0] ) {
199 (void)fprintf( stream, "Multicast interface=[%s] ", ro->mcast_addr );
201 if( ro->rec_channel[0] ) {
202 (void)fprintf( stream, "Channel=[%s:%d] ", ro->rec_channel, ro->rec_port );
204 if( ro->pidfile ) {
205 (void)fprintf( stream, "Pidfile=[%s] ", ro->pidfile );
207 if( ro->dstfile ) {
208 (void)fprintf( stream, "Destination file=[%s] ", ro->dstfile );
210 if( ro->waitupd_sec > 0 ) {
211 (void)fprintf( stream, "Update-wait=[%d] sec ", ro->waitupd_sec );
214 (void) fputs( "\n", stream );
215 return;
218 #endif /* UDPXREC_MOD */
220 /* set verbose output on
222 void
223 set_verbose( flag_t* verbose )
225 assert( verbose );
226 *verbose = uf_TRUE;
231 /* __EOF__ */