-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini-server-for-linux.c
197 lines (161 loc) · 4.51 KB
/
mini-server-for-linux.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
#include "common.h"
//获取文件大小
#include <sys/stat.h>
// #include <sys/inotify.h>
//服务列表配置文件路径
char * PATH = "./ssdp.service";
// static void sighandle(int sign){
// exit(1);
// }
void parseOptions(int argc, char *argv[]){
for (int i = 1; i < argc; ++i)
{
char * opt = argv[i];
if(strcmp("-h", opt) == 0 || strcmp("--help", opt) == 0) {
printf("Usage: \n");
printf(" %s [options]\n\n", argv[0]);
printf("OPTIONS:\n");
printf(" -c, --config : set service config file path\n\n");
printf(" -p, --port : set server listening port.\n\n");
printf(" -d, --debug : run with debug mode, this will print debug log.\n\n");
printf(" -h, --help : print help info.\n\n");
exit(0);
}
else if(strcmp("-p", opt) == 0 || strcmp("--port", opt) == 0){
if (argv[i+1] != NULL){
PORT = atoi(argv[i+1]);
if (PORT < 1025 || PORT > 65535)
{
printf("port must in range 1025~65535.\n");
exit(0);
}
}
}
else if(strcmp("-d", opt) == 0 || strcmp("--debug", opt) == 0){
DEBUG = TRUE;
}
else if(strcmp("-c", opt) == 0 || strcmp("--config", opt) == 0){
if (argv[i+1] != NULL){
PATH = argv[i+1];
}
}
}
}
/*
*获取文件信息中的大小
**/
int file_size(char * filename){
struct stat statbuf;
stat(filename, &statbuf);
return statbuf.st_size;
}
void readServiceConfigFile(char *service, int len){
if (len <= 0) return;
FILE *fp = fopen(PATH, "r");
if (fp == NULL) {
//not exist file
printf("not exist file.\n");
return;
}
fgets(service, len, fp);
if (DEBUG) printf("read config file content: %s\n", service);
// char buff[len];
// int index = 0;
// int count = 0;
// while(fgets(buff, len, fp) != NULL){
// int i = strlen(buff);
// printf("strlen %d\n", i);
// if (DEBUG) printf("%s", buff);
// if (index + i == len) i -= count;
// memcpy(&service[index], buff, i);
// index += i;
// count += 1;
// }
fclose(fp);
}
int main(int argc, char *argv[])
{
parseOptions(argc, argv);
if (DEBUG)
printf("server listen to port: %d & service config file path: %s\n", PORT, PATH);
int sock;
struct sockaddr_in own_addr, peer_addr;
char recv_msg[32] = {0};
socklen_t peer_addrlen = sizeof(struct sockaddr_in);
char peer_name[15] = {0};
int ret = 0;
// signal(SIGINT, sighandle);
// signal(SIGTERM, sighandle);
bzero(&own_addr, sizeof(struct sockaddr_in));
bzero(&peer_addr, sizeof(struct sockaddr_in));
own_addr.sin_family = AF_INET;
own_addr.sin_port = htons(PORT);
own_addr.sin_addr.s_addr = htonl(INADDR_ANY);
sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock == -1)
{
printf("create sock fail.\n");
exit(0);
}
ret = bind(sock, (struct sockaddr *) &own_addr, sizeof(own_addr));
if (ret == -1)
{
printf("bind addr fail.\n");
exit(0);
}
int size = file_size(PATH);
if (DEBUG)
printf("config file size: %d\n", size);
char services[size];
if (size > 0)
{
readServiceConfigFile(services, size);
}
while(TRUE) {
ret = recvfrom(sock, recv_msg, sizeof(recv_msg), 0, (struct sockaddr *) &peer_addr, &peer_addrlen);
if (ret < 0)
{
printf("\nreceived msg error.\n");
bzero(recv_msg, sizeof(recv_msg));
continue;
}
inet_ntop(AF_INET, &peer_addr.sin_addr.s_addr, peer_name, sizeof(peer_name));
if (DEBUG)
printf("\nreceived msg from [%s]: %s\n", peer_name, recv_msg);
if (strcmp(EXIT, recv_msg) == 0) {
printf("client [%s] exit.\n", peer_name);
bzero(recv_msg, sizeof(recv_msg));
continue;
}
if(!verifyReceivedMsg(recv_msg) && DEBUG)
{
printf("verify received msg not passed. [%s]\n", recv_msg);
send_msg(sock, recv_msg, strlen(recv_msg), (struct sockaddr *)&peer_addr, peer_addrlen);
bzero(recv_msg, sizeof(recv_msg));
continue;
}
//parse received msg
if (strcmp(DISCOVER, recv_msg) != 0)
{
send_msg(sock, recv_msg, strlen(recv_msg), (struct sockaddr *)&peer_addr, peer_addrlen);
bzero(recv_msg, sizeof(recv_msg));
continue;
}
if (size > 0)
{
bzero(recv_msg, sizeof(recv_msg));
memcpy(&recv_msg[0], "$resp?", strlen("$resp?"));
memcpy(&recv_msg[strlen("$resp?")], services, strlen(services));
memcpy(&recv_msg[strlen(recv_msg)], "$", 1);
send_msg(sock, recv_msg, strlen(recv_msg), (struct sockaddr *)&peer_addr, peer_addrlen);
bzero(recv_msg, sizeof(recv_msg));
} else {
send_msg(sock, RESP, strlen(RESP), (struct sockaddr *)&peer_addr, peer_addrlen);
}
bzero(recv_msg, sizeof(recv_msg));
}
send_msg(sock, EXIT, strlen(EXIT), (struct sockaddr *)&peer_addr, peer_addrlen);
close(sock);
printf("Done & Exit\n");
return 0;
}