1/* @(#) option-associated functions for udpxy 2 * 3 * Copyright 2008-2011 Pavel V. Cherenkov (pcherenkov@gmail.com) 4 * 5 * This file is part of udpxy. 6 * 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. 11 * 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. 16 * 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/>. 19 */ 20 21#include <assert.h> 22#include <stdlib.h> 23#include <errno.h> 24#include <string.h> 25 26#include "udpxy.h" 27#include "uopt.h" 28#include "util.h" 29 30 31static int 32read_http_footer (char* buf, size_t len) 33{ 34 const char* ev = getenv ("UDPXY_HTTP200_FTR_LN"); 35 ssize_t n = 0; 36 37 assert (buf && len); 38 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; 44 } 45 46 if (NULL != (ev = getenv ("UDPXY_HTTP200_FTR_FILE"))) { 47 n = txtf_read (ev, buf, len, stderr); 48 } 49 50 return (n<0) ? 1 : 0; 51} 52 53 54inline static void 55get_content_type(char *buf, size_t buflen) 56{ 57 static const char DEFAULT_CONTENT_TYPE[] = "Content-Type:application/octet-stream"; 58 const char* ev = getenv("UDPXY_CONTENT_TYPE"); 59 60 if (ev && *ev) 61 (void) snprintf(buf, buflen-1, "Content-Type:%s", ev); 62 else 63 strncpy(buf, DEFAULT_CONTENT_TYPE, buflen); 64 65 buf[buflen - 1] = '\0'; 66} 67 68 69/* populate options with default/initial values 70 */ 71int 72init_uopt( struct udpxy_opt* uo ) 73{ 74 int rc = 0; 75 assert( uo ); 76 77 uo->is_verbose = uf_FALSE; 78 uo->cl_tpstat = uf_FALSE; 79 uo->nice_incr = 0; 80 uo->rbuf_len = DEFAULT_CACHE_LEN; 81 uo->rbuf_msgs = 1; 82 uo->max_clients = DEFAULT_CLIENT_COUNT; 83 uo->rcv_tmout = get_timeval( "UDPXY_RCV_TMOUT", 84 RLY_SOCK_TIMEOUT ); 85 uo->dhold_tmout = get_timeval( "UDPXY_DHOLD_TMOUT", 86 DHOLD_TIMEOUT ); 87 uo->sr_tmout = get_timeval ( "UDPXY_SREAD_TMOUT", SRVSOCK_TIMEOUT ); 88 uo->sw_tmout = get_timeval ( "UDPXY_SWRITE_TMOUT", SRVSOCK_TIMEOUT ); 89 uo->ssel_tmout = get_timeval ( "UDPXY_SSEL_TMOUT", SSEL_TIMEOUT ); 90 uo->lq_backlog = (int) get_sizeval ("UDPXY_LQ_BACKLOG", LQ_BACKLOG); 91 uo->rcv_lwmark = (int) get_sizeval ("UDPXY_RCV_LWMARK", RCV_LWMARK); 92 93 uo->nosync_sbuf = (u_short)get_flagval( "UDPXY_SSOCKBUF_NOSYNC", 0 ); 94 uo->nosync_dbuf = (u_short)get_flagval( "UDPXY_DSOCKBUF_NOSYNC", 0 ); 95 96 uo->srcfile = NULL; 97 uo->dstfile = NULL; 98 uo->mcast_refresh = DEFAULT_MCAST_REFRESH; 99 100 if (-1 == read_http_footer (uo->h200_ftr, sizeof(uo->h200_ftr))) { 101 rc = -1; /* modify rc only if there is an error */ 102 } 103 104 get_content_type(uo->cnt_type, sizeof(uo->cnt_type)); 105 assert( uo->cnt_type[0] ); 106 107 uo->tcp_nodelay = (flag_t)get_flagval( "UDPXY_TCP_NODELAY", 1); 108 return rc; 109} 110 111 112/* release resources allocated for udpxy options 113 */ 114void 115free_uopt( struct udpxy_opt* uo ) 116{ 117 assert( uo ); 118 119 if( uo->srcfile ) { 120 free( uo->srcfile ); uo->srcfile = NULL; 121 } 122 123 if( uo->dstfile ) { 124 free( uo->dstfile ); uo->dstfile = NULL; 125 } 126} 127 128 129#ifdef UDPXREC_MOD 130 131/* populate udpxrec options with default/initial values 132 */ 133int 134init_recopt( struct udpxrec_opt* ro ) 135{ 136 int rc = 0; 137 138 assert( ro ); 139 140 ro->is_verbose = uf_FALSE; 141 ro->nice_incr = 0; 142 143 ro->bg_time = ro->end_time = 0; 144 ro->max_fsize = 0; 145 ro->bufsize = DEFAULT_CACHE_LEN; 146 ro->rbuf_msgs = 1; 147 148 ro->mcast_addr[0] = '\0'; 149 150 ro->dstfile = NULL; 151 ro->pidfile = NULL; 152 ro->rec_channel[0] = '\0'; 153 ro->rec_port = 0; 154 ro->waitupd_sec = -1; 155 156 ro->nosync_sbuf = 157 (flag_t)get_flagval( "UDPXY_SSOCKBUF_NOSYNC", 0 ); 158 ro->nosync_dbuf = 159 (flag_t)get_flagval( "UDPXY_DSOCKBUF_NOSYNC", 0 ); 160 161 ro->rcv_tmout = 0; 162 163 return rc; 164} 165 166 167/* release resources allocated for udpxy options 168 */ 169void 170free_recopt( struct udpxrec_opt* ro ) 171{ 172 assert( ro ); 173 174 if( ro->dstfile ) { 175 free( ro->dstfile ); ro->dstfile = NULL; 176 } 177 if( ro->pidfile ) { 178 free( ro->pidfile ); ro->pidfile = NULL; 179 } 180 181 ro->rec_channel[0] = '\0'; 182} 183 184 185/* print udpxrec options to stream 186 */ 187void 188fprint_recopt( FILE* stream, struct udpxrec_opt* ro ) 189{ 190 assert( stream && ro ); 191 192 if( ro->is_verbose ) { 193 (void)fprintf( stream, "verbose=[ON] " ); 194 } 195 if( ro->nice_incr ) { 196 (void)fprintf( stream, "nice_incr=[%d] ", ro->nice_incr ); 197 } 198 if( ro->bg_time > 0 ) { 199 (void)fprintf( stream, "begin_time=[%s] ", 200 Zasctime( localtime(&(ro->bg_time) )) ); 201 } 202 if( ro->end_time > 0 ) { 203 (void)fprintf( stream, "end_time=[%s] ", 204 Zasctime( localtime(&(ro->end_time) )) ); 205 } 206 if( ro->max_fsize > 0 ) { 207 (void)fprintf( stream, "Max filesize=[%.0f] bytes ", 208 (double)ro->max_fsize ); 209 } 210 211 (void)fprintf( stream, "Buffer size=[%ld] bytes ", 212 (long)ro->bufsize ); 213 214 if( ro->rbuf_msgs > 0 ) { 215 (void)fprintf( stream, "Max messages=[%ld] ", 216 (long)ro->rbuf_msgs ); 217 } 218 if( ro->mcast_addr[0] ) { 219 (void)fprintf( stream, "Multicast interface=[%s] ", ro->mcast_addr ); 220 } 221 if( ro->rec_channel[0] ) { 222 (void)fprintf( stream, "Channel=[%s:%d] ", ro->rec_channel, ro->rec_port ); 223 } 224 if( ro->pidfile ) { 225 (void)fprintf( stream, "Pidfile=[%s] ", ro->pidfile ); 226 } 227 if( ro->dstfile ) { 228 (void)fprintf( stream, "Destination file=[%s] ", ro->dstfile ); 229 } 230 if( ro->waitupd_sec > 0 ) { 231 (void)fprintf( stream, "Update-wait=[%d] sec ", ro->waitupd_sec ); 232 } 233 234 (void) fputs( "\n", stream ); 235 return; 236} 237 238#endif /* UDPXREC_MOD */ 239 240/* set verbose output on 241 */ 242void 243set_verbose( flag_t* verbose ) 244{ 245 assert( verbose ); 246 *verbose = uf_TRUE; 247} 248 249 250 251/* __EOF__ */ 252 253