1 //==============================================================================
2 //
3 //  This file is part of GPSTk, the GPS Toolkit.
4 //
5 //  The GPSTk is free software; you can redistribute it and/or modify
6 //  it under the terms of the GNU Lesser General Public License as published
7 //  by the Free Software Foundation; either version 3.0 of the License, or
8 //  any later version.
9 //
10 //  The GPSTk is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU Lesser General Public License for more details.
14 //
15 //  You should have received a copy of the GNU Lesser General Public
16 //  License along with GPSTk; if not, write to the Free Software Foundation,
17 //  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
18 //
19 //  This software was developed by Applied Research Laboratories at the
20 //  University of Texas at Austin.
21 //  Copyright 2004-2020, The Board of Regents of The University of Texas System
22 //
23 //==============================================================================
24 
25 //==============================================================================
26 //
27 //  This software was developed by Applied Research Laboratories at the
28 //  University of Texas at Austin, under contract to an agency or agencies
29 //  within the U.S. Department of Defense. The U.S. Government retains all
30 //  rights to use, duplicate, distribute, disclose, or release this software.
31 //
32 //  Pursuant to DoD Directive 523024
33 //
34 //  DISTRIBUTION STATEMENT A: This software has been approved for public
35 //                            release, distribution is unlimited.
36 //
37 //==============================================================================
38 
39 #ifndef TCPSTREAMBUFF_HPP
40 #define TCPSTREAMBUFF_HPP
41 
42 #include <sstream>
43 #include <errno.h>
44 #include <stdlib.h>
45 
46 #include <unistd.h>
47 #include <netdb.h>
48 #include <fcntl.h>
49 #include <sys/file.h>
50 #include <netinet/in.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
53 
54 #include "FDStreamBuff.hpp"
55 
56 namespace gpstk
57 {
58    class SocketAddr;
59 
60    class IPaddress
61    {
62       friend class SocketAddr;
63 
64       unsigned long address;                // Address: 4 bytes in the network byte order
IPaddress(const unsigned int netaddr)65       IPaddress(const unsigned int netaddr) : address(netaddr) {}
66 
67    public:
IPaddress()68       IPaddress() : address(INADDR_ANY) {}   // Wildcard address
69       IPaddress(const std::string& name);         // Involves the name resolution
net_addr() const70       unsigned long net_addr() const { return address; }
71 
72       friend std::ostream& operator <<(std::ostream& os, const IPaddress addr);
73       friend std::ostream& operator <<(std::ostream& os, const SocketAddr& addr);
74    };
75 
76    class SocketAddr : sockaddr_in
77    {
78       friend class StreamSocket;
79       friend class UDPsocketIn;
SocketAddr()80       SocketAddr() {}
81 
82    public:
83       SocketAddr(const IPaddress host, const short port_no);
operator sockaddr*() const84       operator sockaddr * () const      { return (sockaddr *)this; }
85       friend std::ostream& operator <<(std::ostream& os, const SocketAddr& addr);
86    };
87 
88 
89    class TCPStreamBuff : public FDStreamBuff
90    {
91    public:
TCPStreamBuff()92       TCPStreamBuff() : FDStreamBuff(-1) {}
93 
~TCPStreamBuff()94       ~TCPStreamBuff() {close();}
95 
96       int connect(const SocketAddr target_address);
97 
98       // Take a file handle (which is supposed to be a listening socket),
99       // accept a connection if any,  and return the corresponding TCPbuf
100       // for that connection. On exit, peeraddr would be an addr of the
101       // connected peer
102       int accept(int listening_socket, SocketAddr& peeraddr);
103 
104    };
105 
106 } // end of namespace
107 #endif
108