summaryrefslogtreecommitdiff
path: root/icqj_mod/cookies.c
blob: 23919a1a21431390fa2e196b94f55e3d6ec8367d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// ---------------------------------------------------------------------------80
//                ICQ plugin for Miranda Instant Messenger
//                ________________________________________
// 
// Copyright © 2000,2001 Richard Hughes, Roland Rabien, Tristan Van de Vreede
// Copyright © 2001,2002 Jon Keating, Richard Hughes
// Copyright © 2002,2003,2004 Martin Öberg, Sam Kothari, Robert Rainwater
// Copyright © 2004,2005,2006 Joe Kucera
// 
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
// -----------------------------------------------------------------------------
//
// File name      : $Source: /cvsroot/miranda/miranda/protocols/IcqOscarJ/cookies.c,v $
// Revision       : $Revision: 3069 $
// Last change on : $Date: 2006-06-07 20:05:39 +0200 (st, 07 VI 2006) $
// Last change by : $Author: jokusoftware $
//
// DESCRIPTION:
//
//  Handles packet & message cookies
//
// -----------------------------------------------------------------------------

#include "icqoscar.h"



static WORD wCookieSeq;
static icq_cookie_info *cookie = NULL;
static int cookieCount = 0;
static int cookieSize = 0;
CRITICAL_SECTION cookieMutex; // we want this in avatar thread, used as queue lock

static int ResizeCookieList(int nSize)
{
  if ((cookieSize < nSize) || ((cookieSize > nSize + 6) && nSize))
  {
    icq_cookie_info *pNew;
    int newSize;

    if (cookieSize < nSize)
      newSize = cookieSize + 4;
    else
      newSize = cookieSize - 4;

    pNew = (icq_cookie_info *)realloc(cookie, sizeof(icq_cookie_info) * newSize);

    if (!pNew)
    { // realloc failed, cookies intact... try again
      NetLog_Server("ResizeCookieList: realloc failed.");

      return 1; // Failure
    }
    else
    {
      cookie = pNew;
      cookieSize = newSize;
    }
  }
  return 0; // Success
}


#define INVALID_COOKIE_INDEX -1

static int FindCookieIndex(DWORD dwCookie)
{
  int i;

  for (i = 0; i < cookieCount; i++)
  {
    if (dwCookie == cookie[i].dwCookie)
    {
      return i;
    }
  }
  return INVALID_COOKIE_INDEX;
}



static void RemoveCookieIndex(int iCookie)
{
  cookieCount--;
  memmove(&cookie[iCookie], &cookie[iCookie + 1], sizeof(icq_cookie_info) * (cookieCount - iCookie));
  ResizeCookieList(cookieCount);
}



static void RemoveExpiredCookies()
{
  int i;
  DWORD tNow = time(NULL);

  for (i = 0; i < cookieCount; i++)
  {
    if ((cookie[i].dwTime + COOKIE_TIMEOUT) < tNow)
    { // cookie expired, remove too
      RemoveCookieIndex(i);
      i--; // fix the loop
    }
  }
}



void InitCookies(void)
{
  InitializeCriticalSection(&cookieMutex);

  cookieCount = 0;
  cookieSize = 0;
  cookie = NULL;
  wCookieSeq = 2;

  ResizeCookieList(4);
}



void UninitCookies(void)
{
  SAFE_FREE(&cookie);

  DeleteCriticalSection(&cookieMutex);
}



// Generate and allocate cookie
DWORD AllocateCookie(BYTE bType, WORD wIdent, DWORD dwUin, void *pvExtra)
{
  DWORD dwThisSeq;

  EnterCriticalSection(&cookieMutex);

  if (ResizeCookieList(cookieCount + 1))
  { // resizing failed...
    LeaveCriticalSection(&cookieMutex);
    // this is horrible, but can't do anything better
    return GenerateCookie(wIdent);
  }

  dwThisSeq = wCookieSeq++;
  dwThisSeq &= 0x7FFF;
  dwThisSeq |= wIdent<<0x10;

  cookie[cookieCount].bType = bType;
  cookie[cookieCount].dwCookie = dwThisSeq;
  cookie[cookieCount].dwUin = dwUin;
  cookie[cookieCount].pvExtra = pvExtra;
  cookie[cookieCount].dwTime = time(NULL);
  cookieCount++;

  LeaveCriticalSection(&cookieMutex);

  return dwThisSeq;
}



DWORD GenerateCookie(WORD wIdent)
{
  DWORD dwThisSeq;

  EnterCriticalSection(&cookieMutex);
  dwThisSeq = wCookieSeq++;
  dwThisSeq &= 0x7FFF;
  dwThisSeq |= wIdent<<0x10;
  LeaveCriticalSection(&cookieMutex);

  return dwThisSeq;
}



int GetCookieType(DWORD dwCookie)
{
  int i;

  EnterCriticalSection(&cookieMutex);

  i = FindCookieIndex(dwCookie);

  if (i != INVALID_COOKIE_INDEX)
    i = cookie[i].bType;

  LeaveCriticalSection(&cookieMutex);

  return i;
}



int FindCookie(DWORD dwCookie, DWORD *pdwUin, void **ppvExtra)
{
  int i;
  int nFound = 0;


  EnterCriticalSection(&cookieMutex);

  i = FindCookieIndex(dwCookie);

  if (i != INVALID_COOKIE_INDEX)
  {
    if (pdwUin)
      *pdwUin = cookie[i].dwUin;
    if (ppvExtra)
      *ppvExtra = cookie[i].pvExtra;

    // Cookie found
    nFound = 1;
  }

  LeaveCriticalSection(&cookieMutex);

  return nFound;
}



int FindCookieByData(void *pvExtra,DWORD *pdwCookie, DWORD *pdwUin)
{
  int i;
  int nFound = 0;


  EnterCriticalSection(&cookieMutex);

  for (i = 0; i < cookieCount; i++)
  {
    if (pvExtra == cookie[i].pvExtra)
    {
      if (pdwUin)
        *pdwUin = cookie[i].dwUin;
      if (pdwCookie)
        *pdwCookie = cookie[i].dwCookie;

      // Cookie found, exit loop
      nFound = 1;
      break;

    }
  }

  LeaveCriticalSection(&cookieMutex);

  return nFound;
}



int FindMessageCookie(DWORD dwMsgID1, DWORD dwMsgID2, DWORD *pdwCookie, DWORD *pdwUin, message_cookie_data **ppvExtra)
{
  int i;
  int nFound = 0;


  EnterCriticalSection(&cookieMutex);

  for (i = 0; i < cookieCount; i++)
  {
    if (cookie[i].bType == CKT_MESSAGE || cookie[i].bType == CKT_FILE || cookie[i].bType == CKT_REVERSEDIRECT)
    { // message cookie found
      message_cookie_data *pCookie = (message_cookie_data*)cookie[i].pvExtra;

      if (pCookie->dwMsgID1 == dwMsgID1 && pCookie->dwMsgID2 == dwMsgID2)
      {
        if (pdwUin)
          *pdwUin = cookie[i].dwUin;
        if (pdwCookie)
          *pdwCookie = cookie[i].dwCookie;
        if (ppvExtra)
          *ppvExtra = pCookie;

        // Cookie found, exit loop
        nFound = 1;
        break;
      }
    }
  }

  LeaveCriticalSection(&cookieMutex);

  return nFound;
}



void FreeCookie(DWORD dwCookie)
{
  int i;

  EnterCriticalSection(&cookieMutex);

  i = FindCookieIndex(dwCookie);

  if (i != INVALID_COOKIE_INDEX)
  { // Cookie found, remove from list
    RemoveCookieIndex(i);
  }
  RemoveExpiredCookies();

  LeaveCriticalSection(&cookieMutex);
}



void ReleaseCookie(DWORD dwCookie)
{
  int i;

  EnterCriticalSection(&cookieMutex);

  i = FindCookieIndex(dwCookie);

  if (i != INVALID_COOKIE_INDEX)
  { // Cookie found, remove from list
    SAFE_FREE(&cookie[i].pvExtra);
    RemoveCookieIndex(i);
  }
  RemoveExpiredCookies();

  LeaveCriticalSection(&cookieMutex);
}



message_cookie_data *CreateMessageCookie(WORD bMsgType, BYTE bAckType)
{
  message_cookie_data *pCookie;
  DWORD dwMsgID1;
  DWORD dwMsgID2;

  do
  { // ensure that message ids are unique
    dwMsgID1 = time(NULL);
    dwMsgID2 = RandRange(0, 0x0FFFF);
  } while (FindMessageCookie(dwMsgID1, dwMsgID2, NULL, NULL, NULL));

  pCookie = (message_cookie_data*)SAFE_MALLOC(sizeof(message_cookie_data));
  if (pCookie)
  {
    pCookie->bMessageType = bMsgType;
    pCookie->nAckType = bAckType;
    pCookie->dwMsgID1 = dwMsgID1;
    pCookie->dwMsgID2 = dwMsgID2;
  }
  return pCookie;
}