mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-11-08 14:43:04 +08:00
add ping, tftp client, sntp client and chargen network applications
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@137 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
1 parent
e252eb75eb
commit
36dfee4075
4 files changed
+783
No files matched your search
File diff suppressed because it is too large.
Load diff
+195
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* netutils: ping implementation
|
||||
*/
|
||||
|
||||
#include "lwip/opt.h"
|
||||
|
||||
#include "lwip/mem.h"
|
||||
#include "lwip/icmp.h"
|
||||
#include "lwip/netif.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/inet.h"
|
||||
#include "lwip/inet_chksum.h"
|
||||
#include "lwip/ip.h"
|
||||
|
||||
/**
|
||||
* PING_DEBUG: Enable debugging for PING.
|
||||
*/
|
||||
#ifndef PING_DEBUG
|
||||
#define PING_DEBUG LWIP_DBG_ON
|
||||
#endif
|
||||
|
||||
/** ping target - should be a "struct ip_addr" */
|
||||
#ifndef PING_TARGET
|
||||
#define PING_TARGET (netif_default?netif_default->gw:ip_addr_any)
|
||||
#endif
|
||||
|
||||
/** ping receive timeout - in milliseconds */
|
||||
#ifndef PING_RCV_TIMEO
|
||||
#define PING_RCV_TIMEO 1000
|
||||
#endif
|
||||
|
||||
/** ping delay - in milliseconds */
|
||||
#ifndef PING_DELAY
|
||||
#define PING_DELAY 100
|
||||
#endif
|
||||
|
||||
/** ping identifier - must fit on a u16_t */
|
||||
#ifndef PING_ID
|
||||
#define PING_ID 0xAFAF
|
||||
#endif
|
||||
|
||||
/** ping additional data size to include in the packet */
|
||||
#ifndef PING_DATA_SIZE
|
||||
#define PING_DATA_SIZE 32
|
||||
#endif
|
||||
|
||||
/** ping result action - no default action */
|
||||
#ifndef PING_RESULT
|
||||
#define PING_RESULT(ping_ok)
|
||||
#endif
|
||||
|
||||
/* ping variables */
|
||||
static u16_t ping_seq_num;
|
||||
struct _ip_addr
|
||||
{
|
||||
rt_uint8_t addr0, addr1, addr2, addr3;
|
||||
};
|
||||
|
||||
/** Prepare a echo ICMP request */
|
||||
static void ping_prepare_echo( struct icmp_echo_hdr *iecho, u16_t len)
|
||||
{
|
||||
size_t i;
|
||||
size_t data_len = len - sizeof(struct icmp_echo_hdr);
|
||||
|
||||
ICMPH_TYPE_SET(iecho, ICMP_ECHO);
|
||||
ICMPH_CODE_SET(iecho, 0);
|
||||
iecho->chksum = 0;
|
||||
iecho->id = PING_ID;
|
||||
iecho->seqno = htons(++ping_seq_num);
|
||||
|
||||
/* fill the additional data buffer with some data */
|
||||
for(i = 0; i < data_len; i++)
|
||||
{
|
||||
((char*)iecho)[sizeof(struct icmp_echo_hdr) + i] = (char)i;
|
||||
}
|
||||
|
||||
iecho->chksum = inet_chksum(iecho, len);
|
||||
}
|
||||
|
||||
/* Ping using the socket ip */
|
||||
static err_t ping_send(int s, struct ip_addr *addr)
|
||||
{
|
||||
int err;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
struct sockaddr_in to;
|
||||
size_t ping_size = sizeof(struct icmp_echo_hdr) + PING_DATA_SIZE;
|
||||
LWIP_ASSERT("ping_size is too big", ping_size <= 0xffff);
|
||||
|
||||
iecho = rt_malloc(ping_size);
|
||||
if (iecho == RT_NULL)
|
||||
{
|
||||
return ERR_MEM;
|
||||
}
|
||||
|
||||
ping_prepare_echo(iecho, (u16_t)ping_size);
|
||||
|
||||
to.sin_len = sizeof(to);
|
||||
to.sin_family = AF_INET;
|
||||
to.sin_addr.s_addr = addr->addr;
|
||||
|
||||
err = lwip_sendto(s, iecho, ping_size, 0, (struct sockaddr*)&to, sizeof(to));
|
||||
rt_free(iecho);
|
||||
|
||||
return (err ? ERR_OK : ERR_VAL);
|
||||
}
|
||||
|
||||
static void ping_recv(int s)
|
||||
{
|
||||
char buf[64];
|
||||
int fromlen, len;
|
||||
struct sockaddr_in from;
|
||||
struct ip_hdr *iphdr;
|
||||
struct icmp_echo_hdr *iecho;
|
||||
struct _ip_addr *addr;
|
||||
|
||||
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)))
|
||||
{
|
||||
addr = (struct _ip_addr *)&(from.sin_addr);
|
||||
rt_kprintf("ping: recv %d.%d.%d.%d\n", addr->addr0, addr->addr1, addr->addr2, addr->addr3);
|
||||
|
||||
iphdr = (struct ip_hdr *)buf;
|
||||
iecho = (struct icmp_echo_hdr *)(buf+(IPH_HL(iphdr) * 4));
|
||||
if ((iecho->id == PING_ID) && (iecho->seqno == htons(ping_seq_num)))
|
||||
{
|
||||
/* do some ping result processing */
|
||||
PING_RESULT((ICMPH_TYPE(iecho) == ICMP_ER));
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_kprintf("ping: drop\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (len == 0)
|
||||
{
|
||||
LWIP_DEBUGF( PING_DEBUG, ("ping: recv - %lu ms - timeout\n", (sys_now()-ping_time)));
|
||||
}
|
||||
|
||||
/* do some ping result processing */
|
||||
PING_RESULT(0);
|
||||
}
|
||||
|
||||
rt_err_t ping(char* target, rt_uint32_t time, rt_size_t size)
|
||||
{
|
||||
int s;
|
||||
int timeout = PING_RCV_TIMEO;
|
||||
struct ip_addr ping_target;
|
||||
rt_uint32_t send_time;
|
||||
struct _ip_addr
|
||||
{
|
||||
rt_uint8_t addr0, addr1, addr2, addr3;
|
||||
} *addr;
|
||||
|
||||
send_time = 0;
|
||||
|
||||
if (inet_aton(target, (struct in_addr*)&ping_target) == 0) return -RT_ERROR;
|
||||
addr = (struct _ip_addr*)&ping_target;
|
||||
|
||||
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
|
||||
{
|
||||
rt_kprintf("create socket failled\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
lwip_setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (ping_send(s, &ping_target) == 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 ++;
|
||||
if (send_time > time) break; /* send ping times reached, stop */
|
||||
|
||||
rt_thread_delay(PING_DELAY); /* take a delay */
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
#ifdef RT_USING_FINSH
|
||||
#include <finsh.h>
|
||||
FINSH_FUNCTION_EXPORT(ping, ping network host);
|
||||
#endif
|
||||
+213
File diff suppressed because it is too large.
Load diff
+171
@@ -0,0 +1,171 @@
|
||||
#include <dfs_posix.h>
|
||||
#include <lwip/sockets.h>
|
||||
|
||||
#include <finsh.h>
|
||||
|
||||
#define TFTP_PORT 69
|
||||
/* opcode */
|
||||
#define TFTP_RRQ 1 /* read request */
|
||||
#define TFTP_WRQ 2 /* write request */
|
||||
#define TFTP_DATA 3 /* data */
|
||||
#define TFTP_ACK 4 /* ACK */
|
||||
#define TFTP_ERROR 5 /* error */
|
||||
|
||||
rt_uint8_t tftp_buffer[512 + 4];
|
||||
/* tftp client */
|
||||
void tftp_get(const char* host, const char* filename)
|
||||
{
|
||||
int fd, sock_fd;
|
||||
struct sockaddr_in tftp_addr, from_addr;
|
||||
rt_uint32_t length;
|
||||
socklen_t fromlen;
|
||||
|
||||
/* make local file name */
|
||||
getcwd((char*)tftp_buffer, sizeof(tftp_buffer));
|
||||
strcat((char*)tftp_buffer, "/");
|
||||
strcat((char*)tftp_buffer, filename);
|
||||
|
||||
/* open local file for write */
|
||||
fd = open((char*)tftp_buffer, O_RDWR, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("can't open local filename\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* connect to tftp server */
|
||||
inet_aton(host, (struct in_addr*)&(tftp_addr.sin_addr));
|
||||
tftp_addr.sin_family = AF_INET;
|
||||
tftp_addr.sin_port = htons(TFTP_PORT);
|
||||
|
||||
sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
|
||||
if (sock_fd < 0)
|
||||
{
|
||||
close(fd);
|
||||
rt_kprintf("can't create a socket\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
/* make tftp request */
|
||||
tftp_buffer[0] = 0; /* opcode */
|
||||
tftp_buffer[1] = TFTP_RRQ; /* RRQ */
|
||||
length = rt_sprintf((char *)&tftp_buffer[2], "%s", filename) + 2;
|
||||
tftp_buffer[length] = 0; length ++;
|
||||
length += rt_sprintf((char*)&tftp_buffer[length], "%s", "octet");
|
||||
tftp_buffer[length] = 0; length ++;
|
||||
|
||||
fromlen = sizeof(struct sockaddr_in);
|
||||
|
||||
/* send request */
|
||||
lwip_sendto(sock_fd, tftp_buffer, length, 0,
|
||||
(struct sockaddr *)&tftp_addr, fromlen);
|
||||
|
||||
do
|
||||
{
|
||||
length = lwip_recvfrom(sock_fd, tftp_buffer, sizeof(tftp_buffer), 0,
|
||||
(struct sockaddr *)&from_addr, &fromlen);
|
||||
|
||||
if (length > 0)
|
||||
{
|
||||
write(fd, &tftp_buffer[4], length - 4);
|
||||
rt_kprintf("#");
|
||||
|
||||
/* make ACK */
|
||||
tftp_buffer[0] = 0; tftp_buffer[1] = TFTP_ACK; /* opcode */
|
||||
/* send ACK */
|
||||
lwip_sendto(sock_fd, tftp_buffer, 4, 0,
|
||||
(struct sockaddr *)&from_addr, fromlen);
|
||||
}
|
||||
} while (length != 516);
|
||||
|
||||
close(fd);
|
||||
lwip_close(sock_fd);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tftp_get, get file from tftp server);
|
||||
|
||||
void tftp_put(const char* host, const char* filename)
|
||||
{
|
||||
int fd, sock_fd;
|
||||
struct sockaddr_in tftp_addr, from_addr;
|
||||
rt_uint32_t length, block_number = 0;
|
||||
socklen_t fromlen;
|
||||
|
||||
/* make local file name */
|
||||
getcwd((char*)tftp_buffer, sizeof(tftp_buffer));
|
||||
strcat((char*)tftp_buffer, "/");
|
||||
strcat((char*)tftp_buffer, filename);
|
||||
|
||||
/* open local file for write */
|
||||
fd = open((char*)tftp_buffer, O_RDONLY, 0);
|
||||
if (fd < 0)
|
||||
{
|
||||
rt_kprintf("can't open local filename\n");
|
||||
return;
|
||||
}
|
||||
|
||||
/* connect to tftp server */
|
||||
inet_aton(host, (struct in_addr*)&(tftp_addr.sin_addr));
|
||||
tftp_addr.sin_family = AF_INET;
|
||||
tftp_addr.sin_port = htons(TFTP_PORT);
|
||||
tftp_addr.sin_addr.s_addr = htonl(tftp_addr.sin_addr.s_addr);
|
||||
|
||||
sock_fd = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
|
||||
if (sock_fd < 0)
|
||||
{
|
||||
close(fd);
|
||||
rt_kprintf("can't create a socket\n");
|
||||
return ;
|
||||
}
|
||||
|
||||
/* make tftp request */
|
||||
tftp_buffer[0] = 0; /* opcode */
|
||||
tftp_buffer[1] = TFTP_WRQ; /* WRQ */
|
||||
length = rt_sprintf((char *)&tftp_buffer[2], "%s", filename) + 2;
|
||||
tftp_buffer[length] = 0; length ++;
|
||||
length += rt_sprintf((char*)&tftp_buffer[length], "%s", "octet");
|
||||
tftp_buffer[length] = 0; length ++;
|
||||
|
||||
fromlen = sizeof(struct sockaddr_in);
|
||||
|
||||
/* send request */
|
||||
lwip_sendto(sock_fd, tftp_buffer, length, 0,
|
||||
(struct sockaddr *)&tftp_addr, fromlen);
|
||||
|
||||
/* wait ACK 0 */
|
||||
length = lwip_recvfrom(sock_fd, tftp_buffer, sizeof(tftp_buffer), 0,
|
||||
(struct sockaddr *)&from_addr, &fromlen);
|
||||
if (!(tftp_buffer[0] == 0 &&
|
||||
tftp_buffer[1] == TFTP_ACK &&
|
||||
tftp_buffer[2] == 0 &&
|
||||
tftp_buffer[3] == 0))
|
||||
{
|
||||
rt_kprintf("tftp server error\n");
|
||||
close(fd);
|
||||
return;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
length = read(fd, &tftp_buffer[4], 512);
|
||||
if (length > 0)
|
||||
{
|
||||
/* make opcode and block number */
|
||||
tftp_buffer[0] = 0; tftp_buffer[1] = TFTP_DATA;
|
||||
tftp_buffer[2] = block_number ; tftp_buffer[3] = block_number;
|
||||
|
||||
lwip_sendto(sock_fd, tftp_buffer, length + 4, 0,
|
||||
(struct sockaddr *)&from_addr, fromlen);
|
||||
}
|
||||
|
||||
/* receive ack */
|
||||
length = lwip_recvfrom(sock_fd, tftp_buffer, sizeof(tftp_buffer), 0,
|
||||
(struct sockaddr *)&from_addr, &fromlen);
|
||||
if (length > 0)
|
||||
{
|
||||
}
|
||||
} while (length != 516);
|
||||
|
||||
close(fd);
|
||||
lwip_close(sock_fd);
|
||||
}
|
||||
FINSH_FUNCTION_EXPORT(tftp_put, put file to tftp server);
|
||||
Reference in new issue
Block a user