-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcp.c
169 lines (140 loc) · 2.98 KB
/
tcp.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <poll.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <linux/if_packet.h>
#include "tcp.h"
TCP *
tcpAccept(int listenfd) {
TCP *t;
int clientfd;
struct sockaddr_in client_sin;
socklen_t sin_len = sizeof(client_sin);
char s[32];
if((clientfd = accept(listenfd, (struct sockaddr *)&client_sin, &sin_len)) < 0) {
perror("accept");
return NULL;
}
if(sin_len <= 0) {
perror("wtf");
return NULL;
}
t = malloc(sizeof(*t));
if(!t)
return NULL;
t->fd = clientfd;
memcpy(&t->addr, &client_sin, sin_len);
inet_ntop(AF_INET, &t->addr.sin_addr, s, 32);
printf("accept @%s\n", s);
return t;
}
static void
tcpFree(TCP *t) {
close(t->fd);
free(t);
}
TCP *
tcpConnect(const char *host, int port, int myport) {
int sock;
int yes = 1;
TCP *t;
struct sockaddr_in sin;
socklen_t sin_len = sizeof sin;
struct addrinfo hint = {0}, *res;
char port_s[8];
char s[32];
sprintf(port_s, "%d", port);
hint.ai_family = PF_INET;
hint.ai_socktype = SOCK_STREAM;
if(getaddrinfo(host, port_s, &hint, &res) < 0) {
// perror("getaddrinfo");
return NULL;
}
if((sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
// perror("socket");
return NULL;
}
if (myport) {
if (getsockname(sock, (struct sockaddr *)&sin, &sin_len) < 0) {
perror("getsockname");
goto err;
}
sin.sin_port = htons(myport);
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
perror("setsockopt");
goto err;
}
if(bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind");
goto err;
}
}
if(connect(sock, res->ai_addr, res->ai_addrlen) < 0)
goto err;
t = malloc(sizeof(*t));
if(!t)
goto err;
t->fd = sock;
memcpy(&t->addr, res->ai_addr, res->ai_addrlen);
inet_ntop(AF_INET, &t->addr.sin_addr, s, 32);
printf("connection done: %s\n", s);
return t;
err:
close(sock);
return NULL;
}
void
tcpDisconnect(TCP *t) {
char s[32];
inet_ntop(AF_INET, &t->addr.sin_addr, s, 32);
printf("disconnect @%s\n", s);
tcpFree(t);
}
// IPv4 only
int
tcpListen(char *ipaddr, int port) {
int sock;
int yes = 1;
struct sockaddr_in sin;
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
return -1;
}
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
inet_pton(AF_INET, ipaddr, &sin.sin_addr);
if(setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
perror("setsockopt");
goto err;
}
if(bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
perror("bind");
goto err;
}
if(listen(sock, 5) < 0) {
perror("listen");
goto err;
}
return sock;
err:
close(sock);
return -1;
}
ssize_t
tcpSend(TCP *t, void *buf, size_t n) {
return write(t->fd, buf, n);
}
ssize_t
tcpRecv(TCP *t, void *buf, size_t n) {
return read(t->fd, buf, n);
}