home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Source / OIP_Socket.cpp < prev    next >
C/C++ Source or Header  |  1997-02-12  |  6KB  |  291 lines

  1. // OCL - OS/2 Class Library
  2. // (c) Cubus 1996
  3. // (c) 1982, 1985, 1986 Regents of the University of California.
  4. // (c) Raoul Gema 1996
  5. // All Rights Reserved
  6. // OIP_Socket.cpp
  7.  
  8. /*
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Neither the name Cubus nor the name Team OCL may be used to
  15.  *    endorse or promote products derived from this software
  16.  *    without specific prior written permission.
  17.  * 3. See OCL.INF for a detailed copyright notice.
  18.  *
  19.  *              THIS SOFTWARE IS PROVIDED ``AS IS'' AND
  20.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  23.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29.  * SUCH DAMAGE.
  30.  */
  31.  
  32.  
  33. // $Header: W:/Projects/OCL/Source/rcs/OIP_Socket.cpp 1.50 1996/08/11 23:49:20 B.STEIN Release $
  34.  
  35. #define __OCL_SOURCE__
  36.  
  37. #define OINCL_OSTRING
  38. #define OINCL_BASE
  39.  
  40. #include <ocl.hpp>
  41. #include <OIP_Socket.hpp>
  42. #include <OMessage.hpp>
  43.  
  44. OIP_Socket::OIP_Socket()
  45. {
  46.  phost = NULL;
  47.  s     = 0;
  48. }
  49.  
  50.  
  51. OIP_Socket::OIP_Socket(phostent p)
  52. {
  53.  phost = new OIP_Host;
  54.  phost->Set(p);
  55.  s     = 0;
  56. }
  57.  
  58.  
  59. OIP_Socket::~OIP_Socket()
  60. {
  61.  close();
  62.  if (phost) delete phost;
  63. }
  64.  
  65.  
  66. PSZ OIP_Socket::isOfType() const
  67. {
  68.  return("OIP_Socket");
  69. }
  70.  
  71.  
  72. int OIP_Socket::close()
  73. {
  74.  int ok;
  75.  
  76.  if (s)
  77.   {
  78.    ok = ::soclose(s);
  79.    s = 0;
  80.   }
  81.  return(ok);
  82. }
  83.  
  84.  
  85. int OIP_Socket::sysinit(USHORT version)
  86. {
  87.  return(::sock_init());
  88. }
  89.  
  90. int OIP_Socket::sysfree()
  91. {
  92.  return(0);
  93. }
  94.  
  95. int OIP_Socket::Get_socket()
  96. {
  97.  return(s);
  98. }
  99.  
  100. int OIP_Socket::Set_socket(int sock)
  101. {
  102.  return(s = sock);
  103. }
  104.  
  105. pOIP_Host OIP_Socket::Get_phost()
  106. {
  107.  return(phost);
  108. }
  109.  
  110. pOIP_Host OIP_Socket::Sethostent(phostent ph)
  111. {
  112.  if (phost)
  113.    delete phost;
  114.  
  115.  phost = new OIP_Host;
  116.  phost->Set(ph);
  117.  
  118.  return(phost);
  119. }
  120.  
  121. int OIP_Socket::Gethostname(PSZ hostname, UINT size)
  122. {
  123.  return(::gethostname(hostname, size));
  124. }
  125.  
  126. pOIP_Host OIP_Socket::Gethostbyname(PSZ hostname)
  127. {
  128.  phostent p = ::gethostbyname(hostname);
  129.  
  130.  if (!p)
  131.    throw OIP_SocketException(OIP_SocketException::egethostbyname, GetErrorCode());
  132.  return(Sethostent(p));
  133. }
  134.  
  135. pOIP_Host OIP_Socket::Gethostbyaddr(PSZ hostaddress, int type)
  136. {
  137.  return(Sethostent(::gethostbyaddr(hostaddress, 4, type)));
  138. }
  139.  
  140. pservent OIP_Socket::Getservbyname(PSZ name, PSZ proto)
  141. {
  142.  return(::getservbyname(name, proto));
  143. }
  144.  
  145. void OIP_Socket::Setin_port(USHORT port)
  146. {
  147.  sockadd.Setin_port(port);
  148. }
  149.  
  150. USHORT OIP_Socket::Getin_port()
  151. {
  152.  return(sockadd.Getin_port());
  153. }
  154.  
  155. ULONG OIP_Socket::Set_sin_addr()
  156. {
  157.  return((phost) ? sockadd.Set_sin_addr(Getaddr_list()) : 0);
  158. }
  159.  
  160. ULONG OIP_Socket::Set_sin_addr(ULONG addr)
  161. {
  162.  return(sockadd.Set_sin_addr(addr));
  163. }
  164.  
  165. BOOL OIP_Socket::Set_address(USHORT port, PSZ target)
  166. {
  167.  if ((!Gethostbyname(target)) ||  (!Set_sin_addr()))
  168.    return(TRUE);
  169.  
  170.  Setin_port(port);
  171.  return(FALSE);
  172. }
  173.  
  174.  
  175. ULONG OIP_Socket::Set_sin_addr(PSZ target)
  176. {
  177.  return(sockadd.Set_sin_addr(target));
  178. }
  179.  
  180. ULONG OIP_Socket::Get_sin_addr()
  181. {
  182.  return(sockadd.Get_sin_addr());
  183. }
  184.  
  185. short OIP_Socket::Set_sin_family(short family)
  186. {
  187.  return(sockadd.Set_sin_family(family));
  188. }
  189.  
  190. short OIP_Socket::Get_sin_family()
  191. {
  192.  return(sockadd.Get_sin_family());
  193. }
  194.  
  195. ULONG OIP_Socket::Getaddr_list(UINT index)
  196. {
  197.  return(*((ULONG *)phost->h_data.h_addr_list[index]));
  198. }
  199.  
  200. void OIP_Socket::Get_inet_addr(PSZ adress)
  201. {
  202.  sockadd.Get_inet_addr(adress);
  203. }
  204.  
  205. int OIP_Socket::socket(int net, int typ, int y)
  206. {
  207.  int x = ::socket(net, typ, y);
  208.  if (x == -1)
  209.     throw OIP_SocketException(OIP_SocketException::esocket, GetErrorCode());
  210.  return s = x;
  211. }
  212.  
  213.  
  214. OIP_Socket::operator =(int handle)
  215. {
  216.  return(s = handle);
  217. }
  218.  
  219.  
  220. OIP_Socket::operator int()
  221. {
  222.  return(s);
  223. }
  224.  
  225.  
  226. OIP_Socket::operator OIP_SockAddress()
  227. {
  228.  return(sockadd);
  229. }
  230.  
  231. OIP_Socket::operator pOIP_SockAddress()
  232. {
  233.  return(&sockadd);
  234. }
  235.  
  236. psockaddr OIP_Socket::Get_sockaddr()
  237. {
  238.  return((psockaddr) sockadd.Get_sockaddr_in());
  239. }
  240.  
  241. int OIP_Socket::send(PSZ buffer, int size, int options)
  242. {
  243.  int x = ::send(s, buffer, size, options);
  244.  if (!x)
  245.     throw OIP_SocketException(OIP_SocketException::esend, GetErrorCode());
  246.  return x;
  247. }
  248.  
  249. int OIP_Socket::recv(PSZ buffer, int size, int options)
  250. {
  251.  int x = ::recv(s, buffer, size, options);
  252.  if (!x)
  253.    throw OIP_SocketException(OIP_SocketException::erecv, GetErrorCode());
  254.  return x;
  255. }
  256.  
  257. int OIP_Socket::GetErrorCode()
  258. {
  259.  return(::sock_errno());
  260. }
  261.  
  262.  
  263. OIP_Socket::OIP_SocketException::OIP_SocketException(excepttyp typ,
  264.                                                      int errorvalue)
  265.    : OException((PSZ)NULL, 0, OException::unrecoverable)
  266. {
  267.  etyp  = typ;
  268.  value = errorvalue;
  269.  rc    = value;  
  270.  description << OIP::error(etyp);
  271. }
  272.  
  273.  
  274. OIP_Socket::OIP_SocketException::~OIP_SocketException()
  275.   {}
  276.  
  277.  
  278. PSZ OIP_Socket::OIP_SocketException::isOfType() const
  279. {
  280.  return("OIP_Socket::OIP_SocketException");
  281. }
  282.  
  283.  
  284. void OIP_Socket::OIP_SocketException::viewError()
  285. {
  286.  cout << description.getText() << "rc = " << rc << endl;
  287. }
  288.  
  289.  
  290. // end of source
  291.