-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecretapi.c
98 lines (85 loc) · 2.16 KB
/
secretapi.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
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define BUFLEN 512
#define NPACK 10
#define PORT 9988
#define DATALEN 150
#define HEADERLEN 10
#define MSGLEN (DATALEN+HEADERLEN)
#ifdef TARGET
#include "spi.h"
#endif
void diep(char *s)
{
perror(s);
exit(1);
}
void render(char* buf) {
int j, k;
char* obf;
obf = &buf[HEADERLEN];
#ifdef TARGET
// And send it on its merry way
spi_send(obf, DATALEN);
#endif
#ifdef DEBUG
// This is where we get dirty with spi.h
// For the moment, just show the values in the buffer.
for (j=0, k=0; j < DATALEN; j++) {
printf("%02x ", (uint8_t) obf[j]);
if ((++k % 3) == 0) {
printf(" ");
}
if ((k % 15) == 0) {
printf("\n");
}
}
printf("\n");
#endif
return;
}
int main(void)
{
struct sockaddr_in si_me, si_other;
int s, i, slen=sizeof(si_other);
char buf[BUFLEN];
ssize_t num_datas;
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
diep("socket");
memset((void *) &si_me, (int) 0, (size_t) sizeof(si_me));
si_me.sin_family = AF_INET;
si_me.sin_port = htons(PORT);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(s, (const struct sockaddr*) &si_me, sizeof(si_me))==-1)
diep("bind");
#ifdef TARGET
spi_open();
#endif
//for (i=0; i<NPACK; i++) {
while (1) { // Basically we're gonna loop forever...
num_datas = recvfrom(s, (void*) buf, BUFLEN, 0, (struct sockaddr*) &si_other, &slen);
if (num_datas ==-1)
diep("recvfrom()");
if (num_datas != MSGLEN) {
printf("Rejecting packet of length %d from %s:%d\nData: %s\n\n", (int) num_datas,
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf);
} else {
#ifdef DEBUG
printf("Received packet of length %d from %s:%d\n", (int) num_datas,
inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
#endif
render(buf); // Render from here.
}
}
close(s);
#ifdef TARGET
spi_close();
#endif
return 0;
}