1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* Video On Demand Samples
*
* Copyright (C) 2015 Microchip Technology Germany II GmbH & Co. KG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You may also obtain this software under a propriety license from Microchip.
* Please contact Microchip for further information.
*
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include "Console.h"
/*----------------------------------------------------------*/
/*! \brief Private structure used by UdpStream component.
*/
/*----------------------------------------------------------*/
typedef struct localVar_tag
{
///Socket handle.
int sock;
///Target port.
int port;
///Target IP address as zero terminated string.
char targetIpAddress[32];
///SocketAddress structure to address remote node.
struct sockaddr_in destination;
///Helper variable to suppress a lot of error messages.
bool errorMode;
} localVar_t;
static localVar_t udpLocal;
bool UdpStream_Init( const char *targetIp, int targetPort )
{
if( ( NULL == targetIp ) || ( 0 == targetPort ) )
{
return false;
}
ConsolePrintf( PRIO_HIGH, "Sending UDP stream to IP:'%s', port:%d\n", targetIp, targetPort );
strcpy( udpLocal.targetIpAddress, targetIp );
udpLocal.port = targetPort;
udpLocal.errorMode = false;
memset( &udpLocal.destination, 0, sizeof( udpLocal.destination ) );
/* Default send address */
udpLocal.destination.sin_family = AF_INET;
udpLocal.destination.sin_addr.s_addr = inet_addr( udpLocal.targetIpAddress );
udpLocal.destination.sin_port = htons( udpLocal.port );
if( ( udpLocal.sock = socket( PF_INET, SOCK_DGRAM, IPPROTO_UDP ) ) < 0 )
{
ConsolePrintf( PRIO_ERROR, RED"Failed to create socket"RESETCOLOR"\n" );
return false;
}
#if defined (SO_REUSEADDR) && defined (SO_REUSEPORT)
int one = 1;
setsockopt(udpLocal.sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &one, sizeof(one));
#endif
/* set default destination */
setsockopt( udpLocal.sock, IPPROTO_IP, IP_MULTICAST_IF, &udpLocal.destination, sizeof( udpLocal.destination ) );
// this call is what allows broadcast packets to be sent:
int enableSocketFlag = 1;
if( setsockopt( udpLocal.sock, SOL_SOCKET, SO_BROADCAST, &enableSocketFlag, sizeof( enableSocketFlag ) ) < 0 )
{
ConsolePrintf( PRIO_ERROR, RED"setsockopt (SO_BROADCAST) failed"RESETCOLOR"\n" );
return false;
}
int ttl = 5;
if (!(setsockopt( udpLocal.sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)))) {
ConsolePrintf( PRIO_HIGH, "TTL set successfully to %d\n", ttl );
} else {
ConsolePrintf( PRIO_ERROR, "Error setting TTL: %s\n", strerror(errno));
}
return true;
}
void UdpStream_Close()
{
close( udpLocal.sock );
}
bool UdpStream_Send( const uint8_t *data, uint32_t length )
{
int is_length = sendto( udpLocal.sock, data, length,
0, ( struct sockaddr * )&udpLocal.destination, sizeof( udpLocal.destination ) );
if( is_length != length )
{
if( !udpLocal.errorMode )
{
udpLocal.errorMode = true;
ConsolePrintf( PRIO_ERROR, RED"Send failed; shall len = %d; is length = %d"RESETCOLOR"\n", length, is_length );
}
return false;
}
else
udpLocal.errorMode = false;
return true;
}
|