forked from analogdevicesinc/sigma-tcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigma_tcp.c
298 lines (242 loc) · 6.03 KB
/
sigma_tcp.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
* Copyright (C) 2012 Analog Devices, Inc.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
*
**/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdbool.h>
#include "sigma_tcp.h"
#include <netinet/in.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <sys/ioctl.h>
static void addr_to_str(const struct sockaddr *sa, char *s, size_t maxlen)
{
switch(sa->sa_family) {
case AF_INET:
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr),
s, maxlen);
break;
case AF_INET6:
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr),
s, maxlen);
break;
default:
strncpy(s, "Unkown", maxlen);
break;
}
}
static int show_addrs(int sck)
{
char buf[256];
char ip[INET6_ADDRSTRLEN];
struct ifconf ifc;
struct ifreq *ifr;
unsigned int i, n;
int ret;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
ret = ioctl(sck, SIOCGIFCONF, &ifc);
if (ret < 0) {
perror("ioctl(SIOCGIFCONF)");
return 1;
}
ifr = ifc.ifc_req;
n = ifc.ifc_len / sizeof(struct ifreq);
printf("IP addresses:\n");
for (i = 0; i < n; i++) {
struct sockaddr *addr = &ifr[i].ifr_addr;
if (strcmp(ifr[i].ifr_name, "lo") == 0)
continue;
addr_to_str(addr, ip, INET6_ADDRSTRLEN);
printf("%s: %s\n", ifr[i].ifr_name, ip);
}
return 0;
}
#define COMMAND_READ 0x0a
#define COMMAND_WRITE 0x0b
static uint8_t debug_data[256];
static int debug_read(unsigned int addr, unsigned int len, uint8_t *data)
{
if (addr < 0x4000 || addr + len > 0x4100) {
memset(data, 0x00, len);
return 0;
}
printf("read: %.2x %d\n", addr, len);
addr -= 0x4000;
memcpy(data, debug_data + addr, len);
return 0;
}
static int debug_write(unsigned int addr, unsigned int len, const uint8_t *data)
{
if (addr < 0x4000 || addr + len > 0x4100)
return 0;
printf("write: %.2x %d\n", addr, len);
addr -= 0x4000;
memcpy(debug_data + addr, data, len);
return 0;
}
static const struct backend_ops debug_backend_ops = {
.read = debug_read,
.write = debug_write,
};
static const struct backend_ops *backend_ops = &debug_backend_ops;
static void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET)
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
static void handle_connection(int fd)
{
uint8_t *buf;
size_t buf_size;
uint8_t *p;
unsigned int len;
unsigned int addr;
/* unsigned int total_len;*/
int count, ret;
char command;
count = 0;
buf_size = 256;
buf = malloc(buf_size);
if (!buf)
goto exit;
p = buf;
while (1) {
memmove(buf, p, count);
p = buf + count;
ret = read(fd, p, buf_size - count);
if (ret <= 0)
break;
p = buf;
count += ret;
while (count >= 7) {
command = p[0];
/* total_len = (p[1] << 8) | p[2];*/
len = (p[4] << 8) | p[5];
addr = (p[6] << 8) | p[7];
if (command == COMMAND_READ) {
p += 8;
count -= 8;
buf[0] = COMMAND_WRITE;
buf[1] = (0x4 + len) >> 8;
buf[2] = (0x4 + len) & 0xff;
buf[3] = backend_ops->read(addr, len, buf + 4);
write(fd, buf, 4 + len);
} else {
/* not enough data, fetch next bytes */
if (count < len + 8) {
if (buf_size < len + 8) {
buf_size = len + 8;
buf = realloc(buf, buf_size);
if (!buf)
goto exit;
}
break;
}
backend_ops->write(addr, len, p + 8);
p += len + 8;
count -= len + 8;
}
}
}
exit:
free(buf);
}
int main(int argc, char *argv[])
{
int sockfd, new_fd;
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr;
socklen_t sin_size;
int reuse = 1;
char s[INET6_ADDRSTRLEN];
int ret;
if (argc >= 2) {
if (strcmp(argv[1], "debug") == 0)
backend_ops = &debug_backend_ops;
else if (strcmp(argv[1], "i2c") == 0)
backend_ops = &i2c_backend_ops;
else if (strcmp(argv[1], "regmap") == 0)
backend_ops = ®map_backend_ops;
else {
printf("Usage: %s <backend> <backend arg0> ...\n"
"Available backends: debug, i2c, regmap\n", argv[0]);
exit(0);
}
printf("Using %s backend\n", argv[1]);
}
if (backend_ops->open) {
ret = backend_ops->open(argc, argv);
if (ret)
exit(1);
}
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(NULL, "8086", &hints, &servinfo);
if (ret != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
return 1;
}
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "Failed to bind\n");
return 2;
}
freeaddrinfo(servinfo);
if (listen(sockfd, 0) == -1) {
perror("listen");
exit(1);
}
printf("Waiting for connections...\n");
show_addrs(sockfd);
while (true) {
sin_size = sizeof their_addr;
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (new_fd == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("New connection from %s\n", s);
handle_connection(new_fd);
printf("Connection closed\n");
}
return 0;
}