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