-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
232 lines (182 loc) · 6.69 KB
/
main.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
/*
* File: main.c
* Author: gGediminas Jančys
*
* Created on July 19, 2017, 11:06 PM
*/
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
#include <sys/types.h> // setsockopt,
#include <sys/socket.h> // setsockopt,
#include <netdb.h>
#include <arpa/inet.h> // inet_ntop,
#include <string.h> // sterror
#include <errno.h> // errno
#define CONFIG_FILE "server.conf"
#define TRUE 1
#define PENDDING_CONECTIONS 15
/*
*
*/
int main(int argc, char** argv) {
const char* ip;
const char* port;
config_t *config = malloc(sizeof(config_t));
// Inicijuoju nustatymu nuskaitymo objekta
config_init( config );
// Nuskaitau nustatymu faila
if(! config_read_file(config, CONFIG_FILE))
{
fprintf(stderr, "%s:%d - %s\n", config_error_file(config),
config_error_line(config), config_error_text(config));
config_destroy(config);
return(EXIT_FAILURE);
}
// Nuskaitau IP reiksme
if(!config_lookup_string(config, "IP", &ip)){
fprintf(stderr, "No 'IP' setting in configuration file.\n");
free(config);
return (EXIT_FAILURE);
}
port = malloc(sizeof(int));
// Nuskaitau PORT reiksme
if(!config_lookup_string(config, "PORT", &port)){
fprintf(stderr, "No 'PORT' setting in configuration file.\n");
free(config);
free((char*)port);
free((char *)ip);
return (EXIT_FAILURE);
}
// Daugiau nereikes config kintamojo, atlaisvinu
free(config);
printf("Server settings: %s:%s\n", ip, port);
// Sisteminiu funkciju rezultatu kintamasis
int* rezult = malloc(sizeof(int));
/* ===== Adreso duomenu paieska ===== */
// Ieskau adreso informacijos kuriuo turiu klausytis
struct addrinfo *hints = malloc(sizeof(struct addrinfo)), *res;
// Nustatau kokio tipo socketo noriu
hints->ai_family = AF_UNSPEC; // Nesvarbu IPv4 ar IPv6
hints->ai_socktype = SOCK_STREAM; // Noriu TCP socketo
hints->ai_flags = AI_PASSIVE; // Uzpildo likusius duomensi pagal norima IP
// Bandau gauti norimo adreso informacija
if( (*rezult = getaddrinfo(ip, port, hints, &res)) != 0 ){
// Jei napavyko gauti adreso inforamcijos
fprintf(stderr, "[getaddrinfo] Unable to get %s:%s information. "
"Error: %s\n", ip, port, gai_strerror(*rezult));
free(rezult);
free((char *)ip);
free((char *)port);
free(hints);
free(res);
exit(EXIT_FAILURE);
}
// Pasalinu nereikalingus kintamuosius
free((char *)ip);
free((char *)port);
freeaddrinfo(hints);
/* ===== END Adreso duomenu paieska ===== */
/* ===== Socket kurimas ===== */
// Serverio failu deskriptoriaus kintamasis
int* serverSocket = malloc(sizeof(int));
*serverSocket = -1;
int* option = malloc(sizeof(int));
*option = 1;
// Begu per galimus variantus. Bandau kurti socketa
for(struct addrinfo * p = res;p != NULL; p = p->ai_next) {
// Bandau kurti socketa
if ((*serverSocket = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("[Socket] Unable create socket");
// Nepavyko, bandysiu kitus nustatymus
continue;
}
/* Nustatau socket opcija, kad leistu bindinti jei jau uzbindintas. Del
* nuluzinejimo ir apejimo klaidos kad adresas jau nuadojamas */
if (setsockopt(*serverSocket, SOL_SOCKET, SO_REUSEADDR, option, sizeof(option)) == -1) {
perror("[Setsockopt] Unable to set socket reuse");
// Nepasiseke, baigiu programa
free(rezult);
freeaddrinfo(res); // free the linked list
free(res);
free(p);
free(serverSocket);
exit(EXIT_FAILURE);
}
/* Bandau bindinti nurodyta porta ir adresa prie socketo */
if (bind(*serverSocket, p->ai_addr, p->ai_addrlen) == -1) {
close(*serverSocket);
perror("[Bind] Unable to bind");
continue;
}
break;
}
free(option);
/* Atlaisvinu rezultatu strukturas */
freeaddrinfo(res);
/* Bandau klausytis nurodytu adresu */
if (listen(*serverSocket, PENDDING_CONECTIONS) == -1) {
perror("[Listen] Unable to listen for connections");
free(serverSocket);
exit(EXIT_FAILURE);
}
printf("Waitting for clients to conenct");
/* ===== Main loop ===== */
fd_set* visiSkaitomiSocket, *skaitomiSocket;
// Nustatau i nulius
FD_ZERO(visiSkaitomiSocket);
FD_ZERO(skaitomiSocket);
// Pridedu serverio socketa prei skaitomu saraso
FD_SET(*serverSocket, visiSkaitomiSocket);
// Kintamasis nurodantis kas kiek laiko tikrinti ar yra skaitomu socketu
struct timeval* time;
time = malloc(sizeof(struct timeval));
// Tikrinsiu kas 10 ms
time->tv_sec = 0; // sekundes
time->tv_usec = 10000; // mikso sekundes
// Maksimalus socket numeris
int* maxSocket;
// Inicizacija
maxSocket = malloc(sizeof(int));
*maxSocket = -1;
// Kliento adresas
struct sockaddr_storage* clientAddr = malloc(sizeof(struct sockaddr_storage));
struct socklen_t* addressLeng = malloc(sizeof(socklen_t));
addressLeng = sizeof(struct sockaddr_storage);
int* clientFD = malloc(sizeof(int));
while(TRUE) {
// Nustatau kurius deskriptroius tikrinsiu
*skaitomiSocket = *visiSkaitomiSocket;
// Gaunu visus socketus, kurie turi ka nors nuskaityti
if (select(maxSocket + 1, skaitomiSocket, NULL, NULL, time)
== -1) {
fprintf("[Select] %s", strerror(errno));
}
addressLeng = sizeof &clientAddr;
clientFD = accept(serverSocket, (struct sockaddr *)clientAddr, addressLeng);
}
/* ===== END Main loop ===== */
// Baigesi nustatyami, tikrinu ar pavyko sukurti socketa
/*
// Kuriu skaitomu socketu sarasa ir einamu metu skaitomu socketu sarasa
fd_set* allSockets, *readSockets;
// Inicijuoju visų socketų sąrašą
allSockets = malloc(sizeof(fd_set));
FD_ZERO(allSockets);
// Inicijuoju einamu momentu skaitomu socketu sarasa
readSockets = malloc(sizeof(fd_set));
FD_ZERO(readSockets);
// Iteratorius
int* iterator;
iterator = malloc(sizeof(int));
*iterator = 0;
* */
// free(readSockets);
// free(maxSocket);
// free(iterator);
// free(time);
free(rezult);
//free(res);
return (EXIT_SUCCESS);
}