forked from bcd/exec09
-
Notifications
You must be signed in to change notification settings - Fork 9
/
wpclib.c
143 lines (121 loc) · 2.89 KB
/
wpclib.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "wpclib.h"
#define UDP_PORT 7400
#define CLIENT_PORT_OFFSET 0
#define SERVER_PORT_OFFSET 1
int client_port = UDP_PORT + CLIENT_PORT_OFFSET;
int server_port = UDP_PORT + SERVER_PORT_OFFSET;
void udp_socket_error (void)
{
abort ();
}
int udp_socket_create (int port)
{
int rc;
int s;
struct sockaddr_in myaddr;
s = socket (PF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
fprintf (stderr, "could not open socket, errno=%d\n", errno);
udp_socket_error ();
return s;
}
rc = fcntl (s, F_SETFL, O_NONBLOCK);
if (rc < 0)
{
fprintf (stderr, "could not set nonblocking, errno=%d\n", errno);
udp_socket_error ();
return rc;
}
myaddr.sin_family = AF_INET;
myaddr.sin_port = htons (port);
myaddr.sin_addr.s_addr = INADDR_ANY;
rc = bind (s, (struct sockaddr *)&myaddr, sizeof (myaddr));
if (rc < 0)
{
fprintf (stderr, "could not bind socket, errno=%d\n", errno);
udp_socket_error ();
return rc;
}
return s;
}
int udp_socket_send (int s, int dstport, const void *data, socklen_t len)
{
int rc;
struct sockaddr_in to;
to.sin_family = AF_INET;
to.sin_port = htons (dstport);
to.sin_addr.s_addr = inet_addr ("127.0.0.1");
rc = sendto (s, data, len, 0, (struct sockaddr *)&to, sizeof (to));
if ((rc < 0) && (errno != EAGAIN))
{
fprintf (stderr, "could not send, errno=%d\n", errno);
udp_socket_error ();
}
return rc;
}
int udp_socket_receive (int s, int dstport, void *data, socklen_t len)
{
int rc;
struct sockaddr_in from;
rc = recvfrom (s, data, len, 0, (struct sockaddr *)&from, &len);
if ((rc < 0) && (errno != EAGAIN))
{
fprintf (stderr, "could not receive, errno=%d\n", errno);
udp_socket_error ();
}
return rc;
}
int wpc_msg_init (int code, struct wpc_message *msg)
{
msg->code = code;
msg->timestamp = 0;
msg->len = 0;
return 0;
}
int wpc_msg_send (int s, int dstport, struct wpc_message *msg)
{
int rc = udp_socket_send (s, dstport, msg, msg->len + sizeof (struct wpc_message) - 1000);
if (rc < 0)
{
fprintf (stderr, "error: could not send!\n");
}
return rc;
}
int udp_socket_close (int s)
{
close (s);
return 0;
}
#ifdef STANDALONE
int main (int argc, char *argv[])
{
int server, client;
char sendbuf[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
char recvbuf[8];
server = udp_socket_create (server_port);
client = udp_socket_create (client_port);
printf ("Server = %d, Client = %d\n", server, client);
getchar ();
printf ("Sending data\n");
udp_socket_send (client, server_port, sendbuf, sizeof (sendbuf));
printf ("Receiving data\n");
udp_socket_receive (server, client_port, recvbuf, sizeof (recvbuf));
if (memcmp (sendbuf, recvbuf, sizeof (sendbuf)))
{
printf ("Buffers differ.\n");
}
udp_socket_close (server);
udp_socket_close (client);
}
#endif