-
Notifications
You must be signed in to change notification settings - Fork 0
/
mehl.c
195 lines (160 loc) · 3.76 KB
/
mehl.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
/*
* =====================================================================================
*
* Filename: mehl.c
*
* Description: This is the incredible 'mehl'-Server
*
* Version: 1.0
* Created: 01.02.2013 18:38:41
* Revision: none
* Compiler: gcc
*
* Author: Adrian Vondendriesch (), discostu@zoozer.de
* Company: brainmode
*
* =====================================================================================
*/
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "parser.h"
#include <pthread.h>
#include <signal.h>
#define PORT 6363
#define MAX_QUEUE 1
#define BUF_SIZE 1024
#define MKVAL(e, t); e->val=malloc(sizeof(t));
/* Begin: Global VARS */
element_t *list;
int sock_listen;
/* End: Global VARS */
void *mehl_thread(void *s)
{
int *sock = (int*)s;
int rec_value = 0;
/*send_value = 0;*/
char tmp[BUF_SIZE];
char buf[BUF_SIZE];
strcpy(buf, "Welcome to the official Mehl-Server\n");
/*send_value = write(*sock, buf, strlen(buf));*/;
strcpy(buf, "");
while(0 < (rec_value=read(*sock, tmp, BUF_SIZE)))
{
tmp[rec_value] = '\0';
if(tmp[rec_value-2] == 0x0d && tmp[rec_value-1] == 0x0a)
{
if(rec_value > 2)
snprintf(buf, rec_value-1, tmp);
printf("%d Bytes received\n", rec_value);
if(strcmp(buf, "quit") == 0 || strcmp(buf, "q") == 0)
{
strcpy(buf, "Closing connection...\n");
write(*sock, buf, strlen(buf));
close(*sock);
free(sock);
}
else
{
sprintf(buf, "%s\n", parse(buf));
printf("Parser: %s", buf);
write(*sock, buf, strlen(buf));
}
memset(buf, 0, sizeof(BUF_SIZE));
}
else
{
strncat(buf, tmp, rec_value);
}
}
return 0;
}
/**
* Catches the SIGINT interrupt and joins all threads.
*
* @param: int signr - signal number
* @return: void
*/
void exithandler(int signr)
{
element_t *cur = list;
printf("close all connection and kill threads ...\n");
while(0 != cur->next)
{
pthread_join( ((pthread_t) cur->val), 0 );
free(cur->val);
}
printf("All threads where terminated\n");
close(sock_listen);
}
/**
* Main server entry
*/
int main(int argc, char **argv)
{
int *sock_tmp,
optval = 1;
struct sockaddr_in server = {0};
element_t *tmp_elem;
struct sigaction sig_action;
list = list_create_element();
memset(&sig_action, '\0', sizeof(sig_action));
sig_action.sa_handler = &exithandler;
sigemptyset(&sig_action.sa_mask);
sig_action.sa_flags = 0;
sigaction(SIGINT, &sig_action, NULL);
printf("%s", "try to open a socket\n");
sock_listen = socket(AF_INET, SOCK_STREAM, 0);
if(sock_listen < 0)
{
perror("can't open socket");
exit(1);
}
else
{
printf("%s", "socket is open\n");
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(PORT);
setsockopt(sock_listen, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if(bind(sock_listen, (struct sockaddr *) &server, sizeof(struct sockaddr_in)))
{
perror("bind socket to server_addr");
exit(1);
}
printf("%s", "listen\n");
listen(sock_listen, MAX_QUEUE);
while(1)
{
printf("%s", "waiting ...\n");
sock_tmp = malloc(sizeof(int));
*sock_tmp = accept(sock_listen, 0, 0);
if(*sock_tmp < 0)
{
perror("accept");
exit(1);
}
else
{
tmp_elem = list_create_element();
MKVAL(tmp_elem, pthread_t);
pthread_create( ((pthread_t*) tmp_elem->val), NULL, mehl_thread, sock_tmp);
}
}
/* will never be reached */
tmp_elem = list;
while(0 != tmp_elem->next)
{
pthread_join( *((pthread_t*) tmp_elem->val), 0 );
tmp_elem = tmp_elem->next;
}
close(sock_listen);
return 0;
}