| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/***************************************************************************** |
| 2 |
|
|
* |
| 3 |
|
|
* Copyright 2017 Richard Hacker (lerichi at gmx dot net) |
| 4 |
|
|
* |
| 5 |
|
|
* This file is part of the pdserv library. |
| 6 |
|
|
* |
| 7 |
|
|
* The pdserv library is free software: you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the GNU Lesser General Public License as published |
| 9 |
|
|
* by the Free Software Foundation, either version 3 of the License, or (at |
| 10 |
|
|
* your option) any later version. |
| 11 |
|
|
* |
| 12 |
|
|
* The pdserv library is distributed in the hope that it will be useful, but |
| 13 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 14 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 15 |
|
|
* License for more details. |
| 16 |
|
|
* |
| 17 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
|
|
* along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
|
* |
| 20 |
|
|
****************************************************************************/ |
| 21 |
|
|
|
| 22 |
|
|
#ifndef TCP_H |
| 23 |
|
|
#define TCP_H |
| 24 |
|
|
|
| 25 |
|
|
#include <stdint.h> |
| 26 |
|
|
#include <unistd.h> // ssize_t |
| 27 |
|
|
#include <ostream> |
| 28 |
|
|
#include <vector> |
| 29 |
|
|
#include <string> |
| 30 |
|
|
#include <sys/socket.h> // struct sockaddr |
| 31 |
|
|
#include <netinet/tcp.h> // setsockopt() |
| 32 |
|
|
|
| 33 |
|
|
#include "PThread.h" |
| 34 |
|
|
|
| 35 |
|
|
namespace net { |
| 36 |
|
|
|
| 37 |
|
|
// Open a server socket. |
| 38 |
|
|
class TCPSocket { |
| 39 |
|
|
public: |
| 40 |
|
|
// Returns: true if file descriptor is valid |
| 41 |
|
|
operator bool() const; |
| 42 |
|
|
|
| 43 |
|
|
void setSockOpt(int level, int name, int val); |
| 44 |
|
|
|
| 45 |
|
|
// return: string <IP><sep><port> of listen socket |
| 46 |
|
|
std::string getAddr(char sep = ':') const; |
| 47 |
|
|
|
| 48 |
|
|
// select between multiple sockets. returns first available |
| 49 |
|
|
// socket or NULL |
| 50 |
|
|
static TCPSocket* select(std::vector<TCPSocket*> const& sockets, |
| 51 |
|
|
int msec = -1); |
| 52 |
|
|
|
| 53 |
|
|
protected: |
| 54 |
|
|
/// Constructor |
| 55 |
|
|
TCPSocket(); |
| 56 |
|
|
~TCPSocket(); |
| 57 |
|
|
|
| 58 |
|
|
void close(); |
| 59 |
|
|
|
| 60 |
|
|
// Returns success |
| 61 |
|
|
// throws std::string() on error |
| 62 |
|
|
bool listenTo( |
| 63 |
|
|
const std::string& interface, const std::string& port, |
| 64 |
|
|
int backlog); |
| 65 |
|
|
|
| 66 |
|
|
// return: string of remote host |
| 67 |
|
|
// thows: std::string on error |
| 68 |
|
|
std::string accept(TCPSocket* server); |
| 69 |
|
|
|
| 70 |
|
|
std::string reject(); |
| 71 |
|
|
|
| 72 |
|
|
bool readable(struct timeval *timeout) const; |
| 73 |
|
|
ssize_t readData ( void *buf, size_t len); |
| 74 |
|
|
ssize_t writeData(const void *buf, size_t len); |
| 75 |
|
|
|
| 76 |
|
|
private: |
| 77 |
|
|
int fd; |
| 78 |
|
|
struct sockaddr addr; |
| 79 |
|
|
}; |
| 80 |
|
|
|
| 81 |
|
224 |
class TCPServer: public TCPSocket { |
| 82 |
|
|
public: |
| 83 |
|
|
TCPServer(int backlog); |
| 84 |
|
|
|
| 85 |
|
|
// Returns success |
| 86 |
|
|
// throws std::string() on error |
| 87 |
|
|
bool listenTo( |
| 88 |
|
|
const std::string& interface, const std::string& port); |
| 89 |
|
|
|
| 90 |
|
|
bool isPendingConnection(int msec = -1); |
| 91 |
|
|
std::string reject(); |
| 92 |
|
|
|
| 93 |
|
|
private: |
| 94 |
|
|
int backlog; |
| 95 |
|
|
}; |
| 96 |
|
|
|
| 97 |
|
|
class TCPSession: public pthread::Thread, public TCPSocket { |
| 98 |
|
|
public: |
| 99 |
|
|
// Accept a connection |
| 100 |
|
|
// throws: std::string on error |
| 101 |
|
|
TCPSession(TCPServer *); |
| 102 |
|
|
|
| 103 |
|
|
// return: reverse DNS lookup peer name |
| 104 |
|
|
std::string getPeerName() const; |
| 105 |
|
|
|
| 106 |
|
|
// Wait for socket read to become ready |
| 107 |
|
|
bool isPendingRead(int msec) const; |
| 108 |
|
|
|
| 109 |
|
|
// read() from socket |
| 110 |
|
|
// |
| 111 |
|
|
// return: >=0: bytes read from socket |
| 112 |
|
|
// <0: -errno on error |
| 113 |
|
|
ssize_t readData ( void *buf, size_t len); |
| 114 |
|
|
|
| 115 |
|
|
// write() to socket |
| 116 |
|
|
// |
| 117 |
|
|
// return: >=0: bytes written to socket |
| 118 |
|
|
// <0: -errno on error |
| 119 |
|
|
ssize_t writeData(const void *buf, size_t len); |
| 120 |
|
|
|
| 121 |
|
|
protected: |
| 122 |
|
148 |
~TCPSession() {} |
| 123 |
|
|
|
| 124 |
|
|
private: |
| 125 |
|
|
std::string peerName; |
| 126 |
|
|
}; |
| 127 |
|
|
} |
| 128 |
|
|
|
| 129 |
|
|
#endif // TCP_H |
| 130 |
|
|
|