[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[tyndur-devel] [PATCH] FTP-Client
From: Paul Lange <matheguru94@xxxxxxxxxxxxxx>
+ ftp FTP-Client (nocht nicht ganz ausgereift)
Signed-off-by: Paul Lange <matheguru94@xxxxxxxxxxxxxx>
---
src/modules/c/ftp/Makefile.all | 7 ++
src/modules/c/ftp/command.c | 66 +++++++++++
src/modules/c/ftp/command.h | 36 ++++++
src/modules/c/ftp/ftp.c | 235 ++++++++++++++++++++++++++++++++++++++++
src/modules/c/ftp/ftp.h | 40 +++++++
src/modules/c/ftp/main.c | 89 +++++++++++++++
6 files changed, 473 insertions(+), 0 deletions(-)
create mode 100755 src/modules/c/ftp/Makefile.all
create mode 100644 src/modules/c/ftp/command.c
create mode 100644 src/modules/c/ftp/command.h
create mode 100644 src/modules/c/ftp/ftp.c
create mode 100644 src/modules/c/ftp/ftp.h
create mode 100644 src/modules/c/ftp/main.c
diff --git a/src/modules/c/ftp/Makefile.all b/src/modules/c/ftp/Makefile.all
new file mode 100755
index 0000000..fb31509
--- /dev/null
+++ b/src/modules/c/ftp/Makefile.all
@@ -0,0 +1,7 @@
+shopt -s extglob
+source $LOST_BUILDMK_ROOT/config.sh
+
+echo "LD $1/apps/ftp"
+$LOST_TOOLS_LD -oftp -Ttext=0x40000000 *.o --start-group $2 --end-group
+
+mv ftp $1/apps/
diff --git a/src/modules/c/ftp/command.c b/src/modules/c/ftp/command.c
new file mode 100644
index 0000000..5bc06fc
--- /dev/null
+++ b/src/modules/c/ftp/command.c
@@ -0,0 +1,66 @@
+/**
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Paul Lange.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the tyndur Project
+ * and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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 <stdio.h>
+#include <types.h>
+#include <ftp.h>
+
+#define LINE_LENGTH 80
+
+extern char *host;
+extern FILE *handle;
+
+void ls(void)
+{
+ FILE *data_handle;
+ char c;
+ char buffer[LINE_LENGTH];
+ int i;
+
+ data_handle = ftp_data_connect();
+ request("LIST\r\n");
+ while (!feof(data_handle)) {
+ i = 0;
+ while (((c = fgetc(data_handle)) != '\n') &&
+ i < (LINE_LENGTH - 1) && !feof(data_handle)) {
+ if (c != EOF) {
+ buffer[i++] = c;
+ }
+ }
+ buffer[i] = '\0';
+ printf("%s\n", buffer);
+ }
+ response();
+}
diff --git a/src/modules/c/ftp/command.h b/src/modules/c/ftp/command.h
new file mode 100644
index 0000000..bf1542c
--- /dev/null
+++ b/src/modules/c/ftp/command.h
@@ -0,0 +1,36 @@
+/**
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Paul Lange.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the tyndur Project
+ * and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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.
+ */
+
+void ls(void);
diff --git a/src/modules/c/ftp/ftp.c b/src/modules/c/ftp/ftp.c
new file mode 100644
index 0000000..77679b0
--- /dev/null
+++ b/src/modules/c/ftp/ftp.c
@@ -0,0 +1,235 @@
+/**
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Paul Lange.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the tyndur Project
+ * and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCZLUDING, 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 <stdio.h>
+#include <stdlib.h>
+#include <types.h>
+#include <ctype.h>
+#include <readline/readline.h>
+
+#define COMMAND_PORT 21
+#define LINE_LENGTH 80
+
+extern char *host;
+extern FILE *handle;
+
+/**
+ * sendet einen Befehl an den Server und gibt den Request aus
+ *
+ * @param *command enthaelt den Befehl für den Server
+ */
+void request(const char *command)
+{
+ char buffer;
+ char response[LINE_LENGTH];
+ bool line_end = TRUE;
+ int i = 0;
+
+ fprintf(handle, "%s\r\n", command);
+ fflush(handle);
+
+ while (line_end) {
+ while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) {
+ if (buffer != EOF) {
+ response[i++] = buffer;
+ }
+ }
+ response[i] = '\0';
+ printf("%s\n", response);
+ line_end = FALSE;
+
+ if (response[3] == '-') {
+ line_end = TRUE;
+ i = 0;
+ }
+ }
+}
+
+
+/**
+ * gibt den Request string auf dem Bildschirm aus
+ */
+void response(void)
+{
+ char buffer;
+ char response[LINE_LENGTH];
+ bool line_end = TRUE;
+ int i = 0;
+
+ while (line_end) {
+ while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) {
+ if (buffer != EOF) {
+ response[i++] = buffer;
+ }
+ }
+ response[i] = '\0';
+ printf("%s\n", response);
+ line_end = FALSE;
+
+ if (response[3] == '-') {
+ line_end = TRUE;
+ i = 0;
+ }
+ }
+}
+
+
+/**
+ * Stellt eine Verbindung zum Server her
+ */
+void ftp_connect(void)
+{
+ char *host_path;
+ char *prompt;
+ char *input;
+ char *c;
+
+ puts("\033[1;37mVerbinde...\033[0m");
+ asprintf(&host_path, "tcpip:/%s:%d", host, COMMAND_PORT);
+ handle = fopen(host_path, "r+");
+ free(host_path);
+
+ if (!handle) {
+ puts("\033[1;37mFehler: Host nicht erreichbar!\033[0m");
+ handle = NULL;
+ return;
+ }
+ printf("\033[1;37mVerbunden mit %s, warte auf Antwort...\033[0m\n", host);
+ response();
+
+ asprintf(&prompt, "\033[1;37mBenutzer (%s): \033[0m", host);
+ input = readline(prompt);
+ free(prompt);
+
+ // sorgt dafür, dass die Leerzeichen entfernt werden
+ for (; isspace(*input); input++);
+ for (c = input; !isspace(*c) && *c; c++);
+ *c = '\0';
+
+ fprintf(handle, "USER %s\r\n", input);
+ fflush(handle);
+ response();
+
+ input = readline("\033[1;37mPasswort: \033[0m");
+ // sorgt dafür, dass die Leerzeichen entfernt werden
+ for (; isspace(*input); input++);
+ for (c = input; !isspace(*c) && *c; c++);
+ *c = '\0';
+
+ fprintf(handle, "PASS %s\r\n", input);
+ fflush(handle);
+ response();
+}
+
+
+/**
+ * Schliest die Verbindung zum Server
+ */
+void ftp_disconnect(void)
+{
+ if (!handle) return;
+ request("QUIT");
+ fclose(handle);
+ puts("\033[1;37mVerbindung getrennt...\033[0m");
+}
+
+
+/**
+ * Ã?ffnet eine passive Datenverbindung zum Server
+ *
+ * @return Handle zum Datenfluss
+ */
+FILE* ftp_data_connect(void)
+{
+ char buffer;
+ char *c, *host_path;
+ char response[LINE_LENGTH], portI[4], portII[4];
+ bool line_end = TRUE;
+ unsigned int port, port_low;
+ int i = 0;
+ FILE *data_handle = NULL;
+
+ fprintf(handle, "%s\r\n", "PASV");
+ fflush(handle);
+
+ while (line_end) {
+ while (((buffer = fgetc(handle)) != '\n') && i < (LINE_LENGTH - 1)) {
+ if (buffer != EOF) {
+ response[i++] = buffer;
+ }
+ }
+ response[i] = '\0';
+ printf("%s\n", response);
+ line_end = FALSE;
+
+ if (response[3] == '-') {
+ line_end = TRUE;
+ i = 0;
+ }
+ }
+ for (c = response; *c != '(' || *c == '\n'; c++);
+
+ i = 0;
+ while (i < 4) {
+ for (; *(c++) != ',' || *c == '\n';);
+ i++;
+ }
+
+ i = 0;
+ while (*c != ',' || *c == '\n') {
+ portI[i] = *c;
+ i++;
+ c++;
+ }
+ portI[i++] = '\0';
+ c++;
+ i = 0;
+ while (*c != ')' || *c == '\n') {
+ portII[i] = *c;
+ i++;
+ c++;
+ }
+ portII[i++] = '\0';
+ port = atoi(portI);
+ port *= 256;
+ port_low = atoi(portII);
+ port += port_low;
+
+ asprintf(&host_path, "tcpip:/%s:%d", host, port);
+ data_handle = fopen(host_path, "r+");
+ free(host_path);
+
+ return data_handle;
+}
diff --git a/src/modules/c/ftp/ftp.h b/src/modules/c/ftp/ftp.h
new file mode 100644
index 0000000..bb7189e
--- /dev/null
+++ b/src/modules/c/ftp/ftp.h
@@ -0,0 +1,40 @@
+/**
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Paul Lange.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the tyndur Project
+ * and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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.
+ */
+
+extern void ftp_connect(void);
+extern FILE* ftp_data_connect(void);
+extern void ftp_disconnect(void);
+extern void request(const char*);
+extern void response(void);
diff --git a/src/modules/c/ftp/main.c b/src/modules/c/ftp/main.c
new file mode 100644
index 0000000..4425534
--- /dev/null
+++ b/src/modules/c/ftp/main.c
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2009 The tyndur Project. All rights reserved.
+ *
+ * This code is derived from software contributed to the tyndur Project
+ * by Paul Lange.
+ *
+ * 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.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the tyndur Project
+ * and its contributors.
+ * 4. Neither the name of the tyndur Project nor the names of its
+ * contributors may be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 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 <stdio.h>
+#include <ctype.h>
+#include <readline/readline.h>
+#include <command.h>
+#include <types.h>
+#include <ftp.h>
+
+char *host;
+FILE *handle;
+
+int main(int argc, char *argv[])
+{
+ bool prgm_end = FALSE;
+ char *input, *buffer;
+
+ if (argc == 2) {
+ host = argv[1];
+ ftp_connect();
+ }
+
+ // Bildet den Ablauf des kompletten Programmes
+ do {
+ input = readline("\033[1;37mFTP>\033[0m");
+ // durchlaeuft den String bis zum ersten Zeichen
+ for (; isspace(*input); input++);
+
+ if (!strncmp(input, "exit", 4)) {
+ prgm_end = TRUE;
+ } else if (!strncmp(input, "close", 5)) {
+ ftp_disconnect();
+ handle = NULL;
+ } else if (!strncmp(input, "quit", 4)) {
+ ftp_disconnect();
+ handle = NULL;
+ prgm_end = TRUE;
+ } else if (!strncmp(input, "help", 4)) {
+ } else if (!strncmp(input, "open", 4)) {
+ // parst den host aus dem string
+ input += 5;
+ for (; isspace(*input); input++);
+ for (buffer = input; !isspace(*buffer) && *buffer; buffer++);
+ *buffer = '\0';
+
+ host = input;
+ ftp_connect();
+ } else if (!strncmp(input, "ls", 2)) {
+ ls();
+ } else {
+ puts("\033[1;37mFehler: Falschen Befehl eingegeben! Geben sie "
+ "help ein um eine Liste aller\nBefehle zu erhalten\033[0m");
+ }
+ } while (!prgm_end);
+ return 0;
+}
--
1.6.0.4