-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplain_tcp.c
More file actions
82 lines (59 loc) · 1.61 KB
/
plain_tcp.c
File metadata and controls
82 lines (59 loc) · 1.61 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
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include "plain_tcp.h"
#include "tcpudp.h"
static int plain_tcp_connect(struct addrinfo *hints, const char *host, const char *service)
{
hints->ai_socktype = SOCK_STREAM;
return tcpudp_get_socket(hints, host, service, connect);
}
static int plain_tcp_bind(int socketfd, const struct sockaddr *sockaddr, socklen_t len)
{
int opt = 1;
if ( setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)) < 0 )
return -1;
return bind(socketfd, sockaddr, len);
}
static int plain_tcp_listen(struct addrinfo *hints, const char *host, const char *service)
{
int socketfd,
backlog = 1;
hints->ai_socktype = SOCK_STREAM;
hints->ai_flags = AI_PASSIVE;
if ( strcasecmp(host, "any") == 0 )
host = NULL;
if ( (socketfd = tcpudp_get_socket(hints, host, service, plain_tcp_bind)) < 0 )
return socketfd;
if ( listen(socketfd, backlog) < 0 )
{
perror("listen");
return -1;
}
return socketfd;
}
static ssize_t plain_tcp_send(int sockfd, const void *buf, size_t len, int flags,
const struct sockaddr *destaddr, socklen_t addrlen)
{
(void)destaddr;
(void)addrlen;
return send(sockfd, buf, len, flags);
}
static ssize_t plain_tcp_recv(int sockfd, void *buf, size_t len, int flags,
struct sockaddr *srcaddr, socklen_t *addrlen)
{
(void)srcaddr;
(void)addrlen;
return recv(sockfd, buf, len, flags);
}
struct plugin plain_tcp_plugin =
{
.connect = plain_tcp_connect,
.listen = plain_tcp_listen,
.accept = accept,
.send = plain_tcp_send,
.recv = plain_tcp_recv,
};