forked from alandipert/ncsa-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accept.c
289 lines (237 loc) · 8.1 KB
/
accept.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
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
/****************************************************************************
* NCSA Mosaic for the X Window System *
* Software Development Group *
* National Center for Supercomputing Applications *
* University of Illinois at Urbana-Champaign *
* 605 E. Springfield, Champaign IL 61820 *
* *
* Copyright (C) 1993, Board of Trustees of the University of Illinois *
* *
* NCSA Mosaic software, both binary and source (hereafter, Software) is *
* copyrighted by The Board of Trustees of the University of Illinois *
* (UI), and ownership remains with the UI. *
* *
* The UI grants you (hereafter, Licensee) a license to use the Software *
* for academic, research and internal business purposes only, without a *
* fee. Licensee may distribute the binary and source code (if released) *
* to third parties provided that the copyright notice and this statement *
* appears on all copies and that no charge is associated with such *
* copies. *
* *
* Licensee may make derivative works. However, if Licensee distributes *
* any derivative work based on or derived from the Software, then *
* Licensee will (1) notify NCSA regarding its distribution of the *
* derivative work, and (2) clearly notify users that such derivative *
* work is a modified version and not the original NCSA Mosaic *
* distributed by the UI. *
* *
* Any Licensee wishing to make commercial use of the Software should *
* contact the UI, c/o NCSA, to negotiate an appropriate license for such *
* commercial use. Commercial use includes (1) integration of all or *
* part of the source code into a product for sale or license by or on *
* behalf of Licensee to third parties, or (2) distribution of the binary *
* code or source code to third parties that need it to utilize a *
* commercial product sold or licensed by or on behalf of Licensee. *
* *
* UI MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR *
* ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED *
* WARRANTY. THE UI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE *
* USERS OF THIS SOFTWARE. *
* *
* By using or copying this Software, Licensee agrees to abide by the *
* copyright law and all other applicable laws of the U.S. including, but *
* not limited to, export control laws, and the terms of this license. *
* UI shall have the right to terminate this license immediately by *
* written notice upon Licensee's breach of, or non-compliance with, any *
* of its terms. Licensee may be held legally responsible for any *
* copyright infringement that is caused or encouraged by Licensee's *
* failure to abide by the terms of this license. *
* *
* Comments and questions are welcome and can be sent to *
* [email protected]. *
****************************************************************************/
#include "../config.h"
#ifdef linux
#define SCREWY_BLOCKING
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#ifdef SCREWY_BLOCKING
#include <sys/fcntl.h>
#endif
/*For memset*/
#include <memory.h>
#ifdef MOTOROLA
#include <sys/filio.h>
#endif
#ifdef DGUX
#include <sys/file.h>
#endif
#ifdef SVR4
#ifndef SCO
#ifndef DGUX
#include <sys/filio.h>
#endif
#endif
#endif
#include "port.h"
#include "accept.h"
#include "memStuffForPipSqueeks.h"
#ifndef DISABLE_TRACE
extern int srcTrace;
#endif
ListenAddress NetServerInitSocket(portNumber)
/* return -1 on error */
int portNumber;
{
ListenAddress socketFD;
struct sockaddr_in serverAddress;
struct protoent *protocolEntry;
protocolEntry = getprotobyname("tcp");
if (protocolEntry) {
socketFD = socket(AF_INET, SOCK_STREAM,protocolEntry->p_proto);
}
else {
socketFD = socket(AF_INET, SOCK_STREAM,0);
}
if (socketFD < 0) {
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"Can't create socket.\n");
}
#endif
return(-1);
}
/* bzero((char *) &serverAddress, sizeof(serverAddress));*/
memset((char *) &serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
serverAddress.sin_port = htons(portNumber);
if (bind(socketFD, (struct sockaddr *) &serverAddress,
sizeof(serverAddress))<0){
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"Can't bind to address.\n");
}
#endif
return(-1);
}
#ifdef SCREWY_BLOCKING
/* set socket to non-blocking for linux */
fcntl(socketFD,FNDELAY,0);
#endif
if (listen(socketFD,5) == -1) {
#ifndef DISABLE_TRACE
if (srcTrace) {
fprintf(stderr,"Can't listen.\n");
}
#endif
return(-1);
}
#ifndef SCREWY_BLOCKING
/* set socket to non-blocking */
ioctl(socketFD,FIONBIO,0);
#endif
return(socketFD);
}
PortDescriptor *NetServerAccept(socketFD)
/* accept a connection off of a base socket */
/* do not block! */
/* return NULL if no connection else return PortDescriptor* */
ListenAddress socketFD;
{
int newSocketFD;
struct sockaddr_in clientAddress;
int clientAddressLength;
PortDescriptor *c;
/* it's assumed that the socketFD has already been set to non block*/
clientAddressLength = sizeof(clientAddress);
newSocketFD = accept(socketFD,(struct sockaddr *) &clientAddress,
&clientAddressLength);
if (newSocketFD < 0) {
return(NULL);
}
/* we have connection */
if (!(c =(PortDescriptor *)MALLOC(sizeof(PortDescriptor)))){
return(0);
}
c->socketFD = newSocketFD;
c->numInBuffer = 0;
return(c);
}
int NetRead(c,buffer,bufferSize)
/* read input from port, return number of bytes read */
PortDescriptor *c;
char *buffer;
int bufferSize;
{
int length;
length = read(c->socketFD, buffer, bufferSize);
return(length);
}
int NetServerWrite(c,buffer,bufferSize)
/* send buffer, return number of bytes sent */
PortDescriptor *c;
char *buffer;
int bufferSize;
{
int length;
length = write(c->socketFD,buffer,bufferSize);
return(length);
}
int NetCloseConnection(c)
/* close the connection */
PortDescriptor *c;
{
close(c->socketFD);
}
int NetCloseAcceptPort(s)
int s;
{
close(s);
}
int NetIsThereInput(p)
/* Do a non block check on socket for input and return 1 for yes, 0 for no */
PortDescriptor *p;
{
static struct timeval timeout = { 0L , 0L };
/*int val;*/
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(p->socketFD,&readfds);
if (0 < select(32, &readfds, 0, 0, &timeout)){
return(1);
}
else {
return(0);
}
}
int NetIsThereAConnection(socketFD)
/* Do a non block check on socket for input and return 1 for yes, 0 for no */
int socketFD;
{
static struct timeval timeout = { 0L , 0L };
/*int val;*/
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(socketFD,&readfds);
if (0 < select(32, &readfds, 0, 0, &timeout)){
return(1);
}
else {
return(0);
}
}
int NetGetSocketDescriptor(s)
/* extract socket file descriptor from the Port structure */
PortDescriptor *s;
{
return(s->socketFD);
}