-
Notifications
You must be signed in to change notification settings - Fork 7
/
udpserver.cc
296 lines (266 loc) · 9.32 KB
/
udpserver.cc
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
/*
publish with BSD Licence.
Copyright (c) Terry Lao
*/
#include "udpserver.h"
#include "WinCat.h"
#include "options.h"
#include <io.h>
#include <fcntl.h>
#define DEBUG 1
int udpserver(char *port, char *filename, int keep_listening, char *restrictip, char *akey) {
WSADATA wsaData;
int iResult;
int err;
int singlecmd=0;
char msgbuf [256]; // for a message up to 255 bytes.
msgbuf [0] = '\0'; // Microsoft doesn't guarantee this on man page.
SOCKET ListenSocket = INVALID_SOCKET;
struct sockaddr_in serverAddr,clientAddr;
/* Initialize Winsock */
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0) {
fprintf(stderr, "WSAStartup failed with error: %d\n", iResult);
return 1;
}
//fprintf(stderr, "verbose value: %d\n", verbose);
if (verbose) {
printverbose();
fprintf(stderr, "restrictip from: %s\n", restrictip);
}
/* create UDP socket */
ListenSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (ListenSocket == INVALID_SOCKET) {
err = WSAGetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (verbose) {
printverbose();
fprintf(stderr, "Socket failed with error: %ld,%s\n", err,msgbuf);
}
WSACleanup();
return 1;
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_addr.s_addr = INADDR_ANY;
serverAddr.sin_port = htons( atoi(port) );
/* Setup the UDP listening socket */
iResult = bind( ListenSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
if (iResult == SOCKET_ERROR) {
if (verbose) {
printverbose();
fprintf(stderr, "Port %s is already in use.\n", port);
}
err = WSAGetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (verbose) {
printverbose();
fprintf(stderr, "Bind failed with error: %d,%s\n", err,msgbuf);
}
closesocket(ListenSocket);
WSACleanup();
return 1;
}
WinCat *wincat =new WinCat(filename);
if (akey!=NULL)
wincat->setOTP(akey);
if (filename!=NULL&&memcmp(filename,"cmd /c ",7)==0){
singlecmd=1;
fprintf(stderr, "Single CMD Mode\n");
}else{
fprintf(stderr, "UDPServer Mode\n");
}
wincat->setUdp(restrictip,clientAddr);
do {
if (singlecmd==1){
wincat->SyncProcess(ListenSocket);
}else{
wincat->Process(ListenSocket);
}
} while (keep_listening);
/* No longer need server socket */
closesocket(ListenSocket);
/* Shut down the connection since we're done */
iResult = shutdown(ListenSocket, SD_BOTH);
if (iResult == SOCKET_ERROR) {
err = WSAGetLastError ();
FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, // flags
NULL, // lpsource
err, // message id
MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // languageid
msgbuf, // output buffer
sizeof (msgbuf), // size of msgbuf, bytes
NULL); // va_list of arguments
if (verbose) {
printverbose();
fprintf(stderr, "Shutdown failed with error: %d,%s\n", err,msgbuf);
}
closesocket(ListenSocket);
WSACleanup();
return 1;
}
/* Cleanup */
delete wincat;
WSACleanup();
return 0;
}
int broadcastUDPReceiver(char *port, char *filename, int keep_listening, char *restrictip) {
WSADATA wsaData;
WSAStartup(MAKEWORD(2,2), &wsaData);
SOCKET sock;
sock = socket(AF_INET,SOCK_DGRAM,0);
char broadcast = '1';
// This option is needed on the socket in order to be able to receive broadcast messages
// If not set the receiver will not receive broadcast messages in the local network.
if(setsockopt(sock,SOL_SOCKET,SO_BROADCAST,&broadcast,sizeof(broadcast)) < 0)
{
fprintf(stderr,"Error in setting Broadcast option");
closesocket(sock);
return 0;
}
struct sockaddr_in Recv_addr;
struct sockaddr_in Sender_addr;
int len = sizeof(struct sockaddr_in);
char recvbuff[50];
int recvbufflen = 50;
char sendMSG[]= "Broadcast message from READER";
Recv_addr.sin_family = AF_INET;
Recv_addr.sin_port = htons(atoi(port));
Recv_addr.sin_addr.s_addr = INADDR_ANY;
if (bind(sock,(sockaddr*)&Recv_addr, sizeof (Recv_addr)) < 0)
{
fprintf(stderr,"Error in BINDING %d\n",WSAGetLastError());
//_getch();
closesocket(sock);
return 0;
}
recvfrom(sock,recvbuff,recvbufflen,0,(sockaddr *)&Sender_addr,&len);
fprintf(stderr,"\n\n\tReceived Message is : %s\n",recvbuff);
fprintf(stderr,"\n\n\tPress Any to send message");
//_getch();
if(sendto(sock,sendMSG,strlen(sendMSG)+1,0,(sockaddr *)&Sender_addr,sizeof(Sender_addr)) < 0)
{
fprintf(stderr,"Error in Sending %d\n.",WSAGetLastError());
fprintf(stderr,"\n\n\t\t Press any key to continue....");
//_getch();
closesocket(sock);
return 0;
}
else
fprintf(stderr,"\n\n\n\tREADER sends the broadcast message Successfully");
fprintf(stderr,"\n\n\tpress any key to CONT...");
//_getch();
closesocket(sock);
WSACleanup();
return 0;
}
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
#define MSGBUFSIZE 256
//http://www.steves-internet-guide.com/introduction-multicasting/
//https://www.drdobbs.com/web-development/internet-multicasting/184410294
int multicastUDPReceiver(int argc, char *argv[])
{
if (argc != 3) {
printf("Command line args should be multicast group and port\n");
printf("(e.g. for SSDP, `listener 239.255.255.250 1900`)\n");
return 1;
}
char* group = argv[1]; // e.g. 239.255.255.250 for SSDP
int port = atoi(argv[2]); // 0 if error, which is an invalid port
#ifdef _WIN32
//
// Initialize Windows Socket API with given VERSION.
//
WSADATA wsaData;
if (WSAStartup(0x0101, &wsaData)) {
perror("WSAStartup");
return 1;
}
#endif
// create what looks like an ordinary UDP socket
//
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
// allow multiple sockets to use the same PORT number
//
u_int yes = 1;
if (
setsockopt(
fd, SOL_SOCKET, SO_REUSEADDR, (char*) &yes, sizeof(yes)
) < 0
){
perror("Reusing ADDR failed");
return 1;
}
// set up destination address
//
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_ANY); // differs from sender
addr.sin_port = htons(port);
// bind to receive address
//
if (bind(fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}
// use setsockopt() to request that the kernel join a multicast group
//
struct ip_mreq mreq;
mreq.imr_multiaddr.s_addr = inet_addr(group);
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
if (
setsockopt(
fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &mreq, sizeof(mreq)
) < 0
){
perror("setsockopt");
return 1;
}
// now just enter a read-print loop
//
while (1) {
char msgbuf[MSGBUFSIZE];
int addrlen = sizeof(addr);
int nbytes = recvfrom(
fd,
msgbuf,
MSGBUFSIZE,
0,
(struct sockaddr *) &addr,
&addrlen
);
if (nbytes < 0) {
perror("recvfrom");
return 1;
}
msgbuf[nbytes] = '\0';
puts(msgbuf);
}
#ifdef _WIN32
//
// Program never actually gets here due to infinite loop that has to be
// canceled, but since people on the internet wind up using examples
// they find at random in their own code it's good to show what shutting
// down cleanly would look like.
//
WSACleanup();
#endif
return 0;
}