void list_emplace_back (linked_list_t list, void* val) { node_t n; n = node_create (); n->data = val; list_insert_back (list, n); }
void* heap_wrap_alloc_c(struct heap_wrap* h, int size) { int __size = size + sizeof(struct listlink); void* buff = alloc(h->__parent_alloc, h->__parent, size); dbg_assert(buff != NULL); /* link the allocated block in to list */ { struct listlink* n_link = (struct listlink*)buff; list_insert_back(&h->allocated, n_link); } /* return the 'real' address */ return (char*)buff + sizeof(struct listlink); }
void* heap_wrap_alloc_v(struct heap_wrap* h, int size, const char* file ,int line) { int __size = size + sizeof(struct listlink); //void* buff = alloc(h->__alloc, h->__parent, __size); void* buff = h->__parent_alloc(h->__parent, __size, file, line); dbg_assert(buff != NULL); /* link the allocated block in to list */ { struct listlink* n_link = (struct listlink*)buff; list_insert_back(&h->allocated, n_link); } /* return the 'real' address */ buff = (void*)((char*)buff + sizeof(struct listlink)); return buff; }
struct list* list_remove(struct list* l, int x, int y) { struct list* l_or = l; struct list* l_new = list_new(); while(l != NULL) { if(l->x != x || l->y != y) { l_new = list_insert_back(l_new, l->x, l->y, l->type, l->data); } l = l->next; } l_or = list_delete(l_or); return l_new; }
/*============================================================================= Function route_packet Purpose: routes a packet...that is it decrements the ttl and places it on the appropriate outgoing queue if packet is valid, a route is found, the queue has room, and the packet has not timed out Parameters: *buf (IN) - the buffer containing the packet to route len (IN) - the length of the buffer Returns: nothing =============================================================================*/ void route_packet(char *buf, int len) { struct packet_entry *pe; pthread_t *new_st; /* new send thread */ struct ip_hdr *ip = (struct ip_hdr *)buf; int idx, qsize; printf("Routing received packet:\n"); #ifdef DEBUG printf("recv'ed (%d): ",len); print_packet(buf,len); #endif if (len<sizeof(struct ip_hdr)) { printf("Invalid packet, packet discarded.\n"); return; } if (ip->ttl<=1) { printf("Discarding timed out packet.\n"); return; } ip->ttl-=1; printf("ttl decremented to %d decimal, 0x%02x hex.\n",ip->ttl,ip->ttl); if ((idx = iface_match(ip->destadd))==-1) printf("No matching route found, packet discarded.\n"); else { pthread_mutex_lock(ifaces[idx].qmut); qsize = ifaces[idx].sendq->size; if (qsize<MAX_QUEUE) { printf("Sending packet on iface with port %d.\n",ifaces[idx].lport); pe = packet_entry_init(buf,len); list_insert_back(ifaces[idx].sendq,pe); } else { printf("Send queue full for iface with port %d, packet discarded.\n", ifaces[idx].lport); } pthread_mutex_unlock(ifaces[idx].qmut); if (!qsize) { /* queue was empty, thread must be started */ new_st = (pthread_t *)malloc(sizeof(pthread_t)); if (pthread_create(new_st,NULL,send_packet,&ifaces[idx])!=0) error("pthread_create"); } } }
/*============================================================================= Function process_hrefs Purpose: processes the hrefs on a page adding them to the end of the queue Parameters: *line (IN) - line to process for hrefs *server_context (IN) - server part of URL containing line *full_context (IN) - server and path part of URL containing line context_depth (IN) - the depth in the spider of the page containing line Returns: adds found URLs from href tags to the end of the queue =============================================================================*/ void process_hrefs(char *line, char *server_context, char *full_context, int context_depth) { int len; char *p = line; char url[MAX_URL]; while ((p = strcasestr(p,"a href="))) { p += strlen("a href="); p += strspn(p," '\""); /* skip anything before the URL */ len = strcspn(p," '\">"); /* find the termination of the URL */ if (*p=='#' || strncasecmp(p,"mailto:",strlen("mailto:"))==0) return; url[0] = 0; /* prepare for strncat */ /* three cases for URL, full, relative to server, relative to context */ if (strncasecmp(p,"http://",strlen("http://"))==0 && len+1<MAX_URL) { /* full URL and length OK */ strncat(url,p,len); } else if (*p=='/' && strlen(server_context)+len+1<MAX_URL) { /* relative to server URL and length OK */ strncat(url,server_context,strlen(server_context)); strncat(url,p,len); } else if (strlen(full_context)+len+1<MAX_URL) { /* relative to context URL and length OK */ strncat(url,full_context,strlen(full_context)); strncat(url,p,len); } else { /* URL too long for our MAX_URL */ return; } /* try to put URL in the trie with allow duplicates = false */ if (trie_insert(url_trie,url,0)) { list_insert_back(url_entries,url_entry_init(url,context_depth+1)); #ifdef DEBUG printf("process_hrefs: inserting new URL: %s\n",url); #endif } else { #ifdef DEBUG printf("process_hrefs: rejecting duplicate URL: %s\n",url); #endif } } }
static void heap_buddy_expand_memory(struct heap_buddy* pheap, int expand_size) { struct block_c_pool* n_blk_pool = (struct block_c_pool*) alloc(pheap->__parent_alloc, pheap->__parent, sizeof(struct block_c_pool)); void* n_mem_begin = alloc(pheap->__parent_alloc, pheap->__parent, expand_size); dbg_assert(n_mem_begin != NULL); { /* initialize the new memory */ struct block_c *sent_first = NULL; struct block_c *sent_last = NULL; struct block_c *init_block = NULL; void* n_mem_end = (void*)((char*)n_mem_begin + expand_size); init_block = block_com_make_sentinels( n_mem_begin, n_mem_end, &sent_first, &sent_last); block_com_set_free(init_block, true); n_blk_pool->memory = n_mem_begin; n_blk_pool->size = expand_size; n_blk_pool->bc_first = init_block; n_blk_pool->bc_front_sent = sent_first; n_blk_pool->bc_end_sent = sent_last; } /* link the new memory into memory list */ list_insert_back(&pheap->memlist, &n_blk_pool->link); /* insert the new block into the heap */ { struct heap_buddy_block *n_block = container_of(n_blk_pool->bc_first, struct heap_buddy_block, common); int sz = block_com_data_size(n_blk_pool->bc_first); blink_push(&pheap->buddy[mlog2(sz)], &n_block->link); } }
/*============================================================================= Function main Purpose: main entry point of program, processes input and starts search Parameters: argc (IN) - count of command line arguments *argv (IN) - array of pointers to command line arguments Returns: nothing =============================================================================*/ int main(int argc, char *argv[]) { int c; char *p; struct url_entry *ue; /* used for processing url_entries */ /* first process command line optional arguments */ while(--argc>0 && (*++argv)[0]=='-') while((c = *++argv[0])) switch(c) { case 'i': case_independent = 1; break; case 'l': only_show_url = 1; break; case 'v': show_non_matching = 1; break; case 'd': if (*++argv[0]!='=') { fprintf(stderr,"Illegal depth value, argument will be ignored\n"); break; } search_depth = strtol(++argv[0],&p,0); if (search_depth<0 || errno==ERANGE || p==argv[0]) { fprintf(stderr,"Illegal depth value, argument will be ignored\n"); search_depth = DEFAULT_SEARCH_DEPTH; argv[0] = p-1; /* above ++ will point to first unconverted */ break; } argv[0] = p-1; /* so above ++ will point to first unconverted */ break; case 't': if (*++argv[0]!='=') { fprintf(stderr,"Illegal connect timeout value, " "argument will be ignored\n"); break; } connect_timeout = strtol(++argv[0],&p,0); if (connect_timeout<0 || errno==ERANGE || p==argv[0]) { fprintf(stderr,"Illegal connect timeout value, " "argument will be ignored\n"); connect_timeout = DEFAULT_CONNECT_TIMEOUT; argv[0] = p-1; /* above ++ will point to first unconverted */ break; } argv[0] = p-1; /* so above ++ will point to first unconverted */ break; default: fprintf(stderr,"Unknown option %c ignored\n",c); } /* now process pattern URL+ if they are present */ if (argc<2) { fprintf(stderr, "Usage: websearch [OPTION...] pattern URL+\n" "valid options:\n" " -i\tspecifies case-independent matching\n" " -l\tspecifies show matching URLs, not lines\n" " -v\tspecifies show non-matching lines or URLs\n" " -d=N\tspecifies search depth, default = 0\n" " -t=N\tspecifies timeout in secs, default = 0 (no timeout)\n"); exit(1); } pattern = argv[0]; /* store pattern */ url_entries = list_init(); /* create url entries list */ url_trie = trie_init(); /* create url trie */ /* add all URLs to the URL entries with depth 0 */ while (*++argv) { ue = url_entry_init(argv[0],0); list_insert_back(url_entries,ue); } #ifdef DEBUG printf("Case independence: %d\n",case_independent); printf("Only show url: %d\n",only_show_url); printf("Show non mathcing: %d\n",show_non_matching); printf("Search depth: %d\n",search_depth); printf("Connect timeout: %d\n",connect_timeout); printf("Pattern: %s\n",pattern); printf("URL Count: %d\n",url_entries->size); #endif /* keep processing an entry at a time until the list is empty */ while(url_entries->size>0) { ue = list_remove_front(url_entries); process_url_entry(ue); url_entry_free(ue); } return 0; }