Mixe for Privacy and Anonymity in the Internet
CADatagramSocket.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #include "StdAfx.h"
00029 #include "CADatagramSocket.hpp"
00030 #include "CASocketAddrINet.hpp"
00031 
00032 
00033 #ifdef _DEBUG
00034   extern int sockets;
00035   #include "CAMsg.hpp"
00036 #endif
00037 
00038 CADatagramSocket::CADatagramSocket()
00039   {
00040     m_Socket=0;
00041 //    localPort=-1;
00042   }
00043 
00044 SINT32 CADatagramSocket::create()
00045   {
00046     return create(AF_INET);
00047   }
00048 
00049       
00050 SINT32 CADatagramSocket::create(int type)
00051   {
00052     if(m_Socket==0)
00053       m_Socket=::socket(type,SOCK_DGRAM,0);
00054     if(m_Socket==INVALID_SOCKET)
00055       return SOCKET_ERROR;
00056 //    localPort=-1;
00057     return E_SUCCESS;
00058   }
00059 
00060 SINT32 CADatagramSocket::close()
00061   {
00062 //    EnterCriticalSection(&csClose);
00063 //    localPort=-1;
00064     int ret;
00065     if(m_Socket!=0)
00066       {
00067   /*      LINGER linger;
00068         linger.l_onoff=1;
00069         linger.l_linger=0;
00070         if(::setsockopt(m_Socket,SOL_SOCKET,SO_LINGER,(char*)&linger,sizeof(linger))!=0)
00071           CAMsg::printMsg(LOG_DEBUG,"Fehler bei setsockopt - LINGER!\n");
00072   */      ::closesocket(m_Socket);
00073 #ifdef _DEBUG
00074         sockets--;
00075 #endif
00076         m_Socket=0;
00077         ret=0;
00078       }
00079     else
00080       ret=SOCKET_ERROR;
00081 //    LeaveCriticalSection(&csClose);
00082     return ret;
00083   }
00084 
00085       
00086 SINT32 CADatagramSocket::bind(CASocketAddr & from)
00087   {
00088 //    localPort=-1;
00089     if(m_Socket==0&&create(from.getType())!=E_SUCCESS)
00090       return SOCKET_ERROR;
00091     if(::bind(m_Socket,from.LPSOCKADDR(),from.getSize())==SOCKET_ERROR)
00092         return SOCKET_ERROR;
00093     return E_SUCCESS;
00094   }
00095 
00096 SINT32 CADatagramSocket::bind(UINT16 port)
00097   {
00098     CASocketAddrINet oSocketAddr(port);
00099     return bind(oSocketAddr);
00100   }
00101 
00102 SINT32 CADatagramSocket::send(UINT8* buff,UINT32 len,CASocketAddr & to)
00103   {
00104         if(::sendto(m_Socket,(char*)buff,len,MSG_NOSIGNAL,to.LPSOCKADDR(),to.getSize())==SOCKET_ERROR)
00105       return E_UNKNOWN;
00106     return E_SUCCESS;           
00107   }
00108 
00109 
00110 SINT32 CADatagramSocket::receive(UINT8* buff,UINT32 len,CASocketAddr* from)
00111   {
00112     int ret;  
00113     if(from!=NULL)
00114       {
00115         socklen_t fromlen=from->getSize();
00116         ret=::recvfrom(m_Socket,(char*)buff,len,MSG_NOSIGNAL,(LPSOCKADDR)from,&fromlen);
00117       }
00118     else
00119       {
00120         ret=::recv(m_Socket,(char*)buff,len,MSG_NOSIGNAL);
00121       }
00122     return ret;           
00123   }
00124 
00125 /*
00126 SINT32 CADatagramSocket::getLocalPort()
00127   {
00128     if(localPort==-1)
00129       {
00130         struct sockaddr_in addr;
00131         socklen_t namelen=sizeof(struct sockaddr_in);
00132         if(getsockname(m_Socket,(struct sockaddr*)&addr,&namelen)==SOCKET_ERROR)
00133           return SOCKET_ERROR;
00134         else
00135           localPort=ntohs(addr.sin_port);
00136       }
00137     return localPort;
00138   }
00139 
00140 */