10 #define close(fd) closesocket(fd)
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
24 #define INADDR_BROADCAST ((u32_t) 0xffffffff)
30 uint32_t _magic_header;
31 sockaddr_in _localaddr, _remote_receiver_addr, _remote_sender_addr;
45 bool begin(uint16_t port)
51 iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
69 _fd=socket(AF_INET,SOCK_DGRAM, IPPROTO_UDP);
77 DWORD read_timeout = 10;
79 struct timeval read_timeout;
80 read_timeout.tv_sec = 0;
81 read_timeout.tv_usec = 1000;
84 fcntl(_fd, F_SETFL, O_NONBLOCK);
86 setsockopt(_fd, SOL_SOCKET, SO_RCVTIMEO, (
char *)&read_timeout,
sizeof read_timeout);
90 memset(&_localaddr, 0,
sizeof(_localaddr));
91 _localaddr.sin_family = AF_INET;
92 _localaddr.sin_port = htons(_port);
93 _localaddr.sin_addr.s_addr = INADDR_ANY;
94 if (bind(_fd,(
struct sockaddr *) &_localaddr,
sizeof(_localaddr))==-1) {
98 memset(&_remote_sender_addr, 0,
sizeof(_remote_sender_addr));
101 memset(&_remote_receiver_addr, 0,
sizeof(_remote_receiver_addr));
102 _remote_receiver_addr.sin_family = AF_INET;
103 _remote_receiver_addr.sin_port = htons(_port);
104 _remote_receiver_addr.sin_addr.s_addr = INADDR_BROADCAST;
109 if (setsockopt(_fd,SOL_SOCKET,SO_BROADCAST,(
const char*)&broadcast,
sizeof(broadcast))==-1) {
117 uint16_t receive_frame(uint8_t *
string, uint16_t max_length)
119 struct sockaddr_storage src_addr;
120 socklen_t src_addr_len=
sizeof(src_addr);
121 ssize_t count=recvfrom(_fd,(
char*)
string,max_length,0,(
struct sockaddr*)&src_addr,&src_addr_len);
128 }
else if (count==max_length || count < 4) {
134 memcpy(&_remote_sender_addr, &src_addr,
sizeof(_remote_sender_addr));
138 memcpy(&header,
string, 4);
139 if(header != _magic_header) {
144 for (uint16_t i=0; i<count-4; i++) {
145 string[i] =
string[i+4];
160 buf =
new char[size];
179 void send_frame(
const uint8_t *
string, uint16_t length,
const sockaddr_in &remote_addr)
182 Buf buffer(4 + length);
183 memcpy(buffer(), &_magic_header, 4);
184 memcpy(&(buffer()[4]),
string, length);
185 int res = sendto(_fd,buffer(),buffer.size(),0,(
const sockaddr *)&remote_addr,
sizeof(remote_addr));
190 void send_response(uint8_t *
string, uint16_t length)
192 send_frame((
const uint8_t *)
string, length, _remote_sender_addr);
195 void send_response(uint8_t response)
197 send_frame((
const uint8_t *)&response, 1, _remote_sender_addr);
200 void send_frame(
const uint8_t *
string, uint16_t length)
202 _remote_receiver_addr.sin_port = htons(_port);
203 _remote_receiver_addr.sin_addr.s_addr = INADDR_BROADCAST;
204 send_frame(
string, length, _remote_receiver_addr);
207 void send_frame(
const uint8_t *
string, uint16_t length, uint8_t *remote_ip, uint16_t remote_port)
209 _remote_receiver_addr.sin_port = htons(remote_port);
210 _remote_receiver_addr.sin_addr.s_addr = *(uint32_t*)remote_ip;
211 send_frame(
string, length, _remote_receiver_addr);
214 void set_magic_header(uint32_t magic_header)
216 _magic_header = magic_header;
219 void get_sender(uint8_t *ip, uint16_t &port)
221 memcpy(ip, &_remote_sender_addr.sin_addr.s_addr, 4);
222 port = ntohs(_remote_sender_addr.sin_port);