[En-Nut-Discussion] ICMP Sockets
Ralph Mason
ralph.mason at telogis.com
Mon Aug 11 10:10:17 CEST 2003
I have implemented ICMP sockets for NutOS if anyone is interested.
You can do tracert, ping etc.
You can also receive unreachable messages etc for a given TCP or UDP
connection.
Example code
int ping(char* args[],int argc, FILE* f){
u_long addr;
ICMPSOCKET* sock;
u_char data[32];
int i;
if ( argc < 2 ){
fputs_P(PSTR("usage: ping host [timeout]\r\n"),f);
return -1;
}
if( (addr = resolve_address_name(args[1],f)) == 0 ){
cr(f);
return 0;
}
sock = NutIcmpCreateSocket(IPPROTO_ICMP,1);
fprintf_P(f,PSTR("Pinging %s [%a] with 32 bytes\r\n\r\n"),args[1],addr);
fflush(f);
for( i = 0 ; i < 4 ; i++){
u_long reply_addr;
u_char type;
u_char code;
u_long ticks;
NutIcmpSendTo(sock,addr,ICMP_ECHO,0,data,32);
ticks = NutGetTickCount();
if ( NutIcmpReceiveFrom(sock,&reply_addr,&type,&code,data,32,3000) != 0){
switch( type){
case ICMP_ECHOREPLY:
fprintf_P(f,PSTR("Relpy from %a %i ms\r\n"),reply_addr,
(int)(((NutGetTickCount()-ticks)*1000)/ 40L));
break;
default:
fprintf_P(f,PSTR("Relpy from %a type %i code %i\r\n"),
reply_addr,type,code);
}
}
else{
fputs_P(PSTR("Request timed out.\r\n"),f);
}
fflush(f);
}
NutIcmpDestroySocket(sock);
return 0;
}
If you want the code reply via email.
Cheers
Ralph
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.491 / Virus Database: 290 - Release Date: 18/06/2003
More information about the En-Nut-Discussion
mailing list