Exemplo n.º 1
0
/*prints all the files associated with any word*/
int so(char *str, hashTable *ht) {
  char *word;
  FileNode *fptr; /* Iterates through filenames  */

  if (strcmp(strtok(str, " "), "so") != 0) {
    printf("I did not get 'so' inside sa function!!\n");
    return 1;
  }

  /* goes through each word in input */
  while ((word = strtok(NULL, " "))) {
    for (ptr = getFiles(word, root); ptr != NULL;
        ptr = ptr->next) {
      insert_to_list(t, ptr->filename);
    }
  }

  /* Means we got no matches */
  if (t->files == NULL) {
    printf("No matches found.\n");
    destroyTree(t);
    return 1;
  }

  /* prints all the filenames */
  for (ptr = t->files; ptr->next != NULL; ptr = ptr->next)
    printf("%s, ", ptr->filename);
  if (ptr)
    printf("%s\n", ptr->filename);

  destroyTree(t);
  return 0;
}
Exemplo n.º 2
0
/*
 * coalesce - boundary tag coalescing
 * will delete coalesced old blocks from the free list, and
 * will insert the new block to the free list
 * return pointer to coalesced block
 */
static void *coalesce(void *bp) {
    size_t prev_alloc, next_alloc;
    size_t size;
    prev_alloc = block_prev_alloc(bp);
    next_alloc = block_alloc(next_block(bp));
    size = block_size(bp);
    dbg_printf("want to coalesce %d size block, prev alloc: %d, next alloc: %d\n", (int)size, (int)prev_alloc, (int)next_alloc);
    if (prev_alloc && next_alloc) { // alloc - free - alloc
        return bp;
    } else if (prev_alloc && !next_alloc) { // alloc - free - free
        delete_from_list(bp);
        delete_from_list(next_block(bp));
        if (next_block(bp) == last_block) {
            last_block = bp;
        } 
        size += block_size(next_block(bp));
        mark(bp, size, prev_alloc, 0);
        insert_to_list(bp);
    } else if (!prev_alloc && next_alloc) { // free - free (- alloc)
        delete_from_list(prev_block(bp));
        delete_from_list(bp);
        if (bp == last_block) {
            last_block = prev_block(bp);
        }
        size += block_size(prev_block(bp));
        bp = prev_block(bp);
        mark(bp, size, block_prev_alloc(bp), 0);
        insert_to_list(bp);
    } else if (!prev_alloc && !next_alloc) { // free - free - free
        delete_from_list(prev_block(bp));
        delete_from_list(bp);
        delete_from_list(next_block(bp));
        if (next_block(bp) == last_block) {
            last_block = prev_block(bp);
        }
        size += block_size(prev_block(bp)) + block_size(next_block(bp));
        bp = prev_block(bp);
        mark(bp, size, block_prev_alloc(bp), 0);
        insert_to_list(bp);
    }
    return bp;
}
Exemplo n.º 3
0
// read all inputs from file or user
void read_input(int fd_in, struct list_s *list)
{
    bool more = true;
    struct node_s *tmp;
    
    while (more)
    {
        tmp = (struct node_s *) malloc(sizeof(struct node_s));
        more = read_line(fd_in, tmp);
        if (tmp->data[0] != '\n')
            insert_to_list(list, tmp);
    }
}
Exemplo n.º 4
0
void timer::add_new_timer(unsigned int timeout_msec, timer_node_t* node, timer_handler* handler, void* user_data, timer_req_type_t req_type)
{
	memset(node, 0, sizeof(*node));
	node->handler = handler;
	node->req_type = req_type;
	node->user_data = user_data;
	node->orig_time_msec = timeout_msec;

	BULLSEYE_EXCLUDE_BLOCK_START
	if (IS_NODE_INVALID(node)) {
		free(node);
		return; 
	}
	BULLSEYE_EXCLUDE_BLOCK_END

	insert_to_list(node);
	return;
}
Exemplo n.º 5
0
/*
 * free - free a allocated block
 */
void free(void *bp) {
    if (bp == NULL) {
        return;
    }
    dbg_printf("want to free %d size block in address 0x%lx\n", (int)block_size(bp), (long)bp);
    print_heap();
    if (block_alloc(bp) == 0) {
        return;
    }
    if (heap_head == NULL) {
        mm_init();
    }
    mark(bp, block_size(bp), block_prev_alloc(bp), 0);
    mark(next_block(bp), block_size(next_block(bp)), 0, block_alloc(next_block(bp)));
    insert_to_list(bp);
    bp = coalesce(bp);
    dbg_printf("want return from free %d size block in address 0x%lx\n", (int)block_size(bp), (long)bp);
    print_heap();
}
Exemplo n.º 6
0
/*
 * extend_heap - extend heap with a new free block and return its block pointer
 */
static void *extend_heap(size_t size) {
    void *bp;
    dbg_printf("want to extend heap by %d size\n", (int)size);
    bp = mem_sbrk(size);
    if (bp == (void *)-1) {
        return NULL;
    }
    if (last_block == NULL) {
        last_block = bp;
        mark(bp, size, 1, 0);
    } else {
        mark(bp, size, block_alloc(last_block), 0);
        last_block = bp;
    }
    PUT(HEAD(next_block(bp)), PACK(0, 0, 1)); /* set new epilogue */
    insert_to_list(bp);
    bp = coalesce(bp);
    return bp;
}
Exemplo n.º 7
0
/*
 * place - transform a free block to an allocated block,
 * then delete old free block from its free list
 * and insert the rest part to the corresponding free list
 */
static void place(void *bp, size_t alloc_block_size) {
    size_t size;
    delete_from_list(bp);
    size = block_size(bp);
    dbg_printf("want to place a %d size allocated block from a %d size free block\n", (int)alloc_block_size, (int)size);
    if (size - alloc_block_size >= 4 * WSIZE) {
        /* have rest part for a new free block */
        if (last_block == bp) {
            last_block = bp + alloc_block_size;
        }
        mark(bp, alloc_block_size, block_prev_alloc(bp), 1);
        bp += alloc_block_size;
        mark(bp, size - alloc_block_size, 1, 0);
        insert_to_list(bp);
    } else {
        /* have no rest part for a new free block */
        mark(bp, size, block_prev_alloc(bp), 1);
        mark(next_block(bp), block_size(next_block(bp)), 1, block_alloc(next_block(bp)));
    }
}
Exemplo n.º 8
0
/*prints all the files associated with ALL the words*/
int sa(char *str, tnode root) {
  char *word;
  int count = 0;
  lnode ptr; /* Iterates through filenames  */
  tnode t = create_tnode(); /*lets us keep a list*/

  if (strcmp(strtok(str, " "), "sa") != 0) {
    return 1;
  }


  /* goes through each word in input */
  while ((word = strtok(NULL, " "))) {
    /* goes through each filename for associated word */
    for (ptr = getFiles(word, root); ptr != NULL;
        ptr = ptr->next) {
      insert_to_list(t, ptr->filename);
    }
    count++;
  }



  /* prints all the filenames */
  for (ptr = t->files; ptr != NULL && ptr->count != count; ptr = ptr->next);
  if (ptr != NULL) {
    printf("%s", ptr->filename);
    for (ptr = ptr->next; ptr != NULL; ptr = ptr->next) {
      if(ptr->count == count)
        printf(", %s", ptr->filename);
    }
    printf("\n");
  }
  else {
    printf("No matches found\n");
  }

  destroyTree(t);
  return 0;
}
Exemplo n.º 9
0
void got_packet(u_char *args, const struct pcap_pkthdr *header, const u_char *packet) {
	const struct sniff_ip *ip;              /* The IP header */
	const struct sniff_udp *udp;            /* The UDP header*/
	int size_ip, count = 0;
	bool frthr_analysis = false;
	unsigned offset = 0;
	struct DNS_HEADER *dns;
	struct QUESTION *question;
	char *qname;
	const u_char *temp;
	unsigned int p = 0;
	int i, j, index;
        struct RES_RECORD *response;
	struct node *resp_node, *resp_node1;
	struct timeval packet_tv, tv;
	time_t nowtime;
	struct tm *nowtm;
        char tmbuf[64], buf[64];

	packet_tv = header->ts;
	ip = (struct sniff_ip*)(packet + SIZE_ETHERNET);
        size_ip = IP_HL(ip)*4;

	if (size_ip < 20) {
//		printf("   * Invalid IP header length: %u bytes\n", size_ip); (don't print)
		return;
	}

	switch(ip->ip_p) {
		case IPPROTO_TCP:
			frthr_analysis = false;
			break;
		case IPPROTO_UDP:
			frthr_analysis = true;
			break;
		case IPPROTO_ICMP:
			frthr_analysis = false;
			break;
		case IPPROTO_IP:
			return;
		default:
			return;
        }

	if (frthr_analysis) {
		udp = (struct sniff_udp*)(packet + SIZE_ETHERNET + size_ip);
	
		if (ntohs(udp->uh_sport) == 53) {
			offset = SIZE_ETHERNET + size_ip + SIZE_UDP;

			dns = (struct DNS_HEADER *)(packet + offset);
			offset += DNS_HDRLEN;
			
			/* Parse the QNAMe until \0 */
                        temp = packet + offset;
                        int qname_len = 0, stop = 0;

                        qname = ReadName(temp, &qname_len);
			offset = qname_len; 
			question = (struct QUESTION *)(temp + offset);
			offset = offset + sizeof(struct QUESTION);
			
//			printf("Response Ans Count %d\n", ntohs(dns->ancount));
			count = ntohs(dns->ancount);

			resp_node = Allocate_node();
			resp_node->txid = dns->id;
			resp_node->tv = packet_tv;
			resp_node->uh_sum = udp->uh_sum;
			strncpy(resp_node->question, qname, strlen(qname));
			resp_node->anscount = 0;

			while (count > 0) {
				response = (struct RES_RECORD*)(temp + offset);
				if ((ntohs(response->type) == 1) && (ntohs(response->data_len) == 4)) {
					struct in_addr source;
					memset(&source, 0, sizeof(source));
					source.s_addr = response->rdata;
//					printf("Ans : %s\n", inet_ntoa(source));
					strcpy(resp_node->answers[resp_node->anscount], inet_ntoa(source));
					resp_node->anscount = (resp_node->anscount) + 1;
//					printf("Ans count in the node : %d\n", resp_node->anscount);
					offset = offset + sizeof(struct RES_RECORD);
				} else {
					offset = offset + 12 + ntohs(response->data_len);
				}
				count = count-1;
			}
			resp_node1 = lookup(resp_node);
			if (resp_node1) {
				int count1 = 0, ind = 0;
				memset(tmbuf, 0, 64);
				memset(buf, 0, 64);
				tv = header->ts;
				nowtime = tv.tv_sec;
				nowtm = localtime(&nowtime);
				strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S", nowtm);
				snprintf(buf, sizeof buf, "%s.%06ld", tmbuf, tv.tv_usec);

				printf("%s DNS poisoning attempt\n", buf);
				printf("TXID 0x%x Request %s\n", dns->id, qname);
				printf("Answer1 [");
				count1 = resp_node1->anscount;
				while (ind < count1) {
					printf("%s, ", resp_node1->answers[ind]);
					ind++;
				}
				printf("]\n");
				printf("Answer2 [");
				count1 = resp_node->anscount;
				ind = 0;
				while (ind < count1) {
					printf("%s, ", resp_node->answers[ind]);
					ind++;
				}
				printf("]\n\n");	
			}
			insert_to_list(resp_node);
			free(qname);
		}
	}
}