#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_PEER_LEN 64 typedef unsigned long u64_t; typedef unsigned int u32_t; typedef unsigned short u16_t; typedef unsigned char u8_t; typedef signed char s8_t; typedef u32_t ip_addr_t; typedef u16_t port_t; /* gnat header */ typedef struct gnat_hdr { ip_addr_t lcl_addr; // local address with 32 bytes ip_addr_t ref_addr; // public address with 32 bytes ip_addr_t rel_addr; // relay address with 32 bytes port_t lcl_port; // local port number with 16 bytes port_t ref_port; // public port number with 16 bytes port_t rel_port; // relay port number with 16 bytes u16_t type; // type of action u16_t id_len; // length of actual peer id u8_t peer_id[MAX_PEER_LEN]; // peer_id with 16 bytes defined in peer_length }gnat_hdr_t; typedef struct peer_table{ gnat_hdr_t *peer; struct peer_table *next; } peer_table; /* peer list */ typedef struct peer_list { struct peer_table *first; struct peer_table *last; struct peer_table *head; } peer_list_t; peer_list_t pplist; peer_table *gnat_add_peer(peer_list_t *plist, gnat_hdr_t *peer) { peer_table *ppeer; ppeer = (peer_table *)calloc(1, sizeof(peer_table)); ppeer->peer = peer; if ( ppeer == NULL ) return(NULL); if(plist->first) { ppeer->next = plist->first->next; plist->first->next = ppeer; }else{ ppeer->next = plist->head; plist->head = ppeer; } return ppeer; } static peer_table *gnat_search_peer(peer_list_t *plist, u8_t peer_id[]) { peer_table *pp; pp = plist->head; while(pp) { if (!strcmp((char *)pp->peer->peer_id, (const char *)peer_id)) { break; } pp = pp->next; } return pp; } static u32_t gnat_destroy_peer(peer_list_t *plist, peer_table *peer) { peer_table **pp; pp = &plist->head; while (*pp) { if ( *pp == peer ) break; pp = &((*pp)->next); } if ( *pp == peer) { *pp = peer->next; if (plist->first == peer) plist->first = NULL; if (plist->last == peer ) plist->last = NULL; free(peer); return (0); } else { return -1; } return (0); } int main(void) { gnat_hdr_t msg; u8_t str[32] = "peerA"; peer_table *pp; msg.id_len = 4; msg.lcl_addr = 0; msg.lcl_port = 0; memcpy(msg.peer_id, str, 6); msg.ref_addr = 0; msg.ref_port =0; msg.rel_addr = 0; msg.rel_port = 0; msg.type = 1; gnat_add_peer(&pplist, &msg); pp = gnat_search_peer(&pplist, str); printf("%d\n", pp->peer->id_len); gnat_destroy_peer(&pplist, pp); if((pp = gnat_search_peer(&pplist, str)) == NULL) printf("Delete sucess!\n"); return (0); }
作者:androidzhaoxiaogang 发表于2013-1-1 0:29:58 原文链接
阅读:43 评论:0 查看评论