-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathUDPPingerServer.c
128 lines (117 loc) · 3.23 KB
/
UDPPingerServer.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
ssize_t YHLog(int line, const char *fun, const char *format, ...)
{
va_list ap;
va_start(ap, format);
char buf[512];
ssize_t len = vsnprintf(buf, sizeof(buf), format, ap);
buf[len] = 0;
len = fprintf(stderr, "[%s:%s:%d] %s\n", __FILE__, fun, line, buf);
va_end(ap);
return len;
}
ssize_t YHLog_err(int line, const char *fun, const char *format, ...)
{
va_list ap;
va_start(ap, format);
char buf[512];
ssize_t len = vsnprintf(buf, sizeof(buf), format, ap);
buf[len] = 0;
len = fprintf(stderr, "[%s:%s:%d] %s: %s\n", __FILE__, fun, line, buf, strerror(errno));
va_end(ap);
return len;
}
#define LOG(_format_, ...) YHLog(__LINE__, __FUNCTION__, _format_, ##__VA_ARGS__)
#define ERRLOG(_format_, ...) YHLog_err(__LINE__, __FUNCTION__, _format_, ##__VA_ARGS__)
int start_service(unsigned short port)
{
int retcode = 0;
struct sockaddr_in svraddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(port);
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
int fd;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
ERRLOG("socket error");
retcode = -1;
goto err;
}
if (bind(fd, (struct sockaddr *)&svraddr, sizeof(svraddr)) < 0)
{
ERRLOG("bind error");
retcode = -2;
goto err;
}
return fd;
err:
return retcode;
}
#define PING_MAGIC 0x1234567887654321UL
struct ping
{
unsigned long magic;
unsigned int seq;
unsigned long ping_timestamp;
};
void print_all_bytes(void *buf, size_t buf_len)
{
char *p = (char *)buf;
for (int i = 0; i < buf_len; i++)
LOG("%x", p[i]);
}
int main(int argc, char const *argv[])
{
if (argc != 2)
{
LOG("usage : %s <#port>", argv[0]);
exit(1);
}
unsigned short port = atoi(argv[1]);
int fd;
if ((fd = (start_service(port))) < 0)
{
LOG("start_service error");
exit(1);
}
struct sockaddr_in cliaddr;
socklen_t len = sizeof(cliaddr);
struct ping recvping;
char ipbuf[128];
while (1)
{
int nbytes = 0;
if ((nbytes = recvfrom(fd, &recvping, sizeof(recvping), 0, (struct sockaddr *)&cliaddr, &len)) < 0)
{
ERRLOG("recvfrom error");
break;
}
const char *ip = inet_ntop(AF_INET, &cliaddr.sin_addr, ipbuf, sizeof(ipbuf));
unsigned short port = htons(cliaddr.sin_port);
LOG("receive data from [%s:%d] total %d bytes", ip, port, nbytes);
if (recvping.magic != PING_MAGIC && !(recvping.seq >= 1 && recvping.seq <= 10))
{
LOG("error ping %d bytes pack from [%s:%d]! discard it\n", nbytes, ip, port);
print_all_bytes(&recvping, nbytes);
continue;
}
if (sendto(fd, &recvping, sizeof(recvping), 0, (struct sockaddr *)&cliaddr, len) < 0)
{
ERRLOG("sendto error");
break;
}
LOG("send ping_%d back to [%s:%d] succ!", recvping.seq, ip, port);
}
close(fd);
return 0;
}