aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ppsocket.h
blob: b63f3a00a21ed0367fe72a0acd5321a7fa7f8c49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*-*-c++-*-
 * $Id$
 *
 * This file is part of plptools.
 *
 *  Copyright (C) 1999  Philip Proudman <philip.proudman@btinternet.com>
 *  Copyright (C) 1999-2001 Fritz Elfert <felfert@to.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef _PPSOCKET_H_
#define _PPSOCKET_H_

#include <string>

#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>

class bufferStore;
class IOWatch;

/**
 * A class for dealing with sockets.
 */
class ppsocket
{

public:
    /**
    * Constructs a ppsocket
    */
    ppsocket();

    /**
    * Copy constructor
    */
    ppsocket(const ppsocket&);

    /**
    * Destructor
    */
    virtual ~ppsocket();
	  
    /**
    * Connects to a given host.
    *
    * @param Peer     The Host to connect to (name or dotquad-string).
    * @param PeerPort The port to connect to.
    * @param Host     The local address to bind to.
    * @param HostPort The local port to bind to.
    *
    * @returns true on success, false otherwise.
    */
    virtual bool connect(const char * const Peer, int PeerPort, const char * const Host = NULL, int HostPort = 0);

    /**
    * Reopens the connection after closing it.
    *
    * @returns true on success, false otherwise.
    */
    virtual bool reconnect();

    /**
    * Retrieve a string representation of the ppsocket.
    *
    * @returns a string in the form "<host>:<hostport> -> <peer>:<peerport>"
    *          where elements not known, are replaced by "???" and none-existing
    *          elements are represented by the word "none".
    */
    virtual std::string toString();

    /**
    * Starts listening.
    *
    * @param Host The local address to bind to.
    * @param Port The local port to listen on.
    *
    * @returns true on success, false otherwise.
    */
    virtual bool listen(const char * const Host, int Port);

    /**
    * Accept a connection.
    *
    * @param Peer If non-Null, the peer's name is returned here.
    *
    * @returns A pointer to a new instance for the accepted connection or NULL
    *          if an error happened.
    */
    ppsocket *accept(std::string *Peer, IOWatch *);

    /**
    * Check and optionally wait for incoming data.
    *
    * @param sec Timeout in seconds
    * @param usec Timeout in microseconds
    *
    * @returns true if data is available, false otherwise.
    */
    bool dataToGet(int sec, int usec) const;

    /**
    * Receive data into a @ref bufferStore .
    *
    * @param a The bufferStore to fill with received data.
    * @param wait If true, wait until something is received, else return
    *              if no data is available.
    * @returns 1 if a bufferStore received, 0, if no bufferStore received, -1
    *          on error.
    */
    int getBufferStore(bufferStore &a, bool wait = true);

    /**
    * Sends data from a @ref bufferStore .
    *
    * @param a The bufferStore to send.
    * @returns true on success, false otherwise.
    */
    bool sendBufferStore(const bufferStore &a);

    /**
    * Closes the connection.
    *
    * @returns true on success, false otherwise.
    */
    bool closeSocket(void);

    /**
    * Binds to a local address and port.
    *
    * @param Host The local address to bind to.
    * @param Port The local port to listen on.
    *
    * @returns true on success, false otherwise.
    */
    bool bindSocket(const char * const Host, int Port);

    /**
    * Tries repeated binds to a local address and port.
    * If @p Retries is <= @p High - @p Low, then
    * the port to bind is randomly chosen in the given range.
    * Otherwise, all ports starting from @p High up to @p Low
    * are tried in sequence.
    *
    * @param Host The local address to bind to.
    * @param Low  The lowest local port to listen on.
    * @param High The highest local port to listen on.
    * @param Retries The number of retries until giving up.
    *
    * @returns true on success, false otherwise.
    */
    bool bindInRange(const char * const Host, int Low, int High, int Retries);

    /**
    * Sets the linger parameter of the socket.
    *
    * @param LingerOn true, if lingering should be on.
    * @param LingerTime If lingering is on, the linger-time.
    *
    * @returns true on success, false otherwise.
    */
    bool linger(bool LingerOn, int LingerTime = 0);

    /**
    * Retrieves peer information.
    *
    * @param Peer The peers name is returned here.
    * @param Port The peers port is returned here.
    *
    * @returns true on success, false otherwise.
    */
    bool getPeer(std::string *Peer, int *Port);

    /**
    * Retrieves local information.
    *
    * @param Host The local name is returned here.
    * @param Port The local port is returned here.
    *
    * @returns true on success, false otherwise.
    */
    bool getHost(std::string *Host, int *Port);

    /**
    * Registers an @ref IOWatch for this socket.
    * This IOWatch gets the socket added/removed
    * automatically.
    *
    * @param watch The IOWatch to register.
    */
    void setWatch(IOWatch *watch);
	
private:
    /**
    * Creates the socket.
    */
    virtual bool createSocket(void);

    int getLastError(void) { return(m_LastError); }
    bool setPeer(const char * const Peer, int Port);
    bool setHost(const char * const Host, int Port);
    int recv(void *buf, int len, int flags);
    int send(const void * const buf, int len, int flags);
	
    struct sockaddr m_HostAddr;
    struct sockaddr m_PeerAddr;
    int m_Socket;
    int m_Port;
    bool m_Bound;
    int m_LastError;
    IOWatch *myWatch;
};

#endif

/*
 * Local variables:
 * c-basic-offset: 4
 * End:
 */