Mixe for Privacy and Anonymity in the Internet
CADatagramSocket.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #include "StdAfx.h"
29 #include "CADatagramSocket.hpp"
30 #include "CASocketAddrINet.hpp"
31 
32 
33 #ifdef _DEBUG
34  extern int sockets;
35  #include "CAMsg.hpp"
36 #endif
37 
39  {
40  m_Socket=0;
41 // localPort=-1;
42  }
43 
45  {
46  return create(AF_INET);
47  }
48 
49 
51  {
52  if(m_Socket==0)
53  m_Socket=::socket(type,SOCK_DGRAM,0);
55  return SOCKET_ERROR;
56 // localPort=-1;
57  return E_SUCCESS;
58  }
59 
61  {
62 // EnterCriticalSection(&csClose);
63 // localPort=-1;
64  int ret;
65  if(m_Socket!=0)
66  {
67  /* LINGER linger;
68  linger.l_onoff=1;
69  linger.l_linger=0;
70  if(::setsockopt(m_Socket,SOL_SOCKET,SO_LINGER,(char*)&linger,sizeof(linger))!=0)
71  CAMsg::printMsg(LOG_DEBUG,"Fehler bei setsockopt - LINGER!\n");
73 #ifdef _DEBUG
74  sockets--;
75 #endif
76  m_Socket=0;
77  ret=0;
78  }
79  else
80  ret=SOCKET_ERROR;
81 // LeaveCriticalSection(&csClose);
82  return ret;
83  }
84 
85 
87  {
88 // localPort=-1;
89  if(m_Socket==0&&create(from.getType())!=E_SUCCESS)
90  return SOCKET_ERROR;
91  if(::bind(m_Socket,from.LPSOCKADDR(),from.getSize())==SOCKET_ERROR)
92  return SOCKET_ERROR;
93  return E_SUCCESS;
94  }
95 
97  {
98  CASocketAddrINet oSocketAddr(port);
99  return bind(oSocketAddr);
100  }
101 
103  {
104  if(::sendto(m_Socket,(char*)buff,len,MSG_NOSIGNAL,to.LPSOCKADDR(),to.getSize())==SOCKET_ERROR)
105  return E_UNKNOWN;
106  return E_SUCCESS;
107  }
108 
109 
111  {
112  int ret;
113  if(from!=NULL)
114  {
115  socklen_t fromlen=from->getSize();
116  ret=::recvfrom(m_Socket,(char*)buff,len,MSG_NOSIGNAL,(LPSOCKADDR)from->LPSOCKADDR(),&fromlen);
117  }
118  else
119  {
120  ret=::recv(m_Socket,(char*)buff,len,MSG_NOSIGNAL);
121  }
122  return ret;
123  }
124 
125 /*
126 SINT32 CADatagramSocket::getLocalPort()
127  {
128  if(localPort==-1)
129  {
130  struct sockaddr_in addr;
131  socklen_t namelen=sizeof(struct sockaddr_in);
132  if(getsockname(m_Socket,(struct sockaddr*)&addr,&namelen)==SOCKET_ERROR)
133  return SOCKET_ERROR;
134  else
135  localPort=ntohs(addr.sin_port);
136  }
137  return localPort;
138  }
139 
140 */
#define INVALID_SOCKET
Definition: StdAfx.h:465
SOCKADDR * LPSOCKADDR
Definition: StdAfx.h:459
#define closesocket(s)
Definition: StdAfx.h:463
#define SOCKET_ERROR
Definition: StdAfx.h:464
int socklen_t
Definition: StdAfx.h:393
#define MSG_NOSIGNAL
Definition: StdAfx.h:408
unsigned short UINT16
Definition: basetypedefs.h:133
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 send(UINT8 *buff, UINT32 len, CASocketAddr &to)
SINT32 receive(UINT8 *buff, UINT32 len, CASocketAddr *from)
SINT32 bind(CASocketAddr &psa)
This is an abstract class for representing a socket address used in CASocket, CADatagramSocket and CA...
virtual SINT32 getType() const =0
The type (family) of socket for which this address is useful.
virtual const SOCKADDR * LPSOCKADDR() const =0
Casts to a SOCKADDR struct.
virtual SINT32 getSize() const =0
The size of the SOCKADDR struct needed by function of CASocket and other.
This class represents a socket address for Internet (IP) connections.
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT8 type
Definition: typedefs.hpp:1
UINT16 len
Definition: typedefs.hpp:0