From DoBi-tyndur at gmx.net Sat Jul 2 09:58:12 2011 From: DoBi-tyndur at gmx.net (Dominik Bittner) Date: Sat, 2 Jul 2011 09:58:12 +0200 Subject: [tyndur-devel] [PATCH] [lbuilds] Neues lbuild Sed. Grep geupdatet. Message-ID: <1309593492-15711-1-git-send-email-DoBi-tyndur@gmx.net> + Neues lbuild Sed eingefügt. + Grep auf Version 2.9 geupdatet. Signed-off-by: Dominik Bittner --- lbuilds/4.2.1/files/config.patch | 11 ++++++++ lbuilds/4.2.1/sed-4.2.1.lbuild | 47 +++++++++++++++++++++++++++++++++++++ lbuilds/grep-2.9.lbuild | 42 +++++++++++++++++++++++++++++++++ lbuilds/patches/grep-config.patch | 12 +++++++++ lbuilds/patches/grep-lib.patch | 30 +++++++++++++++++++++++ lbuilds/patches/series | 2 + 6 files changed, 144 insertions(+), 0 deletions(-) create mode 100644 lbuilds/4.2.1/files/config.patch create mode 100644 lbuilds/4.2.1/sed-4.2.1.lbuild create mode 100644 lbuilds/grep-2.9.lbuild create mode 100644 lbuilds/patches/grep-config.patch create mode 100644 lbuilds/patches/grep-lib.patch create mode 100644 lbuilds/patches/series diff --git a/lbuilds/4.2.1/files/config.patch b/lbuilds/4.2.1/files/config.patch new file mode 100644 index 0000000..e21106b --- /dev/null +++ b/lbuilds/4.2.1/files/config.patch @@ -0,0 +1,11 @@ +--- sed-4.2.1.orig/build-aux/config.sub 2009-06-15 11:05:11.000000000 +0200 ++++ sed-4.2.1/build-aux/config.sub 2011-06-27 20:07:26.402073953 +0200 +@@ -1290,7 +1290,7 @@ + | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \ + | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \ + | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \ +- | -skyos* | -haiku* | -rdos* | -toppers* | -drops*) ++ | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -tyndur*) + # Remember, each alternative MUST END IN *, to match a version number. + ;; + -qnx*) diff --git a/lbuilds/4.2.1/sed-4.2.1.lbuild b/lbuilds/4.2.1/sed-4.2.1.lbuild new file mode 100644 index 0000000..0e66a80 --- /dev/null +++ b/lbuilds/4.2.1/sed-4.2.1.lbuild @@ -0,0 +1,47 @@ +include lutils +include toolchain + +DESCRIPTION="" +HOMEPAGE="http://lowlevel.brainsware.org/wiki/index.php/LOST/Pakete/sed" +SRC_URI="http://ftp.gnu.org/gnu/sed/${P}.tar.bz2" + +BUILD_DEPEND="" +RUN_DEPEND="" + +MY_S="${S}/${P}" + +src_unpack() { + lunpack ${P}.tar.bz2 + + cd ${MY_S} || die "${MY_S} existiert nicht" + ln -s ${F} patches + for i in ${F}/*.patch; do + echo `basename $i` >> series + (which quilt > /dev/null 2>&1) || (patch -p1 < $i) || die "Fehler beim Anwenden von $i" + done + (which quilt > /dev/null 2>&1) && quilt push -a +} + +src_compile() { + cd ${MY_S} + + ./configure \ + CC=`tc-cc` \ + --build=`tc-build` \ + --target=`tc-host` \ + --host=`tc-host` \ + --prefix="${R}" \ + --disable-largefile \ + || die "configure failed" + + lmake || die "make failed" + cd .. +} + +src_install() { + cd ${MY_S} + + # Sonst gibts Fehler + mkdir -p "${D}${R}/bin" + lmake -j1 DESTDIR="${D}" install || die "make install failed" +} diff --git a/lbuilds/grep-2.9.lbuild b/lbuilds/grep-2.9.lbuild new file mode 100644 index 0000000..ff386ad --- /dev/null +++ b/lbuilds/grep-2.9.lbuild @@ -0,0 +1,42 @@ +include lutils +include toolchain + +DESCRIPTION="" +HOMEPAGE="http://lowlevel.brainsware.org/wiki/index.php/LOST/Pakete/Grep" +SRC_URI="http://ftp.gnu.org/gnu/grep/grep-2.9.tar.gz" + +LICENSE="GPL" + +RUN_DEPEND="" +BUILD_DEPEND="" + +MY_S="${S}/${P}" + +src_unpack() { + lunpack ${P}.tar.gz + cd ${MY_S} +} + +src_configure() { + cd ${MY_S} + + ./configure \ + CC=`tc-cc` \ + --build=`tc-build` \ + --target=`tc-host` \ + --host=`tc-host` \ + --prefix="${R}" \ + --disable-nls \ + || die "configure failed" +} + +src_compile() { + cd ${MY_S} + lmake +} + +src_install() { + cd ${MY_S} + lmake -j1 DESTDIR="${D}" install || die "make install failed" +} + diff --git a/lbuilds/patches/grep-config.patch b/lbuilds/patches/grep-config.patch new file mode 100644 index 0000000..85ae082 --- /dev/null +++ b/lbuilds/patches/grep-config.patch @@ -0,0 +1,12 @@ +--- src.orig/grep-2.9/build-aux/config.sub 2011-06-18 08:43:14.000000000 +0200 ++++ src/grep-2.9/build-aux/config.sub 2011-07-02 09:35:30.855794175 +0200 +@@ -1342,7 +1342,8 @@ + | -os2* | -vos* | -palmos* | -uclinux* | -nucleus* \ + | -morphos* | -superux* | -rtmk* | -rtmk-nova* | -windiss* \ + | -powermax* | -dnix* | -nx6 | -nx7 | -sei* | -dragonfly* \ +- | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es*) ++ | -skyos* | -haiku* | -rdos* | -toppers* | -drops* | -es* \ ++ | -tyndur*) + # Remember, each alternative MUST END IN *, to match a version number. + ;; + -qnx*) diff --git a/lbuilds/patches/grep-lib.patch b/lbuilds/patches/grep-lib.patch new file mode 100644 index 0000000..fd427bb --- /dev/null +++ b/lbuilds/patches/grep-lib.patch @@ -0,0 +1,30 @@ +--- src.orig/grep-2.9/lib/inttypes.in.h 2011-04-28 10:20:53.000000000 +0200 ++++ src/grep-2.9/lib/inttypes.in.h 2011-07-02 09:39:11.025794074 +0200 +@@ -46,9 +46,9 @@ + /* Get CHAR_BIT. */ + #include + +-#if !(INT_MIN == INT32_MIN && INT_MAX == INT32_MAX) +-# error "This file assumes that 'int' has exactly 32 bits. Please report your platform and compiler to ." +-#endif ++//#if !(INT_MIN == INT32_MIN && INT_MAX == INT32_MAX) ++//# error "This file assumes that 'int' has exactly 32 bits. Please report your platform and compiler to ." ++//#endif + + /* The definition of _GL_ARG_NONNULL is copied here. */ + +--- src.orig/grep-2.9/lib/error.c 2011-06-17 11:31:43.000000000 +0200 ++++ src/grep-2.9/lib/error.c 2011-07-02 09:39:54.925794053 +0200 +@@ -128,9 +128,9 @@ + fcntl, and the gnulib replacement fcntl does not support F_GETFL. */ + return (HANDLE) _get_osfhandle (fd) != INVALID_HANDLE_VALUE; + # else +-# ifndef F_GETFL +-# error Please port fcntl to your platform +-# endif ++//# ifndef F_GETFL ++//# error Please port fcntl to your platform ++//# endif + return 0 <= fcntl (fd, F_GETFL); + # endif + } diff --git a/lbuilds/patches/series b/lbuilds/patches/series new file mode 100644 index 0000000..5940f84 --- /dev/null +++ b/lbuilds/patches/series @@ -0,0 +1,2 @@ +grep-config.patch +grep-lib.patch -- 1.7.4.1 From kevin at tyndur.org Sun Jul 3 13:56:19 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 3 Jul 2011 13:56:19 +0200 Subject: [tyndur-devel] [PATCH] [lbuilds] Neues lbuild Sed. Grep geupdatet. In-Reply-To: <1309593492-15711-1-git-send-email-DoBi-tyndur@gmx.net> References: <1309593492-15711-1-git-send-email-DoBi-tyndur@gmx.net> Message-ID: <20110703115618.GA5589@silvanus> On Sat, Jul 02, 2011 at 09:58:12AM +0200, Dominik Bittner wrote: > + Neues lbuild Sed eingefügt. > + Grep auf Version 2.9 geupdatet. > Signed-off-by: Dominik Bittner Danke, beide Patches sind jetzt drin. From kevin at tyndur.org Sat Jul 16 16:21:07 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 16 Jul 2011 16:21:07 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH=5D_tcpip=3A_UDP-Unterst=C3=BCtz?= =?utf-8?q?ung?= Message-ID: <1310826067-12241-1-git-send-email-kevin@tyndur.org> + tcpip: UDP-Client-Unterstützung. tcpip:/udp/:: öffnet einen UDP-"Socket". Signed-off-by: Kevin Wolf --- src/modules/tcpip/checksum.c | 31 +++- src/modules/tcpip/include/lostio_if.h | 2 + src/modules/tcpip/include/udp.h | 59 ++++++ src/modules/tcpip/ip.c | 2 + src/modules/tcpip/lostio_if.c | 2 + src/modules/tcpip/udp.c | 366 +++++++++++++++++++++++++++++++++ 6 files changed, 461 insertions(+), 1 deletions(-) create mode 100644 src/modules/tcpip/include/udp.h create mode 100644 src/modules/tcpip/udp.c diff --git a/src/modules/tcpip/checksum.c b/src/modules/tcpip/checksum.c index 80db51e..36fbdb7 100644 --- a/src/modules/tcpip/checksum.c +++ b/src/modules/tcpip/checksum.c @@ -44,6 +44,7 @@ #include "ip.h" #include "tcp.h" +#include "udp.h" bool debug_checksum = false; @@ -87,7 +88,6 @@ uint16_t tcp_checksum (struct tcp_header* header, struct tcp_pseudo_header* pseudo_header, void* data, uint32_t data_size) { - //printf("data_size = %d\n", data_size); char buffer[sizeof(struct tcp_header) + sizeof(struct tcp_pseudo_header) + data_size]; @@ -110,3 +110,32 @@ void tcp_update_checksum header->checksum = 0; header->checksum = tcp_checksum(header, pseudo_header, data, data_size); } + +uint16_t udp_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size) +{ + uint16_t sum; + char buffer[sizeof(struct udp_header) + sizeof(struct udp_pseudo_header) + + data_size]; + + memcpy(buffer, pseudo_header, sizeof(struct udp_pseudo_header)); + memcpy(buffer + sizeof(struct udp_pseudo_header), header, + sizeof(struct udp_header)); + memcpy(buffer + sizeof(struct udp_pseudo_header) + sizeof(struct udp_header), + data, data_size); + + sum = ip_checksum(buffer, + sizeof(struct udp_pseudo_header) + sizeof(struct udp_header) + + data_size); + + return sum ? sum : 0xffff; +} + +void udp_update_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size) +{ + header->checksum = 0; + header->checksum = udp_checksum(header, pseudo_header, data, data_size); +} diff --git a/src/modules/tcpip/include/lostio_if.h b/src/modules/tcpip/include/lostio_if.h index 658035b..854de9e 100644 --- a/src/modules/tcpip/include/lostio_if.h +++ b/src/modules/tcpip/include/lostio_if.h @@ -48,6 +48,8 @@ #define LOSTIO_TYPES_SERVER 251 #define LOSTIO_TYPES_SERVER_DIR 250 #define LOSTIO_TYPES_SERVER_CONN 249 +#define LOSTIO_TYPES_UDP_DIR 248 +#define LOSTIO_TYPES_UDP 247 void init_lostio_interface(void); diff --git a/src/modules/tcpip/include/udp.h b/src/modules/tcpip/include/udp.h new file mode 100644 index 0000000..eb7c491 --- /dev/null +++ b/src/modules/tcpip/include/udp.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UDP_H +#define UDP_H + +#include + +struct udp_header { + uint16_t src_port; + uint16_t dest_port; + uint16_t length; + uint16_t checksum; +} __attribute__ ((packed)); + +struct udp_pseudo_header { + uint32_t source_ip; + uint32_t dest_ip; + uint16_t proto; + uint16_t length; +} __attribute__ ((packed)); + + +void udp_register_lostio(void); +void udp_receive(uint32_t source_ip, void* data, uint32_t data_size); + +uint16_t udp_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size); +void udp_update_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size); + +#endif diff --git a/src/modules/tcpip/ip.c b/src/modules/tcpip/ip.c index 49f9e10..d3c9d14 100644 --- a/src/modules/tcpip/ip.c +++ b/src/modules/tcpip/ip.c @@ -47,6 +47,7 @@ #include "arp.h" #include "ethernet.h" #include "tcp.h" +#include "udp.h" //Hier koennen die Debug-Nachrichten aktiviert werden #define DEBUG 0 @@ -278,6 +279,7 @@ void ip_receive(struct device *device, void *data, size_t data_size) case IP_PROTO_UDP: DEBUG_MSG("UDP-Paket eingegangen"); + udp_receive(header->source_ip, packet, packet_size); break; default: diff --git a/src/modules/tcpip/lostio_if.c b/src/modules/tcpip/lostio_if.c index cf7d27c..e37c305 100644 --- a/src/modules/tcpip/lostio_if.c +++ b/src/modules/tcpip/lostio_if.c @@ -47,6 +47,7 @@ #include "network.h" #include "ip.h" #include "tcp.h" +#include "udp.h" #include "lostio_if.h" #include "dns.h" @@ -129,6 +130,7 @@ void init_lostio_interface(void) // Weitere Typen tcp_server_register_lostio(); + udp_register_lostio(); // Dateien/Verzeichnisse anlegen vfstree_create_node("/dns", LOSTIO_TYPES_DNS, 0, NULL, diff --git a/src/modules/tcpip/udp.c b/src/modules/tcpip/udp.c new file mode 100644 index 0000000..8755fef --- /dev/null +++ b/src/modules/tcpip/udp.c @@ -0,0 +1,366 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "udp.h" +#include "ip.h" +#include "dns.h" +#include "lostio_if.h" + +#include +#include +#include +#include +#include +#include + +struct udp_socket { + uint16_t local_port; + uint16_t remote_port; + uint32_t local_ip; + uint32_t remote_ip; + + list_t* incoming_packets; +}; + +struct udp_packet { + size_t length; + char data[]; +}; + +static list_t* udp_sockets; + +/** + * Parst einen gegebenen String, der eine Verbindung im Format + * :: beschreibt. + * + * Rückgabe: + * 0: Erfolg + * -EINVAL: Falsches Format + * -ENOENT: Der DNS-Name konnte nicht aufgelöst werden + */ +static int parse_connection(const char* filename, int* sport, + uint32_t* daddr, int* dport) +{ + int ret, tmp_sport, tmp_dport; + uint32_t tmp_daddr; + char hostname[64]; + + /* Verzeichnisname abschneiden */ + if (strncmp(filename, "/udp/", strlen("/udp/"))) { + return -EINVAL; + } + + filename = filename + strlen("/udp/"); + + /* Wenn der Aufrufer das Ergebnis nicht braucht, lokale Variablen nehmen */ + if (sport == NULL || daddr == NULL || dport == NULL) { + sport = &tmp_sport; + daddr = &tmp_daddr; + dport = &tmp_dport; + } + + /* Dateiname auseinandernehmen */ + ret = sscanf(filename, "%d:%63[^:]:%d", sport, hostname, dport); + if (ret != 3) { + return -EINVAL; + } + + /* Servername auflösen, wenn es keine IP-Adresse war */ + uint32_t ip = string_to_ip(hostname); + if (!ip) { + struct dns_request_result* result = dns_request(hostname); + if (result && (result->ip_count > 0)) { + ip = result->ip_address[0]; + } + } + + if (!ip) { + return -ENOENT; + } + + *daddr = ip; + return 0; +} + +/** + * Legt Knoten im Verzeichnisbaum an, die noch nicht existieren, wenn auf sie + * zugegriffen werden soll + */ +static bool lio_udp_dir_not_found(char** path, uint8_t flags, pid_t pid, + io_resource_t* ps) +{ + int ret; + + ret = parse_connection(*path, NULL, NULL, NULL); + if (ret < 0) { + return false; + } + + return vfstree_create_node(*path, LOSTIO_TYPES_UDP, 0, NULL, + LOSTIO_FLAG_NOAUTOEOF); +} + +/** + * Das Oeffnen einer Datei in tcpip:/tcp-listen registriert einen TCP-Server. + * Wenn ein Client verbindet, wird in der geoeffneten Datei der Dateiname zum + * Oeffnen der Verbindung ausgegeben + */ +static bool lio_udp_pre_open(char** path, uint8_t flags, pid_t pid, + io_resource_t* ps) +{ + int i; + struct udp_socket* s; + vfstree_node_t* node; + int ret, sport, dport; + uint32_t daddr; + struct routing_entry* route; + + /* Knoten raussuchen */ + node = vfstree_get_node_by_path(*path); + if (node == NULL) { + return false; + } + + /* Pfad auseinandernehmen */ + ret = parse_connection(*path, &sport, &daddr, &dport); + if (ret < 0) { + return false; + } + + p(); + route = get_routing_entry(daddr); + if (route == NULL || route->device == NULL) { + v(); + return false; + } + + /* Pruefen, ob der Port nicht schon belegt ist */ + for (i = 0; (s = list_get_element_at(udp_sockets, i)); i++) { + if (s->local_port == sport) { + v(); + return false; + } + } + + /* Neuen Socket anlegen und der Ressource zuordnen */ + s = calloc(1, sizeof(*s)); + s->local_port = sport; + s->local_ip = route->device->ip; + s->remote_port = dport; + s->remote_ip = daddr; + s->incoming_packets = list_create(); + node->data = s; + + list_push(udp_sockets, s); + v(); + + return true; +} + +/** + * Liest ein einzelnes UDP-Paket aus + */ +size_t lio_udp_read(lostio_filehandle_t* fh, void* buf, + size_t blocksize, size_t count) +{ + struct udp_socket* s = fh->node->data; + struct udp_packet* p; + size_t len; + + p = list_pop(s->incoming_packets); + if (p == NULL) { + return 0; + } + + len = p->length; + if (blocksize * count < len) { + len = blocksize * count; + } + + strncpy(buf, p->data, len); + free(p); + + return len; +} + +/** + * Empfängt ein eingehendes UDP-Paket + */ +void udp_receive(uint32_t source_ip, void* data, uint32_t data_size) +{ + struct udp_header* hdr = data; + struct udp_packet* p; + struct udp_socket* s; + void* payload; + size_t payload_size; + size_t pktlen; + int i; + + if (data_size < sizeof(*hdr)) { + return; + } + + p(); + for (i = 0; (s = list_get_element_at(udp_sockets, i)); i++) { + if ( s->remote_ip == source_ip + && s->remote_port == big_endian_word(hdr->src_port) + && s->local_port == big_endian_word(hdr->dest_port)) + { + goto found; + } + } + goto out; + +found: + pktlen = big_endian_word(hdr->length); + if (pktlen > data_size) { + goto out; + } + + payload = hdr + 1; + payload_size = pktlen - sizeof(struct udp_header); + + struct udp_pseudo_header phdr = { + .source_ip = s->remote_ip, + .dest_ip = s->local_ip, + .proto = big_endian_word(IP_PROTO_UDP), + .length = big_endian_word(data_size), + }; + + if (udp_checksum(hdr, &phdr, payload, payload_size) != 0xffff) { + printf("\033[1;31mUDP-Checksumme ungueltig\n\033[0;37m\n\n"); + goto out; + } + + p = malloc(sizeof(*p) + payload_size); + p->length = payload_size; + memcpy(p->data, payload, payload_size); + + list_insert(s->incoming_packets, list_size(s->incoming_packets), p); + +out: + v(); +} + +/** + * Schreibt ein einzelnes UDP-Paket + */ +size_t lio_udp_write(lostio_filehandle_t* fh, size_t blocksize, size_t count, + void* buf) +{ + struct udp_socket* s = fh->node->data; + size_t len = blocksize * count; + + size_t udp_packet_size = len + sizeof(struct udp_header); + uint8_t udp_packet[udp_packet_size]; + + struct udp_header* hdr = (struct udp_header*) udp_packet; + struct udp_pseudo_header phdr; + + phdr = (struct udp_pseudo_header) { + .source_ip = s->local_ip, + .dest_ip = s->remote_ip, + .proto = big_endian_word(IP_PROTO_UDP), + .length = big_endian_word(udp_packet_size), + }; + + *hdr = (struct udp_header) { + .src_port = big_endian_word(s->local_port), + .dest_port = big_endian_word(s->remote_port), + .length = big_endian_word(udp_packet_size), + .checksum = 0, + }; + + memcpy(udp_packet + sizeof(struct udp_header), buf, len); + udp_update_checksum(hdr, &phdr, + udp_packet + sizeof(struct udp_header), len); + + ip_send(s->remote_ip, IP_PROTO_UDP, udp_packet, udp_packet_size); + + return len; +} + +/** + * Beendet eine UDP-Verbindung + */ +int lio_udp_close(lostio_filehandle_t* fh) +{ + struct udp_socket* s = fh->node->data; + struct udp_socket* cur; + struct udp_packet* p; + int i; + + /* Aus der Liste der UDP-Sockets entfernen */ + p(); + for (i = 0; (cur = list_get_element_at(udp_sockets, i)); i++) { + if (s == cur) { + list_remove(udp_sockets, i); + break; + } + } + v(); + + /* VFS-Knoten entfernen */ + fh->node->data = NULL; + while ((p = list_pop(s->incoming_packets))) { + free(p); + } + list_destroy(s->incoming_packets); + free(s); + vfstree_delete_child(fh->node->parent, fh->node->name); + + return 0; +} + +/** Typ fuer einen UDP-Socket */ +static typehandle_t lio_type_udp = { + .id = LOSTIO_TYPES_UDP, + .pre_open = &lio_udp_pre_open, + .read = &lio_udp_read, + .write = &lio_udp_write, + .close = &lio_udp_close, +}; + +/** + * Registriert die LostIO-Typehandles, die fuer UDP benoetigt werden + */ +void udp_register_lostio(void) +{ + udp_sockets = list_create(); + + /* Typ fuer das Verzeichnis udp */ + lostio_type_directory_use_as(LOSTIO_TYPES_UDP_DIR); + get_typehandle(LOSTIO_TYPES_UDP_DIR)->not_found = &lio_udp_dir_not_found; + + /* Typen fuer einen TCP-Server und seine Verbindungen */ + lostio_register_typehandle(&lio_type_udp); + + /* Knoten erstellen */ + vfstree_create_node("/udp", LOSTIO_TYPES_UDP_DIR, 0, NULL, + LOSTIO_FLAG_BROWSABLE); +} -- 1.6.0.2 From kevin at tyndur.org Sun Jul 17 17:33:54 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 17 Jul 2011 17:33:54 +0200 Subject: [tyndur-devel] [PATCH] setup: Auf Netzwerkkartentreiber warten Message-ID: <1310916834-11773-1-git-send-email-kevin@tyndur.org> ! setup: Bevor die IP-Adresse und die Routentabelle geändert werdern können, muss gewartet werden, bis der Treiber wirklich da ist. Ansonsten geht das Schreiben schief und wir kriegen Standardwerte. Signed-off-by: Kevin Wolf --- src/modules/pas/setup/setup_network.pas | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-) diff --git a/src/modules/pas/setup/setup_network.pas b/src/modules/pas/setup/setup_network.pas index 9472bcf..c637ad5 100644 --- a/src/modules/pas/setup/setup_network.pas +++ b/src/modules/pas/setup/setup_network.pas @@ -33,6 +33,10 @@ begin // IP/Gateway setzen path := 'tcpip:/' + driver + '/0/ip'; + while not FileExists(path) do begin + yield; + end; + Assign(f, path); {$i-} Rewrite(f); -- 1.6.0.2 From kevin at tyndur.org Sun Jul 24 21:47:36 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:36 +0200 Subject: [tyndur-devel] [PATCH 0/5] tcp: UDP, ein bisschen DHCP und Fixes Message-ID: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Kevin Wolf (5): tcpip: UDP-Unterstützung tcpip: Unterstützung für Broadcasts tcpip: Include-Guard gefixt tcpip: UDP-Paket über vorgegebene Route verschicken tcpip: Erster Teil der DHCP-Unterstützung src/modules/tcpip/checksum.c | 31 +++- src/modules/tcpip/dhcp.c | 322 ++++++++++++++++++++++++ src/modules/tcpip/include/dhcp.h | 45 ++++ src/modules/tcpip/include/ip.h | 9 +- src/modules/tcpip/include/lostio_if.h | 3 + src/modules/tcpip/include/main.h | 3 + src/modules/tcpip/include/udp.h | 97 ++++++++ src/modules/tcpip/ip.c | 37 ++- src/modules/tcpip/lostio_if.c | 6 + src/modules/tcpip/main.c | 4 +- src/modules/tcpip/udp.c | 435 +++++++++++++++++++++++++++++++++ 11 files changed, 978 insertions(+), 14 deletions(-) create mode 100644 src/modules/tcpip/dhcp.c create mode 100644 src/modules/tcpip/include/dhcp.h create mode 100644 src/modules/tcpip/include/udp.h create mode 100644 src/modules/tcpip/udp.c From kevin at tyndur.org Sun Jul 24 21:47:37 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:37 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_1/5=5D_tcpip=3A_UDP-Unterst?= =?utf-8?q?=C3=BCtzung?= In-Reply-To: <1311536861-12812-1-git-send-email-kevin@tyndur.org> References: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Message-ID: <1311536861-12812-2-git-send-email-kevin@tyndur.org> + tcpip: UDP-Client-Unterstützung. tcpip:/udp/:: öffnet einen UDP-"Socket". Signed-off-by: Kevin Wolf --- src/modules/tcpip/checksum.c | 31 +++- src/modules/tcpip/include/lostio_if.h | 2 + src/modules/tcpip/include/udp.h | 85 +++++++ src/modules/tcpip/ip.c | 2 + src/modules/tcpip/lostio_if.c | 2 + src/modules/tcpip/udp.c | 412 +++++++++++++++++++++++++++++++++ 6 files changed, 533 insertions(+), 1 deletions(-) create mode 100644 src/modules/tcpip/include/udp.h create mode 100644 src/modules/tcpip/udp.c diff --git a/src/modules/tcpip/checksum.c b/src/modules/tcpip/checksum.c index 80db51e..36fbdb7 100644 --- a/src/modules/tcpip/checksum.c +++ b/src/modules/tcpip/checksum.c @@ -44,6 +44,7 @@ #include "ip.h" #include "tcp.h" +#include "udp.h" bool debug_checksum = false; @@ -87,7 +88,6 @@ uint16_t tcp_checksum (struct tcp_header* header, struct tcp_pseudo_header* pseudo_header, void* data, uint32_t data_size) { - //printf("data_size = %d\n", data_size); char buffer[sizeof(struct tcp_header) + sizeof(struct tcp_pseudo_header) + data_size]; @@ -110,3 +110,32 @@ void tcp_update_checksum header->checksum = 0; header->checksum = tcp_checksum(header, pseudo_header, data, data_size); } + +uint16_t udp_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size) +{ + uint16_t sum; + char buffer[sizeof(struct udp_header) + sizeof(struct udp_pseudo_header) + + data_size]; + + memcpy(buffer, pseudo_header, sizeof(struct udp_pseudo_header)); + memcpy(buffer + sizeof(struct udp_pseudo_header), header, + sizeof(struct udp_header)); + memcpy(buffer + sizeof(struct udp_pseudo_header) + sizeof(struct udp_header), + data, data_size); + + sum = ip_checksum(buffer, + sizeof(struct udp_pseudo_header) + sizeof(struct udp_header) + + data_size); + + return sum ? sum : 0xffff; +} + +void udp_update_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size) +{ + header->checksum = 0; + header->checksum = udp_checksum(header, pseudo_header, data, data_size); +} diff --git a/src/modules/tcpip/include/lostio_if.h b/src/modules/tcpip/include/lostio_if.h index 658035b..854de9e 100644 --- a/src/modules/tcpip/include/lostio_if.h +++ b/src/modules/tcpip/include/lostio_if.h @@ -48,6 +48,8 @@ #define LOSTIO_TYPES_SERVER 251 #define LOSTIO_TYPES_SERVER_DIR 250 #define LOSTIO_TYPES_SERVER_CONN 249 +#define LOSTIO_TYPES_UDP_DIR 248 +#define LOSTIO_TYPES_UDP 247 void init_lostio_interface(void); diff --git a/src/modules/tcpip/include/udp.h b/src/modules/tcpip/include/udp.h new file mode 100644 index 0000000..f51d163 --- /dev/null +++ b/src/modules/tcpip/include/udp.h @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef UDP_H +#define UDP_H + +#include +#include + +struct udp_header { + uint16_t src_port; + uint16_t dest_port; + uint16_t length; + uint16_t checksum; +} __attribute__ ((packed)); + +struct udp_pseudo_header { + uint32_t source_ip; + uint32_t dest_ip; + uint16_t proto; + uint16_t length; +} __attribute__ ((packed)); + +/** + * Öffnet einen UDP-Clientsocket + * + * @param sport Lokale Portnummer + * @param daddr IP-Adresse des Remote-Hosts + * @param dport Portnummer des Remote-Hosts + * + * @return Der geöffnete Socket oder NULL bei Fehlern + */ +struct udp_socket* udp_open(uint16_t sport, uint32_t daddr, uint16_t dport); + +/** + * Liest ein einzelnes UDP-Paket aus + */ +size_t udp_read(struct udp_socket* s, void* buf, size_t len); + +/** + * Schreibt ein einzelnes UDP-Paket + */ +size_t udp_write(struct udp_socket* s, void* buf, size_t len); + +/** + * Beendet eine UDP-Verbindung + */ +void udp_close(struct udp_socket *s); + +void udp_register_lostio(void); +void udp_receive(uint32_t source_ip, void* data, uint32_t data_size); + +uint16_t udp_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size); +void udp_update_checksum + (struct udp_header* header, struct udp_pseudo_header* pseudo_header, + void* data, uint32_t data_size); + +#endif diff --git a/src/modules/tcpip/ip.c b/src/modules/tcpip/ip.c index 49f9e10..d3c9d14 100644 --- a/src/modules/tcpip/ip.c +++ b/src/modules/tcpip/ip.c @@ -47,6 +47,7 @@ #include "arp.h" #include "ethernet.h" #include "tcp.h" +#include "udp.h" //Hier koennen die Debug-Nachrichten aktiviert werden #define DEBUG 0 @@ -278,6 +279,7 @@ void ip_receive(struct device *device, void *data, size_t data_size) case IP_PROTO_UDP: DEBUG_MSG("UDP-Paket eingegangen"); + udp_receive(header->source_ip, packet, packet_size); break; default: diff --git a/src/modules/tcpip/lostio_if.c b/src/modules/tcpip/lostio_if.c index cf7d27c..e37c305 100644 --- a/src/modules/tcpip/lostio_if.c +++ b/src/modules/tcpip/lostio_if.c @@ -47,6 +47,7 @@ #include "network.h" #include "ip.h" #include "tcp.h" +#include "udp.h" #include "lostio_if.h" #include "dns.h" @@ -129,6 +130,7 @@ void init_lostio_interface(void) // Weitere Typen tcp_server_register_lostio(); + udp_register_lostio(); // Dateien/Verzeichnisse anlegen vfstree_create_node("/dns", LOSTIO_TYPES_DNS, 0, NULL, diff --git a/src/modules/tcpip/udp.c b/src/modules/tcpip/udp.c new file mode 100644 index 0000000..9a472d9 --- /dev/null +++ b/src/modules/tcpip/udp.c @@ -0,0 +1,412 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "udp.h" +#include "ip.h" +#include "dns.h" +#include "lostio_if.h" + +#include +#include +#include +#include +#include +#include + +#define MIN(a,b) ((a) < (b) ? (a) : (b)) + +struct udp_socket { + uint16_t local_port; + uint16_t remote_port; + uint32_t local_ip; + uint32_t remote_ip; + + list_t* incoming_packets; +}; + +struct udp_packet { + size_t length; + char data[]; +}; + +static list_t* udp_sockets; + +/** + * Parst einen gegebenen String, der eine Verbindung im Format + * :: beschreibt. + * + * Rückgabe: + * 0: Erfolg + * -EINVAL: Falsches Format + * -ENOENT: Der DNS-Name konnte nicht aufgelöst werden + */ +static int parse_connection(const char* filename, int* sport, + uint32_t* daddr, int* dport) +{ + int ret, tmp_sport, tmp_dport; + uint32_t tmp_daddr; + char hostname[64]; + + /* Verzeichnisname abschneiden */ + if (strncmp(filename, "/udp/", strlen("/udp/"))) { + return -EINVAL; + } + + filename = filename + strlen("/udp/"); + + /* Wenn der Aufrufer das Ergebnis nicht braucht, lokale Variablen nehmen */ + if (sport == NULL || daddr == NULL || dport == NULL) { + sport = &tmp_sport; + daddr = &tmp_daddr; + dport = &tmp_dport; + } + + /* Dateiname auseinandernehmen */ + ret = sscanf(filename, "%d:%63[^:]:%d", sport, hostname, dport); + if (ret != 3) { + return -EINVAL; + } + + /* Servername auflösen, wenn es keine IP-Adresse war */ + uint32_t ip = string_to_ip(hostname); + if (!ip) { + struct dns_request_result* result = dns_request(hostname); + if (result && (result->ip_count > 0)) { + ip = result->ip_address[0]; + } + } + + if (!ip) { + return -ENOENT; + } + + *daddr = ip; + return 0; +} + +/** + * Legt Knoten im Verzeichnisbaum an, die noch nicht existieren, wenn auf sie + * zugegriffen werden soll + */ +static bool lio_udp_dir_not_found(char** path, uint8_t flags, pid_t pid, + io_resource_t* ps) +{ + int ret; + + ret = parse_connection(*path, NULL, NULL, NULL); + if (ret < 0) { + return false; + } + + return vfstree_create_node(*path, LOSTIO_TYPES_UDP, 0, NULL, + LOSTIO_FLAG_NOAUTOEOF); +} + +/** + * Das Oeffnen einer Datei in tcpip:/tcp-listen registriert einen TCP-Server. + * Wenn ein Client verbindet, wird in der geoeffneten Datei der Dateiname zum + * Oeffnen der Verbindung ausgegeben + */ +static bool lio_udp_pre_open(char** path, uint8_t flags, pid_t pid, + io_resource_t* ps) +{ + struct udp_socket* s; + vfstree_node_t* node; + int ret, sport, dport; + uint32_t daddr; + + /* Knoten raussuchen */ + node = vfstree_get_node_by_path(*path); + if (node == NULL) { + return false; + } + + /* Pfad auseinandernehmen */ + ret = parse_connection(*path, &sport, &daddr, &dport); + if (ret < 0) { + return false; + } + + /* UDP-Verbindung oeffnen */ + s = udp_open(sport, daddr, dport); + if (s == NULL) { + return false; + } + + node->data = s; + return true; +} + + +/** + * Öffnet einen UDP-Clientsocket + * + * @param sport Lokale Portnummer + * @param daddr IP-Adresse des Remote-Hosts + * @param dport Portnummer des Remote-Hosts + * + * @return Der geöffnete Socket oder NULL bei Fehlern + */ +struct udp_socket* udp_open(uint16_t sport, uint32_t daddr, uint16_t dport) +{ + struct udp_socket* s; + struct routing_entry* route; + int i; + + p(); + route = get_routing_entry(daddr); + if (route == NULL || route->device == NULL) { + v(); + return false; + } + + /* Pruefen, ob der Port nicht schon belegt ist */ + for (i = 0; (s = list_get_element_at(udp_sockets, i)); i++) { + if (s->local_port == sport) { + v(); + return false; + } + } + + /* Neuen Socket anlegen und der Ressource zuordnen */ + s = calloc(1, sizeof(*s)); + s->local_port = sport; + s->local_ip = route->device->ip; + s->remote_port = dport; + s->remote_ip = daddr; + s->incoming_packets = list_create(); + + list_push(udp_sockets, s); + v(); + + return s; +} + +/** + * LostIO-Wrapper für udp_read + */ +static size_t lio_udp_read(lostio_filehandle_t* fh, void* buf, + size_t blocksize, size_t count) +{ + struct udp_socket* s = fh->node->data; + + return udp_read(s, buf, blocksize * count); +} + +/** + * Liest ein einzelnes UDP-Paket aus + */ +size_t udp_read(struct udp_socket* s, void* buf, size_t len) +{ + struct udp_packet* p; + + p = list_pop(s->incoming_packets); + if (p == NULL) { + return 0; + } + + len = MIN(len, p->length); + memcpy(buf, p->data, len); + free(p); + + return len; +} + +/** + * Empfängt ein eingehendes UDP-Paket + */ +void udp_receive(uint32_t source_ip, void* data, uint32_t data_size) +{ + struct udp_header* hdr = data; + struct udp_packet* p; + struct udp_socket* s; + void* payload; + size_t payload_size; + size_t pktlen; + int i; + + if (data_size < sizeof(*hdr)) { + return; + } + + p(); + for (i = 0; (s = list_get_element_at(udp_sockets, i)); i++) { + if ( s->remote_ip == source_ip + && s->remote_port == big_endian_word(hdr->src_port) + && s->local_port == big_endian_word(hdr->dest_port)) + { + goto found; + } + } + goto out; + +found: + pktlen = big_endian_word(hdr->length); + if (pktlen > data_size) { + goto out; + } + + payload = hdr + 1; + payload_size = pktlen - sizeof(struct udp_header); + + struct udp_pseudo_header phdr = { + .source_ip = s->remote_ip, + .dest_ip = s->local_ip, + .proto = big_endian_word(IP_PROTO_UDP), + .length = big_endian_word(data_size), + }; + + if (udp_checksum(hdr, &phdr, payload, payload_size) != 0xffff) { + printf("\033[1;31mUDP-Checksumme ungueltig\n\033[0;37m\n\n"); + goto out; + } + + p = malloc(sizeof(*p) + payload_size); + p->length = payload_size; + memcpy(p->data, payload, payload_size); + + list_insert(s->incoming_packets, list_size(s->incoming_packets), p); + +out: + v(); +} + +/** + * LostIO-Wrapper für udp_write + */ +static size_t lio_udp_write(lostio_filehandle_t* fh, size_t blocksize, + size_t count, void* buf) +{ + struct udp_socket* s = fh->node->data; + + return udp_write(s, buf, blocksize * count); +} + +/** + * Schreibt ein einzelnes UDP-Paket + */ +size_t udp_write(struct udp_socket* s, void* buf, size_t len) +{ + size_t udp_packet_size = len + sizeof(struct udp_header); + uint8_t udp_packet[udp_packet_size]; + + struct udp_header* hdr = (struct udp_header*) udp_packet; + struct udp_pseudo_header phdr; + + phdr = (struct udp_pseudo_header) { + .source_ip = s->local_ip, + .dest_ip = s->remote_ip, + .proto = big_endian_word(IP_PROTO_UDP), + .length = big_endian_word(udp_packet_size), + }; + + *hdr = (struct udp_header) { + .src_port = big_endian_word(s->local_port), + .dest_port = big_endian_word(s->remote_port), + .length = big_endian_word(udp_packet_size), + .checksum = 0, + }; + + memcpy(udp_packet + sizeof(struct udp_header), buf, len); + udp_update_checksum(hdr, &phdr, + udp_packet + sizeof(struct udp_header), len); + + ip_send(s->remote_ip, IP_PROTO_UDP, udp_packet, udp_packet_size); + + return len; +} + +/** + * LostIO-Wrapper für udp_close + */ +static int lio_udp_close(lostio_filehandle_t* fh) +{ + struct udp_socket* s = fh->node->data; + + + udp_close(s); + fh->node->data = NULL; + vfstree_delete_child(fh->node->parent, fh->node->name); + + return 0; +} + +/** + * Beendet eine UDP-Verbindung + */ +void udp_close(struct udp_socket *s) +{ + struct udp_socket* cur; + struct udp_packet* p; + int i; + + /* Aus der Liste der UDP-Sockets entfernen */ + p(); + for (i = 0; (cur = list_get_element_at(udp_sockets, i)); i++) { + if (s == cur) { + list_remove(udp_sockets, i); + break; + } + } + v(); + + /* Übrige Pakete wegwerfen */ + while ((p = list_pop(s->incoming_packets))) { + free(p); + } + list_destroy(s->incoming_packets); + free(s); +} + +/** Typ fuer einen UDP-Socket */ +static typehandle_t lio_type_udp = { + .id = LOSTIO_TYPES_UDP, + .pre_open = &lio_udp_pre_open, + .read = &lio_udp_read, + .write = &lio_udp_write, + .close = &lio_udp_close, +}; + +/** + * Registriert die LostIO-Typehandles, die fuer UDP benoetigt werden + */ +void udp_register_lostio(void) +{ + udp_sockets = list_create(); + + /* Typ fuer das Verzeichnis udp */ + lostio_type_directory_use_as(LOSTIO_TYPES_UDP_DIR); + get_typehandle(LOSTIO_TYPES_UDP_DIR)->not_found = &lio_udp_dir_not_found; + + /* Typen fuer einen TCP-Server und seine Verbindungen */ + lostio_register_typehandle(&lio_type_udp); + + /* Knoten erstellen */ + vfstree_create_node("/udp", LOSTIO_TYPES_UDP_DIR, 0, NULL, + LOSTIO_FLAG_BROWSABLE); +} -- 1.6.0.2 From kevin at tyndur.org Sun Jul 24 21:47:38 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:38 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_2/5=5D_tcpip=3A_Unterst=C3=BCtz?= =?utf-8?q?ung_f=C3=BCr_Broadcasts?= In-Reply-To: <1311536861-12812-1-git-send-email-kevin@tyndur.org> References: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Message-ID: <1311536861-12812-3-git-send-email-kevin@tyndur.org> + tcpip: Bei einem Broadcast sollte man nicht ARP versuchen, sondern einfach die Ethernet-Broadcastadresse benutzen Signed-off-by: Kevin Wolf --- src/modules/tcpip/ip.c | 8 ++++++-- src/modules/tcpip/udp.c | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/modules/tcpip/ip.c b/src/modules/tcpip/ip.c index d3c9d14..705b8e7 100644 --- a/src/modules/tcpip/ip.c +++ b/src/modules/tcpip/ip.c @@ -132,8 +132,12 @@ static bool ip_send_packet_direct(struct device* device, uint32_t dest_ip, char packet[packet_size]; // MAC-Adresse ausfindig machen, an die das Paket gesendet wird - if (!(mac = arp_resolve(device, dest_ip))) { - return false; + if (dest_ip == 0xffffffff) { + mac = 0xffffffffffffULL; + } else { + if (!(mac = arp_resolve(device, dest_ip))) { + return false; + } } // Paket zusammenkopieren in unseren Lokalen Puffer diff --git a/src/modules/tcpip/udp.c b/src/modules/tcpip/udp.c index 9a472d9..49853eb 100644 --- a/src/modules/tcpip/udp.c +++ b/src/modules/tcpip/udp.c @@ -256,7 +256,7 @@ void udp_receive(uint32_t source_ip, void* data, uint32_t data_size) p(); for (i = 0; (s = list_get_element_at(udp_sockets, i)); i++) { - if ( s->remote_ip == source_ip + if ( (s->remote_ip == source_ip || s->remote_ip == 0xffffffff) && s->remote_port == big_endian_word(hdr->src_port) && s->local_port == big_endian_word(hdr->dest_port)) { @@ -275,7 +275,7 @@ found: payload_size = pktlen - sizeof(struct udp_header); struct udp_pseudo_header phdr = { - .source_ip = s->remote_ip, + .source_ip = source_ip, .dest_ip = s->local_ip, .proto = big_endian_word(IP_PROTO_UDP), .length = big_endian_word(data_size), -- 1.6.0.2 From kevin at tyndur.org Sun Jul 24 21:47:39 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:39 +0200 Subject: [tyndur-devel] [PATCH 3/5] tcpip: Include-Guard gefixt In-Reply-To: <1311536861-12812-1-git-send-email-kevin@tyndur.org> References: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Message-ID: <1311536861-12812-4-git-send-email-kevin@tyndur.org> ! tcpip: #ifdef und #define sollten im Idealfall zusammenpassen... Signed-off-by: Kevin Wolf --- src/modules/tcpip/include/ip.h | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modules/tcpip/include/ip.h b/src/modules/tcpip/include/ip.h index 3934689..f7169c2 100644 --- a/src/modules/tcpip/include/ip.h +++ b/src/modules/tcpip/include/ip.h @@ -33,8 +33,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef _IP_H__ -#define _IP_H +#ifndef IP_H +#define IP_H #include #include -- 1.6.0.2 From kevin at tyndur.org Sun Jul 24 21:47:40 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:40 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_4/5=5D_tcpip=3A_UDP-Paket_?= =?utf-8?q?=C3=BCber_vorgegebene_Route_verschicken?= In-Reply-To: <1311536861-12812-1-git-send-email-kevin@tyndur.org> References: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Message-ID: <1311536861-12812-5-git-send-email-kevin@tyndur.org> + tcpip: Möglichkeit, IP- und UDP-Pakete mit einer vorgebenen Route zu verschicken anstatt die Routentabelle zu benutzen. Damit kann DHCP dann einen Broadcast auf die gewünschte Netzwerkkarte machen. Signed-off-by: Kevin Wolf --- src/modules/tcpip/include/ip.h | 5 +++++ src/modules/tcpip/include/udp.h | 12 ++++++++++++ src/modules/tcpip/ip.c | 27 ++++++++++++++++++++------- src/modules/tcpip/udp.c | 25 ++++++++++++++++++++++++- 4 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/modules/tcpip/include/ip.h b/src/modules/tcpip/include/ip.h index f7169c2..73d40b5 100644 --- a/src/modules/tcpip/include/ip.h +++ b/src/modules/tcpip/include/ip.h @@ -81,7 +81,12 @@ struct routing_entry* enum_routing_entry(size_t i); size_t routing_get_entry_count(void); bool ip_send(uint32_t dest_ip, uint8_t proto, void* data, size_t data_size); +bool ip_send_route(uint32_t dest_ip, uint8_t proto, void* data, + size_t data_size, struct routing_entry* route); + bool ip_send_packet(struct ip_header* header, void* data, uint32_t size); +bool ip_send_packet_route(struct ip_header* header, void* data, uint32_t size, + struct routing_entry* route); /** * IP-Paket versenden. Dabei wird das Routing umgangen und direkt die angegebene diff --git a/src/modules/tcpip/include/udp.h b/src/modules/tcpip/include/udp.h index f51d163..c02d17a 100644 --- a/src/modules/tcpip/include/udp.h +++ b/src/modules/tcpip/include/udp.h @@ -29,6 +29,8 @@ #ifndef UDP_H #define UDP_H +#include "ip.h" + #include #include @@ -58,6 +60,16 @@ struct udp_pseudo_header { struct udp_socket* udp_open(uint16_t sport, uint32_t daddr, uint16_t dport); /** + * Öffnet einen UDP-Clientsocket über eine gegebene Route (ignoriert die + * Routentabelle) + * + * @param route Route, die benutzt werden soll + * @see udp_open + */ +struct udp_socket* udp_open_route(uint16_t sport, uint32_t daddr, + uint16_t dport, struct routing_entry* route); + +/** * Liest ein einzelnes UDP-Paket aus */ size_t udp_read(struct udp_socket* s, void* buf, size_t len); diff --git a/src/modules/tcpip/ip.c b/src/modules/tcpip/ip.c index 705b8e7..7fcb253 100644 --- a/src/modules/tcpip/ip.c +++ b/src/modules/tcpip/ip.c @@ -158,15 +158,17 @@ static bool ip_send_packet_direct(struct device* device, uint32_t dest_ip, } -bool ip_send_packet(struct ip_header* header, void* data, uint32_t size) +bool ip_send_packet_route(struct ip_header* header, void* data, uint32_t size, + struct routing_entry* route) { uint32_t dest_ip; - struct routing_entry* route; // Passende Route suchen - route = get_routing_entry(header->dest_ip); - if (!(route = get_routing_entry(header->dest_ip))) { - return false; + if (route == NULL) { + route = get_routing_entry(header->dest_ip); + if (!(route = get_routing_entry(header->dest_ip))) { + return false; + } } // Wenn ein Router eingetragen ist, darueber versenden @@ -175,6 +177,11 @@ bool ip_send_packet(struct ip_header* header, void* data, uint32_t size) return ip_send_packet_direct(route->device, dest_ip, header, data, size); } +bool ip_send_packet(struct ip_header* header, void* data, uint32_t size) +{ + return ip_send_packet_route(header, data, size, NULL); +} + /** * IP-Header befuellen * @@ -199,11 +206,17 @@ static void ip_fill_header(struct ip_header* header, uint32_t dest_ip, header->dest_ip = dest_ip; } -bool ip_send(uint32_t dest_ip, uint8_t proto, void* data, size_t data_size) +bool ip_send_route(uint32_t dest_ip, uint8_t proto, void* data, + size_t data_size, struct routing_entry* route) { struct ip_header ip_header; ip_fill_header(&ip_header, dest_ip, proto, data_size); - return ip_send_packet(&ip_header, data, data_size); + return ip_send_packet_route(&ip_header, data, data_size, route); +} + +bool ip_send(uint32_t dest_ip, uint8_t proto, void* data, size_t data_size) +{ + return ip_send_route(dest_ip, proto, data, data_size, NULL); } /** diff --git a/src/modules/tcpip/udp.c b/src/modules/tcpip/udp.c index 49853eb..b093137 100644 --- a/src/modules/tcpip/udp.c +++ b/src/modules/tcpip/udp.c @@ -47,6 +47,7 @@ struct udp_socket { uint32_t remote_ip; list_t* incoming_packets; + struct routing_entry* route; }; struct udp_packet { @@ -208,6 +209,27 @@ struct udp_socket* udp_open(uint16_t sport, uint32_t daddr, uint16_t dport) } /** + * Öffnet einen UDP-Clientsocket über eine gegebene Route (ignoriert die + * Routentabelle) + * + * @param route Route, die benutzt werden soll + * @see udp_open + */ +struct udp_socket* udp_open_route(uint16_t sport, uint32_t daddr, + uint16_t dport, struct routing_entry* route) +{ + struct udp_socket* s; + + s = udp_open(sport, daddr, dport); + if (s == NULL) { + return NULL; + } + + s->route = route; + return s; +} + +/** * LostIO-Wrapper für udp_read */ static size_t lio_udp_read(lostio_filehandle_t* fh, void* buf, @@ -336,7 +358,8 @@ size_t udp_write(struct udp_socket* s, void* buf, size_t len) udp_update_checksum(hdr, &phdr, udp_packet + sizeof(struct udp_header), len); - ip_send(s->remote_ip, IP_PROTO_UDP, udp_packet, udp_packet_size); + ip_send_route(s->remote_ip, IP_PROTO_UDP, udp_packet, udp_packet_size, + s->route); return len; } -- 1.6.0.2 From kevin at tyndur.org Sun Jul 24 21:47:41 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sun, 24 Jul 2011 21:47:41 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_5/5=5D_tcpip=3A_Erster_Teil_der?= =?utf-8?q?_DHCP-Unterst=C3=BCtzung?= In-Reply-To: <1311536861-12812-1-git-send-email-kevin@tyndur.org> References: <1311536861-12812-1-git-send-email-kevin@tyndur.org> Message-ID: <1311536861-12812-6-git-send-email-kevin@tyndur.org> + tcpip: Der eigentliche DHCP-Prozess kann jetzt durchgeführt werden (solange keine Fehler auftreten jedenfalls...), dazu muss 'fetch' nach tcpip:/$TREIBER/0/dhcp geschrieben werden. Die Karte wird noch nicht entsprechend konfiguriert und zurückgeben kann man ein Lease auch noch nicht. Signed-off-by: Kevin Wolf --- src/modules/tcpip/dhcp.c | 322 +++++++++++++++++++++++++++++++++ src/modules/tcpip/include/dhcp.h | 45 +++++ src/modules/tcpip/include/lostio_if.h | 1 + src/modules/tcpip/include/main.h | 3 + src/modules/tcpip/lostio_if.c | 4 + src/modules/tcpip/main.c | 4 +- 6 files changed, 377 insertions(+), 2 deletions(-) create mode 100644 src/modules/tcpip/dhcp.c create mode 100644 src/modules/tcpip/include/dhcp.h diff --git a/src/modules/tcpip/dhcp.c b/src/modules/tcpip/dhcp.c new file mode 100644 index 0000000..5495ac0 --- /dev/null +++ b/src/modules/tcpip/dhcp.c @@ -0,0 +1,322 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "dhcp.h" +#include "udp.h" +#include "ip.h" +#include "lostio_if.h" + +#include +#include +#include +#include +#include +#include +#include + +struct dhcp_packet { + uint8_t op; + uint8_t htype; + uint8_t hlen; + uint8_t hops; + uint32_t xid; + uint16_t secs; + uint16_t flags; + uint32_t ciaddr; /* Client IP address */ + uint32_t yiaddr; /* 'your' (client) address */ + uint32_t siaddr; + uint32_t giaddr; + uint8_t chaddr[16]; + char sname[64]; + char file[128]; + uint8_t options[312]; +} __attribute__((packed)); + +enum { + BOOTREQUEST = 1, + BOOTREPLY = 2, +}; + +struct dhcp_msgtype { + uint8_t magic[4]; + uint8_t code; + uint8_t len; + uint8_t type; +} __attribute__((packed)); + +struct dhcp_serverid { + uint8_t code; + uint8_t len; + uint32_t addr; +} __attribute__((packed)); + +struct dhcp_end { + uint8_t code; +} __attribute__((packed)); + +struct dhcp_discover_opts { + struct dhcp_msgtype msgtype; + struct dhcp_end end; +} __attribute__((packed)); + +struct dhcp_request_opts { + struct dhcp_msgtype msgtype; + struct dhcp_serverid serverid; + struct dhcp_end end; +} __attribute__((packed)); + +enum { + DHCPDISCOVER = 1, + DHCPOFFER = 2, + DHCPREQUEST = 3, + DHCPDECLINE = 4, + DHCPACK = 5, + DHCPNAK = 6, + DHCPRELEASE = 7, + DHCPINFORM = 8, +}; + +static void dhcp_fill_packet(struct device* device, struct dhcp_packet* p, + struct dhcp_msgtype* m, struct dhcp_end* end, int type) +{ + uint64_t mac = device->dev.mac; + + *p = (struct dhcp_packet) { + .op = BOOTREQUEST, + .htype = 0x01, + .hlen = 6, + .xid = 0, + .chaddr = { mac >> 0, mac >> 8, mac >> 16, + mac >> 24, mac >> 32, mac >> 40 }, + }; + + *m = (struct dhcp_msgtype) { + .magic = { 99, 130, 83, 99 }, + .code = 53, + .len = 1, + .type = type, + }; + + *end = (struct dhcp_end) { + .code = 255, + }; +} + +static int dhcp_discover(struct device* device, struct udp_socket* s) +{ + struct dhcp_discover_opts* opts; + struct dhcp_packet p; + + opts = (void*) &p.options; + dhcp_fill_packet(device, &p, &opts->msgtype, &opts->end, DHCPDISCOVER); + + return udp_write(s, &p, sizeof(p)); +} + +static void dhcp_parse_options(struct device* device, struct dhcp_packet *p) +{ + int i; + + /* Optionen parsen (DHCP-Magic überspringen) */ + i = 4; + + while (i < sizeof(p->options) - 1) { + uint8_t* option = &p->options[i]; + uint8_t op = option[0]; + + if (op == 0xff) { + break; + } else if (op == 0x0) { + i++; + continue; + } + + i += 2 + option[1]; + if (i > sizeof(p->options)) { + break; + } + + switch (op) { + case 1: /* Subnet Mask */ + device->dhcp.subnet = *(uint32_t*) &option[2]; + break; + + case 3: /* Router */ + device->dhcp.gateway = *(uint32_t*) &option[2]; + break; + + case 6: /* Domain Name Server */ + device->dhcp.nameserver = *(uint32_t*) &option[2]; + break; + + case 51: /* IP Address Lease Time */ + /* TODO */ + break; + + case 53: /* Message Type */ + device->dhcp.last_op = option[2]; + break; + + case 54: /* Server Identifier */ + device->dhcp.server_ip = *(uint32_t*) &option[2]; + break; + } + } +} + +static int dhcp_wait_response(struct device* device, struct udp_socket* s, + uint8_t msgtype) +{ + int ret; + uint8_t buf[1024]; + struct dhcp_packet* p = (void*) buf; + +again: + ret = udp_read(s, buf, sizeof(*p)); + + /* TODO Timeout */ + while (ret != sizeof(struct dhcp_packet)) { + yield(); + ret = udp_read(s, buf, sizeof(buf)); + } + + device->dhcp.client_ip = p->yiaddr; + dhcp_parse_options(device, p); + + if (device->dhcp.last_op != msgtype) { + goto again; + } + + return 0; +} + +int dhcp_request(struct device* device, struct udp_socket* s) +{ + struct dhcp_request_opts* opts; + struct dhcp_packet p; + + opts = (void*) &p.options; + dhcp_fill_packet(device, &p, &opts->msgtype, &opts->end, DHCPREQUEST); + + opts->serverid = (struct dhcp_serverid) { + .code = 54, + .len = 4, + .addr = device->dhcp.server_ip, + }; + + return udp_write(s, &p, sizeof(p)); +} + +/** + * Sendet eine DHCP-Anfrage und holt die Informationen vom Server + */ +int dhcp_fetch(struct device* device) +{ + struct udp_socket* s; + struct routing_entry route = { + .target = 0, + .subnet = 0xffffffff, + .gateway = 0, + .device = device, + }; + + device->ip = 0; + memset(&device->dhcp, 0, sizeof(device->dhcp)); + + s = udp_open_route(68, 0xffffffff, 67, &route); + if (s == NULL) { + return -EIO; + } + + dhcp_discover(device, s); + dhcp_wait_response(device, s, DHCPOFFER); + dhcp_request(device, s); + dhcp_wait_response(device, s, DHCPACK); + + udp_close(s); + + printf("ip = %08x\n", device->dhcp.client_ip); + printf("sub = %08x\n", device->dhcp.subnet); + printf("gw = %08x\n", device->dhcp.gateway); + printf("dns = %08x\n", device->dhcp.nameserver); + + return 0; +} + +/** + * TODO Lesen aus der DHCP-Datei + */ +static size_t lio_dhcp_read(lostio_filehandle_t* fh, void* buf, + size_t blocksize, size_t count) +{ + return 0; +} + +/** + * Behandelt Schreibzugriffe in die DHCP-Datei. Mögliche Werte sind "fetch", + * "configure" und "release" + */ +static size_t lio_dhcp_write(lostio_filehandle_t* fh, size_t blocksize, + size_t count, void* buf) +{ + struct device *device = fh->node->data; + size_t len = count * blocksize; + char* p; + + /* Sicherstellen, dass der Puffer nullterminiert ist */ + p = buf; + p[len - 1] = '\0'; + + /* Alles abschneiden, was keine Buchstaben sind */ + while (isalpha(*p)) { + p++; + } + *p = '\0'; + + /* Entsprechende Aktion ausführen */ + if (!strcmp(buf, "fetch")) { + dhcp_fetch(device); + } + + return len; +} + +/** Typ fuer die DHCP-Steuerdatei */ +static typehandle_t lio_type_dhcp = { + .id = LOSTIO_TYPES_DHCP, + .read = &lio_dhcp_read, + .write = &lio_dhcp_write, +}; + +/** + * Registriert die LostIO-Typehandles, die fuer DHCP benoetigt werden + */ +void dhcp_register_lostio(void) +{ + lostio_register_typehandle(&lio_type_dhcp); +} diff --git a/src/modules/tcpip/include/dhcp.h b/src/modules/tcpip/include/dhcp.h new file mode 100644 index 0000000..e320af9 --- /dev/null +++ b/src/modules/tcpip/include/dhcp.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DHCP_H +#define DHCP_H + +#include + +struct dhcp_device_info { + uint8_t last_op; + uint32_t server_ip; + uint32_t client_ip; + uint32_t subnet; + uint32_t gateway; + uint32_t nameserver; +}; + +void dhcp_register_lostio(void); + +#endif diff --git a/src/modules/tcpip/include/lostio_if.h b/src/modules/tcpip/include/lostio_if.h index 854de9e..78f2e03 100644 --- a/src/modules/tcpip/include/lostio_if.h +++ b/src/modules/tcpip/include/lostio_if.h @@ -50,6 +50,7 @@ #define LOSTIO_TYPES_SERVER_CONN 249 #define LOSTIO_TYPES_UDP_DIR 248 #define LOSTIO_TYPES_UDP 247 +#define LOSTIO_TYPES_DHCP 246 void init_lostio_interface(void); diff --git a/src/modules/tcpip/include/main.h b/src/modules/tcpip/include/main.h index 03f11e4..9226038 100644 --- a/src/modules/tcpip/include/main.h +++ b/src/modules/tcpip/include/main.h @@ -36,6 +36,8 @@ #ifndef _MAIN_H_ #define _MAIN_H_ +#include "dhcp.h" + #include #include #include @@ -59,6 +61,7 @@ struct device struct net_device dev; struct driver *driver; uint32_t ip; + struct dhcp_device_info dhcp; }; extern struct module_options options; diff --git a/src/modules/tcpip/lostio_if.c b/src/modules/tcpip/lostio_if.c index e37c305..96c62cd 100644 --- a/src/modules/tcpip/lostio_if.c +++ b/src/modules/tcpip/lostio_if.c @@ -50,6 +50,7 @@ #include "udp.h" #include "lostio_if.h" #include "dns.h" +#include "dhcp.h" //Hier koennen die Debug-Nachrichten aktiviert werden #define DEBUG 0 @@ -131,6 +132,7 @@ void init_lostio_interface(void) // Weitere Typen tcp_server_register_lostio(); udp_register_lostio(); + dhcp_register_lostio(); // Dateien/Verzeichnisse anlegen vfstree_create_node("/dns", LOSTIO_TYPES_DNS, 0, NULL, @@ -163,6 +165,8 @@ void lostio_add_device(struct device *device) vfstree_create_node(fsname, LOSTIO_TYPES_NETCONFIG, 1, device, 0); strcpy(fsname_suffix, "mac"); vfstree_create_node(fsname, LOSTIO_TYPES_NETCONFIG, 2, device, 0); + strcpy(fsname_suffix, "dhcp"); + vfstree_create_node(fsname, LOSTIO_TYPES_DHCP, 0, device, 0); } bool lostio_tcp_not_found(char** path, uint8_t flags, pid_t pid, diff --git a/src/modules/tcpip/main.c b/src/modules/tcpip/main.c index 9581dc9..c0aa5e8 100644 --- a/src/modules/tcpip/main.c +++ b/src/modules/tcpip/main.c @@ -158,8 +158,8 @@ void rpc_register_driver(pid_t pid, uint32_t cid, size_t data_size, void* data) } // Device in die Liste hinzufügen - struct device *device = malloc(sizeof(struct device)); - memcpy(device, data, sizeof(struct net_device)); + struct device *device = calloc(1, sizeof(struct device)); + memcpy(&device->dev, data, sizeof(struct net_device)); device->driver = driver; device->ip = options.ip; list_push(driver->devices, device); -- 1.6.0.2 From kevin at tyndur.org Sat Jul 30 18:52:43 2011 From: kevin at tyndur.org (Kevin Wolf) Date: Sat, 30 Jul 2011 18:52:43 +0200 Subject: [tyndur-devel] =?utf-8?q?=5BPATCH_v2_5/5=5D_tcpip=3A_DHCP-Unterst?= =?utf-8?q?=C3=BCtzung?= In-Reply-To: <1311536861-12812-6-git-send-email-kevin@tyndur.org> References: <1311536861-12812-6-git-send-email-kevin@tyndur.org> Message-ID: <1312044763-18780-1-git-send-email-kevin@tyndur.org> + tcpip: Der eigentliche DHCP-Prozess kann jetzt durchgeführt werden (solange keine Fehler auftreten jedenfalls...), dazu muss 'fetch' nach tcpip:/$TREIBER/0/dhcp geschrieben werden. Wird stattdessen 'configure' geschrieben, wird die Karte mit den Informationen konfiguriert. Zurückgeben kann man ein Lease noch nicht. Signed-off-by: Kevin Wolf --- src/modules/tcpip/dhcp.c | 435 +++++++++++++++++++++++++++++++++ src/modules/tcpip/include/dhcp.h | 47 ++++ src/modules/tcpip/include/ip.h | 1 + src/modules/tcpip/include/lostio_if.h | 1 + src/modules/tcpip/include/main.h | 3 + src/modules/tcpip/ip.c | 15 ++ src/modules/tcpip/lostio_if.c | 4 + src/modules/tcpip/main.c | 4 +- 8 files changed, 508 insertions(+), 2 deletions(-) create mode 100644 src/modules/tcpip/dhcp.c create mode 100644 src/modules/tcpip/include/dhcp.h diff --git a/src/modules/tcpip/dhcp.c b/src/modules/tcpip/dhcp.c new file mode 100644 index 0000000..3cb7aa8 --- /dev/null +++ b/src/modules/tcpip/dhcp.c @@ -0,0 +1,435 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "dhcp.h" +#include "udp.h" +#include "ip.h" +#include "lostio_if.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +struct dhcp_packet { + uint8_t op; + uint8_t htype; + uint8_t hlen; + uint8_t hops; + uint32_t xid; + uint16_t secs; + uint16_t flags; + uint32_t ciaddr; /* Client IP address */ + uint32_t yiaddr; /* 'your' (client) address */ + uint32_t siaddr; + uint32_t giaddr; + uint8_t chaddr[16]; + char sname[64]; + char file[128]; + uint8_t options[312]; +} __attribute__((packed)); + +enum { + BOOTREQUEST = 1, + BOOTREPLY = 2, +}; + +struct dhcp_msgtype { + uint8_t magic[4]; + uint8_t code; + uint8_t len; + uint8_t type; +} __attribute__((packed)); + +struct dhcp_serverid { + uint8_t code; + uint8_t len; + uint32_t addr; +} __attribute__((packed)); + +struct dhcp_requested_ip { + uint8_t code; + uint8_t len; + uint32_t addr; +} __attribute__((packed)); + +struct dhcp_end { + uint8_t code; +} __attribute__((packed)); + +struct dhcp_discover_opts { + struct dhcp_msgtype msgtype; + struct dhcp_end end; +} __attribute__((packed)); + +struct dhcp_request_opts { + struct dhcp_msgtype msgtype; + struct dhcp_serverid serverid; + struct dhcp_requested_ip req_ip; + struct dhcp_end end; +} __attribute__((packed)); + +enum { + DHCPDISCOVER = 1, + DHCPOFFER = 2, + DHCPREQUEST = 3, + DHCPDECLINE = 4, + DHCPACK = 5, + DHCPNAK = 6, + DHCPRELEASE = 7, + DHCPINFORM = 8, +}; + +static void dhcp_fill_packet(struct device* device, struct dhcp_packet* p, + struct dhcp_msgtype* m, struct dhcp_end* end, int type) +{ + uint64_t mac = device->dev.mac; + + *p = (struct dhcp_packet) { + .op = BOOTREQUEST, + .htype = 0x01, + .hlen = 6, + .xid = 0, + .chaddr = { mac >> 0, mac >> 8, mac >> 16, + mac >> 24, mac >> 32, mac >> 40 }, + }; + + *m = (struct dhcp_msgtype) { + .magic = { 99, 130, 83, 99 }, + .code = 53, + .len = 1, + .type = type, + }; + + *end = (struct dhcp_end) { + .code = 255, + }; +} + +static int dhcp_discover(struct device* device, struct udp_socket* s) +{ + struct dhcp_discover_opts* opts; + struct dhcp_packet p; + + opts = (void*) &p.options; + dhcp_fill_packet(device, &p, &opts->msgtype, &opts->end, DHCPDISCOVER); + + return udp_write(s, &p, sizeof(p)); +} + +static void dhcp_parse_options(struct device* device, struct dhcp_packet *p) +{ + int i; + + /* Optionen parsen (DHCP-Magic überspringen) */ + i = 4; + + while (i < sizeof(p->options) - 1) { + uint8_t* option = &p->options[i]; + uint8_t op = option[0]; + + if (op == 0xff) { + break; + } else if (op == 0x0) { + i++; + continue; + } + + i += 2 + option[1]; + if (i > sizeof(p->options)) { + break; + } + + switch (op) { + case 1: /* Subnet Mask */ + device->dhcp.subnet = *(uint32_t*) &option[2]; + break; + + case 3: /* Router */ + device->dhcp.gateway = *(uint32_t*) &option[2]; + break; + + case 6: /* Domain Name Server */ + device->dhcp.nameserver = *(uint32_t*) &option[2]; + break; + + case 51: /* IP Address Lease Time */ + /* TODO */ + break; + + case 53: /* Message Type */ + device->dhcp.last_op = option[2]; + break; + + case 54: /* Server Identifier */ + device->dhcp.server_ip = *(uint32_t*) &option[2]; + break; + } + } +} + +static void do_nothing(void) {} + +static int dhcp_wait_response(struct device* device, struct udp_socket* s, + uint8_t msgtype, uint8_t naktype) +{ + int ret; + uint8_t buf[1024]; + struct dhcp_packet* p = (void*) buf; + +again: + ret = udp_read(s, buf, sizeof(*p)); + + /* Mit Timeout auf die Antwort warten */ + if (ret != sizeof(struct dhcp_packet)) { + uint64_t timeout; + + timeout = get_tick_count() + 4 * 1000000; + timer_register(do_nothing, 5 * 1000000); + + p(); + while ((ret != sizeof(struct dhcp_packet)) + && (get_tick_count() < timeout)) + { + v_and_wait_for_rpc(); + p(); + ret = udp_read(s, buf, sizeof(buf)); + } + v(); + } + + if (ret != sizeof(struct dhcp_packet)) { + return -ETIMEDOUT; + } + + /* Antwort auswerten */ + device->dhcp.client_ip = p->yiaddr; + dhcp_parse_options(device, p); + + if (naktype && device->dhcp.last_op == naktype) { + return -1; + } else if (device->dhcp.last_op != msgtype) { + goto again; + } + + return 0; +} + +static int dhcp_request(struct device* device, struct udp_socket* s) +{ + struct dhcp_request_opts* opts; + struct dhcp_packet p; + + opts = (void*) &p.options; + dhcp_fill_packet(device, &p, &opts->msgtype, &opts->end, DHCPREQUEST); + + opts->serverid = (struct dhcp_serverid) { + .code = 54, + .len = 4, + .addr = device->dhcp.server_ip, + }; + opts->req_ip = (struct dhcp_requested_ip) { + .code = 50, + .len = 4, + .addr = device->dhcp.client_ip, + }; + + return udp_write(s, &p, sizeof(p)); +} + +/** + * Sendet eine DHCP-Anfrage und holt die Informationen vom Server + */ +static int dhcp_fetch(struct device* device) +{ + struct udp_socket* s; + int ret; + + struct routing_entry route = { + .target = 0, + .subnet = 0xffffffff, + .gateway = 0, + .device = device, + }; + + device->ip = 0; + memset(&device->dhcp, 0, sizeof(device->dhcp)); + + s = udp_open_route(68, 0xffffffff, 67, &route); + if (s == NULL) { + return -EIO; + } + +restart: + ret = dhcp_discover(device, s); + if (ret < 0) { + goto out; + } + + ret = dhcp_wait_response(device, s, DHCPOFFER, 0); + if (ret < 0) { + goto out; + } + + dhcp_request(device, s); + if (ret < 0) { + goto out; + } + + dhcp_wait_response(device, s, DHCPACK, DHCPNAK); + if (ret == -1) { + goto restart; + } else if (ret < 0) { + goto out; + } + + printf("ip = %08x\n", device->dhcp.client_ip); + printf("sub = %08x\n", device->dhcp.subnet); + printf("gw = %08x\n", device->dhcp.gateway); + printf("dns = %08x\n", device->dhcp.nameserver); + + device->dhcp.valid = true; + ret = 0; +out: + if (ret != 0) { + printf("DHCP-Anfrage für %s/%d fehlgeschlagen: %s\n", + device->driver->name, device->dev.number, strerror(-ret)); + } + udp_close(s); + + return ret; +} + +/** + * Konfiguriert eine Netzwerkkarte über DHCP + */ +static int dhcp_configure(struct device* device) +{ + int ret; + struct routing_entry* entry; + + /* Falls die DHCP-Daten noch nicht geholt sind, erstmal machen */ + if (!device->dhcp.valid) { + ret = dhcp_fetch(device); + if (ret < 0) { + return ret; + } + } + + /* Netzwerkkarte entsprechend umkonfigurieren */ + device->ip = device->dhcp.client_ip; + + remove_device_routes(device); + + if (device->dhcp.gateway) { + entry = malloc(sizeof(*entry)); + *entry = (struct routing_entry) { + .target = 0, + .subnet = 0, + .gateway = device->dhcp.gateway, + .device = device, + }; + add_routing_entry(entry); + } + + entry = malloc(sizeof(*entry)); + *entry = (struct routing_entry) { + .target = device->dhcp.client_ip & device->dhcp.subnet, + .subnet = device->dhcp.subnet, + .device = device, + }; + add_routing_entry(entry); + + if (!options.nameserver) { + options.nameserver = device->dhcp.nameserver; + } + + return 0; +} + +/** + * TODO Lesen aus der DHCP-Datei + */ +static size_t lio_dhcp_read(lostio_filehandle_t* fh, void* buf, + size_t blocksize, size_t count) +{ + return 0; +} + +/** + * Behandelt Schreibzugriffe in die DHCP-Datei. Mögliche Werte sind "fetch", + * "configure" und "release" + */ +static size_t lio_dhcp_write(lostio_filehandle_t* fh, size_t blocksize, + size_t count, void* buf) +{ + struct device *device = fh->node->data; + size_t len = count * blocksize; + char* p; + int ret; + + /* Sicherstellen, dass der Puffer nullterminiert ist */ + p = buf; + p[len - 1] = '\0'; + + /* Alles abschneiden, was keine Buchstaben sind */ + while (isalpha(*p)) { + p++; + } + *p = '\0'; + + /* Entsprechende Aktion ausführen */ + if (!strcmp(buf, "fetch")) { + ret = dhcp_fetch(device); + } else if (!strcmp(buf, "configure")) { + ret = dhcp_configure(device); + } else { + ret = -EINVAL; + } + + return ret == 0 ? len : 0; +} + +/** Typ fuer die DHCP-Steuerdatei */ +static typehandle_t lio_type_dhcp = { + .id = LOSTIO_TYPES_DHCP, + .read = &lio_dhcp_read, + .write = &lio_dhcp_write, +}; + +/** + * Registriert die LostIO-Typehandles, die fuer DHCP benoetigt werden + */ +void dhcp_register_lostio(void) +{ + lostio_register_typehandle(&lio_type_dhcp); +} diff --git a/src/modules/tcpip/include/dhcp.h b/src/modules/tcpip/include/dhcp.h new file mode 100644 index 0000000..603c490 --- /dev/null +++ b/src/modules/tcpip/include/dhcp.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2011 The tyndur Project. All rights reserved. + * + * This code is derived from software contributed to the tyndur Project + * by Kevin Wolf. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef DHCP_H +#define DHCP_H + +#include +#include + +struct dhcp_device_info { + bool valid; + uint8_t last_op; + uint32_t server_ip; + uint32_t client_ip; + uint32_t subnet; + uint32_t gateway; + uint32_t nameserver; +}; + +void dhcp_register_lostio(void); + +#endif diff --git a/src/modules/tcpip/include/ip.h b/src/modules/tcpip/include/ip.h index 73d40b5..42a5536 100644 --- a/src/modules/tcpip/include/ip.h +++ b/src/modules/tcpip/include/ip.h @@ -79,6 +79,7 @@ void add_routing_entry(struct routing_entry* entry); struct routing_entry* get_routing_entry(uint32_t ip); struct routing_entry* enum_routing_entry(size_t i); size_t routing_get_entry_count(void); +void remove_device_routes(struct device* device); bool ip_send(uint32_t dest_ip, uint8_t proto, void* data, size_t data_size); bool ip_send_route(uint32_t dest_ip, uint8_t proto, void* data, diff --git a/src/modules/tcpip/include/lostio_if.h b/src/modules/tcpip/include/lostio_if.h index 854de9e..78f2e03 100644 --- a/src/modules/tcpip/include/lostio_if.h +++ b/src/modules/tcpip/include/lostio_if.h @@ -50,6 +50,7 @@ #define LOSTIO_TYPES_SERVER_CONN 249 #define LOSTIO_TYPES_UDP_DIR 248 #define LOSTIO_TYPES_UDP 247 +#define LOSTIO_TYPES_DHCP 246 void init_lostio_interface(void); diff --git a/src/modules/tcpip/include/main.h b/src/modules/tcpip/include/main.h index 03f11e4..9226038 100644 --- a/src/modules/tcpip/include/main.h +++ b/src/modules/tcpip/include/main.h @@ -36,6 +36,8 @@ #ifndef _MAIN_H_ #define _MAIN_H_ +#include "dhcp.h" + #include #include #include @@ -59,6 +61,7 @@ struct device struct net_device dev; struct driver *driver; uint32_t ip; + struct dhcp_device_info dhcp; }; extern struct module_options options; diff --git a/src/modules/tcpip/ip.c b/src/modules/tcpip/ip.c index 7fcb253..0a503c8 100644 --- a/src/modules/tcpip/ip.c +++ b/src/modules/tcpip/ip.c @@ -111,6 +111,21 @@ size_t routing_get_entry_count() return list_size(routing_table); } +void remove_device_routes(struct device* device) +{ + int i; + struct routing_entry* entry; + + for (i = 0; (entry = list_get_element_at(routing_table, i)); i++) + { + if (entry->device == device) { + list_remove(routing_table, i); + free(entry); + i--; + } + } +} + /** * IP-Paket versenden und dabei das Routing umgehen und das angegebene Device * benutzen. diff --git a/src/modules/tcpip/lostio_if.c b/src/modules/tcpip/lostio_if.c index e37c305..96c62cd 100644 --- a/src/modules/tcpip/lostio_if.c +++ b/src/modules/tcpip/lostio_if.c @@ -50,6 +50,7 @@ #include "udp.h" #include "lostio_if.h" #include "dns.h" +#include "dhcp.h" //Hier koennen die Debug-Nachrichten aktiviert werden #define DEBUG 0 @@ -131,6 +132,7 @@ void init_lostio_interface(void) // Weitere Typen tcp_server_register_lostio(); udp_register_lostio(); + dhcp_register_lostio(); // Dateien/Verzeichnisse anlegen vfstree_create_node("/dns", LOSTIO_TYPES_DNS, 0, NULL, @@ -163,6 +165,8 @@ void lostio_add_device(struct device *device) vfstree_create_node(fsname, LOSTIO_TYPES_NETCONFIG, 1, device, 0); strcpy(fsname_suffix, "mac"); vfstree_create_node(fsname, LOSTIO_TYPES_NETCONFIG, 2, device, 0); + strcpy(fsname_suffix, "dhcp"); + vfstree_create_node(fsname, LOSTIO_TYPES_DHCP, 0, device, 0); } bool lostio_tcp_not_found(char** path, uint8_t flags, pid_t pid, diff --git a/src/modules/tcpip/main.c b/src/modules/tcpip/main.c index 9581dc9..c0aa5e8 100644 --- a/src/modules/tcpip/main.c +++ b/src/modules/tcpip/main.c @@ -158,8 +158,8 @@ void rpc_register_driver(pid_t pid, uint32_t cid, size_t data_size, void* data) } // Device in die Liste hinzufügen - struct device *device = malloc(sizeof(struct device)); - memcpy(device, data, sizeof(struct net_device)); + struct device *device = calloc(1, sizeof(struct device)); + memcpy(&device->dev, data, sizeof(struct net_device)); device->driver = driver; device->ip = options.ip; list_push(driver->devices, device); -- 1.6.0.2