-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudpbroadcast.c
executable file
·133 lines (114 loc) · 2.86 KB
/
udpbroadcast.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
/* Inspired by code here: http://developerweb.net/viewtopic.php?pid=32260 */
#ifdef WIN32
# include <winsock2.h>
# include <io.h>
# define socklen_t int
# define sockopt_t char
#else
# include <netdb.h>
# include <netinet/in.h>
# include <arpa/inet.h>
# include <sys/time.h>
# include <sys/types.h>
# include <sys/socket.h>
# include <unistd.h>
# include <time.h>
# define sockopt_t int
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define PORT 2080
#include "udpbroadcast.h"
typedef struct speer {
SOCKET socket;
struct sockaddr_in addr;
} speer;
#define mkspeer(tpeer) ((speer *) tpeer)
int initialized = 0;
static void init(){
if (initialized)
return;
initialized = 1;
WSADATA WSAData;
if (WSAStartup (MAKEWORD(2,2), &WSAData) != 0)
{
printf("WSAStartup failed!\n");
exit(2);
}
}
log_fun the_log_fun = NULL;
void set_log_fun(log_fun fun)
{
the_log_fun = fun;
}
void ulog(const char* msg) {
if (the_log_fun != NULL) {
(*the_log_fun)(msg);
}
}
tpeer peer_create(int isServer)
{
init();
speer *p = (speer *)malloc(sizeof(speer));
sockopt_t broadcast=1;
if ((p->socket = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((setsockopt(p->socket, SOL_SOCKET, SO_BROADCAST,
&broadcast, sizeof broadcast)) == -1) {
perror("setsockopt - SO_SOCKET ");
exit(1);
}
printf("Socket created\n");
memset(&p->addr, 0, sizeof p->addr);
p->addr.sin_family = AF_INET;
p->addr.sin_port = htons(PORT);
p->addr.sin_addr.s_addr = INADDR_BROADCAST;
if (isServer) {
p->addr.sin_addr.s_addr = INADDR_ANY;
if (bind(p->socket, (struct sockaddr*)&p->addr,
sizeof p->addr) == -1) {
perror("bind");
char buf[32];
sprintf(buf, "bind %d", WSAGetLastError());
ulog(buf);
exit(1);
}
}
return p;
}
int peer_broadcast(tpeer peerIn, char* msg, unsigned int size)
{
speer* p = mkspeer(peerIn);
int val = sendto(p->socket, msg, size, 0,
(struct sockaddr *)&p->addr, sizeof p->addr);
if (val == -1) {
char buf[32];
sprintf(buf, "broadcast %d", WSAGetLastError());
ulog(buf);
}
return val;
}
void peer_destroy(tpeer p)
{
free((speer*) p);
}
/* timneout is milliseconds */
int peer_select(tpeer peerIn, int timeout) {
speer* p = mkspeer(peerIn);
int n;
fd_set set;
struct timeval timev = { 0, timeout*1000 };
FD_ZERO(&set);
FD_SET(p->socket, &set);
return select(p->socket+1, &set, NULL, NULL, &timev);
}
int peer_receive(tpeer peerIn, char* buf, int bufsize)
{
speer* p = mkspeer(peerIn);
socklen_t addr_len = sizeof p->addr;
return recvfrom(p->socket, buf, bufsize, 0,
(struct sockaddr *)&p->addr, &addr_len);
}