-
Notifications
You must be signed in to change notification settings - Fork 29
/
tcp.c
220 lines (179 loc) · 4.27 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* tcp.c
* By Ron
* Created August, 2008
*
* (See LICENSE.txt)
*/
#include <stdio.h>
#include <string.h>
#ifdef WIN32
#include <winsock2.h>
#else
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#endif
#include "tcp.h"
void winsock_initialize()
{
#ifdef WIN32
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
int error = WSAStartup(wVersionRequested, &wsaData);
switch(error)
{
case WSASYSNOTREADY:
fprintf(stderr, "The underlying network subsystem is not ready for network communication.\n");
exit(1);
break;
case WSAVERNOTSUPPORTED:
fprintf(stderr, "The version of Windows Sockets support requested is not provided by this particular Windows Sockets implementation.\n");
exit(1);
break;
case WSAEINPROGRESS:
fprintf(stderr, "A blocking Windows Sockets 1.1 operation is in progress.\n");
exit(1);
break;
case WSAEPROCLIM:
fprintf(stderr, "A limit on the number of tasks supported by the Windows Sockets implementation has been reached.\n");
exit(1);
break;
case WSAEFAULT:
fprintf(stderr, "The lpWSAData parameter is not a valid pointer.\n");
exit(1);
break;
}
#endif
}
int tcp_connect(char *host, uint16_t port)
{
struct sockaddr_in serv_addr;
struct hostent *server;
/* Create the socket */
int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == -1)
{
nbdie("tcp: couldn't create socket");
}
else
{
/* Look up the host */
server = gethostbyname(host);
if(!server)
{
s = -1;
fprintf(stderr, "Couldn't find host %s\n", host);
}
else
{
/* Set up the server address */
memset(&serv_addr, '\0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
memcpy(&serv_addr.sin_addr, server->h_addr_list[0], server->h_length);
/* Connect */
if (connect(s, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{
s = -1;
nberror("tcp: couldn't connect to host");
}
}
}
return s;
}
int tcp_listen(char *address, uint16_t port)
{
int s;
struct sockaddr_in serv_addr;
/* Get the server address */
memset((char *) &serv_addr, '\0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(address);
serv_addr.sin_port = htons(port);
if(serv_addr.sin_addr.s_addr == INADDR_NONE)
nbdie("tcp: couldn't parse local address");
/* Create a socket */
s = socket(AF_INET, SOCK_STREAM, 0);
if(s < 0)
{
nbdie("tcp: couldn't create socket");
}
else
{
/* Bind the socket */
if (bind(s, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
nbdie("tcp: couldn't bind to socket");
/* Switch the socket to listen mode. TODO: why 20? */
if(listen(s, 20) < 0)
nbdie("tcp: couldn't listen on socket");
}
return s;
}
int tcp_accept(int listen, char **address, uint16_t *port)
{
struct sockaddr_in addr;
socklen_t sockaddr_len = sizeof(struct sockaddr_in);
int s;
s = accept(listen, (struct sockaddr *) &addr, &sockaddr_len);
if(s < 0)
nbdie("tcp: couldn't accept connection");
*address = inet_ntoa(addr.sin_addr);
*port = ntohs(addr.sin_port);
return s;
}
size_t tcp_send(int s, void *data, size_t length)
{
return send(s, data, length, 0);
}
size_t tcp_recv(int s, void *buffer, size_t buffer_length)
{
return recv(s, buffer, buffer_length, 0);
}
int tcp_close(int s)
{
#ifdef WIN32
return closesocket(s);
#else
return close(s);
#endif
}
#if 0
int main(int argc, char *argv[])
{
char buffer[1024];
int s;
int listener;
struct sockaddr_in addr;
int port;
size_t len;
memset(buffer, 0, 1024);
winsock_initialize();
s = tcp_connect("www.google.ca", 80);
if(s < 0)
DIE("Fail");
tcp_send(s, "GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n", 41);
tcp_recv(s, buffer, 1024);
tcp_close(s);
printf("%s\n", buffer);
printf("Listening on TCP/6666\n");
listener = tcp_listen("0.0.0.0", 6666);
s = tcp_accept(listener, &addr, &port);
if(s < 0)
DIE("Fail");
printf("Connection accepted from %s:%d\n", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
memset(buffer, 0, 1024);
strcpy(buffer, "HELLO!");
len = 7;
while(len > 0 && tcp_send(s, buffer, len) >= 0)
{
memset(buffer, 0, 1024);
len = tcp_recv(s, buffer, 1024);
printf("Received: %s [%d]\n", buffer, len);
}
printf("%s\n", buffer);
return 0;
}
#endif