-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
108 lines (90 loc) · 2.12 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/* configuration header */
#include "config.h"
/* tcp headers */
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
typedef struct{
char portnum[6];
int used;
}port_t;
int init_ports(port_t* ports, char* start_port, int num)
{
int i;
ports = (port_t*) malloc(num * sizeof(port_t));
if(ports == NULL)
{
perror("Port init malloc error");
return 1;
}
int start = strtoimax(start_port, NULL, 10);
printf("ports used:\n");
for(i=0; i<num; i++)
{
sprintf(ports[i].portnum, "%d", start+i);
ports[i].used = 0;
#ifdef DEBUG
printf("%s\n", ports[i].portnum);
#endif
}
return 0;
}
int main(int argc, char** argv)
{
/*********************** TCP server ******************/
struct addrinfo hints;
struct addrinfo* res;
int err;
struct sockaddr_in6 address;
socklen_t addrlen;
char ips[NI_MAXHOST];
char servs[NI_MAXSERV];
int ssock, csock;
char buf[256];
int len;
int reuse;
/****************************************************/
config_parameter_t server_parameters;
memset(&server_parameters, 0, sizeof(server_parameters));
port_t port_list;
int sock;
/************************TCP ***************************/
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET6;
hints.ai_socktype = SOCK_STREAM;
/***********************************************************/
configure_server(&server_parameters);
#ifdef DEBUG
printf("%d\n", server_parameters.localhost);
printf("%s\n", server_parameters.port);
printf("%d\n", server_parameters.max_conn);
printf("%d\n", server_parameters.ss_php);
printf("%d\n", server_parameters.log);
#endif
if(init_ports(&port_list, server_parameters.port, server_parameters.max_conn))
{
return 1;
}
/* localhost is set */
if(server_parameters.localhost)
{
address.sin6_family = AF_INET6;
}
/* we need address info */
else
{
printf("getaddr\n");
}
sock = socket(PF_INET, SOCK_STREAM, 0);
return 0;
}