Repository navigation
Expand file tree
/
Copy pathconnection.c
More file actions
174 lines (143 loc) · 4.24 KB
/
Copy pathconnection.c
File metadata and controls
174 lines (143 loc) · 4.24 KB
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
/*
* Copyright 2023 enhua zhou
*
* Authors:
* enhua zhou <zhouenhua@bytedance.com>
*
* Design and some codes are taken from redis.
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include "assert.h"
#include "connection.h"
#include "log.h"
static connection_type *conn_types[CONN_TYPE_MAX];
void conntype_initialize(void)
{
register_conntype_socket();
register_conntype_tls();
}
int conntype_register(connection_type *ct)
{
const char *typename = ct->get_type(NULL);
connection_type *tmpct;
int type;
/* find an empty slot to store the new connection type */
for (type = 0; type < CONN_TYPE_MAX; type++) {
tmpct = conn_types[type];
if (!tmpct)
break;
/* ignore case, we really don't care "tls"/"TLS" */
if (!strcasecmp(typename, tmpct->get_type(NULL))) {
log_err("Connection types %s already registered\n", typename);
return -errno;
}
}
if (type == CONN_TYPE_MAX)
return -ENOMEM;
conn_types[type] = ct;
if (ct->init) {
ct->init();
}
log_info("Connection type %s registered\n", typename);
return -errno;
}
connection_type *get_conntype_by_name(const char *typename)
{
connection_type *ct;
for (int type = 0; type < CONN_TYPE_MAX; type++) {
ct = conn_types[type];
if (!ct)
break;
if (!strcasecmp(typename, ct->get_type(NULL)))
return ct;
}
log_err("Missing implement of connection type %s\n", typename);
return NULL;
}
int get_conntype_index_by_name(const char *typename)
{
connection_type *ct;
for (int type = 0; type < CONN_TYPE_MAX; type++) {
ct = conn_types[type];
if (!ct)
break;
if (!strcasecmp(typename, ct->get_type(NULL)))
return type;
}
log_err("Missing implement of connection type %s. Please re-compile atophttpd.\n", typename);
return -1;
}
int listen_to_port(int port, char *bindaddr, int af)
{
struct addrinfo hints;
struct addrinfo *res = NULL, *p;
int ret, sockfd = -1;
char _port[6];
static const int reuse = 1;
snprintf(_port, 6, "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = af;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if (bindaddr && !strcmp("*", bindaddr))
bindaddr = NULL;
if (af == AF_INET6 && bindaddr && !strcmp("::*", bindaddr))
bindaddr = NULL;
ret = getaddrinfo(bindaddr, _port, &hints, &res);
if (ret != 0) {
log_err("failed to get addr info for [%s]:%s: %s\n", bindaddr ? bindaddr : "*", _port,
gai_strerror(ret));
/* res is left unset on failure; nothing to free */
return -1;
}
for (p = res; p != NULL; p = p->ai_next) {
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd == -1) {
log_err("failed to get socket fd: %s\n", strerror(errno));
continue;
}
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse));
if (ret < 0) {
log_err("socket set REUSEADDR failed: %s\n", strerror(errno));
goto error;
}
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, (const char *)&reuse, sizeof(reuse));
if (ret < 0) {
log_err("socket set REUSEPORT failed: %s\n", strerror(errno));
goto error;
}
ret = bind(sockfd, p->ai_addr, p->ai_addrlen);
if (ret == -1) {
log_err("socket bind failed: %s\n", strerror(errno));
close(sockfd);
sockfd = -1;
continue;
}
ret = listen(sockfd, 128);
if (ret == -1) {
log_err("socket listen failed: %s\n", strerror(errno));
close(sockfd);
sockfd = -1;
continue;
}
goto end;
}
error:
if (sockfd != -1) {
close(sockfd);
sockfd = -1;
}
end:
if (res)
freeaddrinfo(res);
return sockfd;
}