]>
Commit | Line | Data |
---|---|---|
9de3ca7e | 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 | * First edition badly written by Adam Fritzler (afritz@delphid.ml.org) | |
11 | * Current edition nicely rewritten (it even works) by n (n@ml.org) | |
12 | * | |
13 | */ | |
14 | ||
15 | #include <aim.h> | |
16 | #include <assert.h> | |
17 | ||
18 | struct aim_snac_t *aim_outstanding_snacs = NULL; | |
19 | u_long aim_snac_nextid = 0x00000001; | |
20 | ||
21 | u_long aim_newsnac(struct aim_snac_t *newsnac) { | |
22 | struct aim_snac_t *snac = NULL, *cur = aim_outstanding_snacs; | |
23 | ||
24 | assert(newsnac != NULL); | |
25 | snac = calloc(1, sizeof(struct aim_snac_t)); | |
26 | assert(snac != NULL); | |
27 | memcpy(snac, newsnac, sizeof(struct aim_snac_t)); | |
28 | snac->issuetime = time(&snac->issuetime); | |
29 | snac->next = NULL; | |
30 | ||
31 | if (cur == NULL) { | |
32 | aim_outstanding_snacs = snac; | |
33 | return(snac->id); | |
34 | } | |
35 | while (cur->next != NULL) | |
36 | cur = cur->next; | |
37 | cur->next = snac; | |
38 | return(snac->id); | |
39 | } | |
40 | ||
41 | struct aim_snac_t *aim_remsnac(u_long id) { | |
42 | struct aim_snac_t *cur = aim_outstanding_snacs; | |
43 | ||
44 | if (cur == NULL) | |
45 | return(NULL); | |
46 | if (cur->id == id) { | |
47 | aim_outstanding_snacs = cur->next; | |
48 | return(cur); | |
49 | } | |
50 | while (cur->next != NULL) { | |
51 | if (cur->next->id == id) { | |
52 | struct aim_snac_t *tmp = NULL; | |
53 | ||
54 | tmp = cur->next; | |
55 | cur->next = cur->next->next; | |
56 | return(tmp); | |
57 | } | |
58 | cur = cur->next; | |
59 | } | |
60 | return(NULL); | |
61 | } | |
62 | ||
63 | /* | |
64 | * This is for cleaning up old SNACs that either don't get replies or | |
65 | * a reply was never received for. Garabage collection. Plain and simple. | |
66 | * | |
67 | * maxage is the _minimum_ age in seconds to keep SNACs (though I don't know | |
68 | * why its called _max_age). | |
69 | * | |
70 | */ | |
71 | int aim_cleansnacs(int maxage) | |
72 | { | |
73 | struct aim_snac_t *cur = aim_outstanding_snacs; | |
74 | struct aim_snac_t *remed = NULL; | |
75 | time_t curtime; | |
76 | ||
77 | curtime = time(&curtime); | |
78 | ||
79 | while (cur) | |
80 | { | |
81 | if ( (cur) && (((cur->issuetime) + maxage) < curtime)) | |
82 | { | |
83 | #if DEBUG > 1 | |
84 | printf("aimsnac: WARNING purged obsolete snac %ul\n", cur->id); | |
85 | #endif | |
86 | remed = aim_remsnac(cur->id); | |
87 | if (remed) | |
88 | { | |
89 | if (remed->data) | |
90 | free(remed->data); | |
91 | free(remed); | |
92 | } | |
93 | } | |
94 | cur = cur->next; | |
95 | } | |
96 | ||
97 | return 0; | |
98 | } | |
99 | ||
100 | int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) | |
101 | { | |
102 | int curbyte = 0; | |
103 | curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff)); | |
104 | curbyte += aimutil_put16(buf+curbyte, (u_short)(subtype&0xffff)); | |
105 | curbyte += aimutil_put16(buf+curbyte, (u_short)(flags&0xffff)); | |
106 | curbyte += aimutil_put32(buf+curbyte, snacid); | |
107 | return curbyte; | |
108 | } |