source: protocols/oscar/msgcookie.c @ 8358691

Last change on this file since 8358691 was 9ff5737, checked in by Wilmer van der Gaast <wilmer@…>, at 2007-11-29T21:55:14Z

printf() in daemons considered harmful.

  • Property mode set to 100644
File size: 4.0 KB
Line 
1/*
2 * Cookie Caching stuff. Adam wrote this, apparently just some
3 * derivatives of n's SNAC work. I cleaned it up, added comments.
4 *
5 */
6
7/*
8 * I'm assuming that cookies are type-specific. that is, we can have
9 * "1234578" for type 1 and type 2 concurrently. if i'm wrong, then we
10 * lose some error checking. if we assume cookies are not type-specific and are
11 * wrong, we get quirky behavior when cookies step on each others' toes.
12 */
13
14#include <aim.h>
15#include "info.h"
16
17/**
18 * aim_cachecookie - appends a cookie to the cookie list
19 * @sess: session to add to
20 * @cookie: pointer to struct to append
21 *
22 * if cookie->cookie for type cookie->type is found, updates the
23 * ->addtime of the found structure; otherwise adds the given cookie
24 * to the cache
25 *
26 * returns -1 on error, 0 on append, 1 on update.  the cookie you pass
27 * in may be free'd, so don't count on its value after calling this!
28 *
29 */
30int aim_cachecookie(aim_session_t *sess, aim_msgcookie_t *cookie)
31{
32        aim_msgcookie_t *newcook;
33
34        if (!sess || !cookie)
35                return -EINVAL;
36
37        newcook = aim_checkcookie(sess, cookie->cookie, cookie->type);
38       
39        if (newcook == cookie) {
40                newcook->addtime = time(NULL);
41                return 1;
42        } else if (newcook)
43                aim_cookie_free(sess, newcook);
44
45        cookie->addtime = time(NULL); 
46
47        cookie->next = sess->msgcookies;
48        sess->msgcookies = cookie;
49
50        return 0;
51}
52
53/**
54 * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list)
55 * @sess: session to grab cookie from
56 * @cookie: cookie string to look for
57 * @type: cookie type to look for
58 *
59 * takes a cookie string and a cookie type and finds the cookie struct associated with that duple, removing it from the cookie list ikn the process.
60 *
61 * if found, returns the struct; if none found (or on error), returns NULL:
62 */
63aim_msgcookie_t *aim_uncachecookie(aim_session_t *sess, guint8 *cookie, int type)
64{
65        aim_msgcookie_t *cur, **prev;
66
67        if (!cookie || !sess->msgcookies)
68                return NULL;
69
70        for (prev = &sess->msgcookies; (cur = *prev); ) {
71                if ((cur->type == type) && 
72                                (memcmp(cur->cookie, cookie, 8) == 0)) {
73                        *prev = cur->next;
74                        return cur;
75                }
76                prev = &cur->next;
77        }
78
79        return NULL;
80}
81
82/**
83 * aim_mkcookie - generate an aim_msgcookie_t *struct from a cookie string, a type, and a data pointer.
84 * @c: pointer to the cookie string array
85 * @type: cookie type to use
86 * @data: data to be cached with the cookie
87 *
88 * returns NULL on error, a pointer to the newly-allocated cookie on
89 * success.
90 *
91 */
92aim_msgcookie_t *aim_mkcookie(guint8 *c, int type, void *data) 
93{
94        aim_msgcookie_t *cookie;
95
96        if (!c)
97                return NULL;
98
99        if (!(cookie = g_new0(aim_msgcookie_t,1)))
100                return NULL;
101
102        cookie->data = data;
103        cookie->type = type;
104        memcpy(cookie->cookie, c, 8);
105
106        return cookie;
107}
108
109/**
110 * aim_checkcookie - check to see if a cookietuple has been cached
111 * @sess: session to check for the cookie in
112 * @cookie: pointer to the cookie string array
113 * @type: type of the cookie to look for
114 *
115 * this returns a pointer to the cookie struct (still in the list) on
116 * success; returns NULL on error/not found
117 *
118 */
119
120aim_msgcookie_t *aim_checkcookie(aim_session_t *sess, const guint8 *cookie, int type)
121{
122        aim_msgcookie_t *cur;
123
124        for (cur = sess->msgcookies; cur; cur = cur->next) {
125                if ((cur->type == type) && 
126                                (memcmp(cur->cookie, cookie, 8) == 0))
127                        return cur;   
128        }
129
130        return NULL;
131}
132
133/**
134 * aim_cookie_free - free an aim_msgcookie_t struct
135 * @sess: session to remove the cookie from
136 * @cookiep: the address of a pointer to the cookie struct to remove
137 *
138 * this function removes the cookie *cookie from teh list of cookies
139 * in sess, and then frees all memory associated with it. including
140 * its data! if you want to use the private data after calling this,
141 * make sure you copy it first.
142 *
143 * returns -1 on error, 0 on success.
144 *
145 */
146int aim_cookie_free(aim_session_t *sess, aim_msgcookie_t *cookie) 
147{
148        aim_msgcookie_t *cur, **prev;
149
150        if (!sess || !cookie)
151                return -EINVAL;
152
153        for (prev = &sess->msgcookies; (cur = *prev); ) {
154                if (cur == cookie)
155                        *prev = cur->next;
156                else
157                        prev = &cur->next;
158        }
159
160        g_free(cookie->data);
161        g_free(cookie);
162
163        return 0;
164} 
Note: See TracBrowser for help on using the repository browser.