]> andersk Git - libfaim.git/blobdiff - aim_tlv.c
Locking around tx seqnum.
[libfaim.git] / aim_tlv.c
index d03f2f92cfdb41d40ea49c8c389e9e31ec10d3af..42d36027ecb00e30990ff0c74a87a9a9bd6056d3 100644 (file)
--- a/aim_tlv.c
+++ b/aim_tlv.c
@@ -67,6 +67,143 @@ void aim_freetlvchain(struct aim_tlvlist_t **list)
   return;
 }
 
+int aim_counttlvchain(struct aim_tlvlist_t **list)
+{
+  struct aim_tlvlist_t *cur;
+  int count = 0;
+
+  if (!list || !(*list))
+    return 0;
+
+  for (cur = *list; cur; cur = cur->next)
+    count++;
+  return count;
+}
+
+int aim_addtlvtochain_str(struct aim_tlvlist_t **list, unsigned short type, char *str, int len)
+{
+  struct aim_tlvlist_t *new;
+  struct aim_tlvlist_t *cur;
+
+  if (!list)
+    return 0;
+
+  new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
+  memset(new, 0x00, sizeof(struct aim_tlvlist_t));
+
+  new->tlv = aim_createtlv();  
+  new->tlv->type = type;
+  new->tlv->length = len;
+  new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
+  memcpy(new->tlv->value, str, new->tlv->length);
+
+  new->next = NULL;
+
+  if (*list == NULL) {
+    *list = new;
+  } else if ((*list)->next == NULL) {
+    (*list)->next = new;
+  } else {
+    for(cur = *list; cur->next; cur = cur->next)
+      ;
+    cur->next = new;
+  }
+  return new->tlv->length;
+}
+
+int aim_addtlvtochain16(struct aim_tlvlist_t **list, unsigned short type, unsigned short val)
+{
+  struct aim_tlvlist_t *new;
+  struct aim_tlvlist_t *cur;
+
+  if (!list)
+    return 0;
+
+  new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
+  memset(new, 0x00, sizeof(struct aim_tlvlist_t));
+
+  new->tlv = aim_createtlv();  
+  new->tlv->type = type;
+  new->tlv->length = 2;
+  new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
+  aimutil_put16(new->tlv->value, val);
+
+  new->next = NULL;
+
+  if (*list == NULL) {
+    *list = new;
+  } else if ((*list)->next == NULL) {
+    (*list)->next = new;
+  } else {
+    for(cur = *list; cur->next; cur = cur->next)
+      ;
+    cur->next = new;
+  }
+  return 2;
+}
+
+int aim_addtlvtochain32(struct aim_tlvlist_t **list, unsigned short type, unsigned long val)
+{
+  struct aim_tlvlist_t *new;
+  struct aim_tlvlist_t *cur;
+
+  if (!list)
+    return 0;
+
+  new = (struct aim_tlvlist_t *)malloc(sizeof(struct aim_tlvlist_t));
+  memset(new, 0x00, sizeof(struct aim_tlvlist_t));
+
+  new->tlv = aim_createtlv();  
+  new->tlv->type = type;
+  new->tlv->length = 4;
+  new->tlv->value = (u_char *)malloc(new->tlv->length*sizeof(u_char));
+  aimutil_put32(new->tlv->value, val);
+
+  new->next = NULL;
+
+  if (*list == NULL) {
+    *list = new;
+  } else if ((*list)->next == NULL) {
+    (*list)->next = new;
+  } else {
+    for(cur = *list; cur->next; cur = cur->next)
+      ;
+    cur->next = new;
+  }
+  return 4;
+}
+
+int aim_writetlvchain(u_char *buf, int buflen, struct aim_tlvlist_t **list)
+{
+  int goodbuflen = 0;
+  int i = 0;
+  struct aim_tlvlist_t *cur;
+
+  if (!list || !buf || !buflen)
+    return 0;
+
+  /* do an initial run to test total length */
+  for (cur = *list; cur; cur = cur->next) {
+    goodbuflen += 2 + 2; /* type + len */
+    goodbuflen += cur->tlv->length;
+  }
+
+  if (goodbuflen > buflen)
+    return 0; /* not enough buffer */
+
+  /* do the real write-out */
+  for (cur = *list; cur; cur = cur->next) {
+    i += aimutil_put16(buf+i, cur->tlv->type);
+    i += aimutil_put16(buf+i, cur->tlv->length);
+    memcpy(buf+i, cur->tlv->value, cur->tlv->length);
+    i += cur->tlv->length;
+  }
+
+  return i;
+}
+
+
 /*
  * Grab the Nth TLV of type type in the TLV list list.
  */
This page took 0.07579 seconds and 4 git commands to generate.