2018-07-06 00:03:54 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/tcpblaster/tcpblaster_client.c
|
2018-07-06 00:03:54 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* 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
|
2018-07-06 00:03:54 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-07-06 00:03:54 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* 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.
|
2018-07-06 00:03:54 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
2018-09-10 20:13:18 +02:00
|
|
|
#include <poll.h>
|
2018-07-06 00:03:54 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "tcpblaster.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tcpblaster_client(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_EXAMPLES_TCPBLASTER_IPv6
|
|
|
|
struct sockaddr_in6 server;
|
|
|
|
#else
|
|
|
|
struct sockaddr_in server;
|
|
|
|
#endif
|
|
|
|
struct timespec start;
|
|
|
|
socklen_t addrlen;
|
2020-02-10 20:26:47 +01:00
|
|
|
FAR char *outbuf;
|
2018-07-06 00:03:54 +02:00
|
|
|
unsigned long sendtotal;
|
|
|
|
unsigned long totallost;
|
2020-02-25 02:00:08 +01:00
|
|
|
int groupcount;
|
2018-07-06 00:03:54 +02:00
|
|
|
int sendcount;
|
|
|
|
int partials;
|
|
|
|
int sockfd;
|
|
|
|
int nbytessent;
|
|
|
|
int ch;
|
|
|
|
int i;
|
2020-02-25 02:00:08 +01:00
|
|
|
char timebuff[100];
|
2018-07-06 00:03:54 +02:00
|
|
|
|
2020-02-25 02:00:08 +01:00
|
|
|
setbuf(stdout, NULL);
|
2020-03-08 17:31:44 +01:00
|
|
|
|
2018-07-06 00:03:54 +02:00
|
|
|
/* Allocate buffers */
|
|
|
|
|
2020-02-10 20:26:47 +01:00
|
|
|
outbuf = (FAR char *)malloc(SENDSIZE);
|
2018-07-06 00:03:54 +02:00
|
|
|
if (!outbuf)
|
|
|
|
{
|
|
|
|
printf("client: failed to allocate buffers\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new TCP socket */
|
|
|
|
|
|
|
|
sockfd = socket(PF_INETX, SOCK_STREAM, 0);
|
|
|
|
if (sockfd < 0)
|
|
|
|
{
|
|
|
|
printf("client socket failure %d\n", errno);
|
|
|
|
goto errout_with_buffers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set up the server address */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_TCPBLASTER_IPv6
|
2020-03-08 17:31:44 +01:00
|
|
|
server.sin6_family = AF_INET6;
|
|
|
|
server.sin6_port = HTONS(CONFIG_EXAMPLES_TCPBLASTER_SERVER_PORTNO);
|
2020-03-30 07:56:01 +02:00
|
|
|
memcpy(server.sin6_addr.s6_addr, g_tcpblasterserver_ipv6, 16);
|
2020-03-08 17:31:44 +01:00
|
|
|
addrlen = sizeof(struct sockaddr_in6);
|
|
|
|
|
|
|
|
printf("Connecting to IPv6 Address: "
|
|
|
|
"%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
|
2020-02-10 20:26:47 +01:00
|
|
|
g_tcpblasterserver_ipv6[0], g_tcpblasterserver_ipv6[1],
|
|
|
|
g_tcpblasterserver_ipv6[2], g_tcpblasterserver_ipv6[3],
|
|
|
|
g_tcpblasterserver_ipv6[4], g_tcpblasterserver_ipv6[5],
|
|
|
|
g_tcpblasterserver_ipv6[6], g_tcpblasterserver_ipv6[7]);
|
2018-07-06 00:03:54 +02:00
|
|
|
#else
|
2020-03-08 17:31:44 +01:00
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = HTONS(CONFIG_EXAMPLES_TCPBLASTER_SERVER_PORTNO);
|
|
|
|
server.sin_addr.s_addr = (in_addr_t)g_tcpblasterserver_ipv4;
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
2018-07-06 00:03:54 +02:00
|
|
|
|
2020-02-10 20:26:47 +01:00
|
|
|
printf("Connecting to IPv4 Address: %08lx\n",
|
|
|
|
(unsigned long)g_tcpblasterserver_ipv4);
|
2018-07-06 00:03:54 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Connect the socket to the server */
|
|
|
|
|
2020-02-10 20:26:47 +01:00
|
|
|
if (connect(sockfd, (FAR struct sockaddr *)&server, addrlen) < 0)
|
2018-07-06 00:03:54 +02:00
|
|
|
{
|
|
|
|
printf("client: connect failure: %d\n", errno);
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("client: Connected\n");
|
|
|
|
|
|
|
|
/* Initialize the buffer */
|
|
|
|
|
|
|
|
ch = 0x20;
|
|
|
|
for (i = 0; i < SENDSIZE; i++ )
|
|
|
|
{
|
|
|
|
outbuf[i] = ch;
|
|
|
|
if (++ch > 0x7e)
|
|
|
|
{
|
|
|
|
ch = 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then send messages forever */
|
|
|
|
|
2020-02-25 02:00:08 +01:00
|
|
|
groupcount = 0;
|
2018-07-06 00:03:54 +02:00
|
|
|
sendcount = 0;
|
|
|
|
sendtotal = 0;
|
|
|
|
partials = 0;
|
|
|
|
totallost = 0;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
clock_gettime(CLOCK_REALTIME, &start);
|
2018-07-06 00:03:54 +02:00
|
|
|
|
|
|
|
for (; ; )
|
|
|
|
{
|
2018-09-10 20:13:18 +02:00
|
|
|
#ifdef CONFIG_EXAMPLES_TCPBLASTER_POLLOUT
|
|
|
|
struct pollfd fds[1];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
memset(fds, 0, 1 * sizeof(struct pollfd));
|
|
|
|
fds[0].fd = sockfd;
|
2022-10-03 20:28:15 +02:00
|
|
|
fds[0].events = POLLOUT;
|
2018-09-10 20:13:18 +02:00
|
|
|
|
|
|
|
/* Wait until we can send data or until the connection is lost */
|
|
|
|
|
|
|
|
ret = poll(fds, 1, -1);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
printf("client: ERROR poll failed: %d\n", errno);
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fds[0].revents & POLLHUP) != 0)
|
|
|
|
{
|
|
|
|
printf("client: WARNING poll returned POLLHUP\n");
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-07-06 00:03:54 +02:00
|
|
|
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
|
|
|
|
if (nbytessent < 0)
|
|
|
|
{
|
|
|
|
printf("client: send failed: %d\n", errno);
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
else if (nbytessent > 0 && nbytessent < SENDSIZE)
|
|
|
|
{
|
|
|
|
/* Partial buffers can be sent if there is insufficient buffering
|
|
|
|
* space to buffer the whole SENDSIZE request. This is not an
|
|
|
|
* error, but is an interesting thing to keep track of.
|
|
|
|
*/
|
|
|
|
|
|
|
|
partials++;
|
|
|
|
totallost += (SENDSIZE - nbytessent);
|
|
|
|
}
|
|
|
|
else if (nbytessent != SENDSIZE)
|
|
|
|
{
|
2021-12-26 23:10:48 +01:00
|
|
|
printf("client: Bad send length=%d: of %d\n",
|
2018-07-06 00:03:54 +02:00
|
|
|
nbytessent, SENDSIZE);
|
|
|
|
goto errout_with_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendtotal += nbytessent;
|
|
|
|
|
2020-02-25 02:00:08 +01:00
|
|
|
if (++sendcount >= GROUPSIZE)
|
2018-07-06 00:03:54 +02:00
|
|
|
{
|
|
|
|
struct timespec elapsed;
|
|
|
|
struct timespec curr;
|
|
|
|
float fkbrecvd;
|
|
|
|
float felapsed;
|
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
clock_gettime(CLOCK_REALTIME, &curr);
|
2018-07-06 00:03:54 +02:00
|
|
|
|
|
|
|
elapsed.tv_sec = curr.tv_sec - start.tv_sec;
|
|
|
|
if (curr.tv_nsec >= start.tv_nsec)
|
|
|
|
{
|
|
|
|
elapsed.tv_nsec = curr.tv_nsec - start.tv_nsec;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned long borrow = 1000000000 - start.tv_nsec;
|
2018-07-06 22:44:26 +02:00
|
|
|
elapsed.tv_sec--;
|
2018-07-06 00:03:54 +02:00
|
|
|
elapsed.tv_nsec = curr.tv_nsec + borrow;
|
|
|
|
}
|
|
|
|
|
2020-03-08 17:31:44 +01:00
|
|
|
strftime(timebuff, 100,
|
|
|
|
"%Y-%m-%d %H:%M:%S.000", localtime(&curr.tv_sec));
|
2020-02-25 02:00:08 +01:00
|
|
|
|
2020-03-08 17:31:44 +01:00
|
|
|
fkbrecvd = sendtotal / 1024.0f;
|
|
|
|
felapsed = elapsed.tv_sec + elapsed.tv_nsec / 1000000000.0f;
|
|
|
|
printf("[%s] %d: Sent %d %d-byte buffers: %7.1fKB "
|
|
|
|
"(avg %5.1f KB) in %6.2f seconds (%7.1f KB/second)\n",
|
|
|
|
timebuff, groupcount, sendcount, SENDSIZE, fkbrecvd,
|
|
|
|
fkbrecvd / sendcount, felapsed, fkbrecvd / felapsed);
|
2018-07-06 00:03:54 +02:00
|
|
|
|
|
|
|
if (partials > 0)
|
|
|
|
{
|
|
|
|
float fkblost;
|
|
|
|
|
|
|
|
fkblost = (float)totallost / 1024.0;
|
|
|
|
printf("Partial buffers sent: %d Data not sent: %7.1f Kb\n",
|
|
|
|
partials, fkblost);
|
|
|
|
}
|
|
|
|
|
|
|
|
sendcount = 0;
|
|
|
|
sendtotal = 0;
|
|
|
|
partials = 0;
|
|
|
|
totallost = 0;
|
2020-02-25 02:00:08 +01:00
|
|
|
groupcount++;
|
2018-07-06 00:03:54 +02:00
|
|
|
|
2020-01-02 13:09:50 +01:00
|
|
|
clock_gettime(CLOCK_REALTIME, &start);
|
2020-02-10 20:26:47 +01:00
|
|
|
}
|
2018-07-06 00:03:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
free(outbuf);
|
|
|
|
return;
|
|
|
|
|
|
|
|
errout_with_socket:
|
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
errout_with_buffers:
|
|
|
|
free(outbuf);
|
|
|
|
exit(1);
|
|
|
|
}
|