Implement networking syscalls, ftp and wget apps (AI)
This commit is contained in:
@@ -26,6 +26,11 @@ typedef int int32_t;
|
||||
#define SYS_READDIR 10
|
||||
#define SYS_OPEN 11
|
||||
#define SYS_CLOSE 12
|
||||
#define SYS_SOCKET 13
|
||||
#define SYS_CONNECT 14
|
||||
#define SYS_SEND 15
|
||||
#define SYS_RECV 16
|
||||
#define SYS_SOCKSTATE 17
|
||||
|
||||
static inline int32_t syscall0(int num) {
|
||||
int32_t ret;
|
||||
@@ -124,6 +129,71 @@ static inline int32_t close(int32_t fd) {
|
||||
return syscall1(SYS_CLOSE, (uint32_t)fd);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Networking system calls
|
||||
* ================================================================ */
|
||||
|
||||
/** Socket type constants. */
|
||||
#define SOCK_TCP 0
|
||||
#define SOCK_UDP 1
|
||||
|
||||
/** TCP state constants (match kernel tcp.h). */
|
||||
#define TCP_STATE_CLOSED 0
|
||||
#define TCP_STATE_SYN_SENT 2
|
||||
#define TCP_STATE_ESTABLISHED 4
|
||||
#define TCP_STATE_CLOSE_WAIT 7
|
||||
|
||||
/**
|
||||
* Create a network socket.
|
||||
* @param type SOCK_TCP (0) or SOCK_UDP (1).
|
||||
* @return Socket descriptor (>= 0) or -1 on failure.
|
||||
*/
|
||||
static inline int32_t socket(uint32_t type) {
|
||||
return syscall1(SYS_SOCKET, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a TCP socket to a remote host.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param ip Remote IP address (host byte order).
|
||||
* @param port Remote port (host byte order).
|
||||
* @return 0 on success (SYN sent), -1 on failure.
|
||||
*/
|
||||
static inline int32_t connect(int32_t sockfd, uint32_t ip, uint32_t port) {
|
||||
return syscall3(SYS_CONNECT, (uint32_t)sockfd, ip, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data on a connected socket.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param buf Data buffer.
|
||||
* @param len Data length.
|
||||
* @return Bytes sent, or -1 on failure.
|
||||
*/
|
||||
static inline int32_t net_send(int32_t sockfd, const void *buf, uint32_t len) {
|
||||
return syscall3(SYS_SEND, (uint32_t)sockfd, (uint32_t)buf, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive data from a connected socket (non-blocking).
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param buf Buffer.
|
||||
* @param bufsize Buffer size.
|
||||
* @return Bytes received, 0 if no data, -1 on error/closed.
|
||||
*/
|
||||
static inline int32_t net_recv(int32_t sockfd, void *buf, uint32_t bufsize) {
|
||||
return syscall3(SYS_RECV, (uint32_t)sockfd, (uint32_t)buf, bufsize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the state of a TCP socket.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @return TCP state constant, or -1.
|
||||
*/
|
||||
static inline int32_t sockstate(int32_t sockfd) {
|
||||
return syscall1(SYS_SOCKSTATE, (uint32_t)sockfd);
|
||||
}
|
||||
|
||||
/* Basic string operations for user-space */
|
||||
static inline uint32_t strlen(const char *s) {
|
||||
uint32_t len = 0;
|
||||
|
||||
Reference in New Issue
Block a user