2014-11-21 15:20:25 +01:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/bridge/host_main.c
|
2014-11-21 15:20:25 +01: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
|
2014-11-21 15:20:25 +01:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-11-21 15:20:25 +01: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.
|
2014-11-21 15:20:25 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static uint8_t g_sndmessage[] = MESSAGE;
|
|
|
|
static uint8_t g_rdbuffer[EXAMPLES_BRIDGE_SEND_IOBUFIZE];
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* bridge_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2014-11-21 15:50:34 +01:00
|
|
|
struct sockaddr_in receiver;
|
|
|
|
struct sockaddr_in sender;
|
|
|
|
struct sockaddr_in fromaddr;
|
2014-11-21 17:16:19 +01:00
|
|
|
struct sockaddr_in toaddr;
|
2014-11-21 15:20:25 +01:00
|
|
|
socklen_t addrlen;
|
|
|
|
ssize_t nrecvd;
|
|
|
|
ssize_t nsent;
|
|
|
|
int optval;
|
|
|
|
int recvsd;
|
|
|
|
int sndsd;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
/* Create a UDP send socket */
|
|
|
|
|
2014-11-21 15:50:34 +01:00
|
|
|
printf(LABEL "Create send socket: IPHOST=%08x PORT=INPORT_ANY\n",
|
|
|
|
EXAMPLES_BRIDGE_SEND_IPHOST);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
sndsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sndsd < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr, LABEL "ERROR: Failed to create send socket: %d\n",
|
|
|
|
errno);
|
2014-11-21 15:20:25 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
2022-10-08 10:31:21 +02:00
|
|
|
if (setsockopt(sndsd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)) < 0)
|
2014-11-21 15:20:25 +01:00
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
LABEL "ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
2014-11-21 15:20:25 +01:00
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 15:50:34 +01:00
|
|
|
sender.sin_family = AF_INET;
|
|
|
|
sender.sin_port = 0;
|
|
|
|
sender.sin_addr.s_addr = htonl(EXAMPLES_BRIDGE_SEND_IPHOST);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
if (bind(sndsd, (struct sockaddr *)&sender,
|
|
|
|
sizeof(struct sockaddr_in)) < 0)
|
2014-11-21 15:20:25 +01:00
|
|
|
{
|
|
|
|
printf(LABEL "bind failure: %d\n", errno);
|
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a UDP receive socket */
|
|
|
|
|
2014-11-21 15:50:34 +01:00
|
|
|
printf(LABEL "Create receive socket: IPHOST=%08x RECVPORT=%d\n",
|
2014-11-21 17:16:19 +01:00
|
|
|
EXAMPLES_BRIDGE_RECV_IPHOST, EXAMPLES_BRIDGE_SEND_HOSTPORT);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
recvsd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (recvsd < 0)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
LABEL "ERROR: Failed to create receive socket: %d\n", errno);
|
2014-11-21 15:20:25 +01:00
|
|
|
goto errout_with_sendsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set socket to reuse address */
|
|
|
|
|
|
|
|
optval = 1;
|
2022-10-08 10:31:21 +02:00
|
|
|
if (setsockopt(recvsd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int)) < 0)
|
2014-11-21 15:20:25 +01:00
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
LABEL "ERROR: setsockopt SO_REUSEADDR failure: %d\n", errno);
|
2014-11-21 15:20:25 +01:00
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Bind the socket to a local address */
|
|
|
|
|
2014-11-21 15:50:34 +01:00
|
|
|
receiver.sin_family = AF_INET;
|
2014-11-21 17:16:19 +01:00
|
|
|
receiver.sin_port = htons(EXAMPLES_BRIDGE_SEND_HOSTPORT);
|
2014-11-21 15:50:34 +01:00
|
|
|
receiver.sin_addr.s_addr = htonl(EXAMPLES_BRIDGE_RECV_IPHOST);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
if (bind(recvsd, (struct sockaddr *)&receiver,
|
|
|
|
sizeof(struct sockaddr_in)) < 0)
|
2014-11-21 15:20:25 +01:00
|
|
|
{
|
|
|
|
fprintf(stderr, LABEL "ERROR: bind failure: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
2014-11-21 15:50:34 +01:00
|
|
|
/* Send a packet */
|
|
|
|
|
|
|
|
printf(LABEL "Sending %lu bytes: IPTARGET=%08x PORT=%d\n",
|
|
|
|
sizeof(g_sndmessage),
|
2014-11-21 17:16:19 +01:00
|
|
|
EXAMPLES_BRIDGE_RECV_IPADDR, EXAMPLES_BRIDGE_RECV_RECVPORT);
|
2014-11-21 15:50:34 +01:00
|
|
|
|
|
|
|
toaddr.sin_family = AF_INET;
|
2014-11-21 17:16:19 +01:00
|
|
|
toaddr.sin_port = htons(EXAMPLES_BRIDGE_RECV_RECVPORT);
|
|
|
|
toaddr.sin_addr.s_addr = htonl(EXAMPLES_BRIDGE_RECV_IPADDR);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
2022-10-08 10:31:21 +02:00
|
|
|
nsent = sendto(sndsd, g_sndmessage, sizeof(g_sndmessage), 0,
|
|
|
|
(struct sockaddr *)&toaddr, sizeof(struct sockaddr_in));
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
/* Check for send errors */
|
|
|
|
|
|
|
|
if (nsent < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, LABEL "ERROR: sendto failed: %d\n", errno);
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
else if (nsent != sizeof(g_sndmessage))
|
|
|
|
{
|
2014-11-21 15:50:34 +01:00
|
|
|
fprintf(stderr, LABEL "ERROR: Bad send length: %ld Expected: %lu\n",
|
2014-11-21 15:20:25 +01:00
|
|
|
(long)nsent, sizeof(g_sndmessage));
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf(LABEL "Message sent successfully\n");
|
|
|
|
|
|
|
|
/* Read a packet */
|
|
|
|
|
2023-08-18 21:57:42 +02:00
|
|
|
printf(LABEL "Receiving up to %d bytes\n", EXAMPLES_BRIDGE_SEND_IOBUFIZE);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
nrecvd = recvfrom(recvsd, g_rdbuffer, EXAMPLES_BRIDGE_SEND_IOBUFIZE, 0,
|
2022-10-08 10:31:21 +02:00
|
|
|
(struct sockaddr *)&fromaddr, &addrlen);
|
2014-11-21 15:20:25 +01:00
|
|
|
|
2023-08-18 21:57:42 +02:00
|
|
|
printf(LABEL "Received %ld bytes from %s:%u\n",
|
2014-11-21 15:20:25 +01:00
|
|
|
(long)nrecvd,
|
2023-08-18 21:57:42 +02:00
|
|
|
inet_ntoa(fromaddr.sin_addr),
|
2014-11-21 15:50:34 +01:00
|
|
|
ntohs(fromaddr.sin_port));
|
2014-11-21 15:20:25 +01:00
|
|
|
|
|
|
|
/* Check for a receive error or zero bytes received. The negative
|
|
|
|
* return value indicates a receive error; Zero would mean that the
|
|
|
|
* other side of the "connection" performed an "orderly" shutdown.
|
|
|
|
* This should not occur with a UDP socket and so must also be an
|
|
|
|
* error of some kind.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (nrecvd <= 0)
|
|
|
|
{
|
|
|
|
if (nrecvd < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, LABEL "ERROR: recvfrom failed: %d\n", errno);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, LABEL "ERROR: recvfrom returned zero\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
goto errout_with_recvsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Dump the received packet */
|
|
|
|
|
|
|
|
for (i = 0, j = 0; i < nrecvd; i++)
|
|
|
|
{
|
2022-10-08 10:31:21 +02:00
|
|
|
if (g_rdbuffer[i] == ' ' && j >= 64)
|
2014-11-21 15:20:25 +01:00
|
|
|
{
|
|
|
|
putchar('\n');
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
else if (isprint(g_rdbuffer[i]))
|
|
|
|
{
|
|
|
|
putchar(g_rdbuffer[i]);
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
else if (g_rdbuffer[i] == '\n')
|
|
|
|
{
|
|
|
|
putchar('\n');
|
|
|
|
j = 0;
|
|
|
|
}
|
|
|
|
else if (g_rdbuffer[i] != '\r')
|
|
|
|
{
|
|
|
|
printf("\%03o", g_rdbuffer[i]);
|
|
|
|
j += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(recvsd);
|
|
|
|
close(recvsd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
|
|
|
errout_with_recvsd:
|
2022-10-08 10:31:21 +02:00
|
|
|
close(recvsd);
|
2014-11-21 15:20:25 +01:00
|
|
|
errout_with_sendsd:
|
2022-10-08 10:31:21 +02:00
|
|
|
close(sndsd);
|
|
|
|
return EXIT_FAILURE;
|
2014-11-21 15:20:25 +01:00
|
|
|
}
|