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 45 #include "ksockaddr.h" 53 #ifdef HAVE_GETADDRINFO 58 #include <arpa/nameser.h> 72 #if TIME_WITH_SYS_TIME 79 #warning Your header files do not seem to support SOMAXCONN 83 #include <tqapplication.h> 84 #include <tqsocketnotifier.h> 89 #include "tqwindowdefs.h" 95 TQSocketNotifier *readNotifier;
96 TQSocketNotifier *writeNotifier;
99 readNotifier(0), writeNotifier(0)
106 : sock(_sock), d(new KSocketPrivate)
108 struct sockaddr_in sin;
109 ksocklen_t len =
sizeof(sin);
111 memset(&sin, 0, len);
120 sock( -1 ), d(new KSocketPrivate)
122 connect( _host, _port, _timeout );
126 sock( -1 ), d(new KSocketPrivate)
135 if ( !d->readNotifier )
137 d->readNotifier =
new TQSocketNotifier( sock, TQSocketNotifier::Read );
138 TQObject::connect( d->readNotifier, TQT_SIGNAL( activated(
int) ),
this, TQT_SLOT(
slotRead(
int) ) );
141 d->readNotifier->setEnabled(
true );
143 else if ( d->readNotifier )
144 d->readNotifier->setEnabled(
false );
151 if ( !d->writeNotifier )
153 d->writeNotifier =
new TQSocketNotifier( sock, TQSocketNotifier::Write );
154 TQObject::connect( d->writeNotifier, TQT_SIGNAL( activated(
int) ),
this,
158 d->writeNotifier->setEnabled(
true );
160 else if ( d->writeNotifier )
161 d->writeNotifier->setEnabled(
false );
168 int n = recv( sock, buffer, 1, MSG_PEEK );
183 bool KSocket::connect(
const char *_path )
185 KExtendedSocket ks(TQString::null, _path, KExtendedSocket::unixSocket);
197 bool KSocket::connect(
const TQString& _host,
unsigned short int _port,
int _timeout )
210 #ifdef KSOCK_NO_BROKEN 211 unsigned long KSocket::ipv4_addr()
213 unsigned long retval = 0;
220 || sa->
address()->sa_family == PF_INET6
225 const sockaddr_in *sin = ksin->
addressV4();
227 retval = sin->sin_addr.s_addr;
233 bool KSocket::initSockaddr (ksockaddr_in *server_name,
const char *hostname,
unsigned short int port,
int domain)
238 kdWarning(170) <<
"deprecated KSocket::initSockaddr called" <<
endl;
240 if (domain != PF_INET)
244 KExtendedSocket::ipv4Socket);
245 list.setAutoDelete(
true);
250 memset(server_name, 0,
sizeof(*server_name));
257 memcpy(server_name, sin->
addressV4(),
sizeof(*server_name));
258 kdDebug(170) <<
"KSocket::initSockaddr: returning " << sin->
pretty() <<
endl;
267 delete d->readNotifier;
268 delete d->writeNotifier;
277 class KServerSocketPrivate
282 unsigned short int port;
290 d =
new KServerSocketPrivate();
299 d =
new KServerSocketPrivate();
305 bool KServerSocket::init(
const char *_path )
311 KExtendedSocket::unixSocket);
320 bool KServerSocket::init(
unsigned short int _port )
324 ks =
new KExtendedSocket(TQString::null, _port, KExtendedSocket::passiveSocket |
325 KExtendedSocket::inetSocket);
335 if (d == NULL || d->ks == NULL)
339 int ret = d->ks->listen( SOMAXCONN );
342 kdWarning(170) <<
"Error listening on socket: " << ret <<
"\n";
352 connect( d->ks->readNotifier(), TQT_SIGNAL( activated(
int) ),
this, TQT_SLOT(
slotAccept(
int) ) );
359 if (d == NULL || d->ks == NULL ||
sock == -1)
366 sockaddr_in *sin = (sockaddr_in*)sa->
address();
368 if (sin->sin_family == PF_INET)
370 return sin->sin_port;
372 else if (sin->sin_family == PF_INET6)
374 kde_sockaddr_in6 *sin6 = (kde_sockaddr_in6*)sin;
375 return sin6->sin6_port;
381 unsigned long KServerSocket::ipv4_addr()
383 if (d == NULL || d->ks == NULL ||
sock == -1)
387 const sockaddr_in *sin = (sockaddr_in*)sa->
address();
389 if (sin->sin_family == PF_INET)
391 return ntohl(sin->sin_addr.s_addr);
393 else if (sin->sin_family == PF_INET6)
398 return sin->sin_addr.s_addr;
406 if (d == NULL || d->ks == NULL ||
sock == -1)
410 if (d->ks->accept(s) < 0)
412 kdWarning(170) <<
"Error accepting\n";
416 int new_sock = s->
fd();
virtual ~KSocket()
Destructor.
KSocket(int _sock) KDE_DEPRECATED
Constructs a KSocket with the provided file descriptor.
virtual void slotAccept(int)
Called when someone connected to our port.
void enableWrite(bool enable)
Enables the socket for writing.
virtual void release()
Releases the socket and anything we have holding on it.
const sockaddr_in * addressV4() const
Returns the socket address.
const sockaddr * address() const
Returns a sockaddr structure, for passing down to library functions.
void enableRead(bool enable)
Enables the socket for reading.
An Inet (IPv4 or IPv6) socket address.
int sock
The file descriptor for this socket.
void slotWrite(int x)
Connected to the writeNotifier.
int fd() const
Returns the file descriptor.
static KSocks * self()
Return an instance of class KSocks *.
void slotRead(int x)
Connected to the readNotifier.
void readEvent(KSocket *s)
Data has arrived for reading.
unsigned short int port()
Returns the port number which is being monitored.
void closeEvent(KSocket *s)
Raised when the connection is broken.
virtual int connect()
Attempts to connect to the remote host.
int getsockname(int s, sockaddr *name, ksocklen_t *namelen)
This is the re-implementation of libc's function of the same name.
virtual int lookup()
Performs lookup on the addresses we were given before.
const ::KSocketAddress * peerAddress()
Returns the peer socket address.
bool bindAndListen()
Binds the socket and start listening.
bool setTimeout(int secs, int usecs=0)
Sets the timeout value for the connection (if this is not passiveSocket) or acception (if it is)...
The extended socket class.
void accepted(KSocket *s)
A connection has been accepted.
virtual TQString pretty() const
Returns a pretty representation of this address.
void writeEvent(KSocket *s)
Socket is ready for writing.
kndbgstream & endl(kndbgstream &s)
Does nothing.
KServerSocket(unsigned short int _port, bool _bind=true)
Constructor.
virtual ~KServerSocket()
Destructor.