1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 09:27:35 +00:00

Kernel+LibC+Userland: Start working on an IPv4 socket backend.

The first userland networking program will be "ping" :^)
This commit is contained in:
Andreas Kling 2019-03-12 15:51:42 +01:00
parent 8e667747f0
commit a017a77442
23 changed files with 374 additions and 3 deletions

View file

@ -39,6 +39,7 @@ LIBC_OBJS = \
sys/wait.o \
poll.o \
locale.o \
arpa/inet.o \
crt0.o
ASM_OBJS = setjmp.no

19
LibC/arpa/inet.cpp Normal file
View file

@ -0,0 +1,19 @@
#include <arpa/inet.h>
#include <stdio.h>
#include <errno.h>
extern "C" {
const char* inet_ntop(int af, const void* src, char* dst, socklen_t len)
{
if (af != AF_INET) {
errno = EAFNOSUPPORT;
return nullptr;
}
auto* bytes = (const unsigned char*)src;
snprintf(dst, len, "%u.%u.%u.%u", bytes[3], bytes[2], bytes[1], bytes[0]);
return (const char*)dst;
}
}

22
LibC/arpa/inet.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <sys/cdefs.h>
#include <sys/socket.h>
__BEGIN_DECLS
const char* inet_ntop(int af, const void* src, char* dst, socklen_t);
static inline uint16_t htons(uint16_t hs)
{
uint8_t* s = (uint8_t*)&hs;
return (uint16_t)(s[0] << 8 | s[1]);
}
static inline uint16_t ntohs(uint16_t ns)
{
return htons(ns);
}
__END_DECLS

22
LibC/netinet/ip_icmp.h Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <sys/cdefs.h>
#include <stdint.h>
__BEGIN_DECLS
struct icmphdr {
uint8_t type;
uint8_t code;
uint16_t checksum;
union {
struct {
uint16_t id;
uint16_t sequence;
} echo;
uint32_t gateway;
} un;
};
__END_DECLS

View file

@ -34,5 +34,12 @@ int connect(int sockfd, const sockaddr* addr, socklen_t addrlen)
__RETURN_WITH_ERRNO(rc, rc, -1);
}
ssize_t sendto(int sockfd, const void* data, size_t data_length, int flags, const struct sockaddr* addr, socklen_t addr_length)
{
Syscall::SC_sendto_params params { sockfd, data, data_length, flags, addr, addr_length };
int rc = syscall(SC_sendto, &params);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

View file

@ -2,34 +2,55 @@
#include <sys/cdefs.h>
#include <sys/types.h>
#include <stdint.h>
__BEGIN_DECLS
#define AF_MASK 0xff
#define AF_UNSPEC 0
#define AF_LOCAL 1
#define AF_INET 2
#define PF_LOCAL AF_LOCAL
#define PF_INET AF_INET
#define SOCK_TYPE_MASK 0xff
#define SOCK_STREAM 1
#define SOCK_RAW 3
#define SOCK_NONBLOCK 04000
#define SOCK_CLOEXEC 02000000
#define IPPROTO_ICMP 1
#define IPPROTO_TCP 6
#define IPPROTO_UDP 17
struct sockaddr {
unsigned short sa_family;
uint16_t sa_family;
char sa_data[14];
};
#define UNIX_PATH_MAX 108
struct sockaddr_un {
unsigned short sun_family;
uint16_t sun_family;
char sun_path[UNIX_PATH_MAX];
};
struct in_addr {
uint32_t s_addr;
};
struct sockaddr_in {
uint16_t sin_family;
uint16_t sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
int socket(int domain, int type, int protocol);
int bind(int sockfd, const sockaddr* addr, socklen_t);
int listen(int sockfd, int backlog);
int accept(int sockfd, sockaddr*, socklen_t*);
int connect(int sockfd, const sockaddr*, socklen_t);
ssize_t sendto(int sockfd, const void*, size_t, int flags, const struct sockaddr*, socklen_t);
__END_DECLS