Menu

[b84aab]: / src / platform / unix / inet.c  Maximize  Restore  History

Download this file

307 lines (266 with data), 6.4 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// This file is part of SmallBASIC
//
// Network library (byte-stream sockets)
//
// This program is distributed under the terms of the GPL v2.0 or later
// Download the GNU Public License (GPL) from www.gnu.org
//
// Copyright(C) 2000 Nicholas Christopoulos
#include "common/sys.h"
#include "common/inet.h"
#include "common/device.h"
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(_Win32)
static int inetlib_init = 0;
#else
#include <sys/ioctl.h>
#include <netdb.h>
#include <netinet/in.h>
#endif
#ifndef socklen_t
#define socklen_t int
#endif
// the length of time (usec) to block waiting for an event
#define BLOCK_INTERVAL 250000
/**
* prepare to use the network
*/
int net_init() {
#if defined(_Win32)
if (!inetlib_init) {
inetlib_init = 1;
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 0), &wsadata)) {
return 0;
}
return 1;
}
#endif
return 1;
}
/**
* stop using the network
*/
int net_close() {
#if defined(_Win32)
if (inetlib_init) {
WSACleanup();
inetlib_init = 0;
}
#endif
return 1;
}
/**
* sends a string to socket
*/
void net_print(socket_t s, const char *str) {
send(s, str, strlen(str), 0);
}
/**
* sends a string to socket
*/
void net_printf(socket_t s, const char *fmt, ...) {
char buf[1025];
va_list argp;
va_start(argp, fmt);
vsnprintf(buf, sizeof(buf), fmt, argp);
va_end(argp);
net_print(s, buf);
}
/**
* read the specified number of bytes from the socket
*/
int net_read(socket_t s, char *buf, int size) {
fd_set readfds;
struct timeval tv;
int rv;
// clear the set
FD_ZERO(&readfds);
while (1) {
FD_SET(s, &readfds);
tv.tv_sec = 0;
tv.tv_usec = BLOCK_INTERVAL;
rv = select(s + 1, &readfds, NULL, NULL, &tv);
if (rv == -1) {
// an error occured
return 0;
} else if (rv == 0) {
// timeout occured
if (0 != dev_events(0)) {
s = 0;
break;
}
} else {
// ready to read
return recv(s, buf, size, 0);
}
}
return 0;
}
/**
* read a string from a socket until a char from delim str found.
*/
int net_input(socket_t s, char *buf, int size, const char *delim) {
// wait for remote input without eating cpu
fd_set readfds;
struct timeval tv;
char ch;
int count = 0;
// clear the set
FD_ZERO(&readfds);
while (1) {
tv.tv_sec = 0;
tv.tv_usec = BLOCK_INTERVAL; // time is reset in select() call in linux
FD_SET(s, &readfds);
int rv = select(s + 1, &readfds, NULL, NULL, &tv);
if (rv == -1) {
return 0; // an error occured
} else if (rv == 0) {
// timeout occured - check for program break
if (0 != dev_events(0)) {
return 0;
}
} else if (FD_ISSET(s, &readfds)) {
// ready for reading
break;
}
}
FD_ZERO(&readfds);
memset(buf, 0, size);
while (count < size) {
int bytes = net_read(s, &ch, 1);
if (bytes <= 0) {
return count; // no more data
} else {
if (ch == 0) {
return count;
}
if (delim) {
if ((strchr(delim, ch) != NULL)) {
return count; // delimiter found
}
}
if (ch != '\015') { // ignore it
buf[count] = ch;
count += bytes; // actually ++
}
}
}
return count;
}
/**
* return true if there something waiting
*/
int net_peek(socket_t s) {
#if defined(_Win32)
unsigned long bytes;
ioctlsocket(s, FIONREAD, &bytes);
return (bytes > 0);
#else
int bytes;
ioctl(s, FIONREAD, &bytes);
return (bytes > 0);
#endif
}
/**
* connect to server and returns the socket
*/
socket_t net_connect(const char *server_name, int server_port) {
socket_t sock;
dword inaddr;
struct sockaddr_in ad;
struct hostent *hp;
net_init();
memset(&ad, 0, sizeof(ad));
ad.sin_family = AF_INET;
if ((inaddr = inet_addr(server_name)) == INADDR_NONE) {
hp = gethostbyname(server_name);
if (hp == NULL) {
return -1;
}
memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
} else {
memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
}
ad.sin_port = htons(server_port);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <= 0) {
return sock;
}
if (connect(sock, (struct sockaddr *)&ad, sizeof(ad)) < 0) {
return -1;
}
return sock;
}
/**
* listen for an incoming connection on the given port and
* returns the socket once a connection has been established
*/
socket_t net_listen(int server_port) {
int listener;
struct sockaddr_in addr, remoteaddr;
socket_t s;
fd_set readfds;
struct timeval tv;
int rv;
int yes = 1;
// more info about listen sockets:
// http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html#acceptman
net_init();
listener = socket(PF_INET, SOCK_STREAM, 0);
if (listener <= 0) {
return listener;
}
addr.sin_family = AF_INET;
addr.sin_port = htons(server_port); // clients connect to this port
addr.sin_addr.s_addr = INADDR_ANY; // autoselect IP address
// prevent address already in use bind errors
if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (const char*)&yes, sizeof(int)) == -1) {
return -1;
}
// set s up to be a server (listening) socket
if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
return -1;
}
if (listen(listener, 1) == -1) {
return -1;
}
// clear the set
FD_ZERO(&readfds);
while (1) {
tv.tv_sec = 0; // block at 1 second intervals
tv.tv_usec = 500000; // time is reset in select() call in linux
FD_SET(listener, &readfds);
rv = select(listener + 1, &readfds, NULL, NULL, &tv);
if (rv == -1) {
s = 0;
break; // an error occured
} else if (rv == 0) {
// timeout occured - check for program break
if (0 != dev_events(0)) {
s = 0;
break;
}
} else if (FD_ISSET(listener, &readfds)) {
// connection is ready
socklen_t remoteaddr_len = sizeof(remoteaddr);
s = accept(listener, (struct sockaddr *)&remoteaddr, &remoteaddr_len);
break;
}
}
FD_ZERO(&readfds);
net_disconnect(listener);
return s;
}
/**
* disconnect the given network connection
*/
void net_disconnect(socket_t s) {
#if defined(_Win32)
closesocket(s);
#else
close(s);
#endif
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.