Scopira 20080306

netflow.h

00001 
00002 /*
00003  *  Copyright (c) 2002-2003    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013  
00014 #ifndef __INCLUDED_SCOPIRA_TOOL_NETFLOW_H__
00015 #define __INCLUDED_SCOPIRA_TOOL_NETFLOW_H__
00016 
00017 #include <list>
00018 
00019 #include <scopira/tool/export.h>
00020 #include <scopira/tool/array.h>
00021 #include <scopira/tool/flow.h>
00022 #include <scopira/tool/platform.h>
00023 
00024 namespace scopira
00025 {
00026   namespace tool
00027   {
00028 #ifdef PLATFORM_win32
00029     typedef SOCKET socket_handle_t;
00030 #else
00031     typedef int socket_handle_t;
00032 #endif
00033 
00034     class netaddr;
00035     class nethostrec;
00036 
00037     class netflow;  // TCP
00038     class udpflow;
00039 
00040     class unixflow;
00041 
00042     class net_loop;
00043     
00045     SCOPIRA_EXPORT std::string get_hostname(void);
00047     SCOPIRA_EXPORT bool hostname_to_hostrec(const std::string& hname, nethostrec& rec);
00048   }
00049 }
00050 
00051 // win32 implementation notice:
00052 // users dont link to the winsock lib, so DONT
00053 // inline any winsock calls
00054  
00060 class scopira::tool::netaddr : public scopira::tool::fixed_array<unsigned char,4>
00061 {
00062   public:
00064     SCOPIRA_EXPORT netaddr(void);
00066     SCOPIRA_EXPORT netaddr(int _ip0, int _ip1, int _ip2, int _ip3);
00067 
00069     static netaddr zero(void) { return netaddr(0, 0, 0, 0); }
00071     static netaddr any(void) { return zero(); }
00073     static netaddr broadcast(void) { return netaddr(255, 255, 255, 255); }
00075     static netaddr localhost(void) { return netaddr(); }
00076 
00078     SCOPIRA_EXPORT std::string as_string(void) const;
00080     SCOPIRA_EXPORT bool parse_string(const std::string &s);
00081 };
00082 
00083 SCOPIRA_EXPORT scopira::tool::oflow_i& operator <<(scopira::tool::oflow_i& o, const scopira::tool::netaddr &addr);
00084 
00091 class scopira::tool::nethostrec : public virtual scopira::tool::object
00092 {
00093   public: // all public, like a struct
00094 
00095     typedef std::list < std::string > strlist_t;
00096     typedef std::list < netaddr > addrlist_t;
00097 
00098     std::string name;
00099     strlist_t aliases;    // list of <stringobj>
00100     int addrlen;        // length of an address
00101     addrlist_t addrlist;   // list of <netaddr>
00102 
00103   public:
00105     SCOPIRA_EXPORT nethostrec(void);
00107     SCOPIRA_EXPORT virtual ~nethostrec(void);
00108 
00110     SCOPIRA_EXPORT void clear(void);
00111 
00113     SCOPIRA_EXPORT const netaddr & get_addr(void) const;
00114 };
00115 
00116 SCOPIRA_EXPORT scopira::tool::oflow_i& operator <<(scopira::tool::oflow_i& o, const scopira::tool::nethostrec &recs);
00117 
00123 class scopira::tool::netflow : public scopira::tool::iflow_i, public scopira::tool::oflow_i
00124 {
00125   public:
00126     enum {
00127       tcp_nodelay_c = 1,
00128     };
00129   private:    // typedef socket handles to a common name
00131     socket_handle_t dm_sock;
00132 
00134     bool dm_open;
00136     bool dm_fail;
00138     bool dm_sfail;
00140     bool dm_server;
00142     int dm_port;
00144     netaddr dm_addr;
00145 
00146   public:
00152     SCOPIRA_EXPORT netflow(void);
00161     SCOPIRA_EXPORT netflow(const netaddr * _addr, int _port, int socket_options = 0);
00163     SCOPIRA_EXPORT virtual ~netflow();
00164 
00166     SCOPIRA_EXPORT virtual bool failed(void) const;
00167 
00169     SCOPIRA_EXPORT virtual size_t read(byte_t* _buf, size_t _maxsize);
00171     SCOPIRA_EXPORT virtual size_t write(const byte_t* _buf, size_t _size);
00172 
00176     SCOPIRA_EXPORT size_t read_short(byte_t* _buf, size_t _maxsize);
00177 
00184     SCOPIRA_EXPORT void open(const netaddr * _addr, int _port, int socket_options = 0);
00186     SCOPIRA_EXPORT void close(void);
00187 
00190     SCOPIRA_EXPORT bool accept(netflow& nconn);
00191 
00202     SCOPIRA_EXPORT bool read_ready(int msec);
00203 
00205     const netaddr& get_addr(void) const { return dm_addr; }
00207     int get_port(void) const { return dm_port; }
00208 
00209   protected:
00211     SCOPIRA_EXPORT void open_relay(socket_handle_t _fd, const flow_i::byte_t* _addr, int _addrlen, int _port);
00212 
00213 };
00214 
00227 class scopira::tool::net_loop
00228 {
00229   public:
00230 #ifdef PLATFORM_win32
00231 
00232     SCOPIRA_EXPORT net_loop(void);
00234     SCOPIRA_EXPORT ~net_loop();
00235 #else
00236 
00237 
00238     SCOPIRA_EXPORT net_loop(void) { }
00240     SCOPIRA_EXPORT ~net_loop() { }
00241 #endif
00242 };
00243 
00249 class scopira::tool::udpflow : public virtual scopira::tool::object
00250 {
00251   public:
00253     SCOPIRA_EXPORT udpflow(void);
00255     SCOPIRA_EXPORT udpflow(int _port);
00257     SCOPIRA_EXPORT virtual ~udpflow();
00258 
00260     bool failed(void) const { return dm_fail; }
00261 
00267     SCOPIRA_EXPORT size_t read(netaddr &actualsrc, int &actualport, byte_t* _buf, size_t _maxsize);
00271     SCOPIRA_EXPORT size_t write(netaddr dest, int destport, const byte_t* _buf, size_t _size);
00272 
00274     SCOPIRA_EXPORT void open(int _port);
00276     SCOPIRA_EXPORT void close(void);
00277 
00280     SCOPIRA_EXPORT bool read_ready(int msec);
00281 
00283     int get_port(void) const { return dm_port; }
00284 
00285   private:
00286     int dm_port;    
00287     socket_handle_t dm_sock; 
00288     bool dm_fail;
00289     bool dm_open;   
00290 };
00291 
00292 #ifndef PLATFORM_win32 
00293 
00301 class scopira::tool::unixflow : public scopira::tool::iflow_i, public scopira::tool::oflow_i
00302 {
00303   protected:
00304     std::string dm_filename;
00305     bool dm_server, dm_open, dm_fail, dm_sfail;
00306     int dm_sock;
00307 
00308   public:
00310     unixflow(void);
00312     unixflow(const std::string &filename, bool server_mode);
00314     virtual ~unixflow();
00315 
00317     virtual bool failed(void) const;
00318 
00320     virtual size_t read(byte_t* _buf, size_t _maxsize);
00322     virtual size_t write(const byte_t* _buf, size_t _size);
00323 
00325     void open(const std::string &filename, bool server_mode);
00327     void close(void);
00328 
00331     bool accept(unixflow& nconn);
00332 
00335     bool read_ready(int msec);
00336 
00338     const std::string &get_filename(void) const { return dm_filename; }
00340     bool is_server(void) const { return dm_server; }
00341 
00342   protected:
00344     void open_relay(int _fd, const std::string &filename);
00345 };
00346 #endif    //PLATFORM_win32
00347 
00348 #endif