0
|
1 |
"
|
4
|
2 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
42
|
3 |
All Rights Reserved
|
0
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
63
|
13 |
NonPositionableExternalStream subclass:#Socket
|
71
|
14 |
instanceVariableNames:'domain socketType protocol port serviceName
|
42
|
15 |
peerName'
|
0
|
16 |
classVariableNames:''
|
|
17 |
poolDictionaries:''
|
|
18 |
category:'Streams-External'
|
|
19 |
!
|
|
20 |
|
|
21 |
Socket comment:'
|
4
|
22 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
42
|
23 |
All Rights Reserved
|
36
|
24 |
|
85
|
25 |
$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.27 1995-08-10 18:36:21 claus Exp $
|
0
|
26 |
'!
|
|
27 |
|
25
|
28 |
!Socket class methodsFor:'documentation'!
|
|
29 |
|
31
|
30 |
copyright
|
|
31 |
"
|
|
32 |
COPYRIGHT (c) 1992 by Claus Gittinger
|
42
|
33 |
All Rights Reserved
|
31
|
34 |
|
|
35 |
This software is furnished under a license and may be used
|
|
36 |
only in accordance with the terms of that license and with the
|
|
37 |
inclusion of the above copyright notice. This software may not
|
|
38 |
be provided or otherwise made available to, or used by, any
|
|
39 |
other person. No title to or ownership of the software is
|
|
40 |
hereby transferred.
|
|
41 |
"
|
|
42 |
!
|
|
43 |
|
|
44 |
version
|
|
45 |
"
|
85
|
46 |
$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.27 1995-08-10 18:36:21 claus Exp $
|
31
|
47 |
"
|
|
48 |
!
|
|
49 |
|
25
|
50 |
documentation
|
|
51 |
"
|
31
|
52 |
This class provides access to (unix-)sockets for interprocess communication.
|
25
|
53 |
The message protocol is preliminary, until someone tells me how
|
31
|
54 |
other smalltalk's socket interfaces look like.
|
25
|
55 |
|
|
56 |
Also, currently there is almost no support for other than IP
|
|
57 |
sockets - this will be added in the future.
|
28
|
58 |
Due to historic reasons (I started this class, before I got hold of some
|
|
59 |
code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface
|
71
|
60 |
still provided.
|
|
61 |
This will vanish; use the family:type: or newTCPxxx and newUDPxxx interface,
|
|
62 |
together with the bind/listen and accept calls,
|
|
63 |
which are meant to be compatible to ST-80's UnixSocketAccessor interface.
|
|
64 |
|
|
65 |
ST/X does not use IPSocketAddress, UDSocketAddress erc; all addressing
|
|
66 |
is done by passing appropriate string- or byteArray objects containing
|
|
67 |
the addresses. This may change, too.
|
|
68 |
|
|
69 |
TODO: cleanup historic leftovers, implement other than inet domain stuff.
|
48
|
70 |
"
|
|
71 |
!
|
28
|
72 |
|
48
|
73 |
examples
|
|
74 |
"
|
28
|
75 |
example (get help info from an nntp server):
|
|
76 |
|
42
|
77 |
|sock|
|
28
|
78 |
|
42
|
79 |
sock := Socket
|
31
|
80 |
newTCPclientToHost:(OperatingSystem
|
|
81 |
getEnvironment:'NNTPSERVER')
|
|
82 |
port:'nntp'.
|
42
|
83 |
Transcript showCr:sock nextLine.
|
|
84 |
sock buffered:false.
|
28
|
85 |
|
42
|
86 |
sock nextPutAll:'HELP'; cr.
|
|
87 |
[:exit |
|
|
88 |
|line|
|
28
|
89 |
|
42
|
90 |
line := sock nextLine.
|
|
91 |
line = '.' ifTrue:[exit value:nil].
|
|
92 |
Transcript showCr:line.
|
|
93 |
] loopWithExit.
|
|
94 |
sock close
|
28
|
95 |
|
31
|
96 |
|
28
|
97 |
example (connect to an ftp server):
|
|
98 |
|
42
|
99 |
|sock|
|
28
|
100 |
|
42
|
101 |
sock := Socket
|
31
|
102 |
newTCPclientToHost:(OperatingSystem getHostName)
|
|
103 |
port:'ftp'.
|
28
|
104 |
|
42
|
105 |
sock buffered:false.
|
|
106 |
Transcript showCr:sock nextLine.
|
|
107 |
sock nextPutAll:('USER ' , OperatingSystem getLoginName); cr.
|
|
108 |
Transcript showCr:sock nextLine.
|
74
|
109 |
sock nextPutAll:('PASS ' , 'your password here'); cr.
|
42
|
110 |
Transcript showCr:sock nextLine.
|
|
111 |
sock nextPutAll:'LIST'; cr.
|
|
112 |
Transcript showCr:sock nextLine.
|
28
|
113 |
|
42
|
114 |
'dont know enough of the ftp protocol to continue here ...'
|
31
|
115 |
|
28
|
116 |
|
|
117 |
example (connect to an snmp server):
|
|
118 |
|
74
|
119 |
|sock port|
|
42
|
120 |
|
74
|
121 |
sock := Socket newUDP.
|
|
122 |
port := Socket portOfService:'snmp'.
|
|
123 |
sock connectTo:(OperatingSystem getHostName) port:port.
|
42
|
124 |
sock buffered:false.
|
74
|
125 |
Transcript showCr:'got it'.
|
42
|
126 |
|
|
127 |
example (await connection from a client and read some data):
|
|
128 |
|
|
129 |
|connectSock sock|
|
28
|
130 |
|
42
|
131 |
connectSock := Socket provide:9996.
|
|
132 |
Transcript showCr:'listen ..'.
|
|
133 |
connectSock listenFor:5.
|
|
134 |
Transcript showCr:'wait'.
|
|
135 |
connectSock readWait.
|
|
136 |
Transcript showCr:'accept'.
|
|
137 |
sock := Socket new acceptOn:self.
|
|
138 |
Transcript showCr:'close'.
|
|
139 |
connectSock close.
|
|
140 |
sock buffered:false.
|
|
141 |
Transcript showCr:'server: got it'.
|
|
142 |
'can now do transfer via sock'.
|
|
143 |
Transcript showCr:'read'.
|
|
144 |
Transcript showCr:('got: ' , sock nextLine).
|
|
145 |
sock close
|
|
146 |
|
|
147 |
example (connect to above server and send some data):
|
|
148 |
|
|
149 |
|sock|
|
|
150 |
|
|
151 |
sock := Socket connectTo:9996 on:'porty'.
|
|
152 |
sock buffered:false.
|
|
153 |
Transcript showCr:'client: got it'.
|
|
154 |
'can now do transfer via sock'.
|
|
155 |
Transcript showCr:'sending <hello>'.
|
|
156 |
sock nextPutLine:'hello'.
|
|
157 |
sock close
|
25
|
158 |
"
|
|
159 |
! !
|
|
160 |
|
51
|
161 |
!Socket primitiveDefinitions!
|
44
|
162 |
|
|
163 |
%{
|
|
164 |
#include <stdio.h>
|
|
165 |
#include <errno.h>
|
|
166 |
|
|
167 |
#ifdef LINUX
|
|
168 |
/* kludge to avoid some redefines ... */
|
|
169 |
# define _ARPA_NAMESER_H
|
|
170 |
# define _NETINET_TCP_H
|
|
171 |
#endif
|
|
172 |
|
|
173 |
#ifndef transputer
|
|
174 |
# include <fcntl.h>
|
|
175 |
# include <sys/types.h>
|
|
176 |
# ifdef IRIS
|
|
177 |
/* no socket.h on 4.0.5h ?!?!? */
|
|
178 |
# define AF_UNIX 1
|
|
179 |
# define AF_INET 2
|
|
180 |
# define SOCK_STREAM 1
|
|
181 |
# define SOCK_DGRAM 2
|
|
182 |
# define SOCK_RAW 3
|
|
183 |
# else
|
|
184 |
# include <sys/socket.h>
|
|
185 |
# endif
|
|
186 |
# include <netdb.h>
|
|
187 |
# include <netinet/in.h>
|
|
188 |
# if ! (defined(SYSV3) && defined(mc88k))
|
|
189 |
# include <netinet/tcp.h>
|
|
190 |
# endif
|
|
191 |
#endif
|
51
|
192 |
|
|
193 |
/*
|
|
194 |
* on some systems errno is a macro ... check for it here
|
|
195 |
*/
|
|
196 |
#ifndef errno
|
|
197 |
extern errno;
|
|
198 |
#endif
|
|
199 |
|
71
|
200 |
static int __debugging__ = 0;
|
|
201 |
|
|
202 |
#ifdef DEBUG
|
|
203 |
# define DBGPRINTF(x) printf x
|
|
204 |
#else
|
|
205 |
# define DBGPRINTF(x) /* as nothing */
|
|
206 |
#endif
|
|
207 |
|
44
|
208 |
%}
|
|
209 |
! !
|
|
210 |
|
75
|
211 |
!Socket class methodsFor:'Signal constants'!
|
21
|
212 |
|
|
213 |
brokenConnectionSignal
|
|
214 |
"return the signal used to tell broken connections.
|
|
215 |
Since in unix, this is the same as the broken pipe signal,
|
|
216 |
return that one.
|
|
217 |
(for other Operatingsystems, this may change ..)"
|
|
218 |
|
|
219 |
^ PipeStream brokenPipeSignal
|
|
220 |
! !
|
|
221 |
|
71
|
222 |
!Socket class methodsFor:'debugging'!
|
|
223 |
|
|
224 |
debug:aBoolean
|
|
225 |
"turn on/off internal debugprints.
|
|
226 |
This method is for ST/X debugging only and
|
|
227 |
may be removed in later versions"
|
|
228 |
|
|
229 |
%{ /* NOCONTEXT */
|
|
230 |
|
|
231 |
__debugging__ = (aBoolean == true);
|
|
232 |
%}
|
|
233 |
! !
|
|
234 |
|
0
|
235 |
!Socket class methodsFor:'queries'!
|
|
236 |
|
6
|
237 |
ipAddressOfHost:aHostName
|
|
238 |
"return the IP (internet-) number for a hostname"
|
|
239 |
|
|
240 |
|b1 b2 b3 b4|
|
|
241 |
|
|
242 |
%{
|
|
243 |
struct sockaddr_in sa ;
|
|
244 |
struct hostent *hp ;
|
|
245 |
long addr;
|
|
246 |
|
21
|
247 |
if (__isString(aHostName)) {
|
42
|
248 |
bzero(&sa, sizeof(sa)) ;
|
|
249 |
if ((addr = inet_addr((char *) _stringVal(aHostName))) != -1) {
|
|
250 |
/* is Internet addr in octet notation */
|
|
251 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr));
|
|
252 |
sa.sin_family = AF_INET;
|
|
253 |
} else {
|
|
254 |
/* do we know the host's address? */
|
|
255 |
if ((hp = gethostbyname((char *) _stringVal(aHostName))) == NULL) {
|
71
|
256 |
DBGPRINTF(("unknown host\n"));
|
42
|
257 |
RETURN ( nil );
|
|
258 |
}
|
|
259 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
|
|
260 |
sa.sin_family = hp->h_addrtype;
|
|
261 |
}
|
6
|
262 |
}
|
|
263 |
/* if the addressing family is not AF_INET, return nil */
|
|
264 |
if (sa.sin_family != AF_INET) {
|
42
|
265 |
RETURN ( nil );
|
6
|
266 |
}
|
|
267 |
b1 = _MKSMALLINT((sa.sin_addr.s_addr >> 24) & 0xFF);
|
|
268 |
b2 = _MKSMALLINT((sa.sin_addr.s_addr >> 16) & 0xFF);
|
|
269 |
b3 = _MKSMALLINT((sa.sin_addr.s_addr >> 8) & 0xFF);
|
|
270 |
b4 = _MKSMALLINT((sa.sin_addr.s_addr >> 0) & 0xFF);
|
|
271 |
%}
|
|
272 |
.
|
|
273 |
^ ByteArray with:b1 with:b2 with:b3 with:b4
|
21
|
274 |
|
71
|
275 |
"
|
|
276 |
Socket ipAddressOfHost:'clam'
|
|
277 |
Socket ipAddressOfHost:'porty'
|
|
278 |
Socket ipAddressOfHost:'josef'
|
|
279 |
Socket ipAddressOfHost:'styx.com'
|
|
280 |
"
|
6
|
281 |
!
|
|
282 |
|
25
|
283 |
hostWithIpAddress:anAddress
|
|
284 |
"return the hostname for an IP (internet-) address"
|
|
285 |
|
|
286 |
|b1 b2 b3 b4|
|
|
287 |
|
|
288 |
b1 := anAddress at:1.
|
|
289 |
b2 := anAddress at:2.
|
|
290 |
b3 := anAddress at:3.
|
|
291 |
b4 := anAddress at:4.
|
|
292 |
%{
|
|
293 |
struct sockaddr_in sa ;
|
|
294 |
struct hostent *hp ;
|
|
295 |
|
58
|
296 |
if (__bothSmallInteger(b1, b2) && __bothSmallInteger(b3, b4)) {
|
42
|
297 |
bzero(&sa, sizeof(sa)) ;
|
|
298 |
sa.sin_addr.s_addr = _intVal(b1) & 0xFF;
|
|
299 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b2) & 0xFF);
|
|
300 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b3) & 0xFF);
|
|
301 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b4) & 0xFF);
|
|
302 |
sa.sin_family = AF_INET;
|
|
303 |
/* do we know the host's address? */
|
|
304 |
hp = gethostbyaddr((char *) &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr), AF_INET);
|
|
305 |
if (hp == NULL) {
|
71
|
306 |
DBGPRINTF(("unknown address: %d.%d.%d.%d\n",
|
|
307 |
_intVal(b1), _intVal(b2), _intVal(b3), _intVal(b4)));
|
|
308 |
} else {
|
|
309 |
sa.sin_family = hp->h_addrtype;
|
42
|
310 |
}
|
25
|
311 |
}
|
|
312 |
|
|
313 |
/* if the addressing family is not AF_INET, return nil */
|
|
314 |
if (sa.sin_family != AF_INET) {
|
42
|
315 |
RETURN ( nil );
|
25
|
316 |
}
|
|
317 |
if (hp != NULL)
|
42
|
318 |
RETURN (_MKSTRING(hp->h_name COMMA_CON));
|
25
|
319 |
|
|
320 |
RETURN (_MKSTRING(inet_ntoa(sa.sin_addr) COMMA_CON));
|
|
321 |
%}
|
|
322 |
|
|
323 |
"
|
|
324 |
Socket ipAddressOfHost:'clam'
|
71
|
325 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam')
|
25
|
326 |
Socket ipAddressOfHost:'porty'
|
71
|
327 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty')
|
25
|
328 |
Socket hostWithIpAddress:#[1 2 3 4]
|
|
329 |
"
|
|
330 |
!
|
|
331 |
|
0
|
332 |
portOfService:aNameOrNumber
|
|
333 |
"returns the port-number for a given service
|
|
334 |
or nil if no such service exists;
|
|
335 |
- used to convert service names to portNumbers"
|
|
336 |
%{
|
|
337 |
struct servent *servent = NULL;
|
|
338 |
|
58
|
339 |
if (__isSmallInteger(aNameOrNumber)) {
|
42
|
340 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "tcp") ;
|
|
341 |
if (servent != NULL) {
|
|
342 |
RETURN ( aNameOrNumber );
|
|
343 |
}
|
|
344 |
RETURN ( aNameOrNumber );
|
0
|
345 |
}
|
21
|
346 |
if (__isString(aNameOrNumber)) {
|
42
|
347 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "tcp");
|
|
348 |
if (servent != NULL) {
|
|
349 |
RETURN ( _MKSMALLINT(ntohs(servent->s_port)) );
|
|
350 |
}
|
|
351 |
RETURN ( nil );
|
0
|
352 |
}
|
|
353 |
RETURN ( nil );
|
|
354 |
%}
|
28
|
355 |
"
|
|
356 |
Socket portOfService:'finger'
|
74
|
357 |
Socket portOfService:'nntp'
|
|
358 |
Socket portOfService:'echo'
|
|
359 |
Socket portOfService:'snmp'
|
28
|
360 |
"
|
0
|
361 |
!
|
|
362 |
|
|
363 |
protocolOfService:aNameOrNumber
|
|
364 |
"returns the protocol (as string) for a given service
|
28
|
365 |
or nil if no such service exists."
|
0
|
366 |
%{
|
|
367 |
struct servent *servent = NULL;
|
|
368 |
|
58
|
369 |
if (__isSmallInteger(aNameOrNumber)) {
|
42
|
370 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "tcp") ;
|
|
371 |
if (servent == NULL) {
|
|
372 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "udp") ;
|
|
373 |
if (servent == NULL) {
|
|
374 |
RETURN ( nil );
|
|
375 |
}
|
|
376 |
}
|
0
|
377 |
} else {
|
42
|
378 |
if (__isString(aNameOrNumber)) {
|
|
379 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "tcp");
|
|
380 |
if (servent == NULL) {
|
|
381 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "udp");
|
|
382 |
if (servent == NULL) {
|
|
383 |
RETURN ( nil );
|
|
384 |
}
|
|
385 |
}
|
|
386 |
}
|
0
|
387 |
}
|
|
388 |
if (servent) {
|
42
|
389 |
RETURN ( _MKSTRING(servent->s_proto COMMA_CON) );
|
0
|
390 |
}
|
|
391 |
RETURN ( nil );
|
|
392 |
%}
|
28
|
393 |
"
|
74
|
394 |
Socket protocolOfService:'finger'
|
|
395 |
Socket protocolOfService:'nntp'
|
28
|
396 |
Socket protocolOfService:'xxx'
|
|
397 |
Socket protocolOfService:79
|
74
|
398 |
Socket protocolOfService:'snmp'
|
28
|
399 |
"
|
|
400 |
!
|
|
401 |
|
|
402 |
domainOfProtocol:aProtocol
|
|
403 |
"given a protocols name (i.e. tcp, udp etc) return the domain.
|
|
404 |
This method needs more ... - or is there a way to get this from the system ?"
|
|
405 |
|
|
406 |
"
|
|
407 |
tcp/ip stuff
|
|
408 |
"
|
|
409 |
(aProtocol = 'tcp') ifTrue:[^ #inet].
|
|
410 |
(aProtocol = 'udp') ifTrue:[^ #inet].
|
|
411 |
(aProtocol = 'ip') ifTrue:[^ #inet].
|
|
412 |
"
|
71
|
413 |
unix domain
|
|
414 |
"
|
|
415 |
(aProtocol = 'ud') ifTrue:[^ #unix].
|
|
416 |
|
|
417 |
"
|
28
|
418 |
x25 stuff (if any)
|
|
419 |
appletalk stuff (if any)
|
|
420 |
other stuff (if any)
|
|
421 |
"
|
|
422 |
^ nil
|
|
423 |
|
|
424 |
"
|
|
425 |
Socket domainOfProtocol:'tcp'
|
|
426 |
Socket domainOfProtocol:'ucp'
|
|
427 |
Socket domainOfProtocol:(Socket protocolOfService:'nntp')
|
|
428 |
Socket domainOfProtocol:(Socket protocolOfService:'echo')
|
|
429 |
"
|
|
430 |
!
|
|
431 |
|
|
432 |
typeOfProtocol:aProtocol
|
|
433 |
"given a protocols name (i.e. tcp, udp etc) return the connection type.
|
|
434 |
This method needs more ... - or is there a way to get this from the system ?"
|
|
435 |
|
|
436 |
(aProtocol = 'tcp') ifTrue:[^ #stream].
|
|
437 |
(aProtocol = 'udp') ifTrue:[^ #datagram].
|
|
438 |
(aProtocol = 'ip') ifTrue:[^ #raw].
|
|
439 |
"
|
71
|
440 |
unix domain
|
|
441 |
"
|
|
442 |
(aProtocol = 'ud') ifTrue:[^ #stream].
|
|
443 |
"
|
28
|
444 |
x25 stuff (if any)
|
|
445 |
appletalk stuff (if any)
|
|
446 |
other stuff (if any)
|
|
447 |
"
|
|
448 |
^ nil
|
|
449 |
|
|
450 |
"
|
|
451 |
Socket typeOfProtocol:'tcp'
|
|
452 |
Socket typeOfProtocol:'ucp'
|
|
453 |
Socket typeOfProtocol:(Socket protocolOfService:'nntp')
|
|
454 |
Socket typeOfProtocol:(Socket protocolOfService:'echo')
|
|
455 |
"
|
|
456 |
! !
|
|
457 |
|
71
|
458 |
!Socket class methodsFor:'easy tcp/ip instance creation'!
|
28
|
459 |
|
|
460 |
connectTo:service on:host
|
71
|
461 |
"standard & easy client setup:
|
|
462 |
create new client tcp socket, bind and connect;
|
|
463 |
return the socket.
|
28
|
464 |
The system will block (interruptable), until the connection is
|
|
465 |
established."
|
|
466 |
|
|
467 |
^ (self new) for:host port:(self portOfService:service).
|
|
468 |
|
|
469 |
"
|
|
470 |
Socket connectTo:9995 on:'clam'
|
|
471 |
Socket connectTo:'finger' on:'clam'
|
71
|
472 |
Socket connectTo:'ftp' on:'clam'
|
28
|
473 |
Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER')
|
|
474 |
"
|
|
475 |
!
|
|
476 |
|
|
477 |
provide:service
|
71
|
478 |
"standard & easy server setup:
|
|
479 |
create a new TCP server socket providing a service."
|
28
|
480 |
|
|
481 |
|newSock|
|
|
482 |
|
|
483 |
newSock := (self new) for:nil port:(self portOfService:service).
|
|
484 |
newSock notNil ifTrue:[
|
42
|
485 |
newSock listenFor:5.
|
28
|
486 |
].
|
|
487 |
^ newSock
|
|
488 |
|
|
489 |
"
|
|
490 |
Socket provide:9995
|
|
491 |
(Socket provide:9996) accept
|
|
492 |
Socket provide:'nntp'
|
|
493 |
"
|
|
494 |
! !
|
|
495 |
|
71
|
496 |
!Socket class methodsFor:'ST80 queries'!
|
|
497 |
|
|
498 |
sockStream
|
|
499 |
"return the type code for stream sockets"
|
|
500 |
|
|
501 |
^ #stream
|
|
502 |
! !
|
|
503 |
|
|
504 |
!Socket class methodsFor:'ST80 instance creation'!
|
|
505 |
|
|
506 |
family:domainSymbol type:typeSymbol
|
|
507 |
"create a socket for domain and type - ST80 simply uses a different name.
|
|
508 |
Domain must be one of the symbols: #inet, #unix, #ns, #appletalk or #ns;
|
|
509 |
Type must be #stream, #datagram or #raw
|
|
510 |
|
|
511 |
XXX: currently only the #inet domain is supported"
|
|
512 |
|
|
513 |
^ self domain:domainSymbol type:typeSymbol
|
|
514 |
|
|
515 |
"
|
|
516 |
Socket family:#inet type:#stream
|
|
517 |
Socket family:#inet type:#datagram
|
|
518 |
Socket family:#unix type:#stream
|
|
519 |
"
|
|
520 |
! !
|
|
521 |
|
28
|
522 |
!Socket class methodsFor:'general instance creation'!
|
|
523 |
|
|
524 |
domain:domainSymbol type:type
|
|
525 |
"create a socket for domain and type -
|
|
526 |
neither any connect nor binding is done.
|
|
527 |
Domain must be one of the symbols: #inet, #unix, #ns, #appletalk or #ns;
|
|
528 |
Type must be #stream, #datagram or #raw
|
|
529 |
|
|
530 |
XXX: currently only the #inet domain is supported"
|
|
531 |
|
|
532 |
^ self new domain:domainSymbol type:type
|
|
533 |
|
71
|
534 |
"
|
|
535 |
Socket domain:#inet type:#stream
|
|
536 |
Socket domain:#inet type:#datagram
|
|
537 |
Socket domain:#unix type:#stream
|
|
538 |
Socket domain:#appletalk type:#stream
|
|
539 |
Socket domain:#DECnet type:#stream
|
|
540 |
"
|
28
|
541 |
!
|
|
542 |
|
|
543 |
newUDP
|
|
544 |
"create a UDP socket - no binding or other setup is done,
|
|
545 |
neither connect nor connect-wait is done."
|
|
546 |
|
|
547 |
^ self new domain:#inet type:#datagram
|
|
548 |
|
|
549 |
"Socket newUDP"
|
|
550 |
!
|
|
551 |
|
|
552 |
newUDP:aServiceOrNil
|
|
553 |
"create a UDP socket for a service -
|
|
554 |
neither connect nor connect-wait is done."
|
|
555 |
|
|
556 |
|newSock|
|
|
557 |
|
|
558 |
newSock := self newUDP.
|
|
559 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
|
42
|
560 |
newSock bindTo:(self portOfService:aServiceOrNil) address:nil
|
28
|
561 |
].
|
|
562 |
^ newSock
|
|
563 |
|
|
564 |
"Socket newUDP:nil"
|
|
565 |
!
|
|
566 |
|
|
567 |
newTCP
|
|
568 |
"create a TCP socket - no binding or other setup is done,
|
|
569 |
neither connect nor connect-wait is done."
|
|
570 |
|
|
571 |
^ self new domain:#inet type:#stream
|
|
572 |
|
|
573 |
"Socket newUDP"
|
|
574 |
!
|
|
575 |
|
|
576 |
newTCP:aServiceOrNil
|
|
577 |
"create a TCP socket for a service -
|
|
578 |
neither connect nor connect-wait is done."
|
|
579 |
|
|
580 |
|newSock|
|
|
581 |
|
|
582 |
newSock := self newTCP.
|
|
583 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[
|
42
|
584 |
newSock bindTo:(self portOfService:aServiceOrNil) address:nil
|
28
|
585 |
].
|
|
586 |
^ newSock
|
|
587 |
|
|
588 |
|
|
589 |
"Socket newTCP:'nntp'"
|
|
590 |
"Socket newTCP:9995"
|
|
591 |
!
|
|
592 |
|
|
593 |
newTCPclientToHost:hostname port:aService
|
|
594 |
"create a new TCP client socket connecting to a service."
|
|
595 |
|
|
596 |
|newSock|
|
|
597 |
|
|
598 |
newSock := self newTCP.
|
|
599 |
newSock notNil ifTrue:[
|
42
|
600 |
(newSock connectTo:hostname port:(self portOfService:aService)) ifFalse:[
|
|
601 |
^ nil
|
|
602 |
]
|
28
|
603 |
].
|
|
604 |
^ newSock
|
|
605 |
"
|
|
606 |
same as:
|
|
607 |
^ (self new) for:hostname port:(self portOfService:aPort).
|
|
608 |
"
|
|
609 |
"
|
|
610 |
Socket newTCPclientToHost:'slsv6bt' port:'nntp'
|
|
611 |
"
|
|
612 |
!
|
|
613 |
|
|
614 |
newTCPserverAtPort:aService
|
|
615 |
"create a new TCP server socket providing service."
|
|
616 |
|
|
617 |
|newSock|
|
|
618 |
|
|
619 |
newSock := self newTCP.
|
|
620 |
newSock notNil ifTrue:[
|
42
|
621 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[
|
|
622 |
^ nil
|
|
623 |
]
|
28
|
624 |
].
|
|
625 |
^ newSock
|
|
626 |
"
|
|
627 |
same as:
|
|
628 |
^ (self new) for:nil port:aPort
|
|
629 |
"
|
|
630 |
!
|
|
631 |
|
|
632 |
newUDPserverAtPort:aService
|
|
633 |
"create a new UDP server socket providing service."
|
|
634 |
|
|
635 |
|newSock|
|
|
636 |
|
|
637 |
newSock := self newUDP.
|
|
638 |
newSock notNil ifTrue:[
|
42
|
639 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[
|
|
640 |
^ nil
|
|
641 |
]
|
28
|
642 |
].
|
|
643 |
^ newSock
|
|
644 |
"
|
|
645 |
same as:
|
|
646 |
^ (self new) for:nil udpPort:aPort
|
|
647 |
"
|
0
|
648 |
! !
|
|
649 |
|
|
650 |
!Socket methodsFor:'socket setup'!
|
|
651 |
|
25
|
652 |
domain:domainArg type:typeArg
|
71
|
653 |
"set up socket with domain and type.
|
25
|
654 |
This is a low level entry; no binding, listening or connect
|
71
|
655 |
is done. Both arguments must be symbols from one of
|
|
656 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp."
|
0
|
657 |
|
42
|
658 |
|errorNr|
|
|
659 |
|
28
|
660 |
filePointer notNil ifTrue:[
|
42
|
661 |
^ self error:'already created'
|
28
|
662 |
].
|
0
|
663 |
%{
|
|
664 |
FILE *fp;
|
|
665 |
int dom, typ, proto, sock;
|
|
666 |
|
28
|
667 |
if (! __isSymbol(domainArg)) {
|
71
|
668 |
DBGPRINTF(("bad domain\n"));
|
42
|
669 |
RETURN ( nil );
|
21
|
670 |
}
|
28
|
671 |
if (! __isSymbol(typeArg)) {
|
71
|
672 |
DBGPRINTF(("bad type\n"));
|
42
|
673 |
RETURN ( nil );
|
21
|
674 |
}
|
|
675 |
|
0
|
676 |
/*
|
|
677 |
* get address-family
|
|
678 |
*/
|
|
679 |
#ifdef AF_UNIX
|
28
|
680 |
if (domainArg == @symbol(unix))
|
42
|
681 |
dom = AF_UNIX;
|
0
|
682 |
else
|
|
683 |
#endif
|
|
684 |
#ifdef AF_INET
|
28
|
685 |
if (domainArg == @symbol(inet))
|
42
|
686 |
dom = AF_INET;
|
0
|
687 |
else
|
|
688 |
#endif
|
|
689 |
#ifdef AF_NS
|
28
|
690 |
if (domainArg == @symbol(ns))
|
42
|
691 |
dom = AF_NS;
|
0
|
692 |
else
|
|
693 |
#endif
|
28
|
694 |
#ifdef AF_DECnet
|
|
695 |
if (domainArg == @symbol(DECnet))
|
42
|
696 |
dom = AF_DECnet;
|
28
|
697 |
else
|
|
698 |
#endif
|
0
|
699 |
#ifdef AF_APPLETALK
|
28
|
700 |
if (domainArg == @symbol(appletalk))
|
42
|
701 |
dom = AF_APPLETALK;
|
0
|
702 |
else
|
|
703 |
#endif
|
|
704 |
#ifdef AF_X25
|
28
|
705 |
if (domainArg == @symbol(x25))
|
42
|
706 |
dom = AF_X25;
|
0
|
707 |
else
|
|
708 |
#endif
|
|
709 |
{
|
71
|
710 |
DBGPRINTF(("unknown domain <%s>\n", _stringVal(domainArg)));
|
42
|
711 |
RETURN ( nil );
|
0
|
712 |
}
|
|
713 |
|
|
714 |
#ifdef SOCK_STREAM
|
28
|
715 |
if (typeArg == @symbol(stream))
|
42
|
716 |
typ = SOCK_STREAM;
|
0
|
717 |
else
|
|
718 |
#endif
|
|
719 |
#ifdef SOCK_DGRAM
|
28
|
720 |
if (typeArg == @symbol(datagram))
|
42
|
721 |
typ = SOCK_DGRAM;
|
0
|
722 |
else
|
|
723 |
#endif
|
|
724 |
#ifdef SOCK_RAW
|
28
|
725 |
if (typeArg == @symbol(raw))
|
42
|
726 |
typ = SOCK_RAW;
|
0
|
727 |
else
|
|
728 |
#endif
|
|
729 |
#ifdef SOCK_SEQPACKET
|
28
|
730 |
if (typeArg == @symbol(seqPacket))
|
42
|
731 |
typ = SOCK_SEQPACKET;
|
0
|
732 |
else
|
|
733 |
#endif
|
|
734 |
{
|
71
|
735 |
DBGPRINTF(("bad type <%s>\n", _stringVal(typeArg)));
|
42
|
736 |
RETURN ( nil );
|
0
|
737 |
}
|
|
738 |
|
81
|
739 |
__BEGIN_INTERRUPTABLE__
|
6
|
740 |
do {
|
82
|
741 |
printf("opening socket type=%d\n", typ);
|
42
|
742 |
sock = socket(dom, typ, 0);
|
6
|
743 |
} while ((sock < 0) && (errno == EINTR));
|
81
|
744 |
__END_INTERRUPTABLE__
|
6
|
745 |
|
42
|
746 |
if (sock < 0) {
|
71
|
747 |
DBGPRINTF(("socket call failed\n"));
|
42
|
748 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
749 |
} else {
|
|
750 |
/*
|
|
751 |
* make it a FILE *
|
|
752 |
*/
|
|
753 |
fp = fdopen(sock, "r+");
|
|
754 |
if (! fp) {
|
71
|
755 |
DBGPRINTF(("fdopen call failed\n"));
|
42
|
756 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
81
|
757 |
__BEGIN_INTERRUPTABLE__
|
|
758 |
close(sock);
|
|
759 |
__END_INTERRUPTABLE__
|
42
|
760 |
} else {
|
|
761 |
_INST(filePointer) = MKOBJ(fp);
|
|
762 |
}
|
|
763 |
}
|
0
|
764 |
%}
|
21
|
765 |
.
|
|
766 |
"all ok?"
|
42
|
767 |
filePointer notNil ifTrue:[
|
|
768 |
domain := domainArg.
|
|
769 |
socketType := typeArg.
|
|
770 |
] ifFalse:[
|
|
771 |
^ nil
|
|
772 |
].
|
21
|
773 |
|
42
|
774 |
"
|
|
775 |
Socket new domain:#inet type:#stream
|
71
|
776 |
Socket new domain:#unix type:#stream
|
42
|
777 |
"
|
0
|
778 |
!
|
|
779 |
|
|
780 |
for:hostName udpPort:portNr
|
28
|
781 |
"setup for a UDP socket (i.e. inet domain, datagram type)
|
|
782 |
if hostname is nil, a server port is opened;
|
|
783 |
otherwise a client port to the server on host.
|
|
784 |
|
|
785 |
HISTORIC LEFTOVER:
|
|
786 |
This method will vanish, as soon as the low level
|
|
787 |
connect/bind works,"
|
|
788 |
|
71
|
789 |
self obsoleteMethodWarning.
|
|
790 |
|
28
|
791 |
filePointer notNil ifTrue:[
|
42
|
792 |
^ self error:'already created'
|
|
793 |
].
|
|
794 |
(portNr isMemberOf:SmallInteger) ifFalse:[
|
|
795 |
^ self error:'invalid portNr'
|
28
|
796 |
].
|
0
|
797 |
%{
|
|
798 |
struct sockaddr_in sa ;
|
|
799 |
struct hostent *hp ;
|
|
800 |
int a, sock ;
|
|
801 |
long addr;
|
|
802 |
FILE *fp;
|
6
|
803 |
int ret;
|
0
|
804 |
|
|
805 |
if (hostName != nil) {
|
42
|
806 |
bzero(&sa, sizeof(sa)) ;
|
|
807 |
if ((addr = inet_addr((char *) _stringVal(hostName))) != -1) {
|
|
808 |
/*
|
|
809 |
* is Internet addr in octet notation
|
|
810 |
*/
|
|
811 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */
|
|
812 |
sa.sin_family = AF_INET ;
|
|
813 |
} else {
|
|
814 |
/*
|
|
815 |
* is hostname -
|
|
816 |
* do we know the host's address?
|
|
817 |
*/
|
|
818 |
if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
|
71
|
819 |
DBGPRINTF(("unknown host\n"));
|
42
|
820 |
RETURN ( nil );
|
|
821 |
}
|
|
822 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
|
|
823 |
sa.sin_family = hp->h_addrtype ;
|
|
824 |
}
|
0
|
825 |
} else
|
42
|
826 |
sa.sin_family = AF_INET;
|
0
|
827 |
|
25
|
828 |
/*
|
|
829 |
* create the socket
|
|
830 |
*/
|
81
|
831 |
__BEGIN_INTERRUPTABLE__
|
6
|
832 |
do {
|
42
|
833 |
sock = socket(sa.sin_family, SOCK_DGRAM, 0);
|
6
|
834 |
} while ((sock < 0) && (errno == EINTR));
|
81
|
835 |
__END_INTERRUPTABLE__
|
|
836 |
|
0
|
837 |
if (sock < 0) {
|
71
|
838 |
DBGPRINTF(("socket call failed\n"));
|
42
|
839 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
0
|
840 |
} else {
|
42
|
841 |
/*
|
|
842 |
* ok,
|
|
843 |
* connect/bind
|
|
844 |
*/
|
81
|
845 |
__BEGIN_INTERRUPTABLE__
|
42
|
846 |
if (hostName == nil) {
|
|
847 |
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
848 |
do {
|
|
849 |
ret = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
|
|
850 |
} while ((ret < 0) && (errno == EINTR));
|
|
851 |
} else {
|
|
852 |
sa.sin_port = htons((u_short) _intVal(portNr)) ;
|
|
853 |
do {
|
|
854 |
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
|
|
855 |
} while ((ret < 0) && (errno == EINTR));
|
|
856 |
}
|
81
|
857 |
__END_INTERRUPTABLE__
|
0
|
858 |
|
42
|
859 |
if (ret < 0) {
|
71
|
860 |
DBGPRINTF(("bind/connect call failed\n"));
|
42
|
861 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
81
|
862 |
__BEGIN_INTERRUPTABLE__
|
42
|
863 |
close(sock) ;
|
81
|
864 |
__END_INTERRUPTABLE__
|
42
|
865 |
} else {
|
|
866 |
/*
|
|
867 |
* make it a FILE *
|
|
868 |
*/
|
|
869 |
fp = fdopen(sock, "r+");
|
|
870 |
if (! fp) {
|
71
|
871 |
DBGPRINTF(("fdopen call failed\n"));
|
42
|
872 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
81
|
873 |
__BEGIN_INTERRUPTABLE__
|
42
|
874 |
close(sock);
|
81
|
875 |
__END_INTERRUPTABLE__
|
42
|
876 |
} else {
|
|
877 |
/*
|
|
878 |
setbuf(fp, NULL);
|
|
879 |
*/
|
|
880 |
_INST(filePointer) = MKOBJ(fp);
|
|
881 |
}
|
|
882 |
}
|
0
|
883 |
}
|
42
|
884 |
%}.
|
|
885 |
filePointer isNil ifTrue:[
|
|
886 |
^ nil
|
|
887 |
].
|
|
888 |
|
8
|
889 |
"
|
|
890 |
buffered := false.
|
|
891 |
"
|
|
892 |
mode := #readwrite.
|
21
|
893 |
binary := false.
|
|
894 |
|
|
895 |
domain := #inet.
|
|
896 |
socketType := #datagram.
|
|
897 |
protocol := portNr.
|
42
|
898 |
|
|
899 |
"
|
|
900 |
Socket new for:'clam' udpPort:(Socket portOfService:'echo')
|
|
901 |
"
|
0
|
902 |
!
|
|
903 |
|
|
904 |
for:hostName port:portNr
|
28
|
905 |
"setup for a TCP socket (i.e. inet domain, stream type)
|
|
906 |
If hostname is nil, a server port is opened,
|
|
907 |
otherwise a client port to the server on host.
|
|
908 |
|
|
909 |
HISTORIC LEFTOVER:
|
|
910 |
This method will vanish, as soon as the low level
|
|
911 |
connect/bind works,"
|
|
912 |
|
71
|
913 |
self obsoleteMethodWarning.
|
|
914 |
|
28
|
915 |
filePointer notNil ifTrue:[
|
42
|
916 |
^ self error:'already created'
|
|
917 |
].
|
|
918 |
(portNr isMemberOf:SmallInteger) ifFalse:[
|
|
919 |
^ self error:'invalid portNr'
|
28
|
920 |
].
|
0
|
921 |
%{
|
|
922 |
struct sockaddr_in sa ;
|
|
923 |
struct hostent *hp ;
|
|
924 |
int a, sock ;
|
|
925 |
long addr;
|
|
926 |
FILE *fp;
|
6
|
927 |
int ret;
|
25
|
928 |
int on = 1;
|
0
|
929 |
|
25
|
930 |
bzero((char *) &sa, sizeof(sa)) ;
|
|
931 |
sa.sin_family = AF_INET;
|
|
932 |
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
933 |
|
|
934 |
if ((hostName != nil) && __isString(hostName)){
|
42
|
935 |
bzero(&sa, sizeof(sa)) ;
|
|
936 |
if ((addr = inet_addr((char *) _stringVal(hostName))) != -1) {
|
|
937 |
/*
|
|
938 |
* is Internet addr in octet notation
|
|
939 |
*/
|
|
940 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */
|
|
941 |
} else {
|
|
942 |
/*
|
|
943 |
* do we know the host's address?
|
|
944 |
*/
|
|
945 |
if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
|
71
|
946 |
DBGPRINTF(("unknown host\n"));
|
42
|
947 |
RETURN ( nil );
|
|
948 |
}
|
|
949 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
|
|
950 |
sa.sin_family = hp->h_addrtype;
|
|
951 |
}
|
25
|
952 |
}
|
0
|
953 |
|
25
|
954 |
/*
|
|
955 |
* create the socket
|
|
956 |
*/
|
81
|
957 |
__BEGIN_INTERRUPTABLE__
|
6
|
958 |
do {
|
42
|
959 |
sock = socket(sa.sin_family, SOCK_STREAM, 0);
|
6
|
960 |
} while ((sock < 0) && (errno == EINTR));
|
81
|
961 |
__END_INTERRUPTABLE__
|
28
|
962 |
|
0
|
963 |
if (sock < 0) {
|
71
|
964 |
DBGPRINTF(("socket call failed\n"));
|
42
|
965 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
0
|
966 |
} else {
|
42
|
967 |
/*
|
|
968 |
* connect/bind
|
|
969 |
*/
|
|
970 |
sa.sin_port = htons((u_short) _intVal(portNr)) ;
|
81
|
971 |
|
|
972 |
__BEGIN_INTERRUPTABLE__
|
42
|
973 |
if (hostName != nil) {
|
|
974 |
do {
|
|
975 |
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
|
|
976 |
} while ((ret < 0) && (errno == EINTR));
|
|
977 |
} else {
|
25
|
978 |
#ifdef SO_REUSEADDR
|
42
|
979 |
/*
|
|
980 |
* should I also do this for DGRAM sockets ?
|
|
981 |
*/
|
|
982 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
|
71
|
983 |
DBGPRINTF(("setsockopt - SO_REUSEADDR failed\n"));
|
42
|
984 |
}
|
25
|
985 |
#endif /* SO_REUSEADDR */
|
42
|
986 |
sa.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
987 |
do {
|
|
988 |
ret = bind(sock, (struct sockaddr *)&sa, sizeof(sa));
|
|
989 |
} while ((ret < 0) && (errno == EINTR));
|
|
990 |
}
|
81
|
991 |
__END_INTERRUPTABLE__
|
0
|
992 |
|
42
|
993 |
if (ret < 0) {
|
71
|
994 |
DBGPRINTF(("bind/connect call failed\n"));
|
42
|
995 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
81
|
996 |
__BEGIN_INTERRUPTABLE__
|
42
|
997 |
close(sock) ;
|
81
|
998 |
__END_INTERRUPTABLE__
|
42
|
999 |
} else {
|
|
1000 |
/*
|
|
1001 |
* make it a FILE *
|
|
1002 |
*/
|
|
1003 |
fp = fdopen(sock, "r+");
|
|
1004 |
if (! fp) {
|
71
|
1005 |
DBGPRINTF(("fdopen failed\n"));
|
42
|
1006 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
81
|
1007 |
__BEGIN_INTERRUPTABLE__
|
42
|
1008 |
close(sock);
|
81
|
1009 |
__END_INTERRUPTABLE__
|
42
|
1010 |
} else {
|
|
1011 |
/*
|
|
1012 |
setbuf(fp, NULL);
|
|
1013 |
*/
|
|
1014 |
_INST(filePointer) = MKOBJ(fp);
|
|
1015 |
}
|
|
1016 |
}
|
0
|
1017 |
}
|
42
|
1018 |
%}.
|
|
1019 |
filePointer isNil ifTrue:[
|
|
1020 |
^ nil
|
|
1021 |
].
|
8
|
1022 |
"
|
|
1023 |
buffered := false.
|
|
1024 |
"
|
|
1025 |
mode := #readwrite.
|
21
|
1026 |
binary := false.
|
|
1027 |
|
|
1028 |
domain := #inet.
|
|
1029 |
socketType := #stream.
|
|
1030 |
protocol := portNr.
|
42
|
1031 |
|
|
1032 |
"
|
|
1033 |
Socket new for:'clam' port:(Socket portOfService:'echo')
|
|
1034 |
"
|
0
|
1035 |
! !
|
|
1036 |
|
|
1037 |
!Socket methodsFor:'low level'!
|
|
1038 |
|
8
|
1039 |
closeFile
|
28
|
1040 |
"low level close"
|
8
|
1041 |
|
|
1042 |
%{ /* NOCONTEXT */
|
|
1043 |
|
25
|
1044 |
OBJ t;
|
|
1045 |
|
|
1046 |
t = _INST(filePointer);
|
|
1047 |
if (t != nil) {
|
42
|
1048 |
FILE *fp;
|
28
|
1049 |
|
42
|
1050 |
fp = MKFD(t);
|
|
1051 |
fflush(fp);
|
|
1052 |
shutdown(fileno(fp), 2);
|
|
1053 |
fclose(fp);
|
|
1054 |
_INST(filePointer) = nil;
|
25
|
1055 |
}
|
8
|
1056 |
%}
|
|
1057 |
!
|
|
1058 |
|
71
|
1059 |
bindTo:aSocketAddress
|
|
1060 |
"ST80 compatible bind:
|
|
1061 |
the socketAddress object is supposed to respond to
|
|
1062 |
portOrName and address requests."
|
|
1063 |
|
|
1064 |
^ self bindTo:(aSocketAddress portOrName)
|
|
1065 |
address:(aSocketAddress address)
|
|
1066 |
!
|
|
1067 |
|
|
1068 |
bindTo:portNrOrName address:address
|
28
|
1069 |
"low level bind - returns true if ok, false otherwise.
|
71
|
1070 |
Currently only non-address binding is supported;
|
|
1071 |
i.e. address must always be nil.
|
|
1072 |
|
|
1073 |
The interpretation of portNrOrName depends on the domain:
|
|
1074 |
inet uses (4byte) byteArray like internet numbers,
|
|
1075 |
unix uses pathname strings,
|
82
|
1076 |
others use whatever will come up in the future
|
71
|
1077 |
"
|
28
|
1078 |
|
|
1079 |
filePointer isNil ifTrue:[
|
42
|
1080 |
^ self error:'not a valid socket'
|
28
|
1081 |
].
|
0
|
1082 |
%{
|
28
|
1083 |
OBJ t = _INST(filePointer);
|
71
|
1084 |
OBJ myDomain;
|
28
|
1085 |
int sock;
|
0
|
1086 |
struct sockaddr_in insock;
|
6
|
1087 |
int ret;
|
25
|
1088 |
int on = 1;
|
28
|
1089 |
int ok;
|
82
|
1090 |
extern OBJ LargeInteger;
|
0
|
1091 |
|
28
|
1092 |
if (!__isString(_INST(domain)) && !__isSymbol(_INST(domain))) {
|
71
|
1093 |
DBGPRINTF(("invalid domain arg\n"));
|
42
|
1094 |
RETURN (false);
|
28
|
1095 |
}
|
|
1096 |
|
|
1097 |
ok = 0;
|
71
|
1098 |
myDomain = _INST(domain);
|
28
|
1099 |
#ifdef AF_INET
|
71
|
1100 |
if (myDomain == @symbol(inet)) {
|
42
|
1101 |
/*
|
|
1102 |
* INET addresses - port must be a smallinteger
|
|
1103 |
*/
|
|
1104 |
insock.sin_family = AF_INET;
|
71
|
1105 |
if (! __isSmallInteger(portNrOrName)) {
|
|
1106 |
DBGPRINTF(("invalid port arg\n"));
|
42
|
1107 |
RETURN (false);
|
|
1108 |
}
|
82
|
1109 |
insock.sin_port = htons((u_short) _intVal(portNrOrName));
|
42
|
1110 |
if (address == nil) {
|
|
1111 |
insock.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
1112 |
ok = 1;
|
|
1113 |
} else {
|
82
|
1114 |
if (__isInteger(address)) {
|
|
1115 |
insock.sin_addr.s_addr = htonl(__longIntVal(address));
|
|
1116 |
ok = 1;
|
|
1117 |
} else {
|
|
1118 |
printf("SOCKET: address bind not yet supported\n");
|
|
1119 |
RETURN (false);
|
|
1120 |
}
|
42
|
1121 |
}
|
28
|
1122 |
}
|
|
1123 |
#endif
|
|
1124 |
/*
|
|
1125 |
* XXXX add addressing stuff for other domains here ...
|
|
1126 |
*/
|
|
1127 |
#ifdef AF_UNIX
|
71
|
1128 |
if (myDomain == @symbol(unix)) {
|
28
|
1129 |
}
|
|
1130 |
#endif
|
|
1131 |
#ifdef AF_X25
|
71
|
1132 |
if (myDomain == @symbol(x25)) {
|
28
|
1133 |
}
|
|
1134 |
#endif
|
|
1135 |
#ifdef AF_NS
|
71
|
1136 |
if (myDomain == @symbol(ns)) {
|
28
|
1137 |
}
|
|
1138 |
#endif
|
|
1139 |
#ifdef AF_APPLETALK
|
71
|
1140 |
if (myDomain == @symbol(appletalk)) {
|
28
|
1141 |
}
|
|
1142 |
#endif
|
0
|
1143 |
|
28
|
1144 |
if (! ok) {
|
71
|
1145 |
DBGPRINTF(("unsupported domain\n"));
|
42
|
1146 |
RETURN (false);
|
28
|
1147 |
}
|
|
1148 |
|
|
1149 |
sock = fileno(MKFD(t));
|
|
1150 |
|
25
|
1151 |
#ifdef SO_REUSEADDR
|
28
|
1152 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) {
|
71
|
1153 |
DBGPRINTF(("setsockopt - SO_REUSEADDR failed\n"));
|
28
|
1154 |
}
|
25
|
1155 |
#endif /* SO_REUSEADDR */
|
28
|
1156 |
|
|
1157 |
do {
|
42
|
1158 |
ret = bind(sock, (struct sockaddr *)&insock, sizeof(insock));
|
28
|
1159 |
} while ((ret < 0) && (errno == EINTR));
|
|
1160 |
|
|
1161 |
if (ret < 0) {
|
71
|
1162 |
DBGPRINTF(("bind failed\n"));
|
42
|
1163 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
1164 |
RETURN (false);
|
0
|
1165 |
}
|
42
|
1166 |
%}.
|
|
1167 |
|
71
|
1168 |
port := portNrOrName.
|
28
|
1169 |
^ true
|
0
|
1170 |
|
28
|
1171 |
"
|
|
1172 |
(Socket domain:#inet type:#stream)
|
42
|
1173 |
bindTo:9999
|
|
1174 |
address:nil
|
28
|
1175 |
"
|
0
|
1176 |
!
|
|
1177 |
|
71
|
1178 |
connectTo:hostName port:portNrOrName
|
|
1179 |
"low level connect; connect to port, portNrOrName on host, hostName.
|
|
1180 |
Return true if ok, false otherwise.
|
|
1181 |
Hostname must be a string, portNrOrName an integer port number (in inet domain)."
|
28
|
1182 |
|
|
1183 |
filePointer isNil ifTrue:[
|
42
|
1184 |
^ self error:'not a valid socket'
|
28
|
1185 |
].
|
0
|
1186 |
%{
|
28
|
1187 |
OBJ t = _INST(filePointer);
|
71
|
1188 |
OBJ myDomain;
|
28
|
1189 |
struct sockaddr_in sa ;
|
|
1190 |
struct hostent *hp ;
|
|
1191 |
int a, sock ;
|
|
1192 |
long addr;
|
|
1193 |
FILE *fp;
|
|
1194 |
int ret;
|
|
1195 |
int on = 1;
|
|
1196 |
int ok;
|
|
1197 |
|
|
1198 |
if (! __isString(hostName)) {
|
71
|
1199 |
DBGPRINTF(("invalid hostname arg\n"));
|
42
|
1200 |
RETURN (false);
|
28
|
1201 |
}
|
|
1202 |
if (!__isString(_INST(domain)) && !__isSymbol(_INST(domain))) {
|
71
|
1203 |
DBGPRINTF(("invalid domain arg\n"));
|
42
|
1204 |
RETURN (false);
|
28
|
1205 |
}
|
|
1206 |
|
|
1207 |
ok = 0;
|
71
|
1208 |
myDomain = _INST(domain);
|
28
|
1209 |
#ifdef AF_INET
|
71
|
1210 |
if (myDomain == @symbol(inet)) {
|
|
1211 |
if (! __isSmallInteger(portNrOrName)) {
|
|
1212 |
DBGPRINTF(("invalid port arg\n"));
|
42
|
1213 |
RETURN (false);
|
|
1214 |
}
|
28
|
1215 |
|
42
|
1216 |
bzero((char *) &sa, sizeof(sa)) ;
|
|
1217 |
sa.sin_family = AF_INET;
|
71
|
1218 |
sa.sin_port = htons((u_short) _intVal(portNrOrName)) ;
|
28
|
1219 |
|
42
|
1220 |
if ((addr = inet_addr((char *) _stringVal(hostName))) != -1) {
|
|
1221 |
/*
|
|
1222 |
* is Internet addr in octet notation
|
|
1223 |
*/
|
|
1224 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */
|
|
1225 |
ok = 1;
|
|
1226 |
} else {
|
|
1227 |
/*
|
|
1228 |
* do we know the host's address?
|
|
1229 |
*/
|
|
1230 |
if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) {
|
71
|
1231 |
DBGPRINTF(("unknown host:%s\n", _stringVal(hostName)));
|
42
|
1232 |
RETURN (false);
|
|
1233 |
}
|
|
1234 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ;
|
|
1235 |
sa.sin_family = hp->h_addrtype;
|
|
1236 |
ok = 1;
|
|
1237 |
}
|
28
|
1238 |
}
|
|
1239 |
#endif
|
|
1240 |
/*
|
|
1241 |
* XXXX add addressing stuff for other domains here ...
|
|
1242 |
*/
|
|
1243 |
#ifdef AF_UNIX
|
71
|
1244 |
if (myDomain == @symbol(unix)) {
|
28
|
1245 |
}
|
|
1246 |
#endif
|
|
1247 |
#ifdef AF_X25
|
71
|
1248 |
if (myDomain == @symbol(x25)) {
|
28
|
1249 |
}
|
|
1250 |
#endif
|
|
1251 |
#ifdef AF_NS
|
71
|
1252 |
if (myDomain == @symbol(ns)) {
|
28
|
1253 |
}
|
|
1254 |
#endif
|
|
1255 |
#ifdef AF_APPLETALK
|
71
|
1256 |
if (myDomain == @symbol(appletalk)) {
|
28
|
1257 |
}
|
|
1258 |
#endif
|
|
1259 |
|
|
1260 |
if (! ok) {
|
71
|
1261 |
DBGPRINTF(("unsupported domain\n"));
|
42
|
1262 |
RETURN (false);
|
28
|
1263 |
}
|
|
1264 |
|
|
1265 |
sock = fileno(MKFD(t));
|
|
1266 |
|
|
1267 |
/*
|
|
1268 |
* connect
|
|
1269 |
*/
|
81
|
1270 |
__BEGIN_INTERRUPTABLE__
|
28
|
1271 |
|
|
1272 |
do {
|
42
|
1273 |
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa));
|
28
|
1274 |
} while ((ret < 0) && (errno == EINTR));
|
81
|
1275 |
__END_INTERRUPTABLE__
|
28
|
1276 |
|
|
1277 |
if (ret < 0) {
|
71
|
1278 |
DBGPRINTF(("connect failed\n"));
|
42
|
1279 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
1280 |
RETURN (false);
|
28
|
1281 |
}
|
42
|
1282 |
%}.
|
71
|
1283 |
port := portNrOrName.
|
28
|
1284 |
^ true
|
|
1285 |
!
|
|
1286 |
|
|
1287 |
listenWithBacklog:aNumber
|
|
1288 |
"start listening; return true if ok, false on error"
|
|
1289 |
|
|
1290 |
filePointer isNil ifTrue:[
|
42
|
1291 |
^ self error:'not a valid socket'
|
28
|
1292 |
].
|
|
1293 |
%{
|
|
1294 |
OBJ fp = _INST(filePointer);
|
|
1295 |
int sock;
|
6
|
1296 |
int ret;
|
0
|
1297 |
|
58
|
1298 |
if (! __isSmallInteger(aNumber)) {
|
71
|
1299 |
DBGPRINTF(("invalid arg\n"));
|
42
|
1300 |
RETURN (false);
|
28
|
1301 |
}
|
|
1302 |
|
|
1303 |
sock = fileno(MKFD(fp));
|
|
1304 |
|
81
|
1305 |
__BEGIN_INTERRUPTABLE__
|
28
|
1306 |
do {
|
42
|
1307 |
ret = listen(sock, _intVal(aNumber));
|
28
|
1308 |
} while ((ret < 0) && (errno == EINTR));
|
81
|
1309 |
__END_INTERRUPTABLE__
|
28
|
1310 |
|
|
1311 |
if (ret < 0) {
|
71
|
1312 |
DBGPRINTF(("listen call failed\n"));
|
42
|
1313 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
1314 |
RETURN (false);
|
28
|
1315 |
}
|
42
|
1316 |
%}.
|
28
|
1317 |
^ true
|
0
|
1318 |
!
|
|
1319 |
|
25
|
1320 |
listenFor:aNumber
|
|
1321 |
"same as listenWithBacklog: - for ST-80 compatibility"
|
|
1322 |
|
|
1323 |
^ self listenWithBacklog:aNumber
|
|
1324 |
!
|
|
1325 |
|
0
|
1326 |
acceptOn:aSocket
|
21
|
1327 |
"accept a connection on a server port (created with:'Socket>>onIPPort:')
|
25
|
1328 |
usage is: (Socket basicNew acceptOn:(Socket onIPPort:9999)).
|
42
|
1329 |
Return the true if ok; false if not.
|
25
|
1330 |
|
|
1331 |
NOTICE: this method will block, if no connection is already pending.
|
42
|
1332 |
use readWait or Socket>>accept."
|
0
|
1333 |
|
28
|
1334 |
|serverSocketFd|
|
0
|
1335 |
|
|
1336 |
filePointer notNil ifTrue:[
|
42
|
1337 |
^ self error:'already connected'
|
28
|
1338 |
].
|
|
1339 |
|
|
1340 |
domain := aSocket domain.
|
|
1341 |
socketType := aSocket type.
|
42
|
1342 |
serverSocketFd := aSocket fileDescriptor.
|
|
1343 |
serverSocketFd isNil ifTrue:[
|
|
1344 |
^ self error:'invalid server socket'
|
|
1345 |
].
|
|
1346 |
(serverSocketFd isMemberOf:SmallInteger) ifFalse:[
|
|
1347 |
^ self error:'invalid server socket'
|
|
1348 |
].
|
0
|
1349 |
%{
|
|
1350 |
FILE *fp;
|
|
1351 |
int flags;
|
28
|
1352 |
int sock, newSock;
|
0
|
1353 |
struct sockaddr_in sa ;
|
|
1354 |
int alen;
|
|
1355 |
struct hostent *he ;
|
|
1356 |
char dotted[20] ;
|
|
1357 |
|
|
1358 |
alen = sizeof(sa) ;
|
42
|
1359 |
sock = _intVal(serverSocketFd);
|
28
|
1360 |
|
|
1361 |
flags = ioctl(sock, F_GETFL, 0);
|
|
1362 |
ioctl(sock, F_SETFL, flags | O_NDELAY);
|
81
|
1363 |
__BEGIN_INTERRUPTABLE__
|
28
|
1364 |
do {
|
42
|
1365 |
newSock = accept(sock, (struct sockaddr *) &sa, &alen);
|
28
|
1366 |
} while ((newSock < 0) && (errno == EINTR));
|
81
|
1367 |
__END_INTERRUPTABLE__
|
28
|
1368 |
|
|
1369 |
if (newSock < 0) {
|
71
|
1370 |
DBGPRINTF(("accept call failed\n"));
|
42
|
1371 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
1372 |
RETURN (false);
|
8
|
1373 |
}
|
|
1374 |
|
28
|
1375 |
/*
|
|
1376 |
* extract the partners address
|
|
1377 |
*/
|
|
1378 |
#ifdef AF_INET
|
|
1379 |
if (_INST(domain) == @symbol(inet)) {
|
42
|
1380 |
he = gethostbyaddr((char *) &sa.sin_addr.s_addr, alen, AF_INET) ;
|
|
1381 |
if (! he) {
|
84
|
1382 |
unsigned long norder;
|
83
|
1383 |
|
42
|
1384 |
norder = htonl(sa.sin_addr.s_addr) ;
|
|
1385 |
sprintf(dotted, "%d.%d.%d.%d",
|
|
1386 |
(norder >> 24) & 0xFF,
|
|
1387 |
(norder >> 16) & 0xFF,
|
|
1388 |
(norder >> 8) & 0xFF,
|
|
1389 |
norder & 0xFF);
|
|
1390 |
}
|
71
|
1391 |
DBGPRINTF(("accepted connection from host %s\n", (he ? he->h_name : dotted))) ;
|
42
|
1392 |
_INST(peerName) = _MKSTRING((he ? he->h_name : dotted) COMMA_CON);
|
8
|
1393 |
}
|
0
|
1394 |
#endif
|
|
1395 |
|
25
|
1396 |
/*
|
|
1397 |
* make it a FILE *
|
|
1398 |
*/
|
28
|
1399 |
fp = fdopen(newSock, "r+");
|
8
|
1400 |
if (! fp) {
|
71
|
1401 |
DBGPRINTF(("fdopen call failed\n"));
|
42
|
1402 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
|
1403 |
close(newSock);
|
|
1404 |
RETURN (false);
|
|
1405 |
} else {
|
8
|
1406 |
/*
|
42
|
1407 |
setbuf(fp, NULL);
|
8
|
1408 |
*/
|
42
|
1409 |
_INST(filePointer) = MKOBJ(fp);
|
|
1410 |
}
|
|
1411 |
%}.
|
8
|
1412 |
"
|
|
1413 |
buffered := false.
|
|
1414 |
"
|
|
1415 |
mode := #readwrite.
|
28
|
1416 |
binary := false.
|
71
|
1417 |
port := aSocket port.
|
42
|
1418 |
^ true
|
25
|
1419 |
!
|
|
1420 |
|
|
1421 |
accept
|
|
1422 |
"create a new TCP socket from accepting on the receiver.
|
|
1423 |
This method will suspend the current process if no connection is waiting.
|
|
1424 |
For ST-80 compatibility"
|
|
1425 |
|
44
|
1426 |
|newSock|
|
|
1427 |
|
25
|
1428 |
self readWait.
|
44
|
1429 |
newSock := self class new.
|
|
1430 |
(newSock acceptOn:self) ifFalse:[^ nil].
|
|
1431 |
^ newSock
|
25
|
1432 |
|
|
1433 |
"
|
|
1434 |
|sock newSock|
|
|
1435 |
|
|
1436 |
sock := Socket provide:8004.
|
42
|
1437 |
sock listenFor:5.
|
|
1438 |
newSock := sock accept.
|
25
|
1439 |
"
|
|
1440 |
! !
|
|
1441 |
|
|
1442 |
!Socket methodsFor:'queries'!
|
|
1443 |
|
|
1444 |
getPeerName
|
|
1445 |
"return the peer name; thats the hostname (or dotted name) of the
|
|
1446 |
partners host after an accept."
|
|
1447 |
|
|
1448 |
^ peerName
|
28
|
1449 |
!
|
|
1450 |
|
81
|
1451 |
getName
|
|
1452 |
"return the name; here, we return the ports name"
|
|
1453 |
|
|
1454 |
^ port printString
|
|
1455 |
!
|
|
1456 |
|
71
|
1457 |
port
|
|
1458 |
"return the port number (or name for unix-sockets) to which the socket is bound"
|
28
|
1459 |
|
71
|
1460 |
^ port
|
28
|
1461 |
!
|
|
1462 |
|
|
1463 |
domain
|
71
|
1464 |
"return the sockets addressing domain (i.e. #inet, #unix, #x25, #appletalk)"
|
28
|
1465 |
|
|
1466 |
^ domain
|
|
1467 |
!
|
|
1468 |
|
|
1469 |
type
|
|
1470 |
"return the sockets connection type (i.e. #datagram, #stream etc)"
|
|
1471 |
|
|
1472 |
^ socketType
|
81
|
1473 |
!
|
|
1474 |
|
|
1475 |
isActive
|
|
1476 |
^ filePointer notNil
|
|
1477 |
! !
|
|
1478 |
|
|
1479 |
!Socket methodsFor:'ST-80 mimicri'!
|
|
1480 |
|
|
1481 |
errorReporter
|
|
1482 |
^ self
|
|
1483 |
!
|
|
1484 |
|
|
1485 |
notReadySignal
|
|
1486 |
"for now - this is not yet raised"
|
|
1487 |
|
|
1488 |
^ Signal new
|
28
|
1489 |
! !
|
|
1490 |
|
|
1491 |
!Socket methodsFor:'specials'!
|
|
1492 |
|
|
1493 |
sendTimeout:seconds
|
71
|
1494 |
"set the send timeout - for special applications only.
|
|
1495 |
Not all operatingSystems offer this functionality
|
|
1496 |
(returns false, if unsupported)"
|
28
|
1497 |
|
|
1498 |
|millis|
|
|
1499 |
|
|
1500 |
filePointer isNil ifTrue:[
|
42
|
1501 |
^ self error:'not a valid socket'
|
28
|
1502 |
].
|
|
1503 |
millis := (seconds * 1000) rounded.
|
|
1504 |
%{
|
|
1505 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) && defined(HZ)
|
58
|
1506 |
if (__isSmallInteger(millis)) {
|
42
|
1507 |
OBJ fp = _INST(filePointer);
|
|
1508 |
int sock;
|
|
1509 |
int opt;
|
28
|
1510 |
|
42
|
1511 |
sock = fileno(MKFD(fp));
|
|
1512 |
opt = _intVal(millis) / (1000 / HZ);
|
|
1513 |
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&opt, sizeof(int));
|
|
1514 |
RETURN(true);
|
28
|
1515 |
}
|
|
1516 |
#endif
|
71
|
1517 |
%}.
|
28
|
1518 |
^ false
|
|
1519 |
!
|
|
1520 |
|
|
1521 |
receiveTimeout:seconds
|
71
|
1522 |
"set the receive timeout - for special applications only.
|
|
1523 |
Not all operatingSystems offer this functionality
|
|
1524 |
(returns false, if unsupported)"
|
28
|
1525 |
|
|
1526 |
|millis|
|
|
1527 |
|
|
1528 |
filePointer isNil ifTrue:[
|
42
|
1529 |
^ self error:'not a valid socket'
|
28
|
1530 |
].
|
|
1531 |
millis := (seconds * 1000) rounded.
|
|
1532 |
%{
|
|
1533 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) && defined(HZ)
|
58
|
1534 |
if (__isSmallInteger(millis)) {
|
42
|
1535 |
OBJ fp = _INST(filePointer);
|
|
1536 |
int sock;
|
|
1537 |
int opt;
|
28
|
1538 |
|
42
|
1539 |
sock = fileno(MKFD(fp));
|
|
1540 |
opt = _intVal(millis) / (1000 / HZ);
|
|
1541 |
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&opt, sizeof(int));
|
|
1542 |
RETURN(true);
|
28
|
1543 |
}
|
|
1544 |
#endif
|
71
|
1545 |
%}.
|
28
|
1546 |
^ false
|
0
|
1547 |
! !
|
21
|
1548 |
|
|
1549 |
!Socket methodsFor:'datagram transmission'!
|
|
1550 |
|
81
|
1551 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes
|
71
|
1552 |
"receive datagramm data - put address of originating host into
|
81
|
1553 |
anAddressBuffer, data into aBuffer. For ST-80 compatibility,
|
|
1554 |
the addressBuffer may be a non-ByteArray; then, it must understand
|
|
1555 |
the addressBytes-message (i.e. be a SocketAddress instance).
|
71
|
1556 |
Return the number of bytes received, or a negative number on error.
|
|
1557 |
On error, the unix error code is left in the lastErrorNumber
|
|
1558 |
instance variable."
|
25
|
1559 |
|
81
|
1560 |
|addrBytes addrLen nReceived|
|
|
1561 |
|
|
1562 |
addrBytes := ByteArray new:100.
|
25
|
1563 |
%{
|
|
1564 |
OBJ oClass;
|
|
1565 |
OBJ fp = _INST(filePointer);
|
|
1566 |
int nInstVars, nInstBytes, objSize;
|
28
|
1567 |
int sock;
|
25
|
1568 |
struct sockaddr_in sa ;
|
|
1569 |
int alen;
|
|
1570 |
int n;
|
|
1571 |
char *cp;
|
|
1572 |
int flags = 0;
|
|
1573 |
|
|
1574 |
if (fp != nil) {
|
42
|
1575 |
sock = fileno((FILE *)(_intVal(fp)));
|
21
|
1576 |
|
71
|
1577 |
oClass = __Class(aDataBuffer);
|
42
|
1578 |
switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
|
|
1579 |
case BYTEARRAY:
|
|
1580 |
case WORDARRAY:
|
|
1581 |
case LONGARRAY:
|
|
1582 |
case FLOATARRAY:
|
|
1583 |
case DOUBLEARRAY:
|
|
1584 |
break;
|
|
1585 |
default:
|
|
1586 |
goto bad;
|
|
1587 |
}
|
81
|
1588 |
|
42
|
1589 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
|
|
1590 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
|
1591 |
objSize = _Size(aDataBuffer) - nInstBytes;
|
|
1592 |
cp = (char *)_InstPtr(aDataBuffer) + nInstBytes;
|
81
|
1593 |
if (__isSmallInteger(startIndex)) {
|
|
1594 |
cp += __intVal(startIndex);
|
|
1595 |
objSize -= __intVal(startIndex);
|
|
1596 |
}
|
|
1597 |
if (__isSmallInteger(nBytes)) {
|
|
1598 |
if (__intVal(nBytes) < objSize) {
|
|
1599 |
objSize = __intVal(nBytes);
|
|
1600 |
}
|
|
1601 |
}
|
25
|
1602 |
|
81
|
1603 |
__BEGIN_INTERRUPTABLE__
|
|
1604 |
do {
|
|
1605 |
if (addrBytes == nil) {
|
|
1606 |
n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) 0, 0);
|
|
1607 |
} else {
|
|
1608 |
n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) &sa, &alen);
|
|
1609 |
}
|
|
1610 |
} while ((n < 0) && (errno == EINTR));
|
|
1611 |
__END_INTERRUPTABLE__
|
|
1612 |
|
|
1613 |
if (n >= 0) {
|
|
1614 |
if (addrBytes != nil) {
|
|
1615 |
oClass = __Class(addrBytes);
|
|
1616 |
if ((_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) != BYTEARRAY)
|
|
1617 |
goto bad;
|
|
1618 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
|
|
1619 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
|
1620 |
objSize = _Size(addrBytes) - nInstBytes;
|
|
1621 |
cp = (char *)_InstPtr(addrBytes) + nInstBytes;
|
|
1622 |
if (objSize < alen)
|
|
1623 |
goto bad;
|
|
1624 |
bcopy((char *)&sa, cp, alen);
|
|
1625 |
addrLen = _MKSMALLINT(alen);
|
42
|
1626 |
}
|
|
1627 |
}
|
71
|
1628 |
if (n < 0) {
|
|
1629 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
42
|
1630 |
}
|
81
|
1631 |
nReceived = _MKSMALLINT(n);
|
25
|
1632 |
}
|
|
1633 |
bad: ;
|
42
|
1634 |
%}.
|
81
|
1635 |
nReceived notNil ifTrue:[
|
|
1636 |
nReceived < 0 ifTrue:[
|
|
1637 |
(OperatingSystem errorTextForNumber:lastErrorNumber) printNL.
|
|
1638 |
].
|
|
1639 |
addrLen notNil ifTrue:[
|
|
1640 |
anAddressBuffer class isBytes ifTrue:[
|
|
1641 |
anAddressBuffer replaceFrom:1 to:addrLen with:addrBytes
|
|
1642 |
] ifFalse:[
|
|
1643 |
"/ can be SocketAddress for ST-80 compatibility
|
|
1644 |
anAddressBuffer addressBytes:(addrBytes copyTo:addrLen)
|
|
1645 |
].
|
|
1646 |
].
|
|
1647 |
^ nReceived
|
|
1648 |
].
|
42
|
1649 |
"
|
|
1650 |
arrive here if you try to receive into an invalid buffer
|
71
|
1651 |
(i.e. not ByteArray-like),
|
|
1652 |
or if the addressBuffer is nonNil AND not a ByteArray/String
|
|
1653 |
or if the addressBuffer is nonNil AND too small.
|
42
|
1654 |
"
|
25
|
1655 |
self primitiveFailed
|
21
|
1656 |
!
|
|
1657 |
|
81
|
1658 |
receiveFrom:anAddressBuffer buffer:aDataBuffer
|
|
1659 |
"receive datagramm data - put address of originating host into
|
|
1660 |
anAddressBuffer, data into aBuffer.
|
|
1661 |
Both must be ByteArray-like. The addressBuffer must
|
|
1662 |
provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray).
|
|
1663 |
Return the number of bytes received, or a negative number on error.
|
|
1664 |
On error, the unix error code is left in the lastErrorNumber
|
|
1665 |
instance variable."
|
|
1666 |
|
|
1667 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size)
|
|
1668 |
!
|
|
1669 |
|
|
1670 |
sendTo:anAddressBuffer buffer:buffer
|
71
|
1671 |
"send datagramm data - fetch address of destination host from
|
21
|
1672 |
anAddressBuffer, data from aDataBuffer.
|
71
|
1673 |
Both must be ByteArray-like. The bytes in the addressBuffer must
|
|
1674 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
|
|
1675 |
Return the number of bytes transmitted, or a negative number on error.
|
|
1676 |
On error, the unix error code is left in the lastErrorNumber
|
81
|
1677 |
instance variable.
|
|
1678 |
Flags is currently ignored; it is there for ST-80 compatibility."
|
|
1679 |
|
|
1680 |
^ self sendTo:anAddressBuffer buffer:buffer start:1 for:buffer size flags:0
|
|
1681 |
!
|
|
1682 |
|
|
1683 |
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:count flags:flags
|
|
1684 |
"send datagramm data - fetch address of destination host from
|
|
1685 |
anAddressBuffer, data from aDataBuffer starting at startIndex,
|
|
1686 |
sending count bytes.
|
|
1687 |
Both must be ByteArray-like. The bytes in the addressBuffer must
|
|
1688 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray).
|
|
1689 |
Return the number of bytes transmitted, or a negative number on error.
|
|
1690 |
On error, the unix error code is left in the lastErrorNumber
|
71
|
1691 |
instance variable."
|
21
|
1692 |
|
82
|
1693 |
|addrBytes addrLen nReceived portNo|
|
81
|
1694 |
|
82
|
1695 |
"/ addressBuffer can be a 6-byte byteArray (last 2 bytes are portNo, msb-first)
|
|
1696 |
"/ or an instance of IPSocketAddress
|
|
1697 |
"/
|
|
1698 |
anAddressBuffer class isBytes ifTrue:[
|
|
1699 |
addrBytes := anAddressBuffer copyFrom:1 to:4.
|
|
1700 |
portNo := ((anAddressBuffer at:5) bitShift:8)
|
|
1701 |
+ (anAddressBuffer at:6).
|
|
1702 |
] ifFalse:[
|
|
1703 |
addrBytes := anAddressBuffer hostAddress.
|
|
1704 |
portNo := anAddressBuffer port.
|
81
|
1705 |
].
|
25
|
1706 |
%{
|
|
1707 |
OBJ oClass;
|
|
1708 |
OBJ fp = _INST(filePointer);
|
|
1709 |
int nInstVars, nInstBytes, objSize;
|
28
|
1710 |
int sock;
|
81
|
1711 |
struct sockaddr_in sa;
|
|
1712 |
struct sockaddr *saPtr = (struct sockaddr *)&sa;
|
25
|
1713 |
int alen = sizeof(sa);
|
|
1714 |
int n;
|
|
1715 |
char *cp;
|
81
|
1716 |
int _flags = 0;
|
|
1717 |
int offs, nBytes;
|
83
|
1718 |
unsigned long norder;
|
25
|
1719 |
|
81
|
1720 |
_flags = __longIntVal(flags);
|
|
1721 |
|
|
1722 |
if ((fp != nil)
|
|
1723 |
&& __isSmallInteger(startIndex)
|
|
1724 |
&& __isSmallInteger(count)) {
|
42
|
1725 |
sock = fileno((FILE *)(_intVal(fp)));
|
25
|
1726 |
|
81
|
1727 |
if (addrBytes != nil) {
|
82
|
1728 |
if (! __isByteArray(addrBytes)) goto bad;
|
|
1729 |
cp = __ByteArrayInstPtr(addrBytes)->ba_element;
|
|
1730 |
n = __byteArraySize(addrBytes);
|
|
1731 |
if (alen < n) n = alen;
|
|
1732 |
/*
|
|
1733 |
printf("address is %d bytes ... %d.%d.%d.%d", n, cp[0], cp[1], cp[2], cp[3]);
|
|
1734 |
*/
|
|
1735 |
bcopy(cp, &sa.sin_addr.s_addr, n);
|
|
1736 |
sa.sin_family = AF_INET;
|
|
1737 |
sa.sin_port = htons((u_short) __intVal(portNo));
|
81
|
1738 |
} else {
|
|
1739 |
alen = 0;
|
|
1740 |
saPtr = (struct sockaddr *)0;
|
42
|
1741 |
}
|
25
|
1742 |
|
71
|
1743 |
oClass = __Class(aDataBuffer);
|
42
|
1744 |
switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) {
|
|
1745 |
case BYTEARRAY:
|
81
|
1746 |
offs = __intVal(startIndex) - 1;
|
|
1747 |
break;
|
42
|
1748 |
case WORDARRAY:
|
81
|
1749 |
offs = (__intVal(startIndex) - 1) * sizeof(short);
|
|
1750 |
break;
|
42
|
1751 |
case LONGARRAY:
|
81
|
1752 |
offs = (__intVal(startIndex) - 1) * sizeof(long);
|
|
1753 |
break;
|
42
|
1754 |
case FLOATARRAY:
|
81
|
1755 |
offs = (__intVal(startIndex) - 1) * sizeof(float);
|
|
1756 |
break;
|
42
|
1757 |
case DOUBLEARRAY:
|
81
|
1758 |
offs = (__intVal(startIndex) - 1) * sizeof(double);
|
|
1759 |
#ifdef NEED_DOUBLE_ALIGN
|
|
1760 |
offs += sizeof(long);
|
|
1761 |
#endif
|
42
|
1762 |
break;
|
|
1763 |
default:
|
|
1764 |
goto bad;
|
|
1765 |
}
|
81
|
1766 |
nBytes = __intVal(count);
|
|
1767 |
|
42
|
1768 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars);
|
|
1769 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ);
|
81
|
1770 |
objSize = __qSize(aDataBuffer) - nInstBytes;
|
42
|
1771 |
cp = (char *)_InstPtr(aDataBuffer) + nInstBytes;
|
81
|
1772 |
cp += offs;
|
|
1773 |
if ((offs + nBytes) > objSize) {
|
82
|
1774 |
/*
|
81
|
1775 |
printf("cut off ...\n");
|
82
|
1776 |
*/
|
81
|
1777 |
nBytes = objSize - offs;
|
|
1778 |
}
|
25
|
1779 |
|
82
|
1780 |
norder = htonl(sa.sin_addr.s_addr);
|
|
1781 |
/*
|
|
1782 |
printf("sending %d bytes ... to ", nBytes);
|
|
1783 |
printf("%d.%d.%d.%d\n",
|
|
1784 |
(norder >> 24) & 0xFF,
|
|
1785 |
(norder >> 16) & 0xFF,
|
|
1786 |
(norder >> 8) & 0xFF,
|
|
1787 |
norder & 0xFF);
|
|
1788 |
*/
|
|
1789 |
|
81
|
1790 |
__BEGIN_INTERRUPTABLE__
|
|
1791 |
do {
|
|
1792 |
n = sendto(sock, cp, nBytes, _flags, saPtr, alen);
|
|
1793 |
} while ((n < 0) && (errno == EINTR));
|
|
1794 |
__END_INTERRUPTABLE__
|
|
1795 |
|
71
|
1796 |
if (n < 0) {
|
|
1797 |
_INST(lastErrorNumber) = _MKSMALLINT(errno);
|
42
|
1798 |
}
|
71
|
1799 |
RETURN (_MKSMALLINT(n));
|
25
|
1800 |
}
|
|
1801 |
bad: ;
|
42
|
1802 |
%}.
|
71
|
1803 |
"
|
|
1804 |
arrive here if you try to send from an invalid buffer
|
|
1805 |
(i.e. not ByteArray-like),
|
|
1806 |
or if the addressBuffer is nonNil AND not a ByteArray/String
|
|
1807 |
or if the addressBuffer is nonNil AND too small.
|
|
1808 |
"
|
25
|
1809 |
self primitiveFailed
|
21
|
1810 |
! !
|