Main Page | File List | Globals

src/socket.c

Go to the documentation of this file.
00001 /*
00002      This file is part of PlibC.
00003      (C) 2005 Nils Durner (and other contributing authors)
00004 
00005            This library is free software; you can redistribute it and/or
00006            modify it under the terms of the GNU Lesser General Public
00007            License as published by the Free Software Foundation; either
00008            version 2.1 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            Lesser General Public License for more details.
00014         
00015            You should have received a copy of the GNU Lesser General Public
00016            License along with this library; if not, write to the Free Software
00017            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00025 #include "plibc_private.h"
00026 
00030 int _win_accept(SOCKET s, struct sockaddr *addr, int *addrlen)
00031 {
00032   int iRet = accept(s, addr, addrlen);
00033 
00034   SetErrnoFromWinsockError(WSAGetLastError());
00035 
00036   return iRet;
00037 }
00038 
00042 int _win_bind(SOCKET s, const struct sockaddr *name, int namelen)
00043 {
00044   int iRet = bind(s, name, namelen);
00045 
00046   SetErrnoFromWinsockError(WSAGetLastError());
00047 
00048   return iRet;
00049 }
00050 
00054 int _win_connect(SOCKET s,const struct sockaddr *name, int namelen)
00055 {
00056   int iRet, iWSErr;
00057   BOOL bBlocking;
00058 
00059   /* connect()ing a non-blocking socket always fails under Windows.
00060      Check whether it is blocking */
00061   bBlocking = __win_IsHandleMarkedAsBlocking(s);
00062   if (! bBlocking)
00063   {
00064     u_long l;
00065     l = 0;
00066     ioctlsocket(s, FIONBIO, &l);
00067   }
00068 
00069   iRet = connect(s, name, namelen);
00070   iWSErr = WSAGetLastError();
00071 
00072   if (! bBlocking)
00073   {
00074     u_long l;
00075     l = 1;
00076     ioctlsocket(s, FIONBIO, &l);
00077   }
00078 
00079   SetErrnoFromWinsockError(iWSErr);
00080 
00081   return iRet;
00082 }
00083 
00087 int _win_getpeername(SOCKET s, struct sockaddr *name,
00088                 int *namelen)
00089 {
00090   int iRet = getpeername(s, name, namelen);
00091 
00092   SetErrnoFromWinsockError(WSAGetLastError());
00093 
00094   return iRet;
00095 }
00096 
00100 int _win_getsockname(SOCKET s, struct sockaddr *name,
00101                 int *namelen)
00102 {
00103   int iRet = getsockname(s, name, namelen);
00104 
00105   SetErrnoFromWinsockError(WSAGetLastError());
00106 
00107   return iRet;
00108 }
00109 
00113 int _win_getsockopt(SOCKET s, int level, int optname, char *optval, int *optlen)
00114 {
00115   int iRet = getsockopt(s, level, optname, optval, optlen);
00116 
00117   SetErrnoFromWinsockError(WSAGetLastError());
00118 
00119   return iRet;
00120 }
00121 
00125 int _win_listen(SOCKET s, int backlog)
00126 {
00127   int iRet = listen(s, backlog);
00128 
00129   SetErrnoFromWinsockError(WSAGetLastError());
00130 
00131   return iRet;
00132 }
00133 
00137 int _win_recv(SOCKET s, char *buf, int len, int flags)
00138 {
00139   int iRet = recv(s, buf, len, flags);
00140 
00141   SetErrnoFromWinsockError(WSAGetLastError());
00142 
00143   return iRet;
00144 }
00145 
00149 int _win_recvfrom(SOCKET s, void *buf, int len, int flags,
00150              struct sockaddr *from, int *fromlen)
00151 {
00152   int iRet = recvfrom(s, buf, len, flags, from, fromlen);
00153 
00154   SetErrnoFromWinsockError(WSAGetLastError());
00155 
00156   return iRet;
00157 }
00158 
00162 int _win_send(SOCKET s, const char *buf, int len, int flags)
00163 {
00164   int iRet = send(s, buf, len, flags);
00165 
00166   SetErrnoFromWinsockError(WSAGetLastError());
00167 
00168   return iRet;
00169 }
00170 
00174 int _win_sendto(SOCKET s, const char *buf, int len, int flags,
00175                 const struct sockaddr *to, int tolen)
00176 {
00177   int iRet = sendto(s, buf, len, flags, to, tolen);
00178 
00179   SetErrnoFromWinsockError(WSAGetLastError());
00180 
00181   return iRet;
00182 }
00183 
00187 int _win_setsockopt(SOCKET s, int level, int optname, const void *optval,
00188                     int optlen)
00189 {
00190   int iRet = setsockopt(s, level, optname, (const char *) optval, optlen);
00191 
00192   SetErrnoFromWinsockError(WSAGetLastError());
00193 
00194   return iRet;
00195 }
00196 
00200 int _win_shutdown(SOCKET s, int how)
00201 {
00202   int iRet = shutdown(s, how);
00203 
00204   SetErrnoFromWinsockError(WSAGetLastError());
00205 
00206   return iRet;
00207 }
00208 
00212 SOCKET _win_socket(int af, int type, int protocol)
00213 {
00214   int iRet;
00215 
00216   errno = 0;
00217 
00218   iRet = socket(af, type, protocol);
00219   if (iRet == SOCKET_ERROR)
00220   {
00221     SetErrnoFromWinsockError(WSAGetLastError());
00222 
00223     return -1;
00224   }
00225   else
00226   {
00227     /* Sockets are not blocking by default under Windows 9x */
00228     u_long l;
00229     l = 0;
00230     ioctlsocket(iRet, FIONBIO, &l);
00231 
00232     return iRet;
00233   }
00234 }
00235 
00239 struct hostent *_win_gethostbyaddr(const char *addr, int len, int type)
00240 {
00241   struct hostent *pHost = gethostbyaddr(addr, len, type);
00242 
00243   SetHErrnoFromWinError(WSAGetLastError());
00244   SetErrnoFromWinsockError(WSAGetLastError());
00245 
00246   return pHost;
00247 }
00248 
00252 struct hostent *_win_gethostbyname(const char *name)
00253 {
00254   struct hostent *pHost = gethostbyname(name);
00255 
00256   SetHErrnoFromWinError(WSAGetLastError());
00257   SetErrnoFromWinsockError(WSAGetLastError());
00258 
00259   return pHost;
00260 }
00261 
00262 /* end of socket.c */

Generated on Sun Sep 4 11:16:47 2005 for PlibC by  doxygen 1.4.2