1/*************************************************************************
2 * libjson-rpc-cpp
3 *************************************************************************
4 * @file linuxtcpsocketserver.h
5 * @date 17.07.2015
6 * @author Alexandre Poirot <alexandre.poirot@legrand.fr>
7 * @license See attached LICENSE.txt
8 ************************************************************************/
9
10#ifndef JSONRPC_CPP_LINUXTCPSOCKETSERVERCONNECTOR_H_
11#define JSONRPC_CPP_LINUXTCPSOCKETSERVERCONNECTOR_H_
12
13#include <arpa/inet.h>
14#include <netinet/in.h>
15#include <pthread.h>
16#include <stdarg.h>
17#include <stdint.h>
18#include <sys/socket.h>
19#include <sys/time.h>
20#include <sys/types.h>
21#include <unistd.h>
22
23#include "../abstractthreadedserver.h"
24
25namespace jsonrpc {
26 /**
27 * This class is the Linux/UNIX implementation of TCPSocketServer.
28 * It uses the POSIX socket API and POSIX thread API to performs its job.
29 * Each client request is handled in a new thread.
30 */
31 class LinuxTcpSocketServer : public AbstractThreadedServer {
32 public:
33 /**
34 * @brief LinuxTcpSocketServer, constructor of the Linux/UNIX
35 * implementation of class TcpSocketServer
36 * @param ipToBind The ipv4 address on which the server should
37 * bind and listen
38 * @param port The port on which the server should bind and listen
39 */
40 LinuxTcpSocketServer(const std::string &ipToBind, const unsigned int &port, size_t threads = 1);
41
42 virtual ~LinuxTcpSocketServer();
43
44 virtual bool InitializeListener();
45 virtual int CheckForConnection();
46 virtual void HandleConnection(int connection);
47
48 protected:
49 std::string ipToBind;
50 unsigned int port;
51 int socket_fd;
52 struct sockaddr_in address;
53
54 /**
55 * @brief A method that wait for the client to close the tcp session
56 *
57 * This method wait for the client to close the tcp session in order to avoid
58 * the server to enter in TIME_WAIT status.
59 * Entering in TIME_WAIT status with too many clients may occur in a DOS
60 * attack
61 * since server will not be able to use a new socket when a new client
62 * connects.
63 * @param fd The file descriptor of the socket that should be closed by the
64 * client
65 * @param timeout The maximum time the server will wait for the client to
66 * close the tcp session in microseconds.
67 * @returns A boolean indicating the success or the failure of the operation
68 */
69 bool WaitClientClose(const int &fd, const int &timeout = 100000);
70 /**
71 * @brief A method that close a socket by reseting it
72 *
73 * This method reset the tcp session in order to avoid enter in TIME_WAIT
74 * state.
75 * @param fd The file descriptor of the socket that should be reset
76 * @returns The return value of POSIX close() method
77 */
78 int CloseByReset(const int &fd);
79 /**
80 * @brief A method that cleanly close a socket by avoid TIME_WAIT state
81 *
82 * This method uses WaitClientClose and ClodeByReset to clenly close a tcp
83 * session with a client
84 * (avoiding TIME_WAIT to avoid DOS attacks).
85 * @param fd The file descriptor of the socket that should be cleanly closed
86 * @returns The return value of POSIX close() method
87 */
88 int CleanClose(const int &fd);
89 };
90
91} /* namespace jsonrpc */
92#endif /* JSONRPC_CPP_LINUXTCPSOCKETSERVERCONNECTOR_H_ */
93