-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
176 lines (160 loc) · 5.59 KB
/
client.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright 2013 Ka-Ping Yee (Modified by Milkey Mouse)
Licensed 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: http://www.apache.org/licenses/LICENSE-2.0
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. */
#include "bamboozled.h"
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <poll.h>
#include "opc.h"
void opc_resolve(bamboozled_address *info)
{
struct addrinfo *ai;
struct addrinfo *addr;
int err = getaddrinfo(info->dest->hostname, NULL, NULL, &addr);
if (err == 0)
{
for (ai = addr; ai != NULL; ai = ai->ai_next)
{
if (ai->ai_family == AF_INET)
{
memcpy(&info->host, &((struct sockaddr_in *)(addr->ai_addr))->sin_addr, sizeof(in_addr_t));
freeaddrinfo(addr);
return;
}
}
fprintf(stderr, "could not resolve address %s\n", info->dest->hostname);
}
else
{
fprintf(stderr, "could not resolve address %s: %s\n", info->dest->hostname, gai_strerror(err));
}
exit(1);
}
bool opc_connect(bamboozled_address *info, uint32_t timeout_ms)
{
// check if we are waiting for a previous timeout
struct timespec curtime;
clock_gettime(CLOCK_MONOTONIC, &curtime);
if (info->dest != NULL && (curtime.tv_sec < info->dest->timeout_end.tv_sec || (curtime.tv_sec == info->dest->timeout_end.tv_sec && curtime.tv_nsec < info->dest->timeout_end.tv_nsec)))
{
return false;
}
// create the sockaddr we will connect() to
struct sockaddr_in dest;
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = info->host;
dest.sin_port = htons(info->port);
memset(&dest.sin_zero, 0, sizeof(dest.sin_zero));
// do a non-blocking connect so we can control the timeout
int sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
fcntl(sock, F_SETFL, O_NONBLOCK);
if (connect(sock, (struct sockaddr *)&dest, sizeof(dest)) < 0 && errno != EINPROGRESS)
{
fprintf(stderr, "failed to connect to %s: ", info->dest->hostname);
perror(NULL);
close(sock);
return false;
}
struct pollfd sockfd;
sockfd.fd = sock;
sockfd.events = POLLOUT;
sockfd.revents = 0;
// wait for a result
int rv = poll(&sockfd, 1, timeout_ms);
if (rv == 0)
{
fprintf(stderr, "connection to %s:%hu timed out after %d ms\n", info->dest->hostname, info->port, timeout_ms);
return false;
}
else if (rv == -1)
{
fprintf(stderr, "failed to connect to %s: %s\n", info->dest->hostname, strerror(errno));
return false;
}
else if (sockfd.revents & POLLHUP)
{
fprintf(stderr, "failed to connect to %s: connection refused\n", info->dest->hostname);
return false;
}
else if (sockfd.revents & POLLERR)
{
fprintf(stderr, "failed to connect to %s:%hu\n", info->dest->hostname, info->port);
return false;
}
else if (sockfd.revents & POLLNVAL)
{
fprintf(stderr, "failed to connect to %s: poll() returned errors\n", info->dest->hostname);
return false;
}
int opt_errno = 0;
socklen_t opt_len;
getsockopt(sock, SOL_SOCKET, SO_ERROR, &opt_errno, &opt_len);
if (opt_errno != 0)
{
fprintf(stderr, "failed to connect to %s: %s\n", info->dest->hostname, strerror(opt_errno));
close(sock);
if (opt_errno == ECONNREFUSED)
{
// set a timer before another connection attempt
info->dest->timeout_end.tv_nsec = curtime.tv_nsec + (timeout_ms % 1000) * 1000;
info->dest->timeout_end.tv_sec = curtime.tv_sec + timeout_ms / 1000 + info->dest->timeout_end.tv_nsec / 1000000000;
info->dest->timeout_end.tv_nsec %= 1000000000;
}
}
fprintf(stderr, "connected to destination %s:%hu\n", info->dest->hostname, info->port);
info->dest->sock = sock;
return true;
}
bool opc_send(bamboozled_address *info, const uint8_t *data, ssize_t len, uint32_t timeout_ms)
{
ssize_t total_sent = 0;
ssize_t sent;
struct timeval timeout;
timeout.tv_sec = timeout_ms / 1000;
timeout.tv_usec = timeout_ms % 1000;
setsockopt(info->dest->sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
while (total_sent < len)
{
sent = send(info->dest->sock, data + total_sent, len - total_sent, MSG_NOSIGNAL);
if ((errno != EINPROGRESS && errno != 0) || sent <= 0)
{
fputs("error sending data to ", stderr);
perror(info->dest->hostname);
return false;
}
total_sent += sent;
}
return true;
}
bool opc_put_pixels(bamboozled_address *info, uint8_t channel, uint16_t count, rgbPixel *pixels)
{
if (count == 0)
{
return true;
}
if (info->dest->sock == -1 && !opc_connect(info, OPC_SEND_TIMEOUT))
{
return false;
}
uint8_t header[4];
uint16_t len = count * 3;
header[0] = channel;
header[1] = OPC_SET_PIXELS;
header[2] = len >> 8;
header[3] = len & 0xff;
return opc_send(info, header, 4, OPC_SEND_TIMEOUT) &&
opc_send(info, (uint8_t *)pixels, len, OPC_SEND_TIMEOUT);
}