Beispiel #1
0
/*******************************************************************************
* Function Name: SendPing
********************************************************************************
* Summary:
*   Generate and send a Ping request to an IP specified by targetIP
*
* Parameters:
*   targetIP - IP Address to which the ping request will be directed.
*   example: unsigned char LabPC_IP[]={192,168,1,15}; then
*               ..
*              SendPing(LabPC_IP);
* Returns:
*   TRUE(0)- if the Ping request was successfully sent.
*   FALSE(1) - if the Ping request was not successful in transmission.
*******************************************************************************/
unsigned int SendPing( unsigned char* targetIP ){
    unsigned int i;
    
    /*declare an ICMP header for our ping request packet*/
    ICMPhdr ping;
    
    /*Setup the IP header part of it*/
    SetupBasicIPPacket( (unsigned char*)&ping, ICMPPROTOCOL, targetIP );
    
    /*Setup the Ping flags*/
    ping.ip.flags = 0x0;
    ping.type = 0x8;
    ping.codex = 0x0;
    ping.chksum = 0x0;
    ping.iden = (0x1);
    ping.seqNum = (76);
    
    /*Fill in the dummy data*/
    for(i=0;i<18;i++){
        *((unsigned char*)&ping+sizeof(ICMPhdr)+i)='A'+i;
    }
    /*Write the length field*/
    ping.ip.len = (60-sizeof(EtherNetII));
    
    /*Compute the checksums*/
    ping.chksum=checksum(((unsigned char*)&ping) + sizeof(IPhdr ),(sizeof(ICMPhdr) - sizeof(IPhdr))+18,0);
    ping.ip.chksum = checksum(((unsigned char*)&ping) + sizeof(EtherNetII),sizeof(IPhdr) - sizeof(EtherNetII),0);
    
    /*Send it!*/
    return(MACWrite( (unsigned char*)&ping, sizeof(ICMPhdr)+18 ));  
}
Beispiel #2
0
/*******************************************************************************
* Function Name: ReplyTCP_Webserver
********************************************************************************
* Summary:
*   This is used to send the webpage generated by AddWebServerData,in response
*   to the GET query.This function ACK's the GET query,and then sends the reply.
*   
* Parameters:
*    TCPPkt - pointer to a TCP packet that has the Webpage.
*    len  - length of this packet.
* Returns:
*   TRUE(0)- if the packet was successfully sent.
*   FALSE(1) - if the packet was not successful in transmission.
*******************************************************************************/
unsigned int ReplyTCP_Webserver(TCPhdr* TCPPkt,unsigned int datlen){
    
    /*Send an ACK for the GET query*/
    ackTcp(TCPPkt,(TCPPkt->ip.len)+14,0,0,0,0);
    
    /*Based on that ACK we sent,create the reply to the GET query*/
    /*Set the flags.*/
    TCPPkt->ACK=1;
    TCPPkt->PSH=1;
    TCPPkt->FIN=1;
    
    /*Set the length field*/
    TCPPkt->ip.len=(sizeof(TCPhdr)+datlen)-sizeof(EtherNetII);
    
    /*Zero out and then compute IP checksum*/
    TCPPkt->ip.chksum=0x00;
    TCPPkt->ip.chksum=checksum((unsigned char*)TCPPkt + sizeof(EtherNetII),sizeof(IPhdr)-sizeof(EtherNetII),0);
    
    /*Zero out and then compute TCP checksum*/
    TCPPkt->chksum=0x00;
    TCPPkt->chksum=checksum((unsigned char*)TCPPkt->ip.source,0x08+0x14+datlen,2);
    
    /*Send the reply*/
    return(MACWrite((unsigned char*)TCPPkt,sizeof(TCPhdr)+datlen)); 
 }
Beispiel #3
0
/*******************************************************************************
* Function Name: PingReply
********************************************************************************
* Summary:
*   Generate and send a Ping reply from a received request.
*
* Parameters:
*   ping - A pointer of type ICMPhdr,to the received request's packet.
*   len - length of the recd. Ping request.          
* Returns:
*   TRUE(0)- if the Ping Reply was successfully sent.
*   FALSE(1) - if the Ping Reply was not successful in transmission.
*******************************************************************************/
unsigned int PingReply(ICMPhdr* ping,unsigned int len){

    if ( ping->type == ICMPREQUEST ){
   /*Yes,it is a Request,lets reply.*/
   /*Setup the packet as a reply*/
    ping->type = ICMPREPLY;
    ping->chksum = 0x0;
    ping->ip.chksum = 0x0;
    
    /*Swap the MAC Addresses in the ETH header*/
    memcpy( ping->ip.eth.DestAddrs, ping->ip.eth.SrcAddrs, 6);
    memcpy( ping->ip.eth.SrcAddrs, deviceMAC,6 );
    
    /*Swap the IP Addresses in the IP header*/
    memcpy( ping->ip.dest, ping->ip.source,4);
    memcpy( ping->ip.source, deviceIP,4);
    
    /*Compute the checksums*/
    ping->chksum=checksum(((unsigned char*) ping) + sizeof(IPhdr ),len - sizeof(IPhdr),0);
    ping->ip.chksum = checksum(((unsigned char*) ping) + sizeof(EtherNetII),sizeof(IPhdr) - sizeof(EtherNetII),0);
    
    /*Send it!*/
    return(MACWrite((unsigned char*) ping, len));
  }
  return FALSE;
}
void SendArpPacket(unsigned char* targetIP)
{
  ARP arpPacket;
  
  /*----Setup EtherNetII Header----*/
  //The source of the packet will be the device mac address.
  memcpy( arpPacket.eth.SrcAddrs, deviceMAC, sizeof(deviceMAC) );
  //The destination is broadcast ie all bits are 0xff.
  memset( arpPacket.eth.DestAddrs, 0xff, sizeof(deviceMAC) );
  //The type of packet being sent is an ARP
  arpPacket.eth.type = HTONS(ARPPACKET);
  
  /*----Setup ARP Header----*/
  arpPacket.hardware = HTONS(ETHERNET);
  //We are wanting IP address resolved.
  arpPacket.protocol = HTONS(IPPACKET);
  arpPacket.hardwareSize = sizeof(deviceMAC);
  arpPacket.protocolSize = sizeof(deviceIP);
  arpPacket.opCode = HTONS(ARPREQUEST);
  
  //Target MAC is set to 0 as it is unknown.
  memset( arpPacket.targetMAC, 0, sizeof(deviceMAC) );
  //Sender MAC is the device MAC address.
  memcpy( arpPacket.senderMAC, deviceMAC, sizeof(deviceMAC) );
  //The target IP is the IP address we want resolved.
  memcpy( arpPacket.targetIP, targetIP, sizeof(routerIP));
  
  //If we are just making sure this IP address is not in use fill in the 
  //sender IP address as 0. Otherwise use the device IP address.
  if ( !memcmp( targetIP, deviceIP, sizeof(deviceIP) ) )
  { 
    memset( arpPacket.senderIP, 0, sizeof(deviceIP) );
  }
  else
  {
    memcpy( arpPacket.senderIP, deviceIP, sizeof(deviceIP) );
  }
  
  //Send out the packet.
  MACWrite((unsigned char*)&arpPacket,sizeof(ARP));
}