-
Notifications
You must be signed in to change notification settings - Fork 0
/
udp_server.c
146 lines (130 loc) · 2.88 KB
/
udp_server.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
/* Author - anuz
* This file is distibuted under GPLv2
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <semaphore.h>
#include <netinet/in.h>
#include <errno.h>
#include <sys/time.h>
#include "header.h"
extern int errno;
/* be careful between input sock and output sock */
void get_the_buffer(struct sock_s *s, struct sock_in *p)
{
char buf[BUFSIZ];
int addrlen = sizeof(p->pin);
pid_t pid;
memset(buf, 0, sizeof(buf));
if( recvfrom(s->sd , buf, sizeof(buf), 0, (struct sockaddr *)&p->pin, &addrlen) == -1 ) {
printf("recvfrom error");
}
strcpy(p->input, buf);
ap_debug("%s\n",buf);
if(strlen(buf))
log_stuff(buf, __LINE__);
switch(query_type(buf)) {
case REQ_QUESTION:
user_needs_a_question(s,p);
break;
case REQ_ANSWER:
if ((pid = fork()) == 0) {
ap_debug("child process = %d \n ",getpid());
user_wants_an_answer(s,p);
exit(EXIT_SUCCESS);
} else if (pid < 0) {
fprintf(stderr,"fork error %s\n", strerror(errno));
} else {
ap_debug("parent process = %d\n ",getpid());
}
break;
case SAVE_QUESTION:
user_provides_a_question(s,p);
break;
case SAVE_ANSWER:
user_provides_an_answer(s,p);
break;
default:
case -1:
fprintf(stderr, "What the fuck bro?\n");
exit(EXIT_FAILURE);
}
}
static void populate_sock(struct sock_s *s)
{
if ((s->sd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
s->port = port;
memset(&s->sin, 0, sizeof(s->sin));
s->sin.sin_family = AF_INET;
s->sin.sin_addr.s_addr = INADDR_ANY;
s->sin.sin_port = htons(s->port);
if (bind(s->sd, (struct sockaddr *)&s->sin, sizeof(s->sin)) == -1) {
perror("bind");
close(s->sd);
exit(EXIT_FAILURE);
}
}
int create_server()
{
struct sock_s s;
struct sock_in p;
int status;
populate_sock(&s);
while(1) {
get_the_buffer(&s, &p);
sleep(2);
waitpid(-1, &status, WNOHANG);
if(WIFEXITED(status))
printf("child exited normally\n");
}
}
void deamonify(void)
{
pid_t sid;
int fd;
sid = setsid(); /* get a new process group. */
chdir("/");
umask(0);
/*close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
fd = open("/dev/null", O_RDWR);*//* stdin */
//(void) dup(fd); /* stdout */
//(void) dup(fd); /* stderr */
ap_debug ("again process = %d\n ",getpid());
create_server();
}
int create_child()
{
pid_t pid;
if ((pid = fork()) == 0) {
ap_debug("child process = %d \n ",getpid());
deamonify();
} else if (pid < 0) {
fprintf(stderr,"fork error %s\n", strerror(errno));
} else {
ap_debug("parent process = %d\n ",getpid());
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
int main (int argc, char*argv[])
{
if(argc != 2) {
fprintf(stderr, "USAGE: ./udp_server port_number\n");
return -1;
}
if(argv[1])
port = atoi(argv[1]);
create_child();
return 0;
}