• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames/kggzmod

misc_private.h

Go to the documentation of this file.
00001 /*
00002     This file is part of the kggzmod library.
00003     Copyright (c) 2005 - 2007 Josef Spillner <josef@ggzgamingzone.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
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 // work around a bug in the Mac OS X 10.4.0 SDK
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 // The following function is a variant of libggz's ggz_read_fd()
00054 // All the historic Unix compatibility has been removed
00055 // The return code has been C++-ified
00056 // All GGZ debug/error handling code has been removed
00057 
00058 // On windows, this function will never be called
00059 // (no ModulePrivate::msgserverfd will ever arrive)
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         /* We can't use a union here, because CMSG_SPACE can't be evaluated at compile
00070            time on Mac OS X. Use a QVarLengthArray instead (it doesn't malloc, should be
00071            just as fast)
00072 
00073     union {
00074         struct cmsghdr cm;
00075         char control[CMSG_SPACE(sizeof(int))];
00076     } control_un;
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         /* We're just sending a fd, so it's a dummy byte */
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         //ggz_debug(GGZ_SOCKET_DEBUG, "Error reading fd msg.");
00096         return false;
00097     }
00098 
00099         if (n == 0) {
00100         //ggz_debug(GGZ_SOCKET_DEBUG, "Warning: fd is closed.");
00101             return false;
00102     }
00103 
00104         if ( (cmptr = CMSG_FIRSTHDR(&msg)) == NULL
00105          || cmptr->cmsg_len != CMSG_LEN(sizeof(int))) {
00106         //ggz_debug(GGZ_SOCKET_DEBUG, "Bad cmsg.");
00107         return false;
00108     }
00109 
00110     if (cmptr->cmsg_level != SOL_SOCKET) {
00111         //ggz_debug(GGZ_SOCKET_DEBUG, "Bad cmsg.");
00112         return false;
00113     }
00114 
00115     if (cmptr->cmsg_type != SCM_RIGHTS) {
00116         //ggz_debug(GGZ_SOCKET_DEBUG, "Bad cmsg.");
00117         return false;
00118     }
00119 
00120     /* Everything is good */
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 

libkdegames/kggzmod

Skip menu "libkdegames/kggzmod"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal