int tftpget(FAR const char *remote, FAR const char *local, in_addr_t addr, bool binary)
{
- printf("tftpget is called\n");
-
struct sockaddr_in server; /* The address of the TFTP server */
struct sockaddr_in from; /* The address the last UDP message recv'd from */
FAR uint8_t *packet; /* Allocated memory to hold one packet */
ndatabytes = nbytesrecvd - TFTP_DATAHEADERSIZE;
tftp_dumpbuffer("Recvd DATA", packet + TFTP_DATAHEADERSIZE, ndatabytes);
- if (tftp_write(fd, packet + TFTP_DATAHEADERSIZE, ndatabytes) < 0) {
+
+ ssize_t nSize = tftp_write(fd, packet + TFTP_DATAHEADERSIZE, ndatabytes);
+ if (nSize < 0) {
goto errout_with_sd;
}
+ printf("Received %d bytes\n", nSize);
/* Send the acknowledgment */
CSRCS += netcmd_dhcpd.c
endif
+ifeq ($(CONFIG_NETUTILS_TFTPC),y)
+CSRCS += netcmd_tftpc.c
+endif
+
endif #CONFIG_TASH
ifeq ($(CONFIG_ENABLE_DATE),y)
#ifdef CONFIG_NETUTILS_DHCPD
#include "netcmd_dhcpd.h"
#endif
+#ifdef CONFIG_NETUTILS_TFTPC
+#include "netcmd_tftpc.h"
+#endif
#undef HAVE_PING
#undef HAVE_PING6
extern int netdb_main(int argc, char *argv[]);
#endif
-#if defined(CONFIG_NETUTILS_TFTPC)
-struct tftpc_args_s {
- bool binary; /* true:binary ("octet") false:text ("netascii") */
- bool allocated; /* true: destpath is allocated */
- char *destpath; /* Path at destination */
- const char *srcpath; /* Path at src */
- in_addr_t ipaddr; /* Host IP address */
-};
-#endif
-
-
-
-static void
-nic_display_state(void)
+static void nic_display_state(void)
{
struct ifreq *ifr;
struct sockaddr_in *sin;
close(fd);
}
-
-#if defined(CONFIG_NETUTILS_TFTPC)
-int tftpc_parseargs(int argc, char **argv, struct tftpc_args_s *args)
-{
- FAR const char *fmt = fmtarginvalid;
- bool badarg = false;
- int option;
-
- /* Get the ping options */
-
- memset(args, 0, sizeof(struct tftpc_args_s));
- optind = -1;
- while ((option = getopt(argc, argv, ":bnf:h:")) != ERROR) {
- switch (option) {
- case 'b':
- args->binary = true;
- break;
-
- case 'n':
- args->binary = false;
- break;
-
- case 'f':
- args->destpath = optarg;
- break;
-
- case 'h':
- if (!netlib_ipaddrconv(optarg, (FAR unsigned char *)&args->ipaddr)) {
- printf(fmtarginvalid, argv[0]);
- badarg = true;
- }
- break;
-
- case ':':
- printf(fmtargrequired, argv[0]);
- badarg = true;
- break;
-
- case '?':
- default:
- printf(fmtarginvalid, argv[0]);
- badarg = true;
- break;
- }
- }
-
- /* If a bad argument was encountered, then return without processing the command */
-
- if (badarg) {
- return ERROR;
- }
-
- /* There should be exactly one parameter left on the command-line */
-
- if (optind == argc - 1) {
- args->srcpath = argv[optind];
- }
-
- /* optind == argc means that there is nothing left on the command-line */
-
- else if (optind >= argc) {
- fmt = fmtargrequired;
- goto errout;
- }
-
- /* optind < argc-1 means that there are too many arguments on the
- * command-line
- */
-
- else {
- fmt = fmttoomanyargs;
- goto errout;
- }
-
- /* The HOST IP address is also required */
-
- if (!args->ipaddr) {
- fmt = fmtargrequired;
- goto errout;
- }
-
- /* If the destpath was not provided, then we have do a little work. */
-
- if (!args->destpath) {
- char *tmp1;
- char *tmp2;
-
- /* Copy the srcpath... baseanme might modify it */
-
- fmt = fmtcmdoutofmemory;
- tmp1 = strdup(args->srcpath);
- if (!tmp1) {
- goto errout;
- }
-
- /* Get the basename of the srcpath */
-
- tmp2 = basename(tmp1);
- if (!tmp2) {
- free(tmp1);
- goto errout;
- }
-
- /* Use that basename as the destpath */
-
- args->destpath = strdup(tmp2);
- free(tmp1);
- if (!args->destpath) {
- goto errout;
- }
-
- args->allocated = true;
- }
-
- return OK;
-
-errout:
- printf(fmt, argv[0]);
- return ERROR;
-}
-#endif
-
-#if defined(CONFIG_NETUTILS_TFTPC) && !defined(CONFIG_DISABLE_ENVIRON)
-int cmd_get(int argc, char **argv)
-{
- struct tftpc_args_s args;
- char *fullpath;
-
- int i = 0;
- int fd;
- int ret = -1;
- char seek_rbuffer[101];
-
- /* Parse the input parameter list */
- if (tftpc_parseargs(argc, argv, &args) != OK) {
- return ERROR;
- }
-
- /* Get the full path to the local file */
-
- fullpath = (char *)get_fullpath(args.destpath);
-
- /* Then perform the TFTP get operation */
-
- printf("src: %s full: %s addr: %d bin: %d\n", args.srcpath, fullpath, args.ipaddr, args.binary);
- if (tftpget(args.srcpath, fullpath, args.ipaddr, args.binary) != OK) {
- printf(fmtcmdfailed, argv[0], "tftpget");
- }
-
- fd = open("/mnt/test.txt", O_RDONLY);
- if (fd < 0) {
- printf("Open failed: %d\n", errno);
- }
- printf("Opened\n");
-
- ret = read(fd, seek_rbuffer, 100);
- if (ret < 0) {
- printf("Seek read failed %d\n", ret);
- return ERROR;
- } else {
- printf("Read done\n");
- }
-
- for (i = 0; i < 101; i++) {
- printf("%c", seek_rbuffer[i]);
- }
-
- /* Release any allocated memory */
- if (args.allocated) {
- free(args.destpath);
- }
-
- free(fullpath);
- return OK;
-}
-#endif
-
int cmd_ifup(int argc, char **argv)
{
FAR char *intf = NULL;
return OK;
}
-
-
const static tash_cmdlist_t net_utilcmds[] = {
-#if defined(CONFIG_NETUTILS_TFTPC) && !defined(CONFIG_DISABLE_ENVIRON)
- {"ftp_get", cmd_get, TASH_EXECMD_SYNC},
+#ifdef CONFIG_NETUTILS_DHCPD
+ {"dhcpd", cmd_dhcpd, TASH_EXECMD_SYNC},
#endif
{"ifconfig", cmd_ifconfig, TASH_EXECMD_SYNC},
{"ifdown", cmd_ifdown, TASH_EXECMD_SYNC},
{"lwip_stats", stats_display, TASH_EXECMD_ASYNC},
#endif
{"ping", cmd_ping, TASH_EXECMD_SYNC},
-#ifdef CONFIG_NETUTILS_DHCPD
- {"dhcpd", cmd_dhcpd, TASH_EXECMD_SYNC},
+#ifdef CONFIG_NETUTILS_TFTPC
+ {"tftpc", cmd_tftpc, TASH_EXECMD_SYNC},
#endif
{NULL, NULL, 0}
};
--- /dev/null
+/****************************************************************************
+ *
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ ****************************************************************************/
+
+/*
+ * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
+ * All rights reserved.
+ *
+ * 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. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ *
+ * Author: Adam Dunkels <adam@sics.se>
+ *
+ */
+#include <tinyara/config.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
+#include "netcmd.h"
+#include <apps/netutils/tftp.h>
+
+#if defined(CONFIG_NETUTILS_TFTPC)
+struct tftpc_args_s {
+ bool binary; /* true:binary ("octet") false:text ("netascii") */
+ bool allocated; /* true: destpath is allocated */
+ char *destpath; /* Path at destination */
+ const char *srcpath; /* Path at src */
+ in_addr_t ipaddr; /* Host IP address */
+};
+
+// example : tftpc tftptest.txt /mnt/tftptest.txt 192.168.1.216
+// example : tftpc tftptest.bin /mnt/tftptest.bin 192.168.1.216 b
+
+int cmd_tftpc(int argc, char **argv)
+{
+ if (argc < 4) {
+ printf("Usage:\n");
+ printf(" %s [remote] [local] [addr] [binary]\n", argv[0]);
+ printf("Where\n");
+ printf("*************************************************************************\n");
+ printf(" * Name: tftpc\n");
+ printf(" *\n");
+ printf(" * Input Parameters:\n");
+ printf(" * remote - The name of the file on the TFTP server.\n");
+ printf(" * local - Path to the location on a mounted filesystem\n");
+ printf(" * where the file will be stored.\n");
+ printf(" * addr - The IP address of the server in network order\n");
+ printf(" * binary - b: Perform binary ('octect') transfer\n");
+ printf(" * If there is no input, Perform text ('netascii')\n");
+ printf(" * transfer\n");
+ printf("*************************************************************************\n");
+ return 0;
+ }
+
+ in_addr_t ipaddr;
+ ipaddr = inet_addr(argv[3]);
+
+ bool isBinary = false;
+ if (*argv[4] == 'b' || *argv[4] == 'B') {
+ isBinary = true;
+ }
+
+ if (tftpget(argv[1], argv[2], ipaddr, isBinary) != OK) {
+ printf(fmtcmdfailed, argv[0], "cmd_tftpc", "tftpget");
+ }
+
+ return OK;
+}
+
+#endif
--- /dev/null
+/****************************************************************************
+ *
+ * Copyright 2017 Samsung Electronics All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
+ * either express or implied. See the License for the specific
+ * language governing permissions and limitations under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __APP_SYSTEM_UTILS_NETCMD_TFTPC_H
+#define __APP_SYSTEM_UTILS_NETCMD_TFTPC_H
+
+int cmd_tftpc(int argc, char **argv);
+
+#endif //__APP_SYSTEM_UTILS_NETCMD_TFTPC_H