添加udp相关指令
This commit is contained in:
parent
01a5358213
commit
96b9059fa3
|
@ -1,14 +1,17 @@
|
|||
OBJ += xcmd.o xcmd_default_keys.o xcmd_default_cmds.o linux_main.o test.o ex_keys.o ex_cmds.o
|
||||
OBJ += xcmd.o xcmd_default_keys.o xcmd_default_cmds.o linux_main.o test.o ex_keys.o ex_cmds.o socket_cmds.o
|
||||
BIN = xcmder
|
||||
VPATH := ../../src \
|
||||
../../extensions/test \
|
||||
../../extensions/ex_keys \
|
||||
../../extensions/ex_cmds
|
||||
../../extensions/ex_cmds \
|
||||
../../extensions/net_cmds
|
||||
|
||||
INC += -I../../inc \
|
||||
INC += -I./ \
|
||||
-I../../inc \
|
||||
-I../../extensions/test \
|
||||
-I../../extensions/ex_keys \
|
||||
-I../../extensions/ex_cmds
|
||||
-I../../extensions/ex_cmds \
|
||||
-I../../extensions/net_cmds
|
||||
|
||||
OBJ_WITH_BUILD_DIR:=$(addprefix build/,$(OBJ))
|
||||
|
||||
|
|
|
@ -9,13 +9,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <termio.h>
|
||||
#include "xcmd_platform.h"
|
||||
#include "xcmd.h"
|
||||
#include "test.h"
|
||||
#include "ex_keys.h"
|
||||
#include "ex_cmds.h"
|
||||
#include "socket_cmds.h"
|
||||
|
||||
int getch(void)
|
||||
{
|
||||
|
@ -76,6 +75,7 @@ int main(void)
|
|||
user_keys_init();
|
||||
ex_keys_init();
|
||||
ex_cmds_init();
|
||||
socket_cmds_init();
|
||||
while(1)
|
||||
{
|
||||
xcmd_task();
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef XCMD_PLATFORM_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <termio.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#endif
|
|
@ -0,0 +1,100 @@
|
|||
#include "socket_cmds.h"
|
||||
#include "xcmd.h"
|
||||
#include <stdlib.h>
|
||||
#include "xcmd_platform.h"
|
||||
|
||||
|
||||
static int x_inet_send_to(int fd, char* ip, uint16_t port, void* data, uint16_t len)
|
||||
{
|
||||
int ret = 0;
|
||||
struct sockaddr_in addrin;
|
||||
addrin.sin_family = AF_INET;
|
||||
addrin.sin_port = htons(port);
|
||||
ret = inet_aton(ip, &addrin.sin_addr);
|
||||
if(ret >= 0)
|
||||
{
|
||||
ret = sendto(fd, data, len, 0, (struct sockaddr*)&addrin, sizeof(addrin));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void xcmd_udp_client(int argc ,char**argv)
|
||||
{
|
||||
if(argc >= 4)
|
||||
{
|
||||
int udp = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if(udp<0)
|
||||
{
|
||||
xcmd_print("Open socket error!!\r\n");
|
||||
return;
|
||||
}
|
||||
char* ip = argv[1];
|
||||
uint16_t port = atoi(argv[2]);
|
||||
char *data = argv[3];
|
||||
uint16_t len = strlen(data);
|
||||
if(x_inet_send_to(udp, ip, port, data, len) < 0)
|
||||
{
|
||||
xcmd_print("Send msg error\r\n");
|
||||
close(udp);
|
||||
return;
|
||||
}
|
||||
close(udp);
|
||||
}
|
||||
}
|
||||
|
||||
void xcmd_udp_service(int argc, char** argv)
|
||||
{
|
||||
struct sockaddr_in addrin;
|
||||
addrin.sin_family = AF_INET;
|
||||
|
||||
if(argc == 2)
|
||||
{
|
||||
addrin.sin_addr.s_addr = INADDR_ANY;
|
||||
addrin.sin_port = htons(atoi(argv[1]));
|
||||
}
|
||||
else if(argc >= 3)
|
||||
{
|
||||
inet_aton(argv[1], &addrin.sin_addr);
|
||||
addrin.sin_port = htons(atoi(argv[2]));
|
||||
}
|
||||
else
|
||||
{
|
||||
addrin.sin_port = htons(8080);
|
||||
addrin.sin_addr.s_addr = INADDR_ANY;
|
||||
}
|
||||
int udp = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if(udp<0)
|
||||
{
|
||||
xcmd_print("Open socket error!!\r\n");
|
||||
}
|
||||
if(bind(udp, (struct sockaddr*)&addrin, sizeof(addrin)) < 0)
|
||||
{
|
||||
xcmd_print("Bind error\r\n");
|
||||
close(udp);
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in client_addr;
|
||||
char rcv_buf[129];
|
||||
int rcv_len = 0;
|
||||
socklen_t addr_len = sizeof(client_addr);
|
||||
rcv_len = recvfrom(udp, rcv_buf, 128, 0, (struct sockaddr*)&client_addr, &addr_len);
|
||||
if(rcv_len>0)
|
||||
{
|
||||
rcv_buf[rcv_len] = '\0';
|
||||
xcmd_print("rcv from \"ip=%s port=%d\" msg:\r\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
|
||||
xcmd_print(rcv_buf);
|
||||
}
|
||||
close(udp);
|
||||
}
|
||||
|
||||
static xcmd_t cmds[] =
|
||||
{
|
||||
{"udp_client", xcmd_udp_client, "Usage: udp_client ip port msg", NULL},
|
||||
{"udp_service", xcmd_udp_service, "Usage: udp_service [ip] [port:default(8080)]", NULL},
|
||||
};
|
||||
|
||||
void socket_cmds_init(void)
|
||||
{
|
||||
xcmd_cmd_register(cmds, sizeof(cmds)/sizeof(xcmd_t));
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef SOCKET_CMDS_H
|
||||
#define SOCKET_CMDS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void socket_cmds_init(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SOCKET_CMDS_H */
|
Loading…
Reference in New Issue