Update. I should rethink un-merging that from kernel

This commit is contained in:
Mark 2020-03-26 22:20:08 +02:00
parent 63b64ba0ce
commit e060077938
5 changed files with 163 additions and 1 deletions

View File

@ -20,7 +20,10 @@ STAGE_BIN=$(STAGE)/init \
$(STAGE)/bin/ls \
$(STAGE)/bin/rm \
$(STAGE)/bin/reboot \
$(STAGE)/bin/mkdir
$(STAGE)/bin/mkdir \
$(STAGE)/bin/netctl \
$(STAGE)/bin/netmeow \
$(STAGE)/bin/netdump
# $(STAGE)/bin/com \
# $(STAGE)/bin/ase \

44
core/bin/netctl.c Normal file
View File

@ -0,0 +1,44 @@
#include <sys/netctl.h>
#include <errno.h>
#include <stdio.h>
int main(int argc, char **argv) {
if (argc == 2) {
uint32_t inaddr;
if (netctl(argv[1], NETCTL_GET_INADDR, &inaddr) != 0) {
if (errno == ENOENT) {
printf("No address\n");
return 0;
}
perror("netctl()");
return -1;
}
printf("%d.%d.%d.%d\n", inaddr >> 24, (inaddr >> 16) & 0xFF, (inaddr >> 8) & 0xFF, inaddr & 0xFF);
return 0;
} else if (argc == 3) {
uint32_t inaddr_bytes[4];
if (sscanf(argv[2], "%d.%d.%d.%d", &inaddr_bytes[0], &inaddr_bytes[1], &inaddr_bytes[2], &inaddr_bytes[3]) != 4) {
printf("Invalid address format\n");
return -1;
}
uint32_t inaddr = (inaddr_bytes[0] & 0xFF) << 24 |
(inaddr_bytes[1] & 0xFF) << 16 |
(inaddr_bytes[2] & 0xFF) << 8 |
(inaddr_bytes[3] & 0xFF);
if (netctl(argv[1], NETCTL_SET_INADDR, &inaddr) != 0) {
perror("netctl()");
return -1;
}
return 0;
} else {
printf("Usage: netctl <interface> [<address>]\n");
return -1;
}
}

75
core/bin/netdump.c Normal file
View File

@ -0,0 +1,75 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#define LINE_LENGTH 16
static void line_print(size_t off, const char *line, size_t len) {
printf("%08x: ", off);
for (size_t i = 0; i < LINE_LENGTH; ++i) {
// XXX: This is needed because I didn't implement h/hh modifiers in printf
uint64_t byte = (uint64_t) line[i] & 0xFF;
if (i < len) {
printf("%02x", byte);
} else {
printf(" ");
}
if (i % 2) {
printf(" ");
}
}
printf("| ");
for (size_t i = 0; i < len; ++i) {
// TODO: isprint?
if (((uint8_t) line[i]) >= ' ') {
printf("%c", line[i]);
} else {
printf(".");
}
}
printf("\n");
}
void packet_dump(const char *buf, size_t len) {
char line[LINE_LENGTH];
size_t linel = 0;
size_t off = 0;
for (size_t i = 0; i < len; ++i) {
line[linel++] = buf[i];
if (linel == LINE_LENGTH) {
line_print(off, line, linel);
off += LINE_LENGTH;
linel = 0;
}
}
if (linel) {
line_print(off, line, linel);
}
}
int main(int argc, char **argv) {
struct sockaddr sa;
size_t salen;
char buf[4096];
int fd = socket(AF_PACKET, SOCK_RAW, 0);
if (fd < 0) {
perror("socket()");
return -1;
}
while (1) {
ssize_t len = recvfrom(fd, buf, sizeof(buf), &sa, &salen);
if (len < 0) {
perror("recvfrom()");
break;
}
packet_dump(buf, len);
}
return 0;
}

36
core/bin/netmeow.c Normal file
View File

@ -0,0 +1,36 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
int main(int argc, char **argv) {
char buf[4096];
struct sockaddr_in sa;
int fd = socket(AF_INET, SOCK_DGRAM, 0);
size_t salen;
if (fd < 0) {
perror("socket()");
return -1;
}
sa.sin_family = AF_INET;
sa.sin_port = htons(60);
sa.sin_addr = INADDR_ANY;
if (bind(fd, (struct sockaddr *) &sa, sizeof(struct sockaddr_in)) != 0) {
perror("bind()");
return -1;
}
while (1) {
ssize_t d = recvfrom(fd, buf, sizeof(buf), (struct sockaddr *) &sa, &salen);
if (d < 0) {
perror("recvfrom()");
break;
}
write(STDOUT_FILENO, buf, d);
}
return 0;
}

4
init.c
View File

@ -1,3 +1,4 @@
#include <sys/netctl.h>
#include <sys/mount.h>
#include <sys/mman.h>
#include <sys/wait.h>
@ -26,5 +27,8 @@ int main(int argc, char **argv) {
mount(NULL, "/sys", "sysfs", 0, NULL);
mount("/dev/sda1", "/mnt", NULL, 0, NULL);
uint32_t inaddr = 0x0A000001;
netctl("eth0", NETCTL_SET_INADDR, &inaddr);
return start_login();
}