25 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/resource.h>
32 #ifdef HAVE_SYS_SELECT_H
33 #include <sys/select.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
41 #define KSOCK_NO_BROKEN
46 #define TDESOCKET_BINARY_COMPAT_HACK 1
48 #undef TDESOCKET_BINARY_COMPAT_HACK
50 #include "ksockaddr.h"
58 #ifdef HAVE_GETADDRINFO
63 #include <arpa/nameser.h>
77 #if TIME_WITH_SYS_TIME
84 #warning Your header files do not seem to support SOMAXCONN
88 #include <tqapplication.h>
89 #include <tqsocketnotifier.h>
94 #include "tqwindowdefs.h"
97 class TDESocketPrivate
100 TQSocketNotifier *readNotifier;
101 TQSocketNotifier *writeNotifier;
104 readNotifier(0), writeNotifier(0)
111 : sock(_sock), d(new TDESocketPrivate)
113 struct sockaddr_in sin;
114 ksocklen_t len =
sizeof(sin);
116 memset(&sin, 0, len);
125 sock( -1 ), d(new TDESocketPrivate)
127 connect( _host, _port, _timeout );
131 sock( -1 ), d(new TDESocketPrivate)
140 if ( !d->readNotifier )
142 d->readNotifier =
new TQSocketNotifier( sock, TQSocketNotifier::Read );
143 TQObject::connect( d->readNotifier, TQ_SIGNAL( activated(
int) ),
this, TQ_SLOT(
slotRead(
int) ) );
146 d->readNotifier->setEnabled(
true );
148 else if ( d->readNotifier )
149 d->readNotifier->setEnabled(
false );
156 if ( !d->writeNotifier )
158 d->writeNotifier =
new TQSocketNotifier( sock, TQSocketNotifier::Write );
159 TQObject::connect( d->writeNotifier, TQ_SIGNAL( activated(
int) ),
this,
163 d->writeNotifier->setEnabled(
true );
165 else if ( d->writeNotifier )
166 d->writeNotifier->setEnabled(
false );
173 int n = recv( sock, buffer, 1, MSG_PEEK );
188 bool TDESocket::connect(
const char *_path )
190 KExtendedSocket ks(TQString::null, _path, KExtendedSocket::unixSocket);
202 bool TDESocket::connect(
const TQString& _host,
unsigned short int _port,
int _timeout )
205 ks.setTimeout(_timeout, 0);
215 #ifdef KSOCK_NO_BROKEN
216 unsigned long TDESocket::ipv4_addr()
218 unsigned long retval = 0;
225 || sa->
address()->sa_family == PF_INET6
230 const sockaddr_in *sin = ksin->
addressV4();
232 retval = sin->sin_addr.s_addr;
238 bool TDESocket::initSockaddr (ksockaddr_in *server_name,
const char *hostname,
unsigned short int port,
int domain)
243 kdWarning(170) <<
"deprecated TDESocket::initSockaddr called" <<
endl;
245 if (domain != PF_INET)
249 KExtendedSocket::ipv4Socket);
250 list.setAutoDelete(
true);
255 memset(server_name, 0,
sizeof(*server_name));
262 memcpy(server_name, sin->
addressV4(),
sizeof(*server_name));
263 kdDebug(170) <<
"TDESocket::initSockaddr: returning " << sin->
pretty() <<
endl;
272 delete d->readNotifier;
273 delete d->writeNotifier;
282 class TDEServerSocketPrivate
287 unsigned short int port;
295 d =
new TDEServerSocketPrivate();
304 d =
new TDEServerSocketPrivate();
310 bool TDEServerSocket::init(
const char *_path )
316 KExtendedSocket::unixSocket);
325 bool TDEServerSocket::init(
unsigned short int _port )
329 ks =
new KExtendedSocket(TQString::null, _port, KExtendedSocket::passiveSocket |
330 KExtendedSocket::inetSocket);
340 if (d == NULL || d->ks == NULL)
344 int ret = d->ks->listen( SOMAXCONN );
347 if (!suppressFailureMessages)
349 kdWarning(170) <<
"Error listening on socket for port " << d->ks->port() <<
": " << ret <<
"\n";
360 connect( d->ks->readNotifier(), TQ_SIGNAL( activated(
int) ),
this, TQ_SLOT(
slotAccept(
int) ) );
367 if (d == NULL || d->ks == NULL ||
sock == -1)
374 sockaddr_in *sin = (sockaddr_in*)sa->
address();
376 if (sin->sin_family == PF_INET)
378 return sin->sin_port;
380 else if (sin->sin_family == PF_INET6)
382 kde_sockaddr_in6 *sin6 = (kde_sockaddr_in6*)sin;
383 return sin6->sin6_port;
389 unsigned long TDEServerSocket::ipv4_addr()
391 if (d == NULL || d->ks == NULL ||
sock == -1)
395 const sockaddr_in *sin = (sockaddr_in*)sa->
address();
397 if (sin->sin_family == PF_INET)
399 return ntohl(sin->sin_addr.s_addr);
401 else if (sin->sin_family == PF_INET6)
406 return sin->sin_addr.s_addr;
414 if (d == NULL || d->ks == NULL ||
sock == -1)
418 if (d->ks->accept(s) < 0)
420 kdWarning(170) <<
"Error accepting\n";
424 int new_sock = s->
fd();
The extended socket class.
virtual void release()
Releases the socket and anything we have holding on it.
virtual int lookup()
Performs lookup on the addresses we were given before.
int fd() const
Returns the file descriptor.
const ::TDESocketAddress * peerAddress()
Returns the peer socket address.
An Inet (IPv4 or IPv6) socket address.
virtual TQString pretty() const
Returns a pretty representation of this address.
const sockaddr_in * addressV4() const
Returns the socket address.
static KSocks * self()
Return an instance of class KSocks *.
int getsockname(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
unsigned short int port()
Returns the port number which is being monitored.
TDEServerSocket(unsigned short int _port, bool _bind=true)
Constructor.
virtual void slotAccept(int)
Called when someone connected to our port.
virtual ~TDEServerSocket()
Destructor.
int sock
The file descriptor for this socket.
void accepted(TDESocket *s)
A connection has been accepted.
bool bindAndListen(bool suppressFailureMessages=false)
Binds the socket and start listening.
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
void slotWrite(int x)
Connected to the writeNotifier.
void enableRead(bool enable)
Enables the socket for reading.
void slotRead(int x)
Connected to the readNotifier.
void enableWrite(bool enable)
Enables the socket for writing.
TDESocket(int _sock) TDE_DEPRECATED
Constructs a TDESocket with the provided file descriptor.
void closeEvent(TDESocket *s)
Raised when the connection is broken.
virtual ~TDESocket()
Destructor.
void readEvent(TDESocket *s)
Data has arrived for reading.
void writeEvent(TDESocket *s)
Socket is ready for writing.
kndbgstream & endl(kndbgstream &s)
Does nothing.