Project Embedded Systems E-ict Denayer
Dependencies: BSP_DISCO_F746NG F7_Ethernet LCD_DISCO_F746NG TS_DISCO_F746NG mbed-rtos mbed
ping.cpp@2:1a5565ee8219, 2017-01-26 (annotated)
- Committer:
- Ayrton_L
- Date:
- Thu Jan 26 10:40:35 2017 +0000
- Revision:
- 2:1a5565ee8219
- Parent:
- 1:a2f7adf6db3d
presentatie
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ayrton_L | 1:a2f7adf6db3d | 1 | #include "ping.h" |
Ayrton_L | 1:a2f7adf6db3d | 2 | |
Ayrton_L | 1:a2f7adf6db3d | 3 | void v_MaakPingRequest( struct icmp_echo_hdr *x_ICMPEchoHdr, uint16_t us_Lenght ) |
Ayrton_L | 1:a2f7adf6db3d | 4 | { |
Ayrton_L | 1:a2f7adf6db3d | 5 | int32_t ul_I = 0; |
Ayrton_L | 1:a2f7adf6db3d | 6 | int32_t ul_DataLenght = 0; |
Ayrton_L | 1:a2f7adf6db3d | 7 | ul_DataLenght = us_Lenght - sizeof( struct icmp_echo_hdr ); |
Ayrton_L | 1:a2f7adf6db3d | 8 | |
Ayrton_L | 1:a2f7adf6db3d | 9 | ICMPH_TYPE_SET( x_ICMPEchoHdr, ICMP_ECHO ); |
Ayrton_L | 1:a2f7adf6db3d | 10 | ICMPH_CODE_SET( x_ICMPEchoHdr, 0 ); |
Ayrton_L | 1:a2f7adf6db3d | 11 | x_ICMPEchoHdr->chksum = 0; |
Ayrton_L | 1:a2f7adf6db3d | 12 | x_ICMPEchoHdr->id = PING_ID; |
Ayrton_L | 1:a2f7adf6db3d | 13 | x_ICMPEchoHdr->seqno = htons( ++us_PingSequenceNummer ); //byte order to netwerk byte order |
Ayrton_L | 1:a2f7adf6db3d | 14 | |
Ayrton_L | 1:a2f7adf6db3d | 15 | for( ul_I = 0; ul_I < ul_DataLenght; ul_I++ ) //beetje "random" data erin gooien |
Ayrton_L | 1:a2f7adf6db3d | 16 | { |
Ayrton_L | 1:a2f7adf6db3d | 17 | ( ( char* ) x_ICMPEchoHdr )[sizeof( struct icmp_echo_hdr ) + ul_I] = ( char )ul_I; |
Ayrton_L | 1:a2f7adf6db3d | 18 | } |
Ayrton_L | 1:a2f7adf6db3d | 19 | |
Ayrton_L | 1:a2f7adf6db3d | 20 | x_ICMPEchoHdr->chksum = inet_chksum( x_ICMPEchoHdr, us_Lenght ); |
Ayrton_L | 1:a2f7adf6db3d | 21 | } |
Ayrton_L | 1:a2f7adf6db3d | 22 | |
Ayrton_L | 1:a2f7adf6db3d | 23 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 24 | |
Ayrton_L | 1:a2f7adf6db3d | 25 | err_t ux_Ping( int32_t l_SocketReturn, ip_addr_t *x_IPAddr ) //pingske leggen |
Ayrton_L | 1:a2f7adf6db3d | 26 | { |
Ayrton_L | 1:a2f7adf6db3d | 27 | int32_t l_Err = 0; |
Ayrton_L | 1:a2f7adf6db3d | 28 | int32_t l_PingGrootte = 0; |
Ayrton_L | 1:a2f7adf6db3d | 29 | struct icmp_echo_hdr *x_ICMPEchoHdr; |
Ayrton_L | 1:a2f7adf6db3d | 30 | struct sockaddr_in x_VerzendAddr; |
Ayrton_L | 1:a2f7adf6db3d | 31 | |
Ayrton_L | 1:a2f7adf6db3d | 32 | /* NOTE |
Ayrton_L | 1:a2f7adf6db3d | 33 | Ik doe ne malloc omdat: |
Ayrton_L | 1:a2f7adf6db3d | 34 | Ik voor mijn fucntie om een ping request samen te stellen een pointer nodig heb, |
Ayrton_L | 2:1a5565ee8219 | 35 | doordat een pointer geen grootte heeft en slechts enkel een geheugenplaats kan aanduiden |
Ayrton_L | 1:a2f7adf6db3d | 36 | gebruik ik dus malloc om deze pointer zijn geheugenplaats toe te wijzen waar nadien |
Ayrton_L | 1:a2f7adf6db3d | 37 | al mijn data van mijn ICMP-struct inkom*/ |
Ayrton_L | 1:a2f7adf6db3d | 38 | |
Ayrton_L | 1:a2f7adf6db3d | 39 | l_PingGrootte = sizeof( struct icmp_echo_hdr ) + PING_DATA_SIZE; |
Ayrton_L | 1:a2f7adf6db3d | 40 | x_ICMPEchoHdr = ( struct icmp_echo_hdr * ) mem_malloc( ( mem_size_t ) l_PingGrootte ); //mallocske doen, is helaas niet toegelaten volgens mistra C :( |
Ayrton_L | 1:a2f7adf6db3d | 41 | //mem_size_t afhankelijk van hoe groot, uint16_t of uint32_t is van type size_t => malloc heeft verwacht size_t => unsigned type |
Ayrton_L | 1:a2f7adf6db3d | 42 | if( !x_ICMPEchoHdr ) //heeft ni veel nut dat functie verdergaat en error gooit als Echo failed |
Ayrton_L | 1:a2f7adf6db3d | 43 | { |
Ayrton_L | 1:a2f7adf6db3d | 44 | return ERR_MEM; //Out Of memory errorke geven |
Ayrton_L | 1:a2f7adf6db3d | 45 | } |
Ayrton_L | 1:a2f7adf6db3d | 46 | |
Ayrton_L | 1:a2f7adf6db3d | 47 | v_MaakPingRequest( x_ICMPEchoHdr, ( uint16_t ) l_PingGrootte ); |
Ayrton_L | 1:a2f7adf6db3d | 48 | |
Ayrton_L | 1:a2f7adf6db3d | 49 | x_VerzendAddr.sin_len = sizeof( x_VerzendAddr ); |
Ayrton_L | 1:a2f7adf6db3d | 50 | x_VerzendAddr.sin_family = AF_INET; |
Ayrton_L | 1:a2f7adf6db3d | 51 | inet_addr_from_ipaddr( &x_VerzendAddr.sin_addr, x_IPAddr ); |
Ayrton_L | 1:a2f7adf6db3d | 52 | l_Err = lwip_sendto( l_SocketReturn, x_ICMPEchoHdr, l_PingGrootte, 0, ( struct sockaddr* ) &x_VerzendAddr, sizeof( x_VerzendAddr ) ); |
Ayrton_L | 1:a2f7adf6db3d | 53 | |
Ayrton_L | 1:a2f7adf6db3d | 54 | mem_free( x_ICMPEchoHdr ); |
Ayrton_L | 1:a2f7adf6db3d | 55 | |
Ayrton_L | 1:a2f7adf6db3d | 56 | return ( l_Err ? ERR_OK : ERR_VAL ); //geen error = 0 => ERR_OK anders ERR_VAL zelfs als if(l_Err =0) {return ERR_OK;} else {return ERR_VAL;} |
Ayrton_L | 1:a2f7adf6db3d | 57 | } |
Ayrton_L | 1:a2f7adf6db3d | 58 | |
Ayrton_L | 1:a2f7adf6db3d | 59 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 60 | |
Ayrton_L | 1:a2f7adf6db3d | 61 | void v_PingOntvang( int32_t l_SocketReturn ) |
Ayrton_L | 1:a2f7adf6db3d | 62 | { |
Ayrton_L | 1:a2f7adf6db3d | 63 | char c_Buffer[64]; |
Ayrton_L | 1:a2f7adf6db3d | 64 | uint32_t ul_FromLen = 0; |
Ayrton_L | 1:a2f7adf6db3d | 65 | uint32_t ul_RecvLen = 0; |
Ayrton_L | 1:a2f7adf6db3d | 66 | struct sockaddr_in x_OntvangAdres; |
Ayrton_L | 1:a2f7adf6db3d | 67 | struct ip_hdr *px_IPHdr; |
Ayrton_L | 1:a2f7adf6db3d | 68 | struct icmp_echo_hdr *pux_Echo; |
Ayrton_L | 1:a2f7adf6db3d | 69 | ip_addr_t x_RecvAddr; |
Ayrton_L | 1:a2f7adf6db3d | 70 | |
Ayrton_L | 1:a2f7adf6db3d | 71 | ul_RecvLen = lwip_recvfrom( l_SocketReturn, c_Buffer, sizeof( c_Buffer ), 0, ( struct sockaddr* ) &x_OntvangAdres, ( socklen_t* ) &ul_FromLen); //word gebruikt om te kijken vanwaar de data komt, na oproep heeft deze dus een waarde |
Ayrton_L | 1:a2f7adf6db3d | 72 | |
Ayrton_L | 1:a2f7adf6db3d | 73 | while( ul_RecvLen > 0 ) |
Ayrton_L | 1:a2f7adf6db3d | 74 | { |
Ayrton_L | 1:a2f7adf6db3d | 75 | if( ul_RecvLen >= sizeof( struct ip_hdr ) + sizeof( struct icmp_echo_hdr ) ); |
Ayrton_L | 1:a2f7adf6db3d | 76 | { |
Ayrton_L | 1:a2f7adf6db3d | 77 | inet_addr_to_ipaddr(&x_RecvAddr, &x_OntvangAdres.sin_addr); |
Ayrton_L | 1:a2f7adf6db3d | 78 | |
Ayrton_L | 1:a2f7adf6db3d | 79 | px_IPHdr = ( struct ip_hdr * ) c_Buffer; |
Ayrton_L | 1:a2f7adf6db3d | 80 | pux_Echo = ( struct icmp_echo_hdr * ) ( c_Buffer + ( IPH_HL( px_IPHdr ) * 4)); |
Ayrton_L | 1:a2f7adf6db3d | 81 | |
Ayrton_L | 1:a2f7adf6db3d | 82 | if( ( pux_Echo->id == PING_ID ) && ( pux_Echo->seqno == htons( us_PingSequenceNummer ) ) ) //matchen van data, byte order naar netwerk byte order |
Ayrton_L | 1:a2f7adf6db3d | 83 | { |
Ayrton_L | 1:a2f7adf6db3d | 84 | PING_RESULT( ( ICMPH_TYPE( iecho ) == ICMP_ER ) ); //ping resultaat processen |
Ayrton_L | 1:a2f7adf6db3d | 85 | return; |
Ayrton_L | 1:a2f7adf6db3d | 86 | } |
Ayrton_L | 1:a2f7adf6db3d | 87 | } |
Ayrton_L | 1:a2f7adf6db3d | 88 | |
Ayrton_L | 1:a2f7adf6db3d | 89 | ul_RecvLen = lwip_recvfrom( l_SocketReturn, c_Buffer, sizeof( c_Buffer ), 0, ( struct sockaddr* ) &x_OntvangAdres, ( socklen_t* ) &ul_FromLen ); |
Ayrton_L | 1:a2f7adf6db3d | 90 | } |
Ayrton_L | 1:a2f7adf6db3d | 91 | |
Ayrton_L | 1:a2f7adf6db3d | 92 | PING_RESULT( 0 ); //ping resultaat processen |
Ayrton_L | 1:a2f7adf6db3d | 93 | } |
Ayrton_L | 1:a2f7adf6db3d | 94 | |
Ayrton_L | 1:a2f7adf6db3d | 95 | /*-----------------------------------------------------------*/ |
Ayrton_L | 1:a2f7adf6db3d | 96 | |
Ayrton_L | 1:a2f7adf6db3d | 97 | uint32_t ul_Ping( ip_addr_t *x_PingTarget ) |
Ayrton_L | 1:a2f7adf6db3d | 98 | { |
Ayrton_L | 1:a2f7adf6db3d | 99 | int32_t l_SocketReturn = 0; //vooral om te kunnen debuggen binnen LWIP, sockets.c ... -1 on fail |
Ayrton_L | 1:a2f7adf6db3d | 100 | uint32_t ul_TimeOut = 0; |
Ayrton_L | 1:a2f7adf6db3d | 101 | |
Ayrton_L | 1:a2f7adf6db3d | 102 | l_SocketReturn = lwip_socket( AF_INET, SOCK_RAW, IP_PROTO_ICMP ); |
Ayrton_L | 1:a2f7adf6db3d | 103 | ul_TimeOut = PING_RCV_TIMEO; |
Ayrton_L | 1:a2f7adf6db3d | 104 | |
Ayrton_L | 1:a2f7adf6db3d | 105 | if( l_SocketReturn > -1 ) |
Ayrton_L | 1:a2f7adf6db3d | 106 | { |
Ayrton_L | 1:a2f7adf6db3d | 107 | lwip_setsockopt( l_SocketReturn, SOL_SOCKET, SO_RCVTIMEO, &ul_TimeOut, sizeof( ul_TimeOut ) ); |
Ayrton_L | 1:a2f7adf6db3d | 108 | |
Ayrton_L | 1:a2f7adf6db3d | 109 | //ip_addr_t x_PingTarget = { 0x08080808 }; //Google DNS server, uptime 99.9% => vrij betrouwbaar om naar heen te pingen. Behalve in landen zoals DPRK |
Ayrton_L | 1:a2f7adf6db3d | 110 | |
Ayrton_L | 1:a2f7adf6db3d | 111 | if ( ux_Ping( l_SocketReturn, x_PingTarget ) == ERR_OK ) |
Ayrton_L | 1:a2f7adf6db3d | 112 | { |
Ayrton_L | 1:a2f7adf6db3d | 113 | v_PingOntvang( l_SocketReturn ); |
Ayrton_L | 1:a2f7adf6db3d | 114 | } |
Ayrton_L | 1:a2f7adf6db3d | 115 | sys_msleep( PING_DELAY ); |
Ayrton_L | 1:a2f7adf6db3d | 116 | return 0; |
Ayrton_L | 1:a2f7adf6db3d | 117 | } |
Ayrton_L | 1:a2f7adf6db3d | 118 | else |
Ayrton_L | 1:a2f7adf6db3d | 119 | { |
Ayrton_L | 1:a2f7adf6db3d | 120 | return 1; |
Ayrton_L | 1:a2f7adf6db3d | 121 | } |
Ayrton_L | 1:a2f7adf6db3d | 122 | } |