FITkit
Fakulta informačních technologií

enc28j60_icmp.c

Zobrazit dokumentaci tohoto souboru.
00001 /*******************************************************************************
00002    enc28j60_icmp.h: Implementace protokolu ICMP
00003    Copyright (C) 2010 Brno University of Technology,
00004                       Faculty of Information Technology
00005    Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
00006 
00007    LICENSE TERMS
00008 
00009    Redistribution and use in source and binary forms, with or without
00010    modification, are permitted provided that the following conditions
00011    are met:
00012    1. Redistributions of source code must retain the above copyright
00013       notice, this list of conditions and the following disclaimer.
00014    2. Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in
00016       the documentation and/or other materials provided with the
00017       distribution.
00018    3. All advertising materials mentioning features or use of this software
00019       or firmware must display the following acknowledgement:
00020 
00021         This product includes software developed by the University of
00022         Technology, Faculty of Information Technology, Brno and its
00023         contributors.
00024 
00025    4. Neither the name of the Company nor the names of its contributors
00026       may be used to endorse or promote products derived from this
00027       software without specific prior written permission.
00028 
00029    This software or firmware is provided ``as is'', and any express or implied
00030    warranties, including, but not limited to, the implied warranties of
00031    merchantability and fitness for a particular purpose are disclaimed.
00032    In no event shall the company or contributors be liable for any
00033    direct, indirect, incidental, special, exemplary, or consequential
00034    damages (including, but not limited to, procurement of substitute
00035    goods or services; loss of use, data, or profits; or business
00036    interruption) however caused and on any theory of liability, whether
00037    in contract, strict liability, or tort (including negligence or
00038    otherwise) arising in any way out of the use of this software, even
00039    if advised of the possibility of such damage.
00040 
00041    $Id$
00042 
00043 *******************************************************************************/ 
00044 #include "enc28j60_spi.h"
00045 #include "enc28j60_ip.h"
00046 #include "enc28j60_icmp.h"
00047 
00048 ICMP_APP_HANDLER(*icmp_handler_);
00049 
00050 unsigned int icmp_seq_num = 1;
00051 unsigned char handler_set = 0; 
00052 
00053 
00054 
00055 void icmp_recv(struct ip_h* ip_header){
00056         
00057         struct icmp_h icmp_header;
00058         
00059         rx_read(&icmp_header,ICMP_HEADER_LEN);
00060         
00061         if(handler_set == 1){
00062                 //volani handleru
00063                 (*(icmp_handler_))(ip_header, &icmp_header);    
00064         }
00065         else{ 
00066 
00067         
00068                 //obsluha podle typu a kodu
00069                 switch(icmp_header.type){
00070                         case ICMP_ECHO:                         //echo pozadavek
00071                         
00072                                 if( tx_init(ICMP_PROTO) < 0)
00073                                         return;
00074 
00075                                 unsigned int len;
00076                                 unsigned char temp[5];                  //buffer, pres ktery se budou kopirovat data ECHO zpravy        
00077                                 
00078                                 while(rx_left() > 0){                   //precte vsechny data a da je do odesilaciho bufferu
00079                                         len = rx_read(&temp,5);
00080                                         tx_write(&temp,len);
00081                                 }
00082                                 tx_close();
00083                                         
00084                                         //odesle ICMP odpoved
00085                                 icmp_send(NTOHL(ip_header->src_ip),ICMP_ECHO_REPLY, ICMP_ECHO_CODE, NTOHS(icmp_header.id), NTOHS(icmp_header.seq));
00086                                 break;
00087                 }
00088         }
00089 
00090 return;
00091 }
00092 
00093 char icmp_send(unsigned long ip, unsigned char type, unsigned char code, unsigned int id, unsigned int sequence ){
00094 /*      
00095         if(!link_up()){                 //kabel odpojen,nebudeme odesilat
00096                 tx_unlock(TX2);
00097                 return -1;
00098         }
00099 */
00100         
00101         struct icmp_h icmp_header;
00102         
00103         icmp_header.type = type;                //zapsani dat do hlavicky
00104         icmp_header.code = code;
00105         icmp_header.checksum = 0x0000;
00106         icmp_header.id = HTONS(id);
00107         icmp_header.seq = HTONS(sequence);
00108 
00109         tx_seek(ICMP_HEADER);
00110         tx_write(&icmp_header,ICMP_HEADER_LEN);
00111 
00112         tx_seek(ICMP_HEADER);
00113         icmp_header.checksum = HTONS(tx_checksum());
00114 
00115         tx_write(&icmp_header,ICMP_HEADER_LEN); 
00116         
00117 return ip_send(ip,IP_PROTO_ICMP);
00118 }
00119 
00120 
00121 void icmp_bind(void* handler){
00122         
00123         icmp_handler_ = handler;
00124         handler_set = 1;
00125 }
00126 
00127 void icmp_unbind(){
00128         handler_set = 0;
00129 }