X-Git-Url: http://andersk.mit.edu/gitweb/libfaim.git/blobdiff_plain/96f8b1edd2c6bb991cb4fec0a5bef6570ceb502f..37ee990eab0e60fadbc1555d3f4a7e85d2026297:/aim_snac.c?ds=sidebyside diff --git a/aim_snac.c b/aim_snac.c index dd87849..08db68e 100644 --- a/aim_snac.c +++ b/aim_snac.c @@ -1,4 +1,3 @@ - /* * * Various SNAC-related dodads... @@ -13,12 +12,13 @@ * */ +#define FAIM_INTERNAL #include /* * Called from aim_session_init() to initialize the hash. */ -void aim_initsnachash(struct aim_session_t *sess) +faim_internal void aim_initsnachash(struct aim_session_t *sess) { int i; @@ -30,12 +30,31 @@ void aim_initsnachash(struct aim_session_t *sess) return; } +faim_internal unsigned long aim_cachesnac(struct aim_session_t *sess, + const unsigned short family, + const unsigned short type, + const unsigned short flags, + const void *data, const int datalen) +{ + struct aim_snac_t snac; + + snac.id = sess->snac_nextid++; + snac.family = family; + snac.type = type; + snac.flags = flags; + + snac.data = malloc(datalen); + memcpy(snac.data, data, datalen); + + return aim_newsnac(sess, &snac); +} + /* * Clones the passed snac structure and caches it in the * list/hash. */ -u_long aim_newsnac(struct aim_session_t *sess, - struct aim_snac_t *newsnac) +faim_internal unsigned long aim_newsnac(struct aim_session_t *sess, + struct aim_snac_t *newsnac) { struct aim_snac_t *snac = NULL; int index; @@ -56,8 +75,6 @@ u_long aim_newsnac(struct aim_session_t *sess, sess->snac_hash[index] = snac; faim_mutex_unlock(&sess->snac_hash_locks[index]); - printf("faim: cached snac %lx\n", snac->id); - return(snac->id); } @@ -68,8 +85,8 @@ u_long aim_newsnac(struct aim_session_t *sess, * The returned structure must be freed by the caller. * */ -struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, - u_long id) +faim_internal struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, + u_long id) { struct aim_snac_t *cur = NULL; int index; @@ -108,14 +125,15 @@ struct aim_snac_t *aim_remsnac(struct aim_session_t *sess, * maxage is the _minimum_ age in seconds to keep SNACs. * */ -int aim_cleansnacs(struct aim_session_t *sess, - int maxage) +faim_internal int aim_cleansnacs(struct aim_session_t *sess, + int maxage) { - struct aim_snac_t *cur, *next, *prev = NULL; - time_t curtime; int i; for (i = 0; i < FAIM_SNAC_HASH_SIZE; i++) { + struct aim_snac_t *cur, **prev; + time_t curtime; + faim_mutex_lock(&sess->snac_hash_locks[i]); if (!sess->snac_hash[i]) { faim_mutex_unlock(&sess->snac_hash_locks[i]); @@ -124,26 +142,18 @@ int aim_cleansnacs(struct aim_session_t *sess, curtime = time(NULL); /* done here in case we waited for the lock */ - cur = sess->snac_hash[i]; - while (cur) { - next = cur->next; + for (prev = &sess->snac_hash[i]; (cur = *prev); ) { if ((curtime - cur->issuetime) > maxage) { - if (sess->snac_hash[i] == cur) - prev = sess->snac_hash[i] = next; - else - prev->next = next; - printf("faim: killing ancient snac %lx (%lx)\n", cur->id, curtime - cur->issuetime); - + *prev = cur->next; + /* XXX should we have destructors here? */ if (cur->data) free(cur->data); free(cur); - } else { - prev = cur; - } - cur = next; + } else + prev = &cur->next; } faim_mutex_unlock(&sess->snac_hash_locks[i]); @@ -152,7 +162,7 @@ int aim_cleansnacs(struct aim_session_t *sess, return 0; } -int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) +faim_internal int aim_putsnac(u_char *buf, int family, int subtype, int flags, u_long snacid) { int curbyte = 0; curbyte += aimutil_put16(buf+curbyte, (u_short)(family&0xffff));