]> andersk Git - libfaim.git/blob - aim_snac.c
08db68e96509e85e0f0a7624debd986a0e441a61
[libfaim.git] / aim_snac.c
1 /*
2  *
3  * Various SNAC-related dodads... 
4  *
5  * outstanding_snacs is a list of aim_snac_t structs.  A SNAC should be added
6  * whenever a new SNAC is sent and it should remain in the list until the
7  * response for it has been receieved.  
8  *
9  * cleansnacs() should be called periodically by the client in order
10  * to facilitate the aging out of unreplied-to SNACs. This can and does
11  * happen, so it should be handled.
12  *
13  */
14
15 #define FAIM_INTERNAL
16 #include <faim/aim.h>
17
18 /*
19  * Called from aim_session_init() to initialize the hash.
20  */
21 faim_internal void aim_initsnachash(struct aim_session_t *sess)
22 {
23   int i;
24
25   for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
26     sess->snac_hash[i] = NULL;
27     faim_mutex_init(&sess->snac_hash_locks[i]);
28   }
29
30   return;
31 }
32
33 faim_internal unsigned long aim_cachesnac(struct aim_session_t *sess,
34                                           const unsigned short family,
35                                           const unsigned short type,
36                                           const unsigned short flags,
37                                           const void *data, const int datalen)
38 {
39   struct aim_snac_t snac;
40
41   snac.id = sess->snac_nextid++;
42   snac.family = family;
43   snac.type = type;
44   snac.flags = flags;
45
46   snac.data = malloc(datalen);
47   memcpy(snac.data, data, datalen);
48
49   return aim_newsnac(sess, &snac);
50 }
51
52 /*
53  * Clones the passed snac structure and caches it in the
54  * list/hash.
55  */
56 faim_internal unsigned long aim_newsnac(struct aim_session_t *sess,
57                                         struct aim_snac_t *newsnac) 
58 {
59   struct aim_snac_t *snac = NULL;
60   int index;
61
62   if (!newsnac)
63     return 0;
64
65   if (!(snac = calloc(1, sizeof(struct aim_snac_t))))
66     return 0;
67   memcpy(snac, newsnac, sizeof(struct aim_snac_t));
68   snac->issuetime = time(&snac->issuetime);
69   snac->next = NULL;
70
71   index = snac->id % FAIM_SNAC_HASH_SIZE;
72
73   faim_mutex_lock(&sess->snac_hash_locks[index]);
74   snac->next = sess->snac_hash[index];
75   sess->snac_hash[index] = snac;
76   faim_mutex_unlock(&sess->snac_hash_locks[index]);
77
78   return(snac->id);
79 }
80
81 /*
82  * Finds a snac structure with the passed SNAC ID, 
83  * removes it from the list/hash, and returns a pointer to it.
84  *
85  * The returned structure must be freed by the caller.
86  *
87  */
88 faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, 
89                                              u_long id) 
90 {
91   struct aim_snac_t *cur = NULL;
92   int index;
93
94   index = id % FAIM_SNAC_HASH_SIZE;
95
96   faim_mutex_lock(&sess->snac_hash_locks[index]);
97   if (!sess->snac_hash[index])
98     ;
99   else if (sess->snac_hash[index]->id == id) {
100     cur = sess->snac_hash[index];
101     sess->snac_hash[index] = cur->next;
102   } else {
103     cur = sess->snac_hash[index];
104     while (cur->next) {
105       if (cur->next->id == id) {
106         struct aim_snac_t *tmp;
107         
108         tmp = cur->next;
109         cur->next = cur->next->next;
110         cur = tmp;
111         break;
112       }
113       cur = cur->next;
114     }
115   }
116   faim_mutex_unlock(&sess->snac_hash_locks[index]);
117
118   return cur;
119 }
120
121 /*
122  * This is for cleaning up old SNACs that either don't get replies or
123  * a reply was never received for.  Garabage collection. Plain and simple.
124  *
125  * maxage is the _minimum_ age in seconds to keep SNACs.
126  *
127  */
128 faim_internal int aim_cleansnacs(struct aim_session_t *sess,
129                                  int maxage)
130 {
131   int i;
132
133   for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) {
134     struct aim_snac_t *cur, **prev;
135     time_t curtime;
136
137     faim_mutex_lock(&sess->snac_hash_locks[i]);
138     if (!sess->snac_hash[i]) {
139       faim_mutex_unlock(&sess->snac_hash_locks[i]);
140       continue;
141     }
142
143     curtime = time(NULL); /* done here in case we waited for the lock */
144
145     for (prev = &sess->snac_hash[i]; (cur = *prev); ) {
146       if ((curtime - cur->issuetime) > maxage) {
147
148         *prev = cur->next;
149
150         /* XXX should we have destructors here? */
151         if (cur->data)
152           free(cur->data);
153         free(cur);
154
155       } else
156         prev = &cur->next;
157     }
158
159     faim_mutex_unlock(&sess->snac_hash_locks[i]);
160   }
161
162   return 0;
163 }
164
165 faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid)
166 {
167   int curbyte = 0;
168   curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));
169   curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff));
170   curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff));
171   curbyte += aimutil_put32(buf+curbyte, snacid);
172   return curbyte;
173 }
This page took 0.036363 seconds and 3 git commands to generate.