format code Astyle.

This commit is contained in:
aozima 2014-06-14 21:55:51 +08:00
parent 9ab59cf1e0
commit 7a0e9c3fc7
2 changed files with 192 additions and 192 deletions

View File

@ -39,140 +39,140 @@
static u16_t ping_seq_num; static u16_t ping_seq_num;
struct _ip_addr struct _ip_addr
{ {
rt_uint8_t addr0, addr1, addr2, addr3; rt_uint8_t addr0, addr1, addr2, addr3;
}; };
/** Prepare a echo ICMP request */ /** Prepare a echo ICMP request */
static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
{ {
size_t i; size_t i;
size_t data_len = len - sizeof(struct icmp_echo_hdr); size_t data_len = len - sizeof(struct icmp_echo_hdr);
ICMPH_TYPE_SET(iecho, ICMP_ECHO); ICMPH_TYPE_SET(iecho, ICMP_ECHO);
ICMPH_CODE_SET(iecho, 0); ICMPH_CODE_SET(iecho, 0);
iecho->chksum = 0; iecho->chksum = 0;
iecho->id = PING_ID; iecho->id = PING_ID;
iecho->seqno = htons(++ping_seq_num); iecho->seqno = htons(++ping_seq_num);
/* fill the additional data buffer with some data */ /* fill the additional data buffer with some data */
for(i = 0; i < data_len; i++) for(i = 0; i < data_len; i++)
{ {
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
} }
iecho->chksum = inet_chksum(iecho, len); iecho->chksum = inet_chksum(iecho, len);
} }
/* Ping using the socket ip */ /* Ping using the socket ip */
static err_t ping_send(int s, struct ip_addr *addr, int size) static err_t ping_send(int s, struct ip_addr *addr, int size)
{ {
int err; int err;
struct icmp_echo_hdr *iecho; struct icmp_echo_hdr *iecho;
struct sockaddr_in to; struct sockaddr_in to;
size_t ping_size = sizeof(struct icmp_echo_hdr) + size; size_t ping_size = sizeof(struct icmp_echo_hdr) + size;
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
iecho = rt_malloc(ping_size); iecho = rt_malloc(ping_size);
if (iecho == RT_NULL) if (iecho == RT_NULL)
{ {
return ERR_MEM; return ERR_MEM;
} }
ping_prepare_echo(iecho, (u16_t)ping_size); ping_prepare_echo(iecho, (u16_t)ping_size);
to.sin_len = sizeof(to); to.sin_len = sizeof(to);
to.sin_family = AF_INET; to.sin_family = AF_INET;
to.sin_addr.s_addr = addr->addr; to.sin_addr.s_addr = addr->addr;
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
rt_free(iecho); rt_free(iecho);
return (err ? ERR_OK : ERR_VAL); return (err ? ERR_OK : ERR_VAL);
} }
static void ping_recv(int s) static void ping_recv(int s)
{ {
char buf[64]; char buf[64];
int fromlen, len; int fromlen, len;
struct sockaddr_in from; struct sockaddr_in from;
struct ip_hdr *iphdr; struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho; struct icmp_echo_hdr *iecho;
struct _ip_addr *addr; struct _ip_addr *addr;
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0)
{ {
if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr)))
{ {
addr = (struct _ip_addr *)&(from.sin_addr); addr = (struct _ip_addr *)&(from.sin_addr);
rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
iphdr = (struct ip_hdr *)buf; iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4));
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
{ {
return; return;
} }
else else
{ {
rt_kprintf("ping: drop\n"); rt_kprintf("ping: drop\n");
} }
} }
} }
if (len <= 0) if (len <= 0)
{ {
rt_kprintf("ping: timeout\n"); rt_kprintf("ping: timeout\n");
} }
} }
rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size) rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size)
{ {
int s; int s;
int timeout = PING_RCV_TIMEO; int timeout = PING_RCV_TIMEO;
struct ip_addr ping_target; struct ip_addr ping_target;
rt_uint32_t send_time; rt_uint32_t send_time;
struct _ip_addr struct _ip_addr
{ {
rt_uint8_t addr0, addr1, addr2, addr3; rt_uint8_t addr0, addr1, addr2, addr3;
} *addr; } *addr;
send_time = 0; send_time = 0;
if(size == 0)
size = PING_DATA_SIZE;
if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; if(size == 0)
addr = (struct _ip_addr*)&ping_target; size = PING_DATA_SIZE;
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR;
{ addr = (struct _ip_addr*)&ping_target;
rt_kprintf("create socket failled\n");
return -RT_ERROR;
}
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
{
rt_kprintf("create socket failled\n");
return -RT_ERROR;
}
while (1) lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
{
if (ping_send(s, &ping_target, size) == ERR_OK)
{
rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
ping_recv(s);
}
else
{
rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
}
send_time ++; while (1)
if (send_time >= time) break; /* send ping times reached, stop */ {
if (ping_send(s, &ping_target, size) == ERR_OK)
{
rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
ping_recv(s);
}
else
{
rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
}
rt_thread_delay(PING_DELAY); /* take a delay */ send_time ++;
} if (send_time >= time) break; /* send ping times reached, stop */
lwip_close(s);
return RT_EOK; rt_thread_delay(PING_DELAY); /* take a delay */
}
lwip_close(s);
return RT_EOK;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>

View File

@ -39,140 +39,140 @@
static u16_t ping_seq_num; static u16_t ping_seq_num;
struct _ip_addr struct _ip_addr
{ {
rt_uint8_t addr0, addr1, addr2, addr3; rt_uint8_t addr0, addr1, addr2, addr3;
}; };
/** Prepare a echo ICMP request */ /** Prepare a echo ICMP request */
static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len) static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
{ {
size_t i; size_t i;
size_t data_len = len - sizeof(struct icmp_echo_hdr); size_t data_len = len - sizeof(struct icmp_echo_hdr);
ICMPH_TYPE_SET(iecho, ICMP_ECHO); ICMPH_TYPE_SET(iecho, ICMP_ECHO);
ICMPH_CODE_SET(iecho, 0); ICMPH_CODE_SET(iecho, 0);
iecho->chksum = 0; iecho->chksum = 0;
iecho->id = PING_ID; iecho->id = PING_ID;
iecho->seqno = htons(++ping_seq_num); iecho->seqno = htons(++ping_seq_num);
/* fill the additional data buffer with some data */ /* fill the additional data buffer with some data */
for(i = 0; i < data_len; i++) for(i = 0; i < data_len; i++)
{ {
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i; ((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
} }
iecho->chksum = inet_chksum(iecho, len); iecho->chksum = inet_chksum(iecho, len);
} }
/* Ping using the socket ip */ /* Ping using the socket ip */
static err_t ping_send(int s, struct ip_addr *addr, int size) static err_t ping_send(int s, struct ip_addr *addr, int size)
{ {
int err; int err;
struct icmp_echo_hdr *iecho; struct icmp_echo_hdr *iecho;
struct sockaddr_in to; struct sockaddr_in to;
size_t ping_size = sizeof(struct icmp_echo_hdr) + size; size_t ping_size = sizeof(struct icmp_echo_hdr) + size;
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff); LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
iecho = rt_malloc(ping_size); iecho = rt_malloc(ping_size);
if (iecho == RT_NULL) if (iecho == RT_NULL)
{ {
return ERR_MEM; return ERR_MEM;
} }
ping_prepare_echo(iecho, (u16_t)ping_size); ping_prepare_echo(iecho, (u16_t)ping_size);
to.sin_len = sizeof(to); to.sin_len = sizeof(to);
to.sin_family = AF_INET; to.sin_family = AF_INET;
to.sin_addr.s_addr = addr->addr; to.sin_addr.s_addr = addr->addr;
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to)); err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
rt_free(iecho); rt_free(iecho);
return (err ? ERR_OK : ERR_VAL); return (err ? ERR_OK : ERR_VAL);
} }
static void ping_recv(int s) static void ping_recv(int s)
{ {
char buf[64]; char buf[64];
int fromlen, len; int fromlen, len;
struct sockaddr_in from; struct sockaddr_in from;
struct ip_hdr *iphdr; struct ip_hdr *iphdr;
struct icmp_echo_hdr *iecho; struct icmp_echo_hdr *iecho;
struct _ip_addr *addr; struct _ip_addr *addr;
while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0) while((len = lwip_recvfrom(s, buf, sizeof(buf), 0, (struct sockaddr*)&from, (socklen_t*)&fromlen)) > 0)
{ {
if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr))) if (len >= (sizeof(struct ip_hdr)+sizeof(struct icmp_echo_hdr)))
{ {
addr = (struct _ip_addr *)&(from.sin_addr); addr = (struct _ip_addr *)&(from.sin_addr);
rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3); rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
iphdr = (struct ip_hdr *)buf; iphdr = (struct ip_hdr *)buf;
iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4)); iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4));
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num))) if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
{ {
return; return;
} }
else else
{ {
rt_kprintf("ping: drop\n"); rt_kprintf("ping: drop\n");
} }
} }
} }
if (len <= 0) if (len <= 0)
{ {
rt_kprintf("ping: timeout\n"); rt_kprintf("ping: timeout\n");
} }
} }
rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size) rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size)
{ {
int s; int s;
int timeout = PING_RCV_TIMEO; int timeout = PING_RCV_TIMEO;
struct ip_addr ping_target; struct ip_addr ping_target;
rt_uint32_t send_time; rt_uint32_t send_time;
struct _ip_addr struct _ip_addr
{ {
rt_uint8_t addr0, addr1, addr2, addr3; rt_uint8_t addr0, addr1, addr2, addr3;
} *addr; } *addr;
send_time = 0; send_time = 0;
if(size == 0)
size = PING_DATA_SIZE;
if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR; if(size == 0)
addr = (struct _ip_addr*)&ping_target; size = PING_DATA_SIZE;
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0) if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR;
{ addr = (struct _ip_addr*)&ping_target;
rt_kprintf("create socket failled\n");
return -RT_ERROR;
}
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)); if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
{
rt_kprintf("create socket failled\n");
return -RT_ERROR;
}
while (1) lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
{
if (ping_send(s, &ping_target, size) == ERR_OK)
{
rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
ping_recv(s);
}
else
{
rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
}
send_time ++; while (1)
if (send_time >= time) break; /* send ping times reached, stop */ {
if (ping_send(s, &ping_target, size) == ERR_OK)
{
rt_kprintf("ping: send %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
ping_recv(s);
}
else
{
rt_kprintf("ping: send %d.%d.%d.%d - error\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
}
rt_thread_delay(PING_DELAY); /* take a delay */ send_time ++;
} if (send_time >= time) break; /* send ping times reached, stop */
lwip_close(s);
return RT_EOK; rt_thread_delay(PING_DELAY); /* take a delay */
}
lwip_close(s);
return RT_EOK;
} }
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>