File examples/ntime.c changed (mode: 100644) (index 904765f..f00f0e9) |
5 |
5 |
|
|
6 |
6 |
#define _GNU_SOURCE |
#define _GNU_SOURCE |
7 |
7 |
|
|
8 |
|
#include <errno.h> |
|
9 |
|
#include <string.h> |
|
10 |
|
#include <strings.h> |
|
11 |
|
#include <stdlib.h> |
|
12 |
|
#include <unistd.h> |
|
13 |
|
#include <sys/poll.h> |
|
14 |
|
#include <sys/types.h> |
|
15 |
|
#include <sys/socket.h> |
|
16 |
|
#include <stdio.h> |
|
17 |
|
#include <fcntl.h> |
|
18 |
|
#include <netinet/in.h> |
|
19 |
|
#include <sys/time.h> |
|
20 |
|
#include <sys/ioctl.h> |
|
21 |
|
#include <time.h> |
|
22 |
|
#include <arpa/inet.h> |
|
23 |
|
#include <stdarg.h> |
|
24 |
|
|
|
25 |
8 |
#include <Conn.h> |
#include <Conn.h> |
26 |
9 |
|
|
27 |
10 |
/* Global variables */ |
/* Global variables */ |
28 |
|
static unsigned short debug = 20; |
|
|
11 |
|
static unsigned short debug = 20; |
|
12 |
|
|
|
13 |
|
static FILE *Logf = NULL; |
|
14 |
|
static char *log_file = "ntime.log"; |
|
15 |
|
static int port = 9000; |
|
16 |
|
static short ipv4 = 1, ipv6 = 1; |
|
17 |
|
static unsigned long long errors; |
29 |
18 |
|
|
30 |
|
static FILE *Logf = NULL; |
|
31 |
|
static char *log_file = "ntime.log"; |
|
32 |
|
static int port = 9000; |
|
33 |
|
static short ipv4 = 1, ipv6 = 1; |
|
34 |
19 |
|
|
35 |
20 |
static void s_accept_error(struct Conn *C) |
static void s_accept_error(struct Conn *C) |
36 |
21 |
{ |
{ |
37 |
|
Log(0, "%s: Cannot accept a new connection!\n"); |
|
|
22 |
|
Log(0, "%s: Cannot accept a new connection on id %llu!\n", |
|
23 |
|
Conn_getid(C)); |
|
24 |
|
errors++; |
38 |
25 |
} |
} |
39 |
26 |
|
|
40 |
27 |
static void s_accept(struct Conn *C) |
static void s_accept(struct Conn *C) |
|
... |
... |
static void s_accept(struct Conn *C) |
50 |
37 |
|
|
51 |
38 |
int main(void) |
int main(void) |
52 |
39 |
{ |
{ |
53 |
|
struct Conn *I4 = NULL, *I6 = NULL; |
|
|
40 |
|
struct Conn *C; |
54 |
41 |
int ret; |
int ret; |
55 |
42 |
|
|
56 |
43 |
Logf = fopen(log_file, "w"); |
Logf = fopen(log_file, "w"); |
|
... |
... |
int main(void) |
78 |
65 |
} |
} |
79 |
66 |
|
|
80 |
67 |
if (ipv4 == 1) { |
if (ipv4 == 1) { |
81 |
|
Log(9, "Try to register IPv4 socket...\n"); |
|
82 |
|
I4 = Conn_socket(PF_INET, SOCK_STREAM, port); |
|
83 |
|
if (!I4) |
|
84 |
|
Log(1, "Cannot bind on ipv4 socket [%s].\n", Conn_strerror()); |
|
|
68 |
|
printf("Trying to register IPv4 socket...\n"); |
|
69 |
|
C = Conn_alloc(); |
|
70 |
|
if (!C) { |
|
71 |
|
printf("Cannot alloc socket [%s].\n", |
|
72 |
|
Conn_strerror()); |
|
73 |
|
errors++; |
|
74 |
|
} else { |
|
75 |
|
Conn_set_socket_domain(C, PF_INET); |
|
76 |
|
Conn_set_socket_type(C, SOCK_STREAM); |
|
77 |
|
Conn_set_socket_bind_port(C, port); |
|
78 |
|
ret = Conn_commit(C); |
|
79 |
|
if (ret != 0) { |
|
80 |
|
printf("Cannot bind on ipv4 socket [%s].\n", |
|
81 |
|
Conn_strerror()); |
|
82 |
|
errors++; |
|
83 |
|
} |
|
84 |
|
} |
85 |
85 |
} |
} |
86 |
86 |
|
|
87 |
87 |
if (ipv6 == 1) { |
if (ipv6 == 1) { |
88 |
|
Log(9, "Try to register IPv6 socket...\n"); |
|
89 |
|
I6 = Conn_socket(PF_INET6, SOCK_STREAM, port); |
|
90 |
|
if (!I6) |
|
91 |
|
Log(1, "Cannot bind on ipv6 socket [%s].\n", Conn_strerror()); |
|
92 |
|
} |
|
93 |
|
|
|
94 |
|
if ((I6 == NULL) && (I4 == NULL)) { |
|
95 |
|
Log(0, "Cannot bind!\n"); |
|
96 |
|
return 1; |
|
|
88 |
|
printf("Trying to register IPv6 socket...\n"); |
|
89 |
|
C = Conn_alloc(); |
|
90 |
|
if (!C) { |
|
91 |
|
printf("Cannot alloc socket [%s].\n", |
|
92 |
|
Conn_strerror()); |
|
93 |
|
errors++; |
|
94 |
|
} else { |
|
95 |
|
Conn_set_socket_domain(C, PF_INET6); |
|
96 |
|
Conn_set_socket_type(C, SOCK_STREAM); |
|
97 |
|
Conn_set_socket_bind_port(C, port); |
|
98 |
|
ret = Conn_commit(C); |
|
99 |
|
if (ret != 0) { |
|
100 |
|
printf("Cannot bind on ipv6 socket [%s].\n", |
|
101 |
|
Conn_strerror()); |
|
102 |
|
errors++; |
|
103 |
|
} |
|
104 |
|
} |
97 |
105 |
} |
} |
98 |
106 |
|
|
99 |
107 |
Conn_accept_cb = s_accept; |
Conn_accept_cb = s_accept; |
|
... |
... |
int main(void) |
104 |
112 |
if (ret == -1) { |
if (ret == -1) { |
105 |
113 |
Log(0, "Error calling Conn_poll [%s]!\n", Conn_strerror()); |
Log(0, "Error calling Conn_poll [%s]!\n", Conn_strerror()); |
106 |
114 |
return 1; |
return 1; |
107 |
|
} |
|
|
115 |
|
} else if (ret == 0) |
|
116 |
|
break; |
108 |
117 |
} |
} |
109 |
118 |
|
|
110 |
|
Log(0, "Finish!\n\n"); |
|
|
119 |
|
Log(0, "Finish. Errors=%llu!\n\n", |
|
120 |
|
errors); |
111 |
121 |
|
|
112 |
122 |
return 0; |
return 0; |
113 |
123 |
} |
} |