示例#1
0
void flushSockets() {
    if (!(packetLength = enc28j60PacketReceive(BUFFER_SIZE, buffer))) { //No packet available for reading!
        return;
    }
    else if (eth_type_is_arp_and_my_ip(buffer, packetLength)) {
        if (arp_packet_is_myreply_arp(buffer)) {
#ifdef ETHERSHIELD_DEBUG
            ethershieldDebug("Received ARP reply.\r\n");
#endif
			// Add it to the arp cache list
			add_arpcacheentry(buffer+ETH_SRC_MAC_P, buffer+ETH_ARP_DST_IP_P);			
        }
        else {
            make_arp_answer_from_request(buffer);
#ifdef ETHERSHIELD_DEBUG
            ethershieldDebug("Answering ARP request.\r\n");
#endif
        }
    }
    else if (!eth_type_is_ip_and_my_ip(buffer, packetLength)) {
#ifdef ETHERSHIELD_DEBUG
        ethershieldDebug("Ignoring packet not for me.\r\n");
#endif
        return;
    }
    else if (buffer[IP_PROTO_P] == IP_PROTO_ICMP_V &&
            buffer[ICMP_TYPE_P] == ICMP_TYPE_ECHOREQUEST_V) {
        make_echo_reply_from_request(buffer, packetLength);
#ifdef ETHERSHIELD_DEBUG
        sprintf(debugStr, "Replying ICMP ECHO REQUEST from %d.%d.%d.%d.\r\n",
                buffer[IP_SRC_IP_P], buffer[IP_SRC_IP_P + 1],
                buffer[IP_SRC_IP_P + 2], buffer[IP_SRC_IP_P + 3]);
        ethershieldDebug(debugStr);
#endif
    }
    else if (buffer[IP_PROTO_P] == IP_PROTO_TCP_V) {
        //DEBUG: it's TCP and for me! Do I want it?
        uint16_t destinationPort = (buffer[TCP_DST_PORT_H_P] << 8) | buffer[TCP_DST_PORT_L_P];

#ifdef ETHERSHIELD_DEBUG
        sprintf(debugStr, "Received TCP packet from %d.%d.%d.%d:%u on port %d\r\n",
                buffer[IP_SRC_IP_P], buffer[IP_SRC_IP_P + 1],
                buffer[IP_SRC_IP_P + 2], buffer[IP_SRC_IP_P + 3],
                (buffer[TCP_SRC_PORT_H_P] << 8) | buffer[TCP_SRC_PORT_L_P],
                destinationPort);
        ethershieldDebug(debugStr);
#endif

        uint8_t i, socketSelected = MAX_SOCK_NUM;
        for (i = 0; i < MAX_SOCK_NUM; i++) {
            if (_SOCKETS[i].sourcePort == destinationPort) {
                socketSelected = i;
				if(_SOCKETS[i].state == SOCK_LISTEN) {
					_SOCKETS[i].destinationPort = (buffer[TCP_SRC_PORT_H_P] << 8) | buffer[TCP_SRC_PORT_L_P];
					memcpy(_SOCKETS[i].destinationIp, &buffer[IP_SRC_IP_P], 4);
				}
                break;
            }
        }
#ifdef ETHERSHIELD_DEBUG
        ethershieldDebug("  Socket selected: ");
        itoa(socketSelected, debugStr, 10);
        ethershieldDebug(debugStr);
        ethershieldDebug("\r\n");
#endif

        if (socketSelected == MAX_SOCK_NUM) {
            //TODO: reply and say that nobody is listening on that port
			return;
        }
        //TODO: change next 'if' to 'else if'
        //DEBUG: ok, the TCP packet is for me and I want it.
        if (buffer[TCP_FLAGS_P] == (TCP_FLAG_SYN_V | TCP_FLAG_ACK_V)) {
#ifdef ETHERSHIELD_DEBUG
            ethershieldDebug("  It is TCP SYNACK, sending ACK\r\n");
#endif
            make_tcp_ack_from_any(buffer);
            //TODO: verify if I'm waiting for this SYN+ACK
            _SOCKETS[socketSelected].clientState = SOCK_ESTABLISHED;
            return;
        }
        else if (buffer[TCP_FLAGS_P] & TCP_FLAGS_SYN_V) {
#ifdef ETHERSHIELD_DEBUG
            ethershieldDebug("  It is TCP SYN, sending SYNACK\r\n");
#endif
            _SOCKETS[socketSelected].state = SOCK_ESTABLISHED;
            make_tcp_synack_from_syn(buffer);
        }
        else if (buffer[TCP_FLAGS_P] & TCP_FLAGS_ACK_V) {
            uint16_t data;

#ifdef ETHERSHIELD_DEBUG
                ethershieldDebug("  Got an ACK...\r\n");
#endif			
			
            init_len_info(buffer);
			
			_SOCKETS[socketSelected].packets = 0;
			_SOCKETS[socketSelected].ackNumber = (uint32_t)buffer[TCP_SEQ_H_P] << 24 | (uint32_t)buffer[TCP_SEQ_H_P + 1] << 16  |  (uint32_t)buffer[TCP_SEQ_L_P] << 8 | (uint32_t)buffer[TCP_SEQ_L_P + 1];
			
            data = get_tcp_data_pointer();
            if (!data) {
#ifdef ETHERSHIELD_DEBUG
                ethershieldDebug("  It is ACK with no data\r\n");
#endif
                if (buffer[TCP_FLAGS_P] & TCP_FLAGS_FIN_V) {
#ifdef ETHERSHIELD_DEBUG
                    ethershieldDebug("    It is ACKFIN, closing socket\r\n");
#endif
                    make_tcp_ack_from_any(buffer);
                    _SOCKETS[socketSelected].state = SOCK_CLOSED;
                    _SOCKETS[socketSelected].sendPacketLength = 0;
					_SOCKETS[socketSelected].ackNumber = 0;
					_SOCKETS[socketSelected].sentData = 0;
					_SOCKETS[socketSelected].packets = 0;
                    free(_SOCKETS[socketSelected].dataBuffer);
					_SOCKETS[socketSelected].bytesToRead = 0;
                }
                return;
            }
            else {
                int dataSize;

				make_tcp_ack_from_any(buffer); //TODO-ACK

				_SOCKETS[socketSelected].ackNumber = (uint32_t)buffer[TCP_SEQACK_H_P] << 24 | (uint32_t)buffer[TCP_SEQACK_H_P + 1] << 16  |  (uint32_t)buffer[TCP_SEQACK_L_P] << 8 | (uint32_t)buffer[TCP_SEQACK_L_P + 1];

                dataSize = packetLength - (&buffer[data] - buffer);
#ifdef ETHERSHIELD_DEBUG
                itoa(dataSize, debugStr, 10);
                ethershieldDebug("  It is ACK with data, ACK sent\r\n");
                ethershieldDebug("    # bytes: ");
                ethershieldDebug(debugStr);
                ethershieldDebug("\r\n");

#endif
                _SOCKETS[socketSelected].state = SOCK_ESTABLISHED;

				if(_SOCKETS[socketSelected].bytesToRead > 0) {
					_SOCKETS[socketSelected].dataBuffer = realloc(_SOCKETS[socketSelected].dataBuffer, _SOCKETS[socketSelected].bytesToRead + (dataSize * sizeof(char))); //TODO: and about the TCP/IP/Ethernet overhead?
					memcpy(_SOCKETS[socketSelected].dataBuffer+_SOCKETS[socketSelected].bytesToRead, &buffer[data], dataSize);
				} else {
					_SOCKETS[socketSelected].dataBuffer = malloc(dataSize * sizeof(char)); //TODO: and about the TCP/IP/Ethernet overhead?
					memcpy(_SOCKETS[socketSelected].dataBuffer, &buffer[data], dataSize);
				}					
				//TODO: Add handler to malloc returning: No enough SRAM)
				
                _SOCKETS[socketSelected].bytesToRead += dataSize;
#ifdef ETHERSHIELD_DEBUG
                ethershieldDebug("    Data:\r\n");
                for (i = 0; i < dataSize; i++) {
                    serial_write(_SOCKETS[socketSelected].dataBuffer[i]);
                }
                ethershieldDebug("\r\n");
#endif
                return;
            }
        }
        else {
#ifdef ETHERSHIELD_DEBUG
            ethershieldDebug("Don't know what to do!\r\n");
#endif
            //make_tcp_ack_from_any(buffer); //TODO-ACK: send ACK using tcp_client_send_packet
        }
    }
}
示例#2
0
uint8_t EtherShield::ES_arp_packet_is_myreply_arp ( uint8_t *buf ){
	return arp_packet_is_myreply_arp (buf);
}
uint8_t EtherShieldWrapper::ESW_arp_packet_is_myreply_arp ( uint8_t *buf ){
	return arp_packet_is_myreply_arp (buf);
}