-
Notifications
You must be signed in to change notification settings - Fork 7
/
myudp-tx.c
executable file
·479 lines (322 loc) · 8.2 KB
/
myudp-tx.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>
//#include <curses.h>
#include <unistd.h>
#include <aio.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#define PORT 0xBAC0
#define MAXLINE 1500
#define LOG_FILE "bacnet0.log"
#define DIALOG_WIDTH 40
#define DIALOG_HEIGHT 20
#define CONTENT_WIDTH (DIALOG_WIDTH - 2)
uint8_t bqr_whois[]={
0x81,0x0a,0x00,0x0c,0x01,0x20,
0xff,0xff,0x00,0xff,0x10,0x08
};
uint8_t brp_im[]={
0x81,0x0b,0x00,0x19,0x01,0x20,0xff,0xff,0x00,0xff,0x10,0x00,0xc4,0x02,0x00,0x03,
0xf1,0x22,0x04,0x00,0x91,0x03,0x22,0x01,0x04
};
extern int32_t make_internet_address(int8_t * hostname,int32_t port,struct sockaddr_in *addrp);
int sockfd;
char hname[128]={0};
uint8_t buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
struct hostent *hent=NULL;
// a simple log function, works similar to printf
void bacnet0_log(const char *format, ... )
{
va_list args; // argument list
static FILE *logfile = NULL; // file pointer to the logfile
char *fformat; // the modified format of the string which will be written to the logfile
int length; // length of the format
if(!(format == NULL && logfile == NULL)) {
// open the logfile if not already opened
if(logfile == NULL) {
logfile = fopen(LOG_FILE, "w");
// if that doesn't work exit with an error message
if(logfile == NULL) {
fprintf(stderr, "Cannot open logfile %s\n", LOG_FILE);
exit(EXIT_FAILURE);
}
}
// if NULL is given as format, close the opened file
if(format != NULL) {
// increase length by 2 (for \n\0
length = strlen(format) + 2;
// allocate memory
fformat = malloc(sizeof(char) * length);
// copy the format over
strncpy(fformat, format, length - 2);
// append \n\0
//fformat[length - 2] = '\n';
fformat[length - 2] = '\0';
fformat[length - 1] = '\0';
// get the rest of the arguments
va_start(args, format);
// use vfprintf() to
vfprintf(logfile, fformat, args);
// forces the logmessage to be written into the file right now
fflush(logfile);
va_end(args);
// free the allocated memory for the format string
free(fformat);
}
else
{
// close the logfile
fclose(logfile);
}
}
}
#if defined(UBUNTU)
void enable_kbd_signals(void)
{
int fd_flags;
fcntl(0,F_SETOWN,getpid());
fd_flags = fcntl(0,F_GETFL);
fcntl(0,F_SETFL,(fd_flags|O_ASYNC));
//fcntl(0,F_SETFL,(fd_flags|O_RSYNC));
}
#endif
#if 0
struct aiocb kbcbuf;
void setup_aio_buffer(void)
{
static char input[1];
kbcbuf.aio_fildes = 0;
kbcbuf.aio_buf = input;
kbcbuf.aio_nbytes = 1;
kbcbuf.aio_offset = 0;
kbcbuf.aio_sigevent.sigev_notify = SIGEV_SIGNAL;
kbcbuf.aio_sigevent.sigev_signo = SIGIO;
}
#endif
int8_t cmd_str[1024];
#if defined(UBUNTU)
void on_input(int signum)
{
fgets(cmd_str,128,stdin);
printf("cmd_str is %s\n",cmd_str);
if(strcmp(cmd_str,"quit\n") == 0)
{
exit(EXIT_SUCCESS);
}
if(strcmp(cmd_str,"whois\n") ==0)
{
make_internet_address("192.168.0.255",PORT,&cliaddr);
if(sendto(sockfd,bqr_whois,sizeof(bqr_whois),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
}
memset(cmd_str,0,sizeof(cmd_str));
}
#endif
#if 0
void on_input(int signum)
{
int c;
char *cp = (char*)kbcbuf.aio_buf;/*cast to char*/
if(aio_error(&kbcbuf) != 0)
{
perror("reading failed");
}
else
{
if(aio_return(&kbcbuf) == 1)
{
c=*cp;
if(c == 'q')
{
printf("that is it\n");
exit(EXIT_SUCCESS);
}
}
aio_read(&kbcbuf);
}
}
#endif
int32_t make_internet_address(int8_t * hostname,int32_t port,struct sockaddr_in *addrp)
{
struct hostent *hp;
bzero((void*)addrp,sizeof(struct sockaddr_in));
hp = gethostbyname(hostname);
if(hp == NULL)
return -1;
bcopy((void*)hp->h_addr,(void*)&addrp->sin_addr,hp->h_length);
addrp->sin_port = htons(port);
addrp->sin_family= AF_INET;
return 0;
}
int main(int argc, char* argv[])
{
#if 0
int sockfd;
char hname[128]={0};
uint8_t buffer[MAXLINE];
char *hello = "Hello from server";
struct sockaddr_in servaddr, cliaddr;
struct hostent *hent=NULL;
#endif
//gethostname(hname,sizeof(hname));
//hent = gethostbyname(hname);
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
int optval =1;
setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST|SO_REUSEADDR,&optval,sizeof(int));
#if 0
struct timeval timeout;
timeout.tv_sec = 6;
timeout.tv_usec = 0;
setsockopt(sockfd,SOL_SOCKET,SO_RCVTIMEO,&timeout,sizeof(timeout));
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
//servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
//servaddr.sin_port = PORT;
// Bind the socket with the server address
if ( bind(sockfd, (const struct sockaddr *)&servaddr,
sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
len = sizeof(cliaddr); //len is value/resuslt
#endif
#if defined(UBUNTU)
void on_input(int);
signal(SIGIO,on_input);
enable_kbd_signals();
#endif
// setup_aio_buffer();
// aio_read(&kbcbuf);
memset(buffer,0,sizeof(buffer));
#if 0
//make_internet_address("192.168.0.104",PORT,&cliaddr);
make_internet_address("192.168.0.255",PORT,&cliaddr);
if(sendto(sockfd,bqr_whois,sizeof(bqr_whois),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
#endif
#if 0
if(argc > 1)
{
printf("argc is %d\n",argc);
if(strcmp(argv[1],"whois") == 0)
{
make_internet_address("192.168.0.255",PORT,&cliaddr);
if(sendto(sockfd,bqr_whois,sizeof(bqr_whois),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
{
perror("udp send failed\n");
}
}
}
#endif
#if 0
int fds[2];
int ifd=0;
if(pipe(fds) == -1)
{
perror("create pipe failed\n");
exit(EXIT_SUCCESS);
}
//ifd = dup(fds[1]);
close(0);
ifd = dup(fds[0]);
close(fds[0]);
if(ifd !=0)
{
perror("dup failed\n");
exit(EXIT_SUCCESS);
}
#endif
int ffd;
ffd = open("./ufifo",O_RDONLY);
if(ffd == -1)
{
perror("open fifo failed\n");
exit(EXIT_FAILURE);
}
while(1)
{
//pause();
#if 1
// printf("waiting on port %d\n",PORT);
//n = recvfrom(sockfd, (char *)buffer, MAXLINE, 0, ( struct sockaddr *) &cliaddr, &len);
// printf("recvfrom timeout\n");
// printf("buffer is %s\n",buffer);
//buffer[n] = '\0';
// printf("Client : %s\n", buffer);
//
#endif
#if 0
FILE * fp =NULL;
fp = fdopen(ifd,"r");
if(fp ==NULL)
{
perror("fdopen failed\n");
exit(EXIT_SUCCESS);
}
#endif
read(ffd,buffer,sizeof(buffer));
// fgets(buffer,sizeof(buffer),stdin);
//read(ifd,buffer,sizeof(buffer));
//fgets(buffer,sizeof(buffer),fp);
//if(sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
//if(sendto(sockfd,brp_im,sizeof(brp_im),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
if(strcmp(buffer,"whois\n") == 0)
{
printf("got whois\n");
//make_internet_address("192.168.43.255",PORT,&cliaddr);
//make_internet_address("10.78.146.1",PORT,&cliaddr);
//make_internet_address("192.168.0.104",PORT,&cliaddr);
make_internet_address("192.168.0.255",PORT,&cliaddr);
//make_internet_address("127.0.0.1",PORT,&cliaddr);
char* test_udp_str = "hello UDP\n";
uint32_t stn=0;
if((stn = sendto(sockfd,test_udp_str,strlen(test_udp_str),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr))) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
printf("udp send ok: %d\n",stn);
}
if(strcmp(buffer,"quit\n") == 0)
{
char *qt = "tx-quit\n";
if(sendto(sockfd,qt,strlen(qt),0,(struct sockaddr*)&cliaddr,sizeof(cliaddr)) == -1)
{
perror("udp send failed\n");
exit(EXIT_FAILURE);
}
printf("udp send ok\n");
printf("good-bye\n");
exit(EXIT_SUCCESS);
}
memset(buffer,0,sizeof(buffer));
// sleep(1);
}
}