2020-10-21 14:34:10 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 10:26:22 +02:00
|
|
|
* apps/netutils/netcat/netcat_main.c
|
2020-10-21 14:34:10 +02:00
|
|
|
* netcat networking application
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2020-10-21 14:34:10 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-01-04 07:29:05 +01:00
|
|
|
#include <sys/sendfile.h>
|
2020-10-21 14:34:10 +02:00
|
|
|
#include <sys/socket.h>
|
2022-01-04 07:29:05 +01:00
|
|
|
#include <sys/stat.h>
|
2020-10-21 14:34:10 +02:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef NETCAT_PORT
|
|
|
|
# define NETCAT_PORT 31337
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-01-24 16:09:32 +01:00
|
|
|
int do_io(int infd,
|
|
|
|
int outfd,
|
|
|
|
char *buf,
|
|
|
|
size_t buf_size)
|
2021-01-28 01:57:06 +01:00
|
|
|
{
|
2022-01-20 21:24:18 +01:00
|
|
|
ssize_t avail;
|
|
|
|
ssize_t written;
|
2021-01-28 01:57:06 +01:00
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2022-01-24 16:09:32 +01:00
|
|
|
avail = read(infd, buf, buf_size);
|
2021-01-28 01:57:06 +01:00
|
|
|
if (avail == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (avail == -1)
|
|
|
|
{
|
|
|
|
perror("do_io: read error");
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
2022-01-20 21:24:18 +01:00
|
|
|
written = write(outfd, buf, avail);
|
2021-01-28 01:57:06 +01:00
|
|
|
if (written == -1)
|
|
|
|
{
|
|
|
|
perror("do_io: write error");
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-01-18 01:29:03 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
|
|
|
|
int do_io_over_sendfile(int infd, int outfd, ssize_t len)
|
|
|
|
{
|
|
|
|
off_t offset = 0;
|
|
|
|
ssize_t written;
|
|
|
|
|
|
|
|
while (len > 0)
|
|
|
|
{
|
|
|
|
written = sendfile(outfd, infd, &offset, len);
|
|
|
|
|
|
|
|
if (written == -1 && errno == EAGAIN)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (written == -1)
|
|
|
|
{
|
|
|
|
perror("do_io: sendfile error");
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
len -= written;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-10-21 14:34:10 +02:00
|
|
|
int netcat_server(int argc, char * argv[])
|
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
int id = -1;
|
|
|
|
int outfd = STDOUT_FILENO;
|
2020-10-21 14:34:10 +02:00
|
|
|
struct sockaddr_in server;
|
|
|
|
struct sockaddr_in client;
|
|
|
|
int port = NETCAT_PORT;
|
2021-01-28 01:57:06 +01:00
|
|
|
int result = EXIT_SUCCESS;
|
2022-01-20 21:24:18 +01:00
|
|
|
int conn;
|
|
|
|
socklen_t addrlen;
|
2022-01-24 16:09:32 +01:00
|
|
|
char *preallocated_iobuf = NULL;
|
2020-10-21 14:34:10 +02:00
|
|
|
|
|
|
|
if ((1 < argc) && (0 == strcmp("-l", argv[1])))
|
|
|
|
{
|
|
|
|
if (2 < argc)
|
|
|
|
{
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (3 < argc)
|
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
outfd = open(argv[3], O_WRONLY | O_CREAT | O_TRUNC, 0777);
|
|
|
|
if (outfd == -1)
|
2020-10-21 14:34:10 +02:00
|
|
|
{
|
|
|
|
perror("error: io: Failed to create file");
|
2021-01-28 01:57:06 +01:00
|
|
|
outfd = STDOUT_FILENO;
|
|
|
|
result = 1;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-24 16:09:32 +01:00
|
|
|
preallocated_iobuf = (char *)malloc(CONFIG_NETUTILS_NETCAT_BUFSIZE);
|
|
|
|
if (preallocated_iobuf == NULL)
|
|
|
|
{
|
|
|
|
perror("error: malloc: Failed to allocate I/O buffer\n");
|
|
|
|
result = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:34:10 +02:00
|
|
|
id = socket(AF_INET , SOCK_STREAM , 0);
|
|
|
|
if (0 > id)
|
|
|
|
{
|
|
|
|
perror("error: net: Failed to create socket");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 2;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
server.sin_port = htons(port);
|
|
|
|
if (0 > bind(id, (struct sockaddr *)&server , sizeof(server)))
|
|
|
|
{
|
|
|
|
perror("error: net: Failed to bind");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 3;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr, "log: net: listening on :%d\n", port);
|
2021-01-28 01:57:06 +01:00
|
|
|
if (listen(id , 3) == -1)
|
|
|
|
{
|
|
|
|
perror("error: net: Failed to listen");
|
|
|
|
result = 7;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2021-01-28 04:52:05 +01:00
|
|
|
if ((conn = accept(id, (struct sockaddr *)&client, &addrlen)) != -1)
|
2020-10-21 14:34:10 +02:00
|
|
|
{
|
2022-01-24 16:09:32 +01:00
|
|
|
result = do_io(conn, outfd,
|
|
|
|
preallocated_iobuf, CONFIG_NETUTILS_NETCAT_BUFSIZE);
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (0 > conn)
|
|
|
|
{
|
|
|
|
perror("accept failed");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 4;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
out:
|
|
|
|
if (id != -1)
|
2020-10-21 14:34:10 +02:00
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
close(id);
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:09:32 +01:00
|
|
|
if (preallocated_iobuf != NULL)
|
|
|
|
{
|
|
|
|
free(preallocated_iobuf);
|
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
if (outfd != STDOUT_FILENO)
|
|
|
|
{
|
|
|
|
close(outfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int netcat_client(int argc, char * argv[])
|
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
int id = -1;
|
|
|
|
int infd = STDIN_FILENO;
|
2020-10-21 14:34:10 +02:00
|
|
|
char *host = "127.0.0.1";
|
|
|
|
int port = NETCAT_PORT;
|
2021-01-28 01:57:06 +01:00
|
|
|
int result = EXIT_SUCCESS;
|
2022-01-20 21:24:18 +01:00
|
|
|
struct sockaddr_in server;
|
2022-01-24 16:09:32 +01:00
|
|
|
char *preallocated_iobuf = NULL;
|
2022-01-04 07:29:05 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
|
|
|
|
struct stat stat_buf;
|
|
|
|
#endif
|
2020-10-21 14:34:10 +02:00
|
|
|
|
|
|
|
if (argc > 1)
|
|
|
|
{
|
|
|
|
host = argv[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
{
|
|
|
|
port = atoi(argv[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 3)
|
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
infd = open(argv[3], O_RDONLY);
|
|
|
|
if (infd == -1)
|
2020-10-21 14:34:10 +02:00
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
perror("error: io: Failed to open file");
|
|
|
|
infd = STDIN_FILENO;
|
|
|
|
result = 1;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
2022-01-04 07:29:05 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
|
|
|
|
if (fstat(infd, &stat_buf) == -1)
|
|
|
|
{
|
2022-01-18 01:29:03 +01:00
|
|
|
perror("error: fstat: Could not get the input file size");
|
2022-01-04 07:29:05 +01:00
|
|
|
infd = STDIN_FILENO;
|
|
|
|
result = 1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
#endif
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
id = socket(AF_INET , SOCK_STREAM , 0);
|
|
|
|
if (0 > id)
|
|
|
|
{
|
|
|
|
perror("error: net: Failed to create socket");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 2;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(port);
|
|
|
|
if (1 != inet_pton(AF_INET, host, &server.sin_addr))
|
|
|
|
{
|
|
|
|
perror("error: net: Invalid host");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 3;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (connect(id, (struct sockaddr *) &server, sizeof(server)) < 0)
|
|
|
|
{
|
|
|
|
perror("error: net: Failed to connect");
|
2021-01-28 01:57:06 +01:00
|
|
|
result = 4;
|
|
|
|
goto out;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 07:29:05 +01:00
|
|
|
#ifdef CONFIG_NETUTILS_NETCAT_SENDFILE
|
|
|
|
if (argc > 3)
|
|
|
|
{
|
2022-01-18 01:29:03 +01:00
|
|
|
result = do_io_over_sendfile(infd, id, stat_buf.st_size);
|
2022-01-04 07:29:05 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2022-01-24 16:09:32 +01:00
|
|
|
preallocated_iobuf = (char *)malloc(CONFIG_NETUTILS_NETCAT_BUFSIZE);
|
|
|
|
|
|
|
|
if (preallocated_iobuf == NULL)
|
|
|
|
{
|
|
|
|
perror("error: malloc: Failed to allocate I/O buffer\n");
|
|
|
|
result = 2;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = do_io(infd, id,
|
|
|
|
preallocated_iobuf, CONFIG_NETUTILS_NETCAT_BUFSIZE);
|
2022-01-04 07:29:05 +01:00
|
|
|
}
|
2020-10-21 14:34:10 +02:00
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
out:
|
|
|
|
if (id != -1)
|
|
|
|
{
|
|
|
|
close(id);
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
2022-01-24 16:09:32 +01:00
|
|
|
if (preallocated_iobuf != NULL)
|
|
|
|
{
|
|
|
|
free(preallocated_iobuf);
|
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
if (infd != STDIN_FILENO)
|
2020-10-21 14:34:10 +02:00
|
|
|
{
|
2021-01-28 01:57:06 +01:00
|
|
|
close(infd);
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 01:57:06 +01:00
|
|
|
return result;
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* netcat_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
|
|
|
int status = EXIT_SUCCESS;
|
|
|
|
if (2 > argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
2021-01-28 01:32:43 +01:00
|
|
|
"Usage: netcat <destination> [port] [file]\n"
|
|
|
|
"Usage: netcat -l [port] [file]\n");
|
2020-10-21 14:34:10 +02:00
|
|
|
}
|
|
|
|
else if ((1 < argc) && (0 == strcmp("-l", argv[1])))
|
|
|
|
{
|
|
|
|
status = netcat_server(argc, argv);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status = netcat_client(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|