libkdegames/kggzmod
misc_private.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef KGGZMOD_MISC_PRIVATE_H
00022 #define KGGZMOD_MISC_PRIVATE_H
00023
00024 #include <QVarLengthArray>
00025
00026 #include <sys/types.h>
00027 #include <sys/socket.h>
00028 #include <sys/uio.h>
00029 #include <sys/un.h>
00030 #include <arpa/inet.h>
00031 #include <netinet/in.h>
00032 #include <netdb.h>
00033
00034 #ifdef Q_OS_WIN
00035 #include <kdebug.h>
00036 #endif
00037
00038
00039 #if defined(__APPLE__)
00040 # ifndef __DARWIN_ALIGNBYTES
00041 # define __DARWIN_ALIGNBYTES (sizeof(__darwin_size_t) - 1)
00042 # endif
00043 # ifndef __DARWIN_ALIGN
00044 # define __DARWIN_ALIGN(p) ((__darwin_size_t)((char *)(p) + __DARWIN_ALIGNBYTES) &~ __DARWIN_ALIGNBYTES)
00045 # endif
00046 # undef CMSG_SPACE
00047 # define CMSG_SPACE(l) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(l))
00048 #endif
00049
00050 namespace KGGZMod
00051 {
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 bool readfiledescriptor(int sock, int *recvfd)
00062 {
00063 #ifndef Q_OS_WIN
00064 struct msghdr msg;
00065 struct iovec iov[1];
00066 ssize_t n;
00067 char dummy;
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 QVarLengthArray<char> control_un(qMax(sizeof(cmsghdr), static_cast<size_t>(CMSG_SPACE(sizeof(int)))));
00079 struct cmsghdr *cmptr;
00080
00081 msg.msg_control = control_un.data();
00082 msg.msg_controllen = control_un.size();
00083
00084 msg.msg_name = NULL;
00085 msg.msg_namelen = 0;
00086
00087
00088 iov[0].iov_base = &dummy;
00089 iov[0].iov_len = 1;
00090
00091 msg.msg_iov = iov;
00092 msg.msg_iovlen = 1;
00093
00094 if ( (n = recvmsg(sock, &msg, 0)) < 0) {
00095
00096 return false;
00097 }
00098
00099 if (n == 0) {
00100
00101 return false;
00102 }
00103
00104 if ( (cmptr = CMSG_FIRSTHDR(&msg)) == NULL
00105 || cmptr->cmsg_len != CMSG_LEN(sizeof(int))) {
00106
00107 return false;
00108 }
00109
00110 if (cmptr->cmsg_level != SOL_SOCKET) {
00111
00112 return false;
00113 }
00114
00115 if (cmptr->cmsg_type != SCM_RIGHTS) {
00116
00117 return false;
00118 }
00119
00120
00121 *recvfd = *((int *) CMSG_DATA(cmptr));
00122
00123 return true;
00124 #else
00125 kError() << "This path should never be called.";
00126 return false;
00127 #endif
00128 }
00129
00130 }
00131
00132 #endif
00133