summaryrefslogtreecommitdiff
path: root/LibC
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-12 15:51:42 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-12 15:51:42 +0100
commita017a7744279a29ab490c0aa7d2a81deb0997ae4 (patch)
treeb33d8d4c51d437b460ffa2ce9213edf40644bccd /LibC
parent8e667747f0ac744e74b1acb5b83a0b9db0b7acde (diff)
downloadserenity-a017a7744279a29ab490c0aa7d2a81deb0997ae4.zip
Kernel+LibC+Userland: Start working on an IPv4 socket backend.
The first userland networking program will be "ping" :^)
Diffstat (limited to 'LibC')
-rw-r--r--LibC/Makefile1
-rw-r--r--LibC/arpa/inet.cpp19
-rw-r--r--LibC/arpa/inet.h22
-rw-r--r--LibC/netinet/ip_icmp.h22
-rw-r--r--LibC/sys/socket.cpp7
-rw-r--r--LibC/sys/socket.h25
6 files changed, 94 insertions, 2 deletions
diff --git a/LibC/Makefile b/LibC/Makefile
index c11873f17d..ba3a69695b 100644
--- a/LibC/Makefile
+++ b/LibC/Makefile
@@ -39,6 +39,7 @@ LIBC_OBJS = \
sys/wait.o \
poll.o \
locale.o \
+ arpa/inet.o \
crt0.o
ASM_OBJS = setjmp.no
diff --git a/LibC/arpa/inet.cpp b/LibC/arpa/inet.cpp
new file mode 100644
index 0000000000..f4806609e9
--- /dev/null
+++ b/LibC/arpa/inet.cpp
@@ -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;
+}
+
+}
+
diff --git a/LibC/arpa/inet.h b/LibC/arpa/inet.h
new file mode 100644
index 0000000000..8e913233bd
--- /dev/null
+++ b/LibC/arpa/inet.h
@@ -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
+
diff --git a/LibC/netinet/ip_icmp.h b/LibC/netinet/ip_icmp.h
new file mode 100644
index 0000000000..7f18bea110
--- /dev/null
+++ b/LibC/netinet/ip_icmp.h
@@ -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
+
diff --git a/LibC/sys/socket.cpp b/LibC/sys/socket.cpp
index af13c8c052..1a85ffff96 100644
--- a/LibC/sys/socket.cpp
+++ b/LibC/sys/socket.cpp
@@ -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);
+}
+
}
diff --git a/LibC/sys/socket.h b/LibC/sys/socket.h
index d5028388f1..59407872e1 100644
--- a/LibC/sys/socket.h
+++ b/LibC/sys/socket.h
@@ -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