Scopira  20080306
netflow.h
1 
2 /*
3  * Copyright (c) 2002-2003 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_TOOL_NETFLOW_H__
15 #define __INCLUDED_SCOPIRA_TOOL_NETFLOW_H__
16 
17 #include <list>
18 
19 #include <scopira/tool/export.h>
20 #include <scopira/tool/array.h>
21 #include <scopira/tool/flow.h>
22 #include <scopira/tool/platform.h>
23 
24 namespace scopira
25 {
26  namespace tool
27  {
28 #ifdef PLATFORM_win32
29  typedef SOCKET socket_handle_t;
30 #else
31  typedef int socket_handle_t;
32 #endif
33 
34  class netaddr;
35  class nethostrec;
36 
37  class netflow; // TCP
38  class udpflow;
39 
40  class unixflow;
41 
42  class net_loop;
43 
45  SCOPIRA_EXPORT std::string get_hostname(void);
47  SCOPIRA_EXPORT bool hostname_to_hostrec(const std::string& hname, nethostrec& rec);
48  }
49 }
50 
51 // win32 implementation notice:
52 // users dont link to the winsock lib, so DONT
53 // inline any winsock calls
54 
60 class scopira::tool::netaddr : public scopira::tool::fixed_array<unsigned char,4>
61 {
62  public:
64  SCOPIRA_EXPORT netaddr(void);
66  SCOPIRA_EXPORT netaddr(int _ip0, int _ip1, int _ip2, int _ip3);
67 
69  static netaddr zero(void) { return netaddr(0, 0, 0, 0); }
71  static netaddr any(void) { return zero(); }
73  static netaddr broadcast(void) { return netaddr(255, 255, 255, 255); }
75  static netaddr localhost(void) { return netaddr(); }
76 
78  SCOPIRA_EXPORT std::string as_string(void) const;
80  SCOPIRA_EXPORT bool parse_string(const std::string &s);
81 };
82 
83 SCOPIRA_EXPORT scopira::tool::oflow_i& operator <<(scopira::tool::oflow_i& o, const scopira::tool::netaddr &addr);
84 
92 {
93  public: // all public, like a struct
94 
95  typedef std::list < std::string > strlist_t;
96  typedef std::list < netaddr > addrlist_t;
97 
98  std::string name;
99  strlist_t aliases; // list of <stringobj>
100  int addrlen; // length of an address
101  addrlist_t addrlist; // list of <netaddr>
102 
103  public:
105  SCOPIRA_EXPORT nethostrec(void);
107  SCOPIRA_EXPORT virtual ~nethostrec(void);
108 
110  SCOPIRA_EXPORT void clear(void);
111 
113  SCOPIRA_EXPORT const netaddr & get_addr(void) const;
114 };
115 
116 SCOPIRA_EXPORT scopira::tool::oflow_i& operator <<(scopira::tool::oflow_i& o, const scopira::tool::nethostrec &recs);
117 
124 {
125  public:
126  enum {
127  tcp_nodelay_c = 1,
128  };
129  private: // typedef socket handles to a common name
131  socket_handle_t dm_sock;
132 
134  bool dm_open;
136  bool dm_fail;
138  bool dm_sfail;
140  bool dm_server;
142  int dm_port;
144  netaddr dm_addr;
145 
146  public:
152  SCOPIRA_EXPORT netflow(void);
161  SCOPIRA_EXPORT netflow(const netaddr * _addr, int _port, int socket_options = 0);
163  SCOPIRA_EXPORT virtual ~netflow();
164 
166  SCOPIRA_EXPORT virtual bool failed(void) const;
167 
169  SCOPIRA_EXPORT virtual size_t read(byte_t* _buf, size_t _maxsize);
171  SCOPIRA_EXPORT virtual size_t write(const byte_t* _buf, size_t _size);
172 
176  SCOPIRA_EXPORT size_t read_short(byte_t* _buf, size_t _maxsize);
177 
184  SCOPIRA_EXPORT void open(const netaddr * _addr, int _port, int socket_options = 0);
186  SCOPIRA_EXPORT void close(void);
187 
190  SCOPIRA_EXPORT bool accept(netflow& nconn);
191 
202  SCOPIRA_EXPORT bool read_ready(int msec);
203 
205  const netaddr& get_addr(void) const { return dm_addr; }
207  int get_port(void) const { return dm_port; }
208 
209  protected:
211  SCOPIRA_EXPORT void open_relay(socket_handle_t _fd, const flow_i::byte_t* _addr, int _addrlen, int _port);
212 
213 };
214 
228 {
229  public:
230 #ifdef PLATFORM_win32
231  SCOPIRA_EXPORT net_loop(void);
234  SCOPIRA_EXPORT ~net_loop();
235 #else
236  SCOPIRA_EXPORT net_loop(void) { }
240  SCOPIRA_EXPORT ~net_loop() { }
241 #endif
242 };
243 
250 {
251  public:
253  SCOPIRA_EXPORT udpflow(void);
255  SCOPIRA_EXPORT udpflow(int _port);
257  SCOPIRA_EXPORT virtual ~udpflow();
258 
260  bool failed(void) const { return dm_fail; }
261 
267  SCOPIRA_EXPORT size_t read(netaddr &actualsrc, int &actualport, byte_t* _buf, size_t _maxsize);
271  SCOPIRA_EXPORT size_t write(netaddr dest, int destport, const byte_t* _buf, size_t _size);
272 
274  SCOPIRA_EXPORT void open(int _port);
276  SCOPIRA_EXPORT void close(void);
277 
280  SCOPIRA_EXPORT bool read_ready(int msec);
281 
283  int get_port(void) const { return dm_port; }
284 
285  private:
286  int dm_port;
287  socket_handle_t dm_sock;
288  bool dm_fail;
289  bool dm_open;
290 };
291 
292 #ifndef PLATFORM_win32
293 
302 {
303  protected:
304  std::string dm_filename;
305  bool dm_server, dm_open, dm_fail, dm_sfail;
306  int dm_sock;
307 
308  public:
310  unixflow(void);
312  unixflow(const std::string &filename, bool server_mode);
314  virtual ~unixflow();
315 
317  virtual bool failed(void) const;
318 
320  virtual size_t read(byte_t* _buf, size_t _maxsize);
322  virtual size_t write(const byte_t* _buf, size_t _size);
323 
325  void open(const std::string &filename, bool server_mode);
327  void close(void);
328 
331  bool accept(unixflow& nconn);
332 
335  bool read_ready(int msec);
336 
338  const std::string &get_filename(void) const { return dm_filename; }
340  bool is_server(void) const { return dm_server; }
341 
342  protected:
344  void open_relay(int _fd, const std::string &filename);
345 };
346 #endif //PLATFORM_win32
347 
348 #endif
const netaddr & get_addr(void) const
gets the address
Definition: netflow.h:205
Definition: netflow.h:91
static netaddr localhost(void)
makes and returns localhost
Definition: netflow.h:75
static netaddr any(void)
makes and returns the any address
Definition: netflow.h:71
Definition: flow.h:108
int get_port(void) const
gets the port
Definition: netflow.h:283
~net_loop()
dtor
Definition: netflow.h:240
netaddr(void)
default constructor, inits to local host
scopira::tool::byte_t byte_t
Definition: flow.h:73
Definition: archiveflow.h:20
bool is_server(void) const
is this a server socket?
Definition: netflow.h:340
Definition: object.h:71
const std::string & get_filename(void) const
gets the address
Definition: netflow.h:338
std::string get_hostname(void)
gets the hostname of this machine
Definition: netflow.h:249
Definition: netflow.h:123
Definition: netflow.h:227
std::string as_string(void) const
return x.x.x.x form
bool failed(void) const
in a failed state?
Definition: netflow.h:260
Definition: netflow.h:60
Definition: netflow.h:301
Definition: array.h:32
bool parse_string(const std::string &s)
parse from a string
int get_port(void) const
gets the port
Definition: netflow.h:207
static netaddr broadcast(void)
makes and returns the bcast address
Definition: netflow.h:73
static netaddr zero(void)
makes and returns the zero address
Definition: netflow.h:69
bool hostname_to_hostrec(const std::string &hname, nethostrec &rec)
looks up the hname, and saves into rec, returns true on success
Definition: flow.h:159