20 #include <config-network.h>
22 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/select.h>
34 static inline int kSocket(
int af,
int socketype,
int proto)
38 ret = ::socket(af, socketype, proto);
39 }
while (ret == -1 && errno == EINTR);
43 static inline int kBind(
int fd,
const sockaddr *sa,
int len)
47 ret = ::bind(fd, sa, len);
48 }
while (ret == -1 && errno == EINTR);
52 static inline int kConnect(
int fd,
const sockaddr *sa,
int len)
56 ret = ::connect(fd, sa, len);
57 }
while (ret == -1 && errno == EINTR);
61 static inline int kListen(
int fd,
int backlog)
66 }
while (ret == -1 && errno == EINTR);
74 socklen_t len =
sizeof(sa);
76 ret = ::accept(fd, &sa, &len);
77 }
while (ret == -1 && errno == EINTR);
101 #include <QtCore/qfile.h>
102 #include <QtCore/qsocketnotifier.h>
103 #include <QtCore/qvarlengtharray.h>
108 #if !defined(AF_UNIX) && defined(AF_LOCAL)
109 # define AF_UNIX AF_LOCAL
115 QVarLengthArray<char, 128> data;
118 bool ok()
const {
return datalen; }
119 int length()
const {
return datalen; }
120 const sockaddr* address()
121 {
return reinterpret_cast<sockaddr *
>(data.data()); }
131 if (!path.startsWith(QLatin1Char(
'/')))
133 path2.prepend(QLatin1String(
"/tmp/"));
135 QByteArray encodedPath = QFile::encodeName(path2);
140 data.resize(datalen);
142 sockaddr_un *saddr =
reinterpret_cast<sockaddr_un *
>(data.data());
143 saddr->sun_family = AF_UNIX;
144 #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
145 saddr->sun_len = datalen;
149 strcpy(saddr->sun_path, encodedPath.constData());
151 *saddr->sun_path =
'\0';
152 strcpy(saddr->sun_path + 1, encodedPath.constData());
160 int fdflags = fcntl(fd, F_GETFL, 0);
164 fdflags |= O_NONBLOCK;
165 if (fcntl(fd, F_SETFL, fdflags) == -1)
172 QAbstractSocket::OpenMode openMode)
176 KSockaddrUn addr(path, aType);
178 emitError(QAbstractSocket::NetworkError,
i18n(
"Specified socket path is invalid"));
183 int fd =
kSocket(AF_UNIX, SOCK_STREAM, 0);
186 emitError(QAbstractSocket::UnsupportedSocketOperationError,
187 i18n(
"The socket operation is not supported"));
193 if (
kConnect(fd, addr.address(), addr.length()) == -1) {
200 emitError(QAbstractSocket::ConnectionRefusedError,
i18n(
"Connection refused"));
205 emitError(QAbstractSocket::SocketAccessError,
i18n(
"Permission denied"));
209 emitError(QAbstractSocket::SocketTimeoutError,
i18n(
"Connection timed out"));
213 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Unknown error"));
221 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Could not set non-blocking mode"));
230 q->setSocketDescriptor(fd, QAbstractSocket::ConnectedState, openMode);
233 emitError(QAbstractSocket::UnsupportedSocketOperationError,
234 i18n(
"The socket operation is not supported"));
244 KSockaddrUn addr(path, aType);
246 emitError(QAbstractSocket::NetworkError,
i18n(
"Specified socket path is invalid"));
254 emitError(QAbstractSocket::UnsupportedSocketOperationError,
255 i18n(
"The socket operation is not supported"));
268 emitError(QAbstractSocket::SocketAccessError,
i18n(
"Permission denied"));
272 emitError(QAbstractSocket::AddressInUseError,
i18n(
"Address is already in use"));
277 emitError(QAbstractSocket::NetworkError,
i18n(
"Path cannot be used"));
281 emitError(QAbstractSocket::HostNotFoundError,
i18n(
"No such file or directory"));
285 emitError(QAbstractSocket::HostNotFoundError,
i18n(
"Not a directory"));
289 emitError(QAbstractSocket::SocketResourceError,
i18n(
"Read-only filesystem"));
293 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Unknown error"));
301 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Could not set non-blocking mode"));
306 state = QAbstractSocket::ListeningState;
332 state = QAbstractSocket::UnconnectedState;
333 error = QAbstractSocket::UnknownSocketError;
340 tv.tv_sec = msec / 1000;
341 tv.tv_usec = (msec % 1000) * 1000;
348 int code = ::select(
descriptor + 1, &readset, 0, 0, &tv);
349 if (code == -1 && errno == EINTR) {
352 }
else if (code == -1) {
354 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Unknown socket error"));
357 }
else if (code == 0) {
379 if (newDescriptor == -1) {
386 emitError(QAbstractSocket::UnknownSocketError,
i18n(
"Unknown socket error"));
QString i18n(const char *text)
Returns a localized version of a string.
QAbstractSocket::SocketState state
bool processSocketActivity()
QSocketNotifier * readNotifier
void _k_newConnectionActivity()
QTcpServer * listen(const QString &protocol, const QHostAddress &address=QHostAddress::Any, quint16 port=0, QObject *parent=0)
Opens a TCP/IP socket for listening protocol protocol, binding only at address address.
static int kBind(int fd, const sockaddr *sa, int len)
static int kAccept(int fd)
QQueue< KLocalSocket * > pendingConnections
bool listen(const QString &path, KLocalSocket::LocalSocketType type)
QAbstractSocket::SocketError error
void connectToPath(const QString &path, KLocalSocket::LocalSocketType type, QAbstractSocket::OpenMode openMode)
KLocalSocketServer *const q
void emitError(QAbstractSocket::SocketError, const QString &errorString)
static int kListen(int fd, int backlog)
#define MIN_SOCKADDR_UN_LEN
int maxPendingConnections
static int kConnect(int fd, const sockaddr *sa, int len)
static int kSocket(int af, int socketype, int proto)
virtual void incomingConnection(int handle)
static bool setNonBlocking(int fd)
void emitError(QAbstractSocket::SocketError, const QString &errorString)
LocalSocketType
Defines the local socket type.
bool waitForNewConnection(int msec, bool *timedOut)