source: protocols/oscar/msgcookie.c @ 727a68b

Last change on this file since 727a68b was e88fe7da, checked in by Veres Lajos <vlajos@…>, at 2015-08-07T21:53:25Z

typofix - https://github.com/vlajos/misspell_fixer

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[b7d3cc34]1/*
2 * Cookie Caching stuff. Adam wrote this, apparently just some
3 * derivatives of n's SNAC work. I cleaned it up, added comments.
[5ebff60]4 *
[b7d3cc34]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!
[5ebff60]28 *
[b7d3cc34]29 */
30int aim_cachecookie(aim_session_t *sess, aim_msgcookie_t *cookie)
31{
32        aim_msgcookie_t *newcook;
33
[5ebff60]34        if (!sess || !cookie) {
[b7d3cc34]35                return -EINVAL;
[5ebff60]36        }
[b7d3cc34]37
38        newcook = aim_checkcookie(sess, cookie->cookie, cookie->type);
[5ebff60]39
[b7d3cc34]40        if (newcook == cookie) {
41                newcook->addtime = time(NULL);
42                return 1;
[5ebff60]43        } else if (newcook) {
[b7d3cc34]44                aim_cookie_free(sess, newcook);
[5ebff60]45        }
[b7d3cc34]46
[5ebff60]47        cookie->addtime = time(NULL);
[b7d3cc34]48
49        cookie->next = sess->msgcookies;
50        sess->msgcookies = cookie;
51
52        return 0;
53}
54
55/**
56 * aim_uncachecookie - grabs a cookie from the cookie cache (removes it from the list)
57 * @sess: session to grab cookie from
58 * @cookie: cookie string to look for
59 * @type: cookie type to look for
60 *
61 * 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.
62 *
63 * if found, returns the struct; if none found (or on error), returns NULL:
64 */
65aim_msgcookie_t *aim_uncachecookie(aim_session_t *sess, guint8 *cookie, int type)
66{
67        aim_msgcookie_t *cur, **prev;
68
[5ebff60]69        if (!cookie || !sess->msgcookies) {
[b7d3cc34]70                return NULL;
[5ebff60]71        }
[b7d3cc34]72
73        for (prev = &sess->msgcookies; (cur = *prev); ) {
[5ebff60]74                if ((cur->type == type) &&
75                    (memcmp(cur->cookie, cookie, 8) == 0)) {
[b7d3cc34]76                        *prev = cur->next;
77                        return cur;
78                }
79                prev = &cur->next;
80        }
81
82        return NULL;
83}
84
85/**
86 * aim_mkcookie - generate an aim_msgcookie_t *struct from a cookie string, a type, and a data pointer.
87 * @c: pointer to the cookie string array
88 * @type: cookie type to use
89 * @data: data to be cached with the cookie
90 *
91 * returns NULL on error, a pointer to the newly-allocated cookie on
92 * success.
93 *
94 */
[5ebff60]95aim_msgcookie_t *aim_mkcookie(guint8 *c, int type, void *data)
[b7d3cc34]96{
97        aim_msgcookie_t *cookie;
98
[5ebff60]99        if (!c) {
[b7d3cc34]100                return NULL;
[5ebff60]101        }
[b7d3cc34]102
[5ebff60]103        if (!(cookie = g_new0(aim_msgcookie_t, 1))) {
[b7d3cc34]104                return NULL;
[5ebff60]105        }
[b7d3cc34]106
107        cookie->data = data;
108        cookie->type = type;
109        memcpy(cookie->cookie, c, 8);
110
111        return cookie;
112}
113
114/**
115 * aim_checkcookie - check to see if a cookietuple has been cached
116 * @sess: session to check for the cookie in
117 * @cookie: pointer to the cookie string array
118 * @type: type of the cookie to look for
119 *
120 * this returns a pointer to the cookie struct (still in the list) on
121 * success; returns NULL on error/not found
122 *
123 */
124
125aim_msgcookie_t *aim_checkcookie(aim_session_t *sess, const guint8 *cookie, int type)
126{
127        aim_msgcookie_t *cur;
128
129        for (cur = sess->msgcookies; cur; cur = cur->next) {
[5ebff60]130                if ((cur->type == type) &&
131                    (memcmp(cur->cookie, cookie, 8) == 0)) {
132                        return cur;
133                }
[b7d3cc34]134        }
135
136        return NULL;
137}
138
139/**
140 * aim_cookie_free - free an aim_msgcookie_t struct
141 * @sess: session to remove the cookie from
142 * @cookiep: the address of a pointer to the cookie struct to remove
143 *
[e88fe7da]144 * this function removes the cookie *cookie from the list of cookies
[b7d3cc34]145 * in sess, and then frees all memory associated with it. including
146 * its data! if you want to use the private data after calling this,
147 * make sure you copy it first.
148 *
149 * returns -1 on error, 0 on success.
150 *
151 */
[5ebff60]152int aim_cookie_free(aim_session_t *sess, aim_msgcookie_t *cookie)
[b7d3cc34]153{
154        aim_msgcookie_t *cur, **prev;
155
[5ebff60]156        if (!sess || !cookie) {
[b7d3cc34]157                return -EINVAL;
[5ebff60]158        }
[b7d3cc34]159
160        for (prev = &sess->msgcookies; (cur = *prev); ) {
[5ebff60]161                if (cur == cookie) {
[b7d3cc34]162                        *prev = cur->next;
[5ebff60]163                } else {
[b7d3cc34]164                        prev = &cur->next;
[5ebff60]165                }
[b7d3cc34]166        }
167
168        g_free(cookie->data);
169        g_free(cookie);
170
171        return 0;
[5ebff60]172}
Note: See TracBrowser for help on using the repository browser.