1 /* @(#) definition of packet-io 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 #ifndef UDPXY_DPKTH_02182008
22 #define UDPXY_DPKTH_02182008
23 
24 #include <stdio.h>
25 #include "udpxy.h"
26 
27 typedef int upxfmt_t;
28 
29 
30 /*  data stream context
31  */
32 struct dstream_ctx {
33     upxfmt_t        stype;
34     int32_t         flags;
35     size_t          mtu;
36 
37     struct iovec*   pkt;
38     int32_t         max_pkt,
39                     pkt_count;
40 };
41 
42 /* data-stream context flags
43  */
44 static const int32_t F_DROP_PACKET = (1 << 1);
45 static const int32_t F_CHECK_FMT   = (1 << 2);
46 static const int32_t F_SCATTERED   = (1 << 3);
47 static const int32_t F_FILE_INPUT  = (1 << 4);
48 
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /* return human-readable name of file/stream format
55 */
56 const char* fmt2str( upxfmt_t fmt );
57 
58 
59 /* determine type of stream saved in file
60  */
61 upxfmt_t
62 get_fstream_type( int fd, FILE* log );
63 
64 /* determine type of stream in memory
65  */
66 upxfmt_t
67 get_mstream_type( const char* data, size_t len, FILE* log );
68 
69 
70 /* read record of one of the supported types from file
71  */
72 ssize_t
73 read_frecord( int fd, char* data, const size_t len,
74              upxfmt_t* stream_type, FILE* log );
75 
76 
77 /* write record after converting it from source into destination
78  * format
79  */
80 ssize_t
81 write_frecord( int fd, const char* data, size_t len,
82               upxfmt_t sfmt, upxfmt_t dfmt, FILE* log );
83 
84 
85 /* reset packet-buffer registry in stream context
86  */
87 void
88 reset_pkt_registry( struct dstream_ctx* ds );
89 
90 
91 /* release resources allocated for stream context
92  */
93 void
94 free_dstream_ctx( struct dstream_ctx* ds );
95 
96 
97 /* initialize incoming-stream context:
98  *      set data type (if possible) and flags
99  */
100 int
101 init_dstream_ctx( struct dstream_ctx* ds, const char* cmd, const char* fname,
102                   ssize_t nmsgs );
103 
104 
105 /* read data from source of specified type (UDP socket or otherwise);
106  * read as many fragments as specified (max_frgs) into the buffer
107  */
108 struct rdata_opt {
109     int     max_frgs;   /* max fragments to read in */
110     time_t  buf_tmout;  /* max time (sec) to hold data in buffer */
111 };
112 
113 ssize_t
114 read_data( struct dstream_ctx* spc, int fd, char* data,
115            const ssize_t data_len, const struct rdata_opt* opt );
116 
117 
118 /* write data to destination(s)
119  */
120 ssize_t
121 write_data( const struct dstream_ctx* spc,
122             const char* data,
123             const ssize_t len,
124             int fd );
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* UDPXY_DPKTH_02182008 */
131 
132 /* __EOF__ */
133 
134