Esempio n. 1
0
int main(int argc, char **argv) {
	int status;
	tcp_set_rawsck();

	uint8_t *rcv_data = (uint8_t *) malloc(RCP_BUFFER*sizeof(uint8_t));  
	if (rcv_data == NULL) {
		perror("Error allocating reception buffer\n");
		exit(EXIT_FAILURE); 
	}

	struct tcp_ctrl *tcp_ctrl = tcp_new();
	if ((status = tcp_bind(tcp_ctrl, "209.2.233.44", 52000, "wlan0")) < 0){
		perror("Couldn't bind socket to port\n");
		exit(EXIT_FAILURE); 
	} 
	if ((status = tcp_connect(tcp_ctrl, "www.google.com")) < 0) {
		perror("Couldn't connect to server\n");
		exit(EXIT_FAILURE);
	}
	printf("**************** HANDSHAKE COMPLETED ***********************\n");

	char *sd_data1 = "GET / HTTP/1.1\r\n\r\n";
	tcp_write(tcp_ctrl, sd_data1, strlen(sd_data1));
	printf("**************** FIRST REQUEST COMPLETED ***************\n");
	
	int len = tcp_rcv(tcp_ctrl, rcv_data, RCP_BUFFER);
	printf("**************** FIRST TRANSMISSION COMPLETED ***************\n");

	FILE *f1 = fopen("result1", "ab+");
	fwrite(rcv_data, 1, len, f1);
 	printf("**************** RECORDING FIRST PHASE RESULTS COMPLETED ***************\n");
	
	if (len >= 0) {
		tcp_write(tcp_ctrl, sd_data1, strlen(sd_data1));
		printf("**************** SECOND REQUEST COMPLETED ******************\n");
	
		len = tcp_rcv(tcp_ctrl, rcv_data, RCP_BUFFER);
		printf("**************** SECOND TRANSMISSION COMPLETED ***************\n");	
	
		FILE *f2 = fopen("result2", "ab+");
		fwrite(rcv_data, 1, len, f2);
		printf("**************** RECORDING SECOND PHASE RESULTS COMPLETED ***************\n");
	}	
	else {
		tcp_close(tcp_ctrl);
	}
	free(tcp_ctrl);        		
	free(rcv_data);
	return 0;
}
Esempio n. 2
0
void konlite_call(void)
{
	unsigned short tmp;
	k_tmr++;
	for(tmp=0;tmp<ETH_RXBUFNB;tmp++)
	{
		buf = get_mac_data(tmp); // буфер принятых данных
		if(buf->len)
		{
			m_pkt=get_mac_pkt(buf); // формирование структуры мак пакета
			if(check_mac(m_pkt)) // проверка мак адреса назначения
			{

				switch(get_eth_type(m_pkt))
				{
					case IP_TYPE:
						ip_p=ip_rcv(m_pkt);
						if(check_ip_req(ip_p))
						{
							if(check_mask(ip_p->ip_s)==0) {set_gate_cmd();} // запрос выходит за рамки подсети
							else{clr_gate_cmd();add_arp_tab(ip_p->ip_s,m_pkt->mac_s);} // запрос в пределах подсети
							switch(get_ip_type(ip_p))
							{
								case ICMP_TYPE:
									icmp_p=icmp_rcv(ip_p);
									if(check_ping(icmp_p)) {ping_echo(icmp_p,ip_p);}
									break;
								case UDP_TYPE:
									udp_p=udp_rcv(ip_p);
									portudp_scan(udp_p,ip_p);
									break;
								case TCP_TYPE:
									tcp_p=tcp_rcv(ip_p);
									porttcp_scan(tcp_p,ip_p);
									break;
							}
						}
						break;
					case ARP_TYPE:
						arp_p=arp_rcv(m_pkt); // формирование структуры arp пакета
						if(check_arp_req(arp_p))	// arp запрос
						{
							add_arp_tab(arp_p->ip_s,arp_p->mac_s);
							arp_answer(arp_p);
						}else
						if(check_arp_answer_gate(arp_p))	// arp ответ от шлюза
						{
							set_gate_mac(arp_p->mac_s);
						}
						break;
					case UNDEF_TYPE:
						break;
				}
			}
			unlock_mac_rx(tmp);
		}
	}
	if(k_tmr % GATE_PER == 0) get_gate_mac();	// периодический запрос mac шлюза
}
Esempio n. 3
0
void ip_rcv(void)
{
  uint16_t len = pkt_left;
  SNMP(ip_in_receives);
  if (!pkt_pull(&iph, sizeof(struct iphdr)))
    return;
  plen = ntohs(iph.tot_len);
  /* FIXME: for speed fold ihl/version and be smarter */
  if (iph.ihl < 5 || iph.version != 4 || len < plen) {
    SNMP(ip_in_hdr_errors);
    return;
  }
  plen -= sizeof(struct iphdr));
  if (pkt_len > plen)
    pkt_len = plen;

  /* FIXME: checksum */
  if (iph.ihl != 5)
    if (ip_options())
      return;
  /* No frags for now (memory limits on 8bit) */
  if (iph.frag_off)
    return;
  if (iph.daddr == 0xFFFFFFFF)
    pkt_type = PKT_BROADCAST;
  else if (MULTICAST(iph.daddr))
    pkt_type = PKT_MULTICAST;
  else if (iph.daddr == ip_addr || LOOPBACK(iph.daddr))
    pkt_type = PKT_HOST;
  else
    /* No forwarding so we don't have to worry about martians either */
    return;

  /* FIXME: raw sockets ?? */
  if (iph.protocol == IPPROTO_TCP)
    tcp_rcv();
  else if (iph.protocol == IPPROTO_UDP)
    udp_rcv();
  else if (iph.protocol == IPPROTO_ICMP)
    icmp_rcv();
  else
    icmp_send_unreach(ICMP_DEST_UNREACH, ICMP_PROT_UNREACH);
}