File indexing completed on 2024-04-14 14:20:11

0001 /*  -*- C++ -*-
0002  *  Copyright (C) 2003,2005 Thiago Macieira <thiago@kde.org>
0003  *
0004  *
0005  *  Permission is hereby granted, free of charge, to any person obtaining
0006  *  a copy of this software and associated documentation files (the
0007  *  "Software"), to deal in the Software without restriction, including
0008  *  without limitation the rights to use, copy, modify, merge, publish,
0009  *  distribute, sublicense, and/or sell copies of the Software, and to
0010  *  permit persons to whom the Software is furnished to do so, subject to
0011  *  the following conditions:
0012  *
0013  *  The above copyright notice and this permission notice shall be included
0014  *  in all copies or substantial portions of the Software.
0015  *
0016  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0017  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0018  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0020  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0021  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0022  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023  */
0024 
0025 #ifndef KDE_SYSSOCKET_H
0026 #define KDE_SYSSOCKET_H
0027 
0028 #ifdef KSOCKETBASE_H
0029 #error syssocket.h must be included before ksocketbase.h!
0030 #endif
0031 
0032 #include <config-kdelibs4support.h>
0033 
0034 // needed for Solaris, but shouldn't hurt on other operating systems
0035 // and to avoid ifdef mess, rather include them for all
0036 #include <unistd.h>
0037 #if HAVE_STROPTS_H
0038 #include <stropts.h>
0039 #endif
0040 
0041 #include <sys/types.h>
0042 #include <sys/socket.h>
0043 #include <sys/ioctl.h>
0044 
0045 namespace
0046 {
0047 
0048 /*
0049  * These function here are just wrappers for the real system calls.
0050  *
0051  * Unfortunately, a number of systems out there work by redefining
0052  * symbols through the preprocessor -- symbols that we need.
0053  *
0054  * So we write wrappers for all the low-level system calls.
0055  *
0056  * Qt has a very similar implementation. I got the idea from them, but
0057  * I copied no code.
0058  */
0059 
0060 // socket
0061 inline int kde_socket(int af, int style, int protocol)
0062 {
0063     return ::socket(af, style, protocol);
0064 }
0065 
0066 // bind
0067 inline int kde_bind(int fd, const struct sockaddr *sa, socklen_t len)
0068 {
0069     return ::bind(fd, sa, len);
0070 }
0071 
0072 // listen
0073 inline int kde_listen(int fd, int backlog)
0074 {
0075     return ::listen(fd, backlog);
0076 }
0077 
0078 // connect
0079 inline int kde_connect(int fd, const struct sockaddr *sa, socklen_t len)
0080 {
0081     return ::connect(fd, (struct sockaddr *)sa, len);
0082 }
0083 
0084 // accept
0085 inline int kde_accept(int fd, struct sockaddr *sa, socklen_t *len)
0086 {
0087     return ::accept(fd, sa, len);
0088 }
0089 
0090 // getpeername
0091 inline int kde_getpeername(int fd, struct sockaddr *sa, socklen_t *len)
0092 {
0093     return ::getpeername(fd, sa, len);
0094 }
0095 
0096 // getsockname
0097 inline int kde_getsockname(int fd, struct sockaddr *sa, socklen_t *len)
0098 {
0099     return ::getsockname(fd, sa, len);
0100 }
0101 
0102 // ioctl
0103 inline int kde_ioctl(int fd, int cmd, int *argp)
0104 {
0105 #if defined _WIN32 || defined _WIN64
0106     unsigned long l_argp = *argp;
0107     int iRet = ::ioctlsocket(fd, cmd, &l_argp);
0108     *argp = (int) l_argp;
0109     return iRet;
0110 #else
0111     return ::ioctl(fd, cmd, argp);
0112 #endif
0113 }
0114 
0115 } // anonymous namespace
0116 
0117 #endif