-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.c
133 lines (103 loc) · 2.74 KB
/
socket.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
/*
* 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 "httpd.h"
#include <errno.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
static connection_type CT_Socket;
static const char* conn_socket_get_type(connection* conn) {
return CONN_TYPE_SOCKET;
}
static connection* conn_socket_create(int port, char *addr) {
connection* conn = calloc(1, sizeof(connection));
conn->type = &CT_Socket;
conn->fd = -1;
conn->port = port;
conn->bindaddr = addr;
return conn;
}
static int conn_socket_listen(connection* listener) {
int listenfd = -1;
if (listener->port == -1) {
return -EINVAL;
}
if (strchr(listener->bindaddr, ':')) {
listenfd = listen_to_port(listener->port, listener->bindaddr, AF_INET6);
} else {
listenfd = listen_to_port(listener->port, listener->bindaddr, AF_INET);
}
if (listenfd == -1) {
return -EINVAL;
}
listener->fd = listenfd;
// printf("socket listen on port %d, addr %s\n", listener->port, listener->bindaddr);
return 0;
}
static int conn_socket_accept(connection* listener, connection* conn) {
struct sockaddr_in cliaddr;
socklen_t addrlen = sizeof(cliaddr);
int clifd = accept(listener->fd, (struct sockaddr*)&cliaddr, &addrlen);
if (clifd < 0)
return -errno;
conn->fd = clifd;
return 0;
}
static void conn_socket_shutdown(connection* conn) {
if (conn->fd == -1)
return;
shutdown(conn->fd, SHUT_RDWR);
}
static void conn_socket_close(connection* conn) {
if (conn->fd == -1)
return;
close(conn->fd);
conn->fd = -1;
}
static int conn_socket_write(struct connection* conn, const void* data, size_t data_len) {
if (conn->fd == -1)
return -EINVAL;
return write(conn->fd, data, data_len);
}
static int conn_socket_writev(struct connection* conn, const struct iovec* iov, int iovcnt) {
if (conn->fd == -1)
return -EINVAL;
return writev(conn->fd, iov, iovcnt);
}
static int conn_socket_read(struct connection* conn, void* buf, size_t buf_len) {
if (conn->fd == -1)
return -EINVAL;
int ret = read(conn->fd, buf, buf_len);
if (ret <= 0) {
if (errno != EAGAIN) {
return -errno;
}
return -EAGAIN;
}
return ret;
}
static connection_type CT_Socket = {
.get_type = conn_socket_get_type,
.init = NULL,
.configure = NULL,
.cleanup = NULL,
.conn_create = conn_socket_create,
.listen = conn_socket_listen,
.accept = conn_socket_accept,
.shutdown = conn_socket_shutdown,
.close = conn_socket_close,
.write = conn_socket_write,
.writev = conn_socket_writev,
.read = conn_socket_read};
int register_conntype_socket() {
return conntype_register(&CT_Socket);
}