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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
|
/*
* This file is part of the KDE libraries
* Copyright (C) 1997 Torben Weis ([email protected])
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef KSOCK_H
#define KSOCK_H
#include "tdelibs_export.h"
#ifdef Q_OS_UNIX
#include <tqobject.h>
#include <sys/types.h>
// we define STRICT_ANSI to get rid of some warnings in glibc
#ifndef __STRICT_ANSI__
#define __STRICT_ANSI__
#define _WE_DEFINED_IT_
#endif
#include <sys/socket.h>
#ifdef _WE_DEFINED_IT_
#undef __STRICT_ANSI__
#undef _WE_DEFINED_IT_
#endif
#include <sys/un.h>
#include <netinet/in.h>
class TQSocketNotifier;
#ifdef KSOCK_NO_BROKEN
// This is here for compatibility with old applications still using the constants
// Never use them in new programs
// Here are a whole bunch of hackish macros that allow one to
// get at the correct member of ksockaddr_in
// But since ksockaddr_in is IPv4-only, and deprecated...
typedef sockaddr_in ksockaddr_in;
#define get_sin_addr(x) x.sin_addr
#define get_sin_port(x) x.sin_port
#define get_sin_family(x) x.sin_family
#define get_sin_paddr(x) x->sin_addr
#define get_sin_pport(x) x->sin_port
#define get_sin_pfamily(x) x->sin_family
#endif
#define KSOCK_DEFAULT_DOMAIN PF_INET
class TDESocketPrivate;
class TDEServerSocketPrivate;
/** @deprecated
* You can connect this socket to any Internet address.
*
* This class is deprecated and will be removed in the future. For new
* programs, please use KExtendedSocket class.
*
* The socket gives you three signals: When ready for reading,
* ready for writing or if the connection is broken.
* Using socket() you get a file descriptor
* which you can use with the usual UNIX function like write() or
* read().
* If you have already such a socket identifier you can construct a TDESocket
* on this identifier.
*
* If socket() delivers a value of -1 or less, the connection
* was not successful.
*
* @author Torben Weis <[email protected]>
* @short A TCP/IP client socket.
*/
class TDECORE_EXPORT TDESocket : public TQObject
{
TQ_OBJECT
public:
/**
* Constructs a TDESocket with the provided file descriptor.
* @param _sock The file descriptor to use.
*/
TDESocket( int _sock ) TDE_DEPRECATED;
/**
* Creates a socket and connects to a host.
* @param _host The remote host to which to connect.
* @param _port The port on the remote host.
* @param timeOut The number of seconds waiting for connect (default 30).
*/
TDESocket( const char *_host, unsigned short int _port, int timeOut = 30) TDE_DEPRECATED;
/**
* Connects to a UNIX domain socket.
* @param _path The filename of the socket.
*/
TDESocket( const char * _path ) TDE_DEPRECATED;
/**
* Destructor. Closes the socket if it is still open.
*/
virtual ~TDESocket();
/**
* Returns a file descriptor for this socket.
* @return the file descriptor, or -1 when an error occurred.
*/
int socket() const { return sock; }
/**
* Enables the socket for reading.
*
* If you enable read mode, the socket will emit the signal
* readEvent() whenever there is something to read out of this
* socket.
* @param enable true to enable reading signals
*/
void enableRead( bool enable );
/**
* Enables the socket for writing.
*
* If you enable write mode, the socket will emit the signal
* writeEvent() whenever the socket is ready for writing.
*
* Warning: If you forget to call enableWrite(false) when you are
* not ready to send data, you will get lots of writeEvent() signals,
* in the order of thousands a second !
* @param enable true to enable writing signals
*/
void enableWrite( bool enable );
#ifdef KSOCK_NO_BROKEN
// BCI: remove in libtdecore.so.4
/**
* Return address.
* This function is dumb. Don't ever use it
* if you need the peer address of this socket, use KExtendedSocket::peerAddress(int)
* instead
* @deprecated
*/
unsigned long ipv4_addr() TDE_DEPRECATED;
// BCI: remove in libtdecore.so.4
/**
* A small wrapper around gethostbyname() and such.
* Don't use this in new programs. Use KExtendedSocket::lookup
* @deprecated
*/
static bool initSockaddr(ksockaddr_in *server_name, const char *hostname, unsigned short int port, int domain = PF_INET) TDE_DEPRECATED;
#endif
signals:
/**
* Data has arrived for reading.
*
* This signal will only be raised if enableRead( @p true ) was called
* first.
* @param s the TDESocket that triggered the event
*/
void readEvent( TDESocket *s );
/**
* Socket is ready for writing.
*
* This signal will only be raised if enableWrite( @p true ) was
* called first.
*
* Warning: If you forget to call enableWrite(false) when you are
* not ready to send data, you will get lots of writeEvent() signals,
* in the order of thousands a second !
* @param s the TDESocket that triggered the event
*/
void writeEvent( TDESocket *s );
/**
* Raised when the connection is broken.
* @param s the TDESocket that triggered the event
*/
void closeEvent( TDESocket *s );
public slots:
/**
* Connected to the writeNotifier.
*
* Called when
* the socket is ready for writing.
* @param x ignored
*/
void slotWrite( int x);
/**
* Connected to the readNotifier.
*
* Called when
* the socket is ready for reading.
* @param x ignored
*/
void slotRead( int x );
protected:
bool connect( const TQString& _host, unsigned short int _port, int timeout = 0 );
bool connect( const char *_path );
/******************************************************
* The file descriptor for this socket. sock may be -1.
* This indicates that it is not connected.
*/
int sock;
private:
TDESocket(const TDESocket&);
TDESocket& operator=(const TDESocket&);
TDESocketPrivate *d;
};
/**
* @short Monitors a port for incoming TCP/IP connections.
*
* @deprecated
* This class is deprecated and will be removed in the future.
* Please use the classes in KNetwork for new programs.
* In special, this class is replaced by KNetwork::KStreamSocket
* and KNetwork::TDEServerSocket.
*
* You can use a TDEServerSocket to listen on a port for incoming
* connections. When a connection arrived in the port, a TDESocket
* is created and the signal accepted is raised. Make sure you
* always connect to this signal. If you don't the ServerSocket will
* create new TDESocket's and no one will delete them!
*
* If socket() is -1 or less the socket was not created properly.
*
* @author Torben Weis <[email protected]>
*/
class TDECORE_EXPORT TDEServerSocket : public TQObject
{
TQ_OBJECT
public:
/**
* Constructor.
* @param _port the port number to monitor for incoming connections.
* @param _bind if false you need to call bindAndListen yourself.
* This gives you the opportunity to set options on the
* socket.
*/
TDEServerSocket( unsigned short int _port, bool _bind = true );
/**
* Creates a UNIX domain server socket.
* @param _path path used for the socket.
* @param _bind if false you need to call bindAndListen yourself.
* This gives you the opportunity to set options on the
* socket.
*/
TDEServerSocket( const char *_path, bool _bind = true);
/**
* Destructor. Closes the socket if it was not already closed.
*/
virtual ~TDEServerSocket();
/**
* Binds the socket and start listening. This should only be called
* once when the constructor was called with _bind false.
* On error the socket will be closed.
* @param suppressFailureMessages suppress warning messages generated if the socket cannot be opened.
* @return true on success. false on error.
* @warning If suppressFailureMessages is TRUE future debugging may be made more difficult. Only set it
* if your application expects to bind to unavailable ports, e.g. while scanning for open ports in a range.
*/
bool bindAndListen(bool suppressFailureMessages = false);
/**
* Returns the file descriptor associated with the socket.
* @return the file descriptor, -1 when an error occurred during
* construction or bindAndListen
*/
int socket() const { return sock; }
/**
* Returns the port number which is being monitored.
* @return the port number
*/
unsigned short int port();
#ifdef KSOCK_NO_BROKEN
// BCI: remove in libtdecore.so.4
/**
* The address.
* This is dumb. Don't use it
* Refer to KExtendedSocket::localAddress(int)
* @deprecated
*/
unsigned long ipv4_addr();
#endif
public slots:
/**
* Called when someone connected to our port.
*/
virtual void slotAccept( int ); // why is this virtual?
signals:
/**
* A connection has been accepted.
* It is your task to delete the TDESocket if it is no longer needed.
*
* WARNING: this signal is always emitted, even if you don't connect
* anything to it. That would mean memory loss, because the TDESockets
* created go to oblivion.
* @param s the socket that accepted
*/
void accepted( TDESocket*s );
protected:
bool init( unsigned short int );
bool init( const char *_path );
/**
* The file descriptor for this socket. sock may be -1.
* This indicates that it is not connected.
*/
int sock;
private:
// HACK
#ifdef TDESOCKET_BINARY_COMPAT_HACK
TDE_EXPORT bool bindAndListen();
#endif // TDESOCKET_BINARY_COMPAT_HACK
TDEServerSocket(const TDEServerSocket&);
TDEServerSocket& operator=(const TDEServerSocket&);
TDEServerSocketPrivate *d;
};
#endif //Q_OS_UNIX
#endif
|