From 3b036b6c5d4235fda9f2774ce691834cbdc3f427 Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Tue, 26 Jul 2016 02:33:40 +0200 Subject: [PATCH] Add getdnsconfig(2) and setdnsconfig(2). --- kernel/Makefile | 1 + kernel/dnsconfig.cpp | 77 ++++++++++++++++++++++++++ kernel/include/sortix/kernel/syscall.h | 3 + kernel/include/sortix/syscall.h | 4 +- kernel/syscall.cpp | 2 + libc/Makefile | 2 + libc/include/sys/dnsconfig.h | 60 ++++++++++++++++++++ libc/sys/dnsconfig/getdnsconfig.c | 28 ++++++++++ libc/sys/dnsconfig/setdnsconfig.c | 28 ++++++++++ 9 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 kernel/dnsconfig.cpp create mode 100644 libc/include/sys/dnsconfig.h create mode 100644 libc/sys/dnsconfig/getdnsconfig.c create mode 100644 libc/sys/dnsconfig/setdnsconfig.c diff --git a/kernel/Makefile b/kernel/Makefile index 4b50d3ff..7a6a61f3 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -91,6 +91,7 @@ disk/ata/ata.o \ disk/ata/hba.o \ disk/ata/port.o \ disk/node.o \ +dnsconfig.o \ dtable.o \ elf.o \ fcache.o \ diff --git a/kernel/dnsconfig.cpp b/kernel/dnsconfig.cpp new file mode 100644 index 00000000..23494776 --- /dev/null +++ b/kernel/dnsconfig.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * dnsconfig.cpp + * System calls for managing the dns configuration of the current system. + */ + +#include + +#include +#include + +#include +#include +#include +#include + +namespace Sortix { + +static kthread_mutex_t dnsconfig_lock = KTHREAD_MUTEX_INITIALIZER; +static struct dnsconfig dnsconfig; + +int sys_getdnsconfig(struct dnsconfig* user_cfg) +{ + ScopedLock lock(&dnsconfig_lock); + if ( !CopyToUser(user_cfg, &dnsconfig, sizeof(dnsconfig)) ) + return -1; + return 0; +} + +int sys_setdnsconfig(const struct dnsconfig* user_cfg) +{ + ScopedLock lock(&dnsconfig_lock); + struct dnsconfig newcfg; + if ( !CopyFromUser(&newcfg, user_cfg, sizeof(dnsconfig)) ) + return -1; + if ( DNSCONFIG_MAX_SERVERS < newcfg.servers_count ) + return errno = EINVAL, -1; + for ( size_t i = 0; i < newcfg.servers_count; i++ ) + { + struct dnsconfig_server* server = &newcfg.servers[i]; + if ( server->family == AF_INET ) + { + if ( server->addrsize != sizeof(struct in_addr) ) + return errno = EINVAL, -1; + memset(&server->addr.in + 1, 0, + sizeof(server->addr) - sizeof(server->addr.in)); + } + else if ( server->family == AF_INET6 ) + { + if ( server->addrsize != sizeof(struct in6_addr) ) + return errno = EINVAL, -1; + memset(&server->addr.in6 + 1, 0, + sizeof(server->addr) - sizeof(server->addr.in6)); + } + else + return errno = EAFNOSUPPORT, -1; + } + for ( size_t i = newcfg.servers_count; i < DNSCONFIG_MAX_SERVERS; i++ ) + memset(&newcfg.servers[i], 0, sizeof(newcfg.servers[i])); + memcpy(&dnsconfig, &newcfg, sizeof(dnsconfig)); + return 0; +} + +} // namespace Sortix diff --git a/kernel/include/sortix/kernel/syscall.h b/kernel/include/sortix/kernel/syscall.h index 5d710f8c..b1ef64d8 100644 --- a/kernel/include/sortix/kernel/syscall.h +++ b/kernel/include/sortix/kernel/syscall.h @@ -20,6 +20,7 @@ #ifndef INCLUDE_SORTIX_KERNEL_SYSCALL_H #define INCLUDE_SORTIX_KERNEL_SYSCALL_H +#include #include #include @@ -89,6 +90,7 @@ int sys_fstatvfsat(int, const char*, struct statvfs*, int); int sys_fsync(int); int sys_ftruncate(int, off_t); int sys_futimens(int, const struct timespec*); +int sys_getdnsconfig(struct dnsconfig*); gid_t sys_getegid(void); int sys_getentropy(void*, size_t); uid_t sys_geteuid(void); @@ -142,6 +144,7 @@ void sys_scram(int, const void*); int sys_sched_yield(void); ssize_t sys_send(int, const void*, size_t, int); ssize_t sys_sendmsg(int, const struct msghdr*, int); +int sys_setdnsconfig(const struct dnsconfig*); int sys_setegid(gid_t); int sys_seteuid(uid_t); int sys_setgid(gid_t); diff --git a/kernel/include/sortix/syscall.h b/kernel/include/sortix/syscall.h index 2cd0b303..73d137e5 100644 --- a/kernel/include/sortix/syscall.h +++ b/kernel/include/sortix/syscall.h @@ -186,6 +186,8 @@ #define SYSCALL_GETSID 163 #define SYSCALL_SETSID 164 #define SYSCALL_SOCKET 165 -#define SYSCALL_MAX_NUM 166 /* index of highest constant + 1 */ +#define SYSCALL_GETDNSCONFIG 166 +#define SYSCALL_SETDNSCONFIG 167 +#define SYSCALL_MAX_NUM 168 /* index of highest constant + 1 */ #endif diff --git a/kernel/syscall.cpp b/kernel/syscall.cpp index 8f7be87d..ebe8497a 100644 --- a/kernel/syscall.cpp +++ b/kernel/syscall.cpp @@ -200,6 +200,8 @@ void* syscall_list[SYSCALL_MAX_NUM + 1] = [SYSCALL_GETSID] = (void*) sys_getsid, [SYSCALL_SETSID] = (void*) sys_setsid, [SYSCALL_SOCKET] = (void*) sys_socket, + [SYSCALL_GETDNSCONFIG] = (void*) sys_getdnsconfig, + [SYSCALL_SETDNSCONFIG] = (void*) sys_setdnsconfig, [SYSCALL_MAX_NUM] = (void*) sys_bad_syscall, }; } /* extern "C" */ diff --git a/libc/Makefile b/libc/Makefile index 1856fe06..8a32503d 100644 --- a/libc/Makefile +++ b/libc/Makefile @@ -555,6 +555,8 @@ stdlib/system.o \ stdlib/unlockpt.o \ stdlib/unsetenv.o \ sys/display/dispmsg_issue.o \ +sys/dnsconfig/getdnsconfig.o \ +sys/dnsconfig/setdnsconfig.o \ sys/ioctl/ioctl.o \ sys/kernelinfo/kernelinfo.o \ syslog/closelog.o \ diff --git a/libc/include/sys/dnsconfig.h b/libc/include/sys/dnsconfig.h new file mode 100644 index 00000000..5af9daab --- /dev/null +++ b/libc/include/sys/dnsconfig.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * sys/dnsconfig.h + * Domain name system configuration. + */ + +#ifndef INCLUDE_SYS_DNSCONFIG_H +#define INCLUDE_SYS_DNSCONFIG_H + +#include + +#include +#include + +union dnsconfig_server_union +{ + struct in_addr in; + struct in6_addr in6; +}; + +struct dnsconfig_server +{ + sa_family_t family; + size_t addrsize; + union dnsconfig_server_union addr; +}; + +#define DNSCONFIG_MAX_SERVERS 3 + +struct dnsconfig +{ + size_t servers_count; + struct dnsconfig_server servers[DNSCONFIG_MAX_SERVERS]; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +int getdnsconfig(struct dnsconfig*); +int setdnsconfig(const struct dnsconfig*); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif diff --git a/libc/sys/dnsconfig/getdnsconfig.c b/libc/sys/dnsconfig/getdnsconfig.c new file mode 100644 index 00000000..5667a1bb --- /dev/null +++ b/libc/sys/dnsconfig/getdnsconfig.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * sys/dnsconfig/getdnsconfig.c + * Get the domain name system configuration. + */ + +#include +#include + +DEFN_SYSCALL1(int, sys_getdnsconfig, SYSCALL_GETDNSCONFIG, struct dnsconfig*); + +int getdnsconfig(struct dnsconfig* cfg) +{ + return sys_getdnsconfig(cfg); +} diff --git a/libc/sys/dnsconfig/setdnsconfig.c b/libc/sys/dnsconfig/setdnsconfig.c new file mode 100644 index 00000000..d39caee2 --- /dev/null +++ b/libc/sys/dnsconfig/setdnsconfig.c @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2016 Jonas 'Sortie' Termansen. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * + * sys/dnsconfig/setdnsconfig.c + * Set the domain name system configuration. + */ + +#include +#include + +DEFN_SYSCALL1(int, sys_setdnsconfig, SYSCALL_SETDNSCONFIG, const struct dnsconfig*); + +int setdnsconfig(const struct dnsconfig* cfg) +{ + return sys_setdnsconfig(cfg); +}