void pkt_to_nowhere(void)
{
    sock *Conn = s_malloc(sizeof(sock));
    pkt *Pkt = pkt_alloc(500);
    uint32_t len = sizeof(Conn->local);
    Conn->sock = s_socket(SOCK_DOMAIN, SOCK_TYPE, DEFAULT_PROTOCOL);
    Conn->local.sin_family = SOCK_DOMAIN;
    Conn->local.sin_addr.s_addr = htonl(INADDR_ANY); //???
    Conn->local.sin_port = htons(DEFAULT_PORT);
    s_bind(Conn->sock, (struct sockaddr *)&(Conn->local),
           sizeof(Conn->local));
    s_getsockname(Conn->sock, (struct sockaddr *)&(Conn->local),
                  (socklen_t *)&len);

    create_pkt(Pkt, 0, 0, NULL, 0);
    send_pkt(Pkt, Conn->sock, Conn->local);
    shutdown(Conn->sock, SHUT_RDWR);
    free(Conn);
}
//DOES NOT APPROPRIATELY MAKE A PACKET YET
void file_fill_frame(window *Window, file *File, uint32_t seq)
{
    static uint8_t data[MAX_BUFF_SIZE];
    frame *Frame = Window->Frame[get_frame_num(Window, seq)];
    uint32_t data_len = s_fread(data, sizeof(uint8_t), Window->buffsize, File->fp);

    if (data_len == 0)
        Frame->state = FRAME_EMPTY;
    else
    {
        create_pkt(Frame->Pkt, DATA, seq, data, data_len);
        Frame->state = FRAME_FULL;
    }

    printf("Pkt data_len %u\n", data_len);

    if (feof(File->fp))
        Window->eof = TRUE;

    if (Window->eof)
        printf("File is now at EOF\n");
}
Exemple #3
0
void
test_case(uint16_t payload_sz, uint8_t *ip_opt, int ip_opt_size, custom_opt_t *opt,
	uint16_t expected_opt_len)
{
	pkt_t		*pkt;
	error_t		err;
	ip_t		*new_ip;
	ip_t		*ip;
	uint16_t	new_hdr_len;
	int		res;
	uint16_t	old_csum;
	uint16_t	old_len;
	uint16_t	old_hdr_len;
	uint16_t	new_len;
	pkt_t		*copy;
	uint8_t		*pay1, *pay2;

	LOG("Test case : creating packet, appending IP option\n");
	pkt = create_pkt(payload_sz, ip_opt, ip_opt_size);
	ip = (ip_t *)pkt->msg;
	old_hdr_len = ip->ip_hl * 4;
	old_csum = ip->ip_sum;
	old_len = ntohs(ip->ip_len);
	copy = copy_pkt(pkt);

	res = ip_add_custom_option(pkt, opt, &err);
	if (res) {
		LOG_ERR("Test Case failed with %d\n", res);
		free(pkt);
		return;
	}

	LOG("Test case : checking new header len\n");
	new_ip = (ip_t *)pkt->msg;
	new_hdr_len = new_ip->ip_hl * 4;
	LOG("New hdr len : %d\n", new_hdr_len);
	if (new_hdr_len <= MIN_IP_SIZE) {
		LOG_ERR("Test Case failed, expecting bigger header size");
		LOG_ERR("Recieved header size %d\n", new_hdr_len);
		free(pkt);
		return;
	}	

	LOG("Test case : testing new packet len\n");
	new_len = payload_sz + MIN_IP_SIZE + expected_opt_len;
	if (ntohs(new_ip->ip_len) != new_len) {
		LOG_ERR("Test case failed, incorrect packet len\n");
		LOG_ERR("Expected %d, Recvd %d\n", new_len, ntohs(new_ip->ip_len));
		free(pkt);
		return;
	}

	LOG("Test case : checking ip checksum\n");
	if (old_csum == new_ip->ip_sum) {
		LOG_ERR("Test case failed, expecting a different checksum\n");
		free(pkt);
		return;
	}

	LOG("Test case : comparing payload\n");
	pay1 = (uint8_t *)copy->msg + MIN_IP_SIZE + ip_opt_size;
	pay2 = (uint8_t *)new_ip + new_hdr_len;
	if (compare_payload(pay1, pay2, payload_sz)) {
		LOG_ERR("Test case failed, payloads didn't match\n");
		free(pkt);
		return;
	}

	LOG("Test case : remove option\n");
	res = ip_remove_custom_option(pkt, opt, &err);
	if (res) {
		LOG_ERR("Test case failed, remove option failed with %d\n", res);
		free(pkt);
		return;
	}

	LOG("Test case : comparing packet len after removing options\n");
	new_ip = (ip_t *)pkt->msg;
	new_hdr_len = new_ip->ip_hl * 4;
	if (ntohs(new_ip->ip_len) != copy->len) {
		LOG_ERR("Test case failed, recvd len (%d), expected (%d)\n",
		    pkt->len, copy->len);
		free(pkt);
		return;
	}

	LOG("Test case : comparing packet contents after removing options\n");
	pay1 = (uint8_t *)copy->msg + MIN_IP_SIZE + ip_opt_size;
	pay2 = (uint8_t *)new_ip + new_hdr_len;
	if (memcmp(pay1, pay2, payload_sz)) {
		LOG_ERR("Test case failed, contents didn't match\n");
		free(pkt);
		return;
	}

	free(pkt);
}