File examples/s_bind.c deleted (index f3fede1..0000000) |
1 |
|
/* |
|
2 |
|
* Copyright: Catalin(ux) M. BOIE |
|
3 |
|
* Shows how to bind to a specific address. |
|
4 |
|
*/ |
|
5 |
|
|
|
6 |
|
#define _GNU_SOURCE |
|
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 |
|
#include <Conn.h> |
|
26 |
|
|
|
27 |
|
/* Global variables */ |
|
28 |
|
static unsigned short debug = 20; |
|
29 |
|
|
|
30 |
|
static FILE *Logf = NULL; |
|
31 |
|
static char *log_file = "s_bind.log"; |
|
32 |
|
static int port = 9000; |
|
33 |
|
static short ipv4 = 1, ipv6 = 1; |
|
34 |
|
static int maxconn = 0; |
|
35 |
|
|
|
36 |
|
static void s_accept_error(struct Conn *C) |
|
37 |
|
{ |
|
38 |
|
Log(0, "%s: Cannot accept a new connection!\n"); |
|
39 |
|
} |
|
40 |
|
|
|
41 |
|
static void s_accept(struct Conn *C) |
|
42 |
|
{ |
|
43 |
|
char *s; |
|
44 |
|
|
|
45 |
|
Log(4, "%s (A connection was accepted from [%s/%d]. Enqueue greeting...\n", |
|
46 |
|
__FUNCTION__, C->addr, C->port); |
|
47 |
|
|
|
48 |
|
s = "Hello!\r\n"; |
|
49 |
|
Conn_enqueue(C, s, strlen(s)); |
|
50 |
|
} |
|
51 |
|
|
|
52 |
|
static void s_close(struct Conn *C) |
|
53 |
|
{ |
|
54 |
|
Log(5, "%s (A connection will be closed [%s/%d] id=%lld)\n", |
|
55 |
|
__FUNCTION__, C->addr, C->port, Conn_getid(C)); |
|
56 |
|
} |
|
57 |
|
|
|
58 |
|
static int s_data_cb(struct Conn *C, char *line) |
|
59 |
|
{ |
|
60 |
|
char *dump; |
|
61 |
|
char buf[128]; |
|
62 |
|
|
|
63 |
|
if (debug >= 8) { |
|
64 |
|
dump = Conn_dump(line, strlen(line)); |
|
65 |
|
Log(8, "%s: recv head=%d tail=%d bytes on slot %d: [%s]\n", |
|
66 |
|
__FUNCTION__, C->ibuf_head, C->ibuf_tail, |
|
67 |
|
C->slot, dump); |
|
68 |
|
free(dump); |
|
69 |
|
} |
|
70 |
|
|
|
71 |
|
if (strcasecmp(line, "quit") == 0) |
|
72 |
|
exit(0); |
|
73 |
|
|
|
74 |
|
snprintf(buf, sizeof(buf), "%s\n", line); |
|
75 |
|
Log(8, "Send back: %s", buf); |
|
76 |
|
Conn_enqueue(C, buf, strlen(buf)); |
|
77 |
|
|
|
78 |
|
return 0; |
|
79 |
|
} |
|
80 |
|
|
|
81 |
|
static void s_data(struct Conn *C) |
|
82 |
|
{ |
|
83 |
|
Conn_for_every_line(C, s_data_cb); |
|
84 |
|
} |
|
85 |
|
|
|
86 |
|
static void s_error(struct Conn *C) |
|
87 |
|
{ |
|
88 |
|
Log(1, "%s id=%llu (%s)\n", |
|
89 |
|
__FUNCTION__, Conn_getid(C), Conn_strerror()); |
|
90 |
|
} |
|
91 |
|
|
|
92 |
|
int main(void) |
|
93 |
|
{ |
|
94 |
|
struct Conn *I4 = NULL, *I6 = NULL; |
|
95 |
|
int ret; |
|
96 |
|
struct timeval start; |
|
97 |
|
|
|
98 |
|
Logf = fopen(log_file, "w"); |
|
99 |
|
if (!Logf) { |
|
100 |
|
fprintf(stderr, "Cannot open log file [%s] [%s]\n", |
|
101 |
|
log_file, strerror(errno)); |
|
102 |
|
return 1; |
|
103 |
|
} |
|
104 |
|
if (debug > 0) |
|
105 |
|
setlinebuf(Logf); |
|
106 |
|
|
|
107 |
|
/*daemon(0, 0);*/ |
|
108 |
|
|
|
109 |
|
gettimeofday(&start, NULL); |
|
110 |
|
Log(0, "\nStarting at %ld...\n", start.tv_sec); |
|
111 |
|
Log(0, "\tPort=%d\n", port); |
|
112 |
|
Log(0, "\tLogFile=%s Debug=%d\n", log_file, debug); |
|
113 |
|
Log(0, "\tipv4=%s ipv6=%s\n", (ipv4 == 1) ? "on" : "off", (ipv6 == 1) ? "on" : "off"); |
|
114 |
|
|
|
115 |
|
/* set library output debug */ |
|
116 |
|
Conn_debug(Logf, debug); |
|
117 |
|
|
|
118 |
|
ret = Conn_init(maxconn); |
|
119 |
|
if (ret == -1) { |
|
120 |
|
Log(0, "%s", Conn_strerror()); |
|
121 |
|
return 1; |
|
122 |
|
} |
|
123 |
|
|
|
124 |
|
if (ipv4 == 1) { |
|
125 |
|
Log(9, "Try to register IPv4 socket...\n"); |
|
126 |
|
I4 = Conn_socket_addr(PF_INET, SOCK_STREAM, "127.0.0.1", port); |
|
127 |
|
if (!I4) |
|
128 |
|
Log(1, "Cannot bind on ipv4 socket [%s].\n", Conn_strerror()); |
|
129 |
|
} |
|
130 |
|
|
|
131 |
|
if (ipv6 == 1) { |
|
132 |
|
Log(9, "Try to register IPv6 socket...\n"); |
|
133 |
|
I6 = Conn_socket_addr(PF_INET6, SOCK_STREAM, "::1", port); |
|
134 |
|
if (!I6) |
|
135 |
|
Log(1, "Cannot bind on ipv6 socket [%s].\n", Conn_strerror()); |
|
136 |
|
} |
|
137 |
|
|
|
138 |
|
if ((I6 == NULL) && (I4 == NULL)) { |
|
139 |
|
Log(0, "Cannot bind!\n"); |
|
140 |
|
return 1; |
|
141 |
|
} |
|
142 |
|
|
|
143 |
|
Conn_accept_cb = s_accept; |
|
144 |
|
Conn_data_cb = s_data; |
|
145 |
|
Conn_close_cb = s_close; |
|
146 |
|
Conn_error_cb = s_error; |
|
147 |
|
Conn_accept_error_cb = s_accept_error; |
|
148 |
|
|
|
149 |
|
while (1) { |
|
150 |
|
ret = Conn_poll(10000); |
|
151 |
|
if (ret == -1) { |
|
152 |
|
Log(0, "Error calling Conn_poll [%s]!\n", Conn_strerror()); |
|
153 |
|
return 1; |
|
154 |
|
} |
|
155 |
|
|
|
156 |
|
/* Allow 30 seconds and exit */ |
|
157 |
|
/* |
|
158 |
|
loops++; |
|
159 |
|
if (loops > 30) |
|
160 |
|
break; |
|
161 |
|
*/ |
|
162 |
|
} |
|
163 |
|
|
|
164 |
|
Log(0, "Finish!\n\n"); |
|
165 |
|
|
|
166 |
|
return 0; |
|
167 |
|
} |
|