-
Notifications
You must be signed in to change notification settings - Fork 0
/
houseportaludp.c
133 lines (114 loc) · 3.93 KB
/
houseportaludp.c
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
128
129
130
131
132
/* houseportal - A simple Web portal for home servers.
*
* Copyright 2020, Pascal Martin
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*
* houseportaludp.c - Manage UDP communications.
*
* This module opens a UDP server socket, hiding the IP structure details.
*
* SYNOPSYS:
*
* int hp_udp_client (const char *destination, const char *service);
*
* Open UDP sockets for the specified destination and returns the count
* of sockets that was opened (0 indicates failure).
*
* void hp_udp_send (const char *data, int length)
*
* Send a data packet.
*
* LIMITATIONS:
*
* Only supports IPv4 addresses for the time being.
* Only supports local broadcast (address 255.255.255.255).
* Only supports one socket per process.
*/
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include "houseportalclient.h"
int UdpClient[16];
int UdpClientCount = 0;
int hp_udp_client (const char *destination, const char *service) {
int value;
int s;
static struct addrinfo hints;
struct addrinfo *resolved;
struct addrinfo *cursor;
hints.ai_flags = AI_ADDRCONFIG;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
int retries = 200;
for (;;) {
int status = getaddrinfo (destination, service, &hints, &resolved);
if (!status) break; // Success.
fprintf (stderr, "cannot resolve %s:%s (%s)\n",
destination, service, gai_strerror(status));
if (retries-- <= 0) return 0;
sleep(3); // Retry later. It might be a network initialization issue.
}
for (cursor = resolved; cursor; cursor = cursor->ai_next) {
if (cursor->ai_family != AF_INET && cursor->ai_family != AF_INET6)
continue;
s = socket(cursor->ai_family, cursor->ai_socktype, cursor->ai_protocol);
if (s < 0) {
fprintf (stderr, "cannot open socket for port %s (%s): %s\n",
service,
(cursor->ai_family==AF_INET6)?"ipv6":"ipv4",
strerror(errno));
continue;
}
if (connect(s, cursor->ai_addr, cursor->ai_addrlen) < 0) {
fprintf (stderr, "cannot connect to port %s (%s): %s\n",
service,
(cursor->ai_family==AF_INET6)?"ipv6":"ipv4",
strerror(errno));
close(s);
continue;
}
value = 256 * 1024;
if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &value, sizeof(value)) < 0) {
fprintf (stderr, "cannot set receive buffer to %d: %s\n",
value, strerror(errno));
}
value = 256 * 1024;
if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, &value, sizeof(value)) < 0) {
fprintf (stderr, "cannot set send buffer to %d: %s\n",
value, strerror(errno));
}
UdpClient[UdpClientCount++] = s;
if (UdpClientCount >= 16) break;
}
freeaddrinfo(resolved);
return UdpClientCount;
}
void hp_udp_send (const char *data, int length) {
int i;
for (i = 0; i < UdpClientCount; ++i) {
send (UdpClient[i], data, length, 0);
}
}