Mixe for Privacy and Anonymity in the Internet
CASocketList.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 "CASocketList.hpp"
30 #include "CAUtil.hpp"
31 #define POOL_SIZE 1000
32 
33 typedef struct t_MEMBLOCK
34  {
38 
40  {
42  if(tmp==NULL)
43  return E_UNKNOWN;
44  _MEMBLOCK* tmpMem=new _MEMBLOCK;
45  if(tmpMem==NULL)
46  {
47  delete[] tmp;
48  tmp = NULL;
49  return E_UNKNOWN;
50  }
51  memset(tmp,0,sizeof(CONNECTIONLIST)*POOL_SIZE);
52  for(int i=1;i<POOL_SIZE;i++)
53  {
54  tmp[i-1].next=&tmp[i];
55  }
56  tmp[POOL_SIZE-1].next=m_Pool;
57  m_Pool=tmp;
58  tmpMem->next=m_Memlist;
59  tmpMem->mem=tmp;
60  m_Memlist=tmpMem;
61  return E_SUCCESS;
62  }
63 
65  {
66  m_Connections=NULL;
67  m_Pool=NULL;
68  m_Memlist=NULL;
69  m_AktEnumPos=NULL;
70  m_bThreadSafe=false;
71  setThreadSafe(false);
72  increasePool();
73  m_Size=0;
74  }
75 
76 CASocketList::CASocketList(bool bThreadSafe)
77  {
78  m_Connections=NULL;
79  m_Pool=NULL;
80  m_Memlist=NULL;
81  m_AktEnumPos=NULL;
82  m_bThreadSafe=false;
83  setThreadSafe(bThreadSafe);
84  increasePool();
85  }
86 
88  {
89  clear();
90  }
91 
93  {
94  _MEMBLOCK* tmp;
95  tmp=m_Memlist;
96  while(tmp!=NULL)
97  {
98  delete []tmp->mem;
99  tmp->mem = NULL;
100  m_Memlist=tmp;
101  tmp=tmp->next;
102  delete m_Memlist;
103  m_Memlist = NULL;
104  }
105  m_Connections=NULL;
106  m_Pool=NULL;
107  m_Memlist=NULL;
108  m_AktEnumPos=NULL;
109  return E_SUCCESS;
110  }
111 
113  {
114  m_bThreadSafe=b;
115  return E_SUCCESS;
116  }
117 
126  {
127  if(m_bThreadSafe)
128  cs.lock();
129  CONNECTIONLIST* tmp;
130  if(m_Pool==NULL)
131  {
132  if(increasePool()!=E_SUCCESS)
133  {
134  if(m_bThreadSafe)
135  cs.unlock();
136  return E_UNKNOWN;
137  }
138  }
139  tmp=m_Pool;
140  m_Pool=m_Pool->next;
141  tmp->next=m_Connections;
142  m_Connections=tmp;
143  m_Connections->pSocket=pSocket;
144  m_Connections->pCiphers=pCiphers;
147  for(;;)
148  {
149 SELECT_RANDOM_CHANNEL_ID:
151  tmp=m_Connections->next;
152  while(tmp!=NULL)
153  {
155  goto SELECT_RANDOM_CHANNEL_ID;
156  tmp=tmp->next;
157  }
158  break;
159  }
160  m_Size++;
161  if(m_bThreadSafe)
162  cs.unlock();
163  return E_SUCCESS;
164  }
165 
174  {
175  if(m_bThreadSafe)
176  cs.lock();
177  CONNECTIONLIST* tmp;
178  tmp=m_Connections;
179  while(tmp!=NULL)
180  {
181  if(tmp->outChannel==in)
182  {
183  memcpy(out,tmp,sizeof(CONNECTION));
184  if(m_bThreadSafe)
185  cs.unlock();
186  return E_SUCCESS;
187  }
188  tmp=tmp->next;
189  }
190  if(m_bThreadSafe)
191  cs.unlock();
192  return E_UNKNOWN;
193  }
194 
196  {
197  if(m_bThreadSafe)
198  cs.lock();
199  CONNECTIONLIST* tmp;
200  tmp=m_Connections;
201  while(tmp!=NULL)
202  {
203  if(tmp->outChannel==in)
204  {
205  tmp->currentSendMeCounter+=value;
206  if(m_bThreadSafe)
207  cs.unlock();
208  return E_SUCCESS;
209  }
210  tmp=tmp->next;
211  }
212  if(m_bThreadSafe)
213  cs.unlock();
214  return E_UNKNOWN;
215  }
216 
218  {
219  if(m_bThreadSafe)
220  cs.lock();
221  CONNECTIONLIST* tmp,*before;
222  CASocket* ret;
223  tmp=m_Connections;
224  before=NULL;
225  while(tmp!=NULL)
226  {
227  if(tmp->outChannel==id)
228  {
229  if(m_AktEnumPos==tmp)
230  m_AktEnumPos=tmp->next;
231  if(before!=NULL)
232  before->next=tmp->next;
233  else
234  m_Connections=tmp->next;
235  tmp->next=m_Pool;
236  m_Pool=tmp;
237  ret=tmp->pSocket;
238  m_Size--;
239  if(m_bThreadSafe)
240  cs.unlock();
241  return ret;
242  }
243  before=tmp;
244  tmp=tmp->next;
245  }
246  if(m_bThreadSafe)
247  cs.unlock();
248  return NULL;
249  }
250 
struct t_MEMBLOCK _MEMBLOCK
#define POOL_SIZE
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
signed int SINT32
Definition: basetypedefs.h:132
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
SINT32 setThreadSafe(bool b)
CONNECTIONLIST * m_AktEnumPos
SINT32 increasePool()
SINT32 add(CASocket *pSocket, CASymChannelCipher **pCiphers)
Add a new channel to the channel-list.
SINT32 addSendMeCounter(HCHANNEL in, SINT32 value)
CASocket * remove(HCHANNEL id)
t_MEMBLOCK * m_Memlist
SINT32 get(HCHANNEL in, CONNECTION *out)
Gets a copy of an entry form the channel-list.
CONNECTIONLIST * m_Connections
CONNECTIONLIST * m_Pool
SINT32 clear()
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
CASymChannelCipher ** pCiphers
UINT32 upstreamBytes
connlist * next
UINT32 currentSendMeCounter
HCHANNEL outChannel
CASocket * pSocket
t_MEMBLOCK * next
CONNECTIONLIST * mem
UINT32 HCHANNEL
Definition: typedefs.hpp:34