forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netout.c
118 lines (100 loc) · 2.86 KB
/
netout.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
/*
* Copyright (c) 2015 Thierry Leconte
* Copyright (c) 2024 Thibaut VARENE
*
*
* This code is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License version 2
* published by the Free Software Foundation.
*
* 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include <errno.h>
#include "acarsdec.h"
#include "netout.h"
#define ERRPFX "ERROR: UDP: "
#define WARNPFX "WARNING: UDP: "
// params is "host=xxx,port=yyy"
netout_t *Netoutinit(char *params)
{
char *addr = NULL;
char *port = NULL;
struct params_s netp[] = {
{ .name = "host", .valp = &addr, },
{ .name = "port", .valp = &port, },
};
char *retp;
struct addrinfo hints, *servinfo, *p;
int sockfd, rv;
netout_t *netpriv = NULL;
retp = parse_params(¶ms, netp, ARRAY_SIZE(netp));
if (retp) {
fprintf(stderr, ERRPFX "invalid parameter '%s'\n", retp);
return NULL;
}
if (!addr) {
fprintf(stderr, ERRPFX "missing host parameter\n");
return NULL;
}
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;
if (port == NULL)
port = "5555";
vprerr("UDP: Attempting to resolve '%s:%s'.\n", addr, port);
if ((rv = getaddrinfo(addr, port, &hints, &servinfo)) != 0) {
fprintf(stderr, ERRPFX "Invalid/unknown error '%s' resolving '%s:%s'\n", gai_strerror(rv), addr, port);
return NULL;
}
for (p = servinfo; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (-1 != sockfd)
break; // success
}
if (!p) {
fprintf(stderr, ERRPFX "failed to resolve: '%s:%s'\n", addr, port);
goto fail;
}
netpriv = malloc(sizeof(*netpriv));
if (!netpriv) {
perror(NULL);
goto fail;
}
memcpy(&netpriv->netOutputAddr, p->ai_addr, p->ai_addrlen);
netpriv->netOutputAddrLen = p->ai_addrlen;
netpriv->sockfd = sockfd;
fail:
freeaddrinfo(servinfo);
return netpriv;
}
void Netwrite(const void *buf, size_t count, netout_t *net)
{
int res;
if (!net->netOutputAddrLen)
return;
res = sendto(net->sockfd, buf, count, 0, (struct sockaddr *)&net->netOutputAddr, net->netOutputAddrLen);
if (res < 0)
vprerr(WARNPFX "error on sendto(): %s, ignoring.\n", strerror(errno));
}
void Netexit(netout_t *net)
{
free(net);
}