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