author | Claus Gittinger <cg@exept.de> |
Tue, 25 Mar 1997 09:53:21 +0100 | |
changeset 509 | eb5a2d5bab0b |
parent 508 | 55d1e49c7157 |
child 510 | 0d076bd01e91 |
permissions | -rw-r--r-- |
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 |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
14 |
instanceVariableNames:'domain socketType protocol port serviceName peerName' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
15 |
classVariableNames:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
16 |
poolDictionaries:'' |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
17 |
category:'Streams-External' |
0 | 18 |
! |
19 |
||
126 | 20 |
!Socket primitiveDefinitions! |
21 |
%{ |
|
444 | 22 |
|
23 |
#ifdef LINUX |
|
24 |
# define BUGGY_STDIO_LIB |
|
25 |
#endif |
|
126 | 26 |
|
420 | 27 |
#ifdef WIN32 |
28 |
# undef INT |
|
421 | 29 |
# undef Array |
30 |
# undef Number |
|
31 |
# undef Method |
|
32 |
# undef Point |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
33 |
# undef Rectangle |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
34 |
# undef Block |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
35 |
# undef Context |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
36 |
# undef String |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
37 |
# undef Message |
463 | 38 |
# undef Object |
420 | 39 |
#endif |
40 |
||
126 | 41 |
#include <stdio.h> |
42 |
#include <errno.h> |
|
43 |
||
418 | 44 |
#if defined(transputer) || defined(MSDOS_LIKE) |
45 |
# define NO_SOCKET |
|
46 |
#endif |
|
47 |
||
126 | 48 |
#ifdef LINUX |
220 | 49 |
/* use inline string macros ... */ |
50 |
# define __STRINGDEFS__ |
|
51 |
# include <linuxIntern.h> |
|
52 |
||
126 | 53 |
/* kludge to avoid some redefines ... */ |
54 |
# define _ARPA_NAMESER_H |
|
55 |
# define _NETINET_TCP_H |
|
56 |
#endif |
|
57 |
||
465 | 58 |
#ifdef WIN32 |
463 | 59 |
|
418 | 60 |
# include <WINSOCK.H> |
421 | 61 |
|
62 |
# ifdef __DEF_Array |
|
63 |
# define Array __DEF_Array |
|
64 |
# endif |
|
65 |
# ifdef __DEF_Number |
|
66 |
# define Number __DEF_Number |
|
67 |
# endif |
|
68 |
# ifdef __DEF_Method |
|
69 |
# define Method __DEF_Method |
|
70 |
# endif |
|
71 |
# ifdef __DEF_Point |
|
72 |
# define Point __DEF_Point |
|
465 | 73 |
# endif |
74 |
# ifdef __DEF_Block |
|
75 |
# define Block __DEF_Block |
|
76 |
# endif |
|
77 |
# ifdef __DEF_Context |
|
78 |
# define Context __DEF_Context |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
79 |
# endif |
465 | 80 |
# ifdef __DEF_String |
81 |
# define String __DEF_String |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
82 |
# endif |
465 | 83 |
# ifdef __DEF_Message |
84 |
# define Message __DEF_Message |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
85 |
# endif |
465 | 86 |
# ifdef __DEF_Object |
87 |
# define Object __DEF_Object |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
88 |
# endif |
421 | 89 |
|
418 | 90 |
#else |
91 |
||
92 |
# if !defined(NO_SOCKET) |
|
93 |
# include <fcntl.h> |
|
94 |
# include <sys/types.h> |
|
126 | 95 |
|
418 | 96 |
# if defined(IRIS) && !defined(IRIX5) |
97 |
/* no socket.h on 4.0.5h ?!?!? */ |
|
98 |
# define AF_UNIX 1 |
|
99 |
# define AF_INET 2 |
|
100 |
# define SOCK_STREAM 1 |
|
101 |
# define SOCK_DGRAM 2 |
|
102 |
# define SOCK_RAW 3 |
|
103 |
# else |
|
104 |
# include <sys/socket.h> |
|
105 |
# endif |
|
126 | 106 |
|
418 | 107 |
# ifdef NEXT3 |
108 |
# include <netinet/in_systm.h> |
|
109 |
# endif |
|
126 | 110 |
|
418 | 111 |
# include <netdb.h> |
112 |
# include <netinet/in.h> |
|
113 |
# if ! (defined(SYSV3) && defined(mc88k)) |
|
114 |
# include <netinet/tcp.h> |
|
115 |
# endif |
|
126 | 116 |
# endif |
117 |
#endif |
|
118 |
||
119 |
/* |
|
120 |
* just in case those PF_xxx's are undefined |
|
121 |
*/ |
|
122 |
#ifdef AF_UNIX |
|
123 |
# ifndef PF_UNIX |
|
124 |
# define PF_UNIX AF_UNIX |
|
125 |
# endif |
|
126 |
#endif |
|
127 |
#ifdef AF_INET |
|
128 |
# ifndef PF_INET |
|
129 |
# define PF_INET AF_INET |
|
130 |
# endif |
|
131 |
#endif |
|
132 |
#ifdef AF_DECnet |
|
133 |
# ifndef PF_DECnet |
|
134 |
# define PF_DECnet AF_DECnet |
|
135 |
# endif |
|
136 |
#endif |
|
137 |
#ifdef AF_APPLETALK |
|
138 |
# ifndef PF_APPLETALK |
|
139 |
# define PF_APPLETALK AF_APPLETALK |
|
140 |
# endif |
|
141 |
#endif |
|
142 |
#ifdef AF_X25 |
|
143 |
# ifndef PF_X25 |
|
144 |
# define PF_X25 AF_X25 |
|
145 |
# endif |
|
146 |
#endif |
|
147 |
#ifdef AF_NS |
|
148 |
# ifndef PF_NS |
|
149 |
# define PF_NS AF_NS |
|
150 |
# endif |
|
151 |
#endif |
|
152 |
#ifdef AF_SNA |
|
153 |
# ifndef PF_SNA |
|
154 |
# define PF_SNA AF_SNA |
|
155 |
# endif |
|
156 |
#endif |
|
157 |
#ifdef AF_RAW |
|
158 |
# ifndef PF_RAW |
|
159 |
# define PF_RAW AF_RAW |
|
160 |
# endif |
|
161 |
#endif |
|
162 |
||
163 |
#ifdef AF_UNIX |
|
465 | 164 |
# ifndef WIN32 |
463 | 165 |
# include <sys/un.h> |
465 | 166 |
# endif |
126 | 167 |
#endif |
168 |
||
169 |
/* |
|
170 |
* on some systems errno is a macro ... check for it here |
|
171 |
*/ |
|
172 |
#ifndef errno |
|
173 |
extern errno; |
|
174 |
#endif |
|
175 |
||
176 |
static int __debugging__ = 0; |
|
177 |
||
178 |
#ifdef DEBUG |
|
179 |
# define DBGPRINTF(x) { if (__debugging__) printf x; } |
|
180 |
#else |
|
181 |
# define DBGPRINTF(x) /* as nothing */ |
|
182 |
#endif |
|
183 |
||
184 |
%} |
|
185 |
! ! |
|
186 |
||
464 | 187 |
!Socket class methodsFor:'documentation'! |
25 | 188 |
|
31 | 189 |
copyright |
190 |
" |
|
191 |
COPYRIGHT (c) 1992 by Claus Gittinger |
|
42 | 192 |
All Rights Reserved |
31 | 193 |
|
194 |
This software is furnished under a license and may be used |
|
195 |
only in accordance with the terms of that license and with the |
|
196 |
inclusion of the above copyright notice. This software may not |
|
197 |
be provided or otherwise made available to, or used by, any |
|
198 |
other person. No title to or ownership of the software is |
|
199 |
hereby transferred. |
|
200 |
" |
|
201 |
! |
|
202 |
||
25 | 203 |
documentation |
204 |
" |
|
31 | 205 |
This class provides access to (unix-)sockets for interprocess communication. |
25 | 206 |
The message protocol is preliminary, until someone tells me how |
31 | 207 |
other smalltalk's socket interfaces look like. |
25 | 208 |
|
209 |
Also, currently there is almost no support for other than IP |
|
210 |
sockets - this will be added in the future. |
|
28 | 211 |
Due to historic reasons (I started this class, before I got hold of some |
212 |
code using ST-80 Sockets i.e. RemoteInvocation), there is some old interface |
|
71 | 213 |
still provided. |
214 |
This will vanish; use the family:type: or newTCPxxx and newUDPxxx interface, |
|
215 |
together with the bind/listen and accept calls, |
|
216 |
which are meant to be compatible to ST-80's UnixSocketAccessor interface. |
|
217 |
||
218 |
ST/X does not use IPSocketAddress, UDSocketAddress erc; all addressing |
|
219 |
is done by passing appropriate string- or byteArray objects containing |
|
220 |
the addresses. This may change, too. |
|
221 |
||
222 |
TODO: cleanup historic leftovers, implement other than inet domain stuff. |
|
259 | 223 |
|
224 |
[author:] |
|
425 | 225 |
Claus Gittinger |
48 | 226 |
" |
227 |
! |
|
28 | 228 |
|
48 | 229 |
examples |
230 |
" |
|
28 | 231 |
example (get help info from an nntp server): |
425 | 232 |
[exBegin] |
233 |
|sock host| |
|
209 | 234 |
|
425 | 235 |
host := OperatingSystem getEnvironment:'NNTPSERVER'. |
28 | 236 |
|
425 | 237 |
sock := Socket newTCPclientToHost:host port:'nntp'. |
238 |
Transcript showCR:sock nextLine. |
|
239 |
sock buffered:false. |
|
93 | 240 |
|
425 | 241 |
sock nextPutAll:'HELP'; cr. |
242 |
[:exit | |
|
243 |
|line| |
|
209 | 244 |
|
425 | 245 |
line := sock nextLine. |
246 |
line = '.' ifTrue:[exit value:nil]. |
|
247 |
Transcript showCR:line. |
|
248 |
] loopWithExit. |
|
249 |
sock close |
|
250 |
[exEnd] |
|
209 | 251 |
|
28 | 252 |
|
209 | 253 |
example (connect to finger daemon, get users entry): |
425 | 254 |
[exBegin] |
255 |
|sock host entry| |
|
209 | 256 |
|
425 | 257 |
host := OperatingSystem getHostName. |
28 | 258 |
|
425 | 259 |
sock := Socket newTCPclientToHost:host port:'finger'. |
260 |
sock useCRLF:true. |
|
261 |
sock buffered:false. |
|
262 |
sock isNil ifTrue:[ |
|
263 |
Transcript showCR:'cannot connect to local finger daemon' |
|
264 |
] ifFalse:[ |
|
265 |
sock nextPutAll:(OperatingSystem getLoginName). |
|
266 |
sock cr. |
|
28 | 267 |
|
425 | 268 |
entry := sock nextLine. |
269 |
Transcript showCR:entry. |
|
209 | 270 |
|
425 | 271 |
sock close |
272 |
] |
|
273 |
[exEnd] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
274 |
|
28 | 275 |
example (connect to an ftp server): |
425 | 276 |
[exBegin] |
277 |
|sock host| |
|
28 | 278 |
|
425 | 279 |
host := OperatingSystem getHostName. |
280 |
sock := Socket newTCPclientToHost:host port:'ftp'. |
|
28 | 281 |
|
425 | 282 |
sock buffered:false. |
283 |
Transcript showCR:sock nextLine. |
|
284 |
sock nextPutAll:('USER ' , 'anonymous'); cr. |
|
285 |
Transcript showCR:sock nextLine. |
|
286 |
sock nextPutAll:('PASS ' , 'fooBar'); cr. |
|
287 |
Transcript showCR:sock nextLine. |
|
288 |
sock nextPutAll:'LIST'; cr. |
|
289 |
Transcript showCR:sock nextLine. |
|
290 |
sock close. |
|
28 | 291 |
|
425 | 292 |
'dont know enough of the ftp protocol to continue here ...' |
293 |
[exEnd] |
|
28 | 294 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
295 |
|
93 | 296 |
example (connect to an snmp server [UDP]): |
425 | 297 |
[exBegin] |
298 |
|sock port| |
|
42 | 299 |
|
425 | 300 |
sock := Socket newUDP. |
301 |
port := Socket portOfService:'snmp'. |
|
302 |
sock connectTo:(OperatingSystem getHostName) port:port. |
|
303 |
sock buffered:false. |
|
304 |
Transcript showCR:'got it'. |
|
305 |
sock close. |
|
306 |
[exEnd] |
|
42 | 307 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
308 |
|
42 | 309 |
example (await connection from a client and read some data): |
425 | 310 |
|connectSock sock| |
28 | 311 |
|
425 | 312 |
connectSock := Socket newTCPserverAtPort:9998. |
313 |
connectSock isNil ifTrue:[ |
|
314 |
Transcript showCR:'socket setup failed.'. |
|
315 |
] ifFalse:[ |
|
316 |
Transcript showCR:'listen ..'. |
|
317 |
(connectSock listenFor:5) ifFalse:[ |
|
318 |
Transcript showCR:'listen failed.'. |
|
319 |
] ifTrue:[ |
|
320 |
Transcript showCR:'wait'. |
|
321 |
connectSock readWait. |
|
322 |
Transcript showCR:'accept'. |
|
323 |
sock := connectSock accept. |
|
324 |
sock isNil ifTrue:[ |
|
325 |
Transcript showCR:'accept failed.'. |
|
326 |
] ifFalse:[ |
|
327 |
sock buffered:false. |
|
328 |
Transcript showCR:'server: got it'. |
|
329 |
'can now do transfer via sock'. |
|
330 |
Transcript showCR:'read'. |
|
331 |
Transcript showCR:('got: ' , sock nextLine). |
|
93 | 332 |
|
425 | 333 |
Transcript showCR:'close'. |
334 |
sock close |
|
335 |
]. |
|
336 |
connectSock close. |
|
337 |
] |
|
338 |
] |
|
42 | 339 |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
340 |
|
42 | 341 |
example (connect to above server and send some data): |
425 | 342 |
|sock| |
42 | 343 |
|
425 | 344 |
sock := Socket newTCPclientToHost:(OperatingSystem getHostName) port:9998. |
345 |
sock isNil ifTrue:[ |
|
346 |
Transcript showCR:'nope' |
|
347 |
] ifFalse:[ |
|
348 |
sock buffered:false. |
|
349 |
Transcript showCR:'client: got it'. |
|
350 |
'can now do transfer via sock'. |
|
351 |
Transcript showCR:'sending <hello>'. |
|
352 |
sock nextPutLine:'hello'. |
|
353 |
sock close |
|
354 |
] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
355 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
356 |
example: UNIX domain socket (await connection from a client and read some data): |
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
357 |
|
425 | 358 |
|connectSock sock| |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
359 |
|
425 | 360 |
'/tmp/ud_socket' asFilename remove. |
361 |
connectSock := Socket newUNIXserverAt:'/tmp/ud_socket'. |
|
362 |
connectSock isNil ifTrue:[ |
|
363 |
Transcript showCR:'socket setup failed.'. |
|
364 |
] ifFalse:[ |
|
365 |
Transcript showCR:'listen ..'. |
|
366 |
(connectSock listenFor:5) ifFalse:[ |
|
367 |
Transcript showCR:'listen failed.'. |
|
368 |
] ifTrue:[ |
|
369 |
Transcript showCR:'wait'. |
|
370 |
connectSock buffered:false. |
|
371 |
connectSock readWait. |
|
372 |
Transcript showCR:'accept'. |
|
373 |
sock := connectSock accept. |
|
374 |
sock isNil ifTrue:[ |
|
375 |
Transcript showCR:'accept failed.'. |
|
376 |
] ifFalse:[ |
|
377 |
sock buffered:false. |
|
378 |
Transcript showCR:'server: got it'. |
|
379 |
'can now do transfer via sock'. |
|
380 |
Transcript showCR:'read'. |
|
381 |
Transcript showCR:('got: ' , sock nextLine). |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
382 |
|
425 | 383 |
Transcript showCR:'close'. |
384 |
sock close |
|
385 |
]. |
|
386 |
connectSock close. |
|
387 |
] |
|
388 |
] |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
389 |
|
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
390 |
|
118 | 391 |
example (connect to above server and send some data; |
425 | 392 |
Notice, this fails, if above server code is executed in the same ST/X image |
393 |
(at least on LINUX), since the OS does not correctly handle |
|
394 |
a connect from within an interrupted accept system call |
|
395 |
On SGI's SVR4, this works ok |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
396 |
|
425 | 397 |
|sock| |
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
398 |
|
425 | 399 |
sock := Socket newUNIXclientTo:'/tmp/ud_socket'. |
400 |
sock isNil ifTrue:[ |
|
401 |
Transcript showCR:'nope' |
|
402 |
] ifFalse:[ |
|
403 |
sock buffered:false. |
|
404 |
Transcript showCR:'client: got it'. |
|
405 |
'can now do transfer via sock'. |
|
406 |
Transcript showCR:'sending <hello>'. |
|
407 |
sock nextPutLine:'hello'. |
|
408 |
sock close |
|
409 |
] |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
410 |
|
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
411 |
|
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
412 |
example: pingWalk (try to ping hosts on the local network) |
425 | 413 |
[exBegin] |
414 |
|myName myAddress list top hosts walkProcess port| |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
415 |
|
425 | 416 |
myName := OperatingSystem getHostName. |
417 |
myAddress := Socket ipAddressOfHost:myName. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
418 |
|
425 | 419 |
port := Socket portOfService:'echo'. |
420 |
port isNil ifTrue:[ |
|
421 |
self error:'dont know echo port'. |
|
422 |
^ self |
|
423 |
]. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
424 |
|
425 | 425 |
top := StandardSystemView new. |
426 |
top label:'PING net walk'. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
427 |
|
425 | 428 |
list := ScrollableView for:ListView in:top. |
429 |
list origin:0.0@0.0 corner:1.0@1.0. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
430 |
|
425 | 431 |
top openAndWait. |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
432 |
|
425 | 433 |
walkProcess := [ |
434 |
|l low hi direction tryHostID dottedName hostName conn addr| |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
435 |
|
425 | 436 |
l := SortedCollection new. |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
437 |
|
425 | 438 |
' only works with type C-net |
439 |
the code below could simply do 1 to:254 do:[:hostID } |
|
440 |
but, to probe likely hosts earlier, the probing is done |
|
441 |
ping-pong like around my ip-address (assuming, that other machines |
|
442 |
have numbers around my own)'. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
443 |
|
425 | 444 |
low := hi := (myAddress at:4). |
445 |
direction := 1. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
446 |
|
425 | 447 |
[low > 0 or:[hi < 255]] whileTrue:[ |
448 |
direction > 0 ifTrue:[ |
|
449 |
hi := hi + 1. |
|
450 |
tryHostID := hi. |
|
451 |
direction := -1. |
|
452 |
] ifFalse:[ |
|
453 |
low := low - 1. |
|
454 |
tryHostID := low. |
|
455 |
direction := 1. |
|
456 |
]. |
|
457 |
(tryHostID between:1 and:254) ifTrue:[ |
|
458 |
dottedName := (myAddress at:1) printString |
|
459 |
, '.' , (myAddress at:2) printString |
|
460 |
, '.' , (myAddress at:3) printString |
|
461 |
, '.' , tryHostID printString. |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
462 |
|
425 | 463 |
top label:'PING net walk - trying ' , dottedName. |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
464 |
|
425 | 465 |
top windowGroup withCursor:Cursor wait do:[ |
466 |
conn := Socket newTCPclientToHost:dottedName port:port withTimeout:1000. |
|
467 |
conn notNil ifTrue:[ |
|
468 |
addr := Socket ipAddressOfHost:dottedName. |
|
469 |
hostName := Socket hostWithIpAddress:addr. |
|
470 |
hostName isNil ifTrue:[ |
|
471 |
hostName :='?' |
|
472 |
]. |
|
473 |
l add:(dottedName paddedTo:15 with:Character space) |
|
474 |
, ' ' |
|
475 |
, (hostName paddedTo:15 with:Character space) |
|
476 |
, ' up & reachable'. |
|
477 |
list list:l. |
|
478 |
conn close. |
|
479 |
] |
|
480 |
]. |
|
481 |
]. |
|
482 |
]. |
|
483 |
top label:'PING reachable hosts'. |
|
484 |
] forkAt:(Processor userBackgroundPriority). |
|
485 |
walkProcess name:'ping net walker'. |
|
486 |
[exEnd] |
|
25 | 487 |
" |
488 |
! ! |
|
489 |
||
464 | 490 |
!Socket class methodsFor:'ST80 instance creation'! |
91 | 491 |
|
126 | 492 |
family:domainSymbol type:typeSymbol |
493 |
"create a socket for domain and type - ST80 simply uses a different name. |
|
494 |
Domain must be one of the symbols: #inet, #unix, #ns, #appletalk or #ns; |
|
495 |
Type must be #stream, #datagram or #raw |
|
91 | 496 |
|
126 | 497 |
XXX: currently only the #inet domain is supported" |
91 | 498 |
|
126 | 499 |
^ self domain:domainSymbol type:typeSymbol |
51 | 500 |
|
126 | 501 |
" |
502 |
Socket family:#inet type:#stream |
|
503 |
Socket family:#inet type:#datagram |
|
504 |
Socket family:#unix type:#stream |
|
505 |
" |
|
506 |
! ! |
|
93 | 507 |
|
464 | 508 |
!Socket class methodsFor:'ST80 queries'! |
115 | 509 |
|
126 | 510 |
sockStream |
511 |
"return the type code for stream sockets" |
|
51 | 512 |
|
126 | 513 |
^ #stream |
44 | 514 |
! ! |
515 |
||
464 | 516 |
!Socket class methodsFor:'Signal constants'! |
21 | 517 |
|
518 |
brokenConnectionSignal |
|
519 |
"return the signal used to tell broken connections. |
|
520 |
Since in unix, this is the same as the broken pipe signal, |
|
521 |
return that one. |
|
522 |
(for other Operatingsystems, this may change ..)" |
|
523 |
||
524 |
^ PipeStream brokenPipeSignal |
|
525 |
! ! |
|
526 |
||
464 | 527 |
!Socket class methodsFor:'debugging'! |
71 | 528 |
|
529 |
debug:aBoolean |
|
530 |
"turn on/off internal debugprints. |
|
531 |
This method is for ST/X debugging only and |
|
532 |
may be removed in later versions" |
|
533 |
||
534 |
%{ /* NOCONTEXT */ |
|
535 |
||
536 |
__debugging__ = (aBoolean == true); |
|
537 |
%} |
|
93 | 538 |
" |
539 |
Socket debug:true |
|
540 |
Socket debug:false |
|
541 |
" |
|
71 | 542 |
! ! |
543 |
||
464 | 544 |
!Socket class methodsFor:'easy tcp/ip instance creation'! |
126 | 545 |
|
546 |
connectTo:service on:host |
|
547 |
"standard & easy client setup: |
|
548 |
create new client tcp socket, bind and connect; |
|
549 |
return the socket. |
|
550 |
The system may block (interruptable), until the connection is |
|
551 |
established." |
|
552 |
||
553 |
^ (self new) for:host port:(self portOfService:service). |
|
554 |
||
555 |
" |
|
556 |
Socket connectTo:9995 on:'clam' |
|
557 |
Socket connectTo:'finger' on:'clam' |
|
558 |
Socket connectTo:'ftp' on:'clam' |
|
559 |
Socket connectTo:'nntp' on:(OperatingSystem getEnvironment:'NNTPSERVER') |
|
560 |
" |
|
561 |
! |
|
562 |
||
563 |
provide:service |
|
564 |
"standard & easy server setup: |
|
565 |
create a new TCP server socket providing a service." |
|
566 |
||
567 |
|newSock| |
|
568 |
||
569 |
newSock := (self new) for:nil port:(self portOfService:service). |
|
570 |
newSock notNil ifTrue:[ |
|
571 |
newSock listenFor:5. |
|
572 |
]. |
|
573 |
^ newSock |
|
574 |
||
575 |
" |
|
576 |
Socket provide:9995 |
|
577 |
(Socket provide:9996) accept |
|
578 |
Socket provide:'nntp' |
|
579 |
" |
|
580 |
! ! |
|
581 |
||
464 | 582 |
!Socket class methodsFor:'general instance creation'! |
126 | 583 |
|
584 |
domain:domainSymbol type:type |
|
585 |
"create a socket for domain and type - |
|
586 |
neither any connect nor binding is done. |
|
587 |
Domain must be one of the symbols: #inet, #unix, #ns, #appletalk or #ns; |
|
588 |
Type must be #stream, #datagram or #raw |
|
589 |
||
590 |
XXX: currently only the #inet domain is supported" |
|
591 |
||
592 |
^ self new domain:domainSymbol type:type |
|
593 |
||
594 |
" |
|
595 |
Socket domain:#inet type:#stream |
|
596 |
Socket domain:#inet type:#datagram |
|
597 |
Socket domain:#unix type:#stream |
|
598 |
Socket domain:#appletalk type:#stream |
|
599 |
Socket domain:#DECnet type:#stream |
|
600 |
" |
|
601 |
! |
|
602 |
||
603 |
newTCP |
|
604 |
"create a TCP socket - no binding or other setup is done, |
|
605 |
neither connect nor connect-wait is done." |
|
606 |
||
607 |
^ self new domain:#inet type:#stream |
|
608 |
||
609 |
"Socket newUDP" |
|
610 |
! |
|
611 |
||
612 |
newTCP:aServiceOrNil |
|
613 |
"create a TCP socket for a service - |
|
614 |
neither connect nor connect-wait is done." |
|
615 |
||
616 |
|newSock| |
|
617 |
||
618 |
newSock := self newTCP. |
|
619 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
620 |
newSock bindTo:(self portOfService:aServiceOrNil) address:nil |
|
621 |
]. |
|
622 |
^ newSock |
|
623 |
||
624 |
||
625 |
"Socket newTCP:'nntp'" |
|
626 |
"Socket newTCP:9995" |
|
627 |
! |
|
628 |
||
629 |
newTCPclientToHost:hostname port:aService |
|
630 |
"create a new TCP client socket connecting to a service. |
|
631 |
Return a socket instance if ok, nil on failure. |
|
632 |
Block until a connection is established (but only the current thread; |
|
633 |
not the whole smalltalk). |
|
634 |
See also: #newTCPclientToHost:port:withTimeout:" |
|
635 |
||
636 |
^ self newTCPclientToHost:hostname port:aService withTimeout:nil |
|
637 |
||
638 |
" |
|
639 |
Socket newTCPclientToHost:'localhost' port:'nntp' |
|
640 |
" |
|
641 |
||
642 |
"Created: 31.10.1995 / 18:54:11 / cg" |
|
643 |
! |
|
644 |
||
645 |
newTCPclientToHost:hostname port:aService withTimeout:millis |
|
646 |
"create a new TCP client socket connecting to a service. |
|
647 |
Return a socket instance if ok, nil on failure. |
|
648 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
649 |
and return nil.." |
|
650 |
||
651 |
|newSock| |
|
652 |
||
653 |
newSock := self newTCP. |
|
654 |
newSock notNil ifTrue:[ |
|
655 |
(newSock connectTo:hostname port:(self portOfService:aService) withTimeout:millis) ifFalse:[ |
|
656 |
newSock close. |
|
657 |
^ nil |
|
658 |
] |
|
659 |
]. |
|
660 |
^ newSock |
|
661 |
" |
|
662 |
same as: |
|
663 |
^ (self new) for:hostname port:(self portOfService:aPort). |
|
664 |
" |
|
665 |
" |
|
666 |
Socket newTCPclientToHost:'slsv6bt' port:'nntp' |
|
667 |
Socket newTCPclientToHost:'localhost' port:'nntp' withTimeout:1000 |
|
668 |
" |
|
669 |
! |
|
670 |
||
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
671 |
newTCPserverAtAnonymousPort |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
672 |
"create a new TCP server socket providing service on |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
673 |
a new anonymous port. The portNr is assigned by the OS." |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
674 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
675 |
^ self |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
676 |
newTCPserverAtPort:0 |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
677 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
678 |
! |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
679 |
|
126 | 680 |
newTCPserverAtPort:aService |
681 |
"create a new TCP server socket providing service." |
|
682 |
||
683 |
|newSock| |
|
684 |
||
685 |
newSock := self newTCP. |
|
686 |
newSock notNil ifTrue:[ |
|
687 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[ |
|
688 |
^ nil |
|
689 |
] |
|
690 |
]. |
|
691 |
^ newSock |
|
692 |
" |
|
693 |
same as: |
|
694 |
^ (self new) for:nil port:aPort |
|
695 |
" |
|
696 |
! |
|
697 |
||
698 |
newUDP |
|
699 |
"create a UDP socket - no binding or other setup is done, |
|
700 |
neither connect nor connect-wait is done." |
|
701 |
||
702 |
^ self new domain:#inet type:#datagram |
|
703 |
||
704 |
"Socket newUDP" |
|
705 |
! |
|
706 |
||
707 |
newUDP:aServiceOrNil |
|
708 |
"create a UDP socket for a service - |
|
709 |
neither connect nor connect-wait is done." |
|
710 |
||
711 |
|newSock| |
|
712 |
||
713 |
newSock := self newUDP. |
|
714 |
(newSock notNil and:[aServiceOrNil notNil]) ifTrue:[ |
|
715 |
newSock bindTo:(self portOfService:aServiceOrNil) address:nil |
|
716 |
]. |
|
717 |
^ newSock |
|
718 |
||
719 |
"Socket newUDP:nil" |
|
720 |
! |
|
721 |
||
722 |
newUDPserverAtPort:aService |
|
723 |
"create a new UDP server socket providing service." |
|
724 |
||
725 |
|newSock| |
|
726 |
||
727 |
newSock := self newUDP. |
|
728 |
newSock notNil ifTrue:[ |
|
729 |
(newSock bindTo:(self portOfService:aService) address:nil) ifFalse:[ |
|
730 |
^ nil |
|
731 |
] |
|
732 |
]. |
|
733 |
^ newSock |
|
734 |
" |
|
735 |
same as: |
|
736 |
^ (self new) for:nil udpPort:aPort |
|
737 |
" |
|
738 |
! |
|
739 |
||
740 |
newUNIX |
|
741 |
"create a UNIX domain socket - no binding or other setup is done, |
|
742 |
neither connect nor connect-wait is done." |
|
743 |
||
744 |
^ self new domain:#unix type:#stream |
|
745 |
||
746 |
" |
|
747 |
Socket newUNIX |
|
748 |
" |
|
749 |
! |
|
750 |
||
751 |
newUNIXclientTo:pathName |
|
752 |
"create a new UNIX client socket connecting to a pathname. |
|
753 |
Return a socket instance if ok, nil on failure. |
|
754 |
Block until a connection is established (but only the current thread; |
|
755 |
not the whole smalltalk). |
|
756 |
See also: #newUNIXclientTo:withTimeout:" |
|
757 |
||
758 |
^ self newUNIXclientTo:pathName withTimeout:nil |
|
759 |
||
760 |
! |
|
761 |
||
762 |
newUNIXclientTo:pathName withTimeout:millis |
|
763 |
"create a new UNIX client socket connecting to a pathname. |
|
764 |
Return a socket instance if ok, nil on failure. |
|
765 |
If the millis arg is nonNil, stop trying to connect after that many milliseconds |
|
766 |
and return nil.." |
|
767 |
||
768 |
|newSock| |
|
769 |
||
770 |
newSock := self newUNIX. |
|
771 |
newSock notNil ifTrue:[ |
|
772 |
(newSock connectTo:pathName port:nil withTimeout:millis) ifFalse:[ |
|
773 |
newSock close. |
|
774 |
^ nil |
|
775 |
] |
|
776 |
]. |
|
777 |
^ newSock |
|
778 |
||
779 |
" |
|
780 |
|s| |
|
781 |
||
782 |
s := Socket newUNIXclientTo:'/tmp/foo' |
|
783 |
" |
|
784 |
! |
|
785 |
||
786 |
newUNIXserverAt:pathName |
|
787 |
"create a new UNIX server socket providing service at a pathname." |
|
788 |
||
789 |
|newSock| |
|
790 |
||
791 |
newSock := self newUNIX. |
|
792 |
newSock notNil ifTrue:[ |
|
793 |
(newSock bindTo:pathName address:nil) ifFalse:[ |
|
794 |
^ nil |
|
795 |
] |
|
796 |
]. |
|
797 |
^ newSock |
|
798 |
||
799 |
" |
|
800 |
|s s2| |
|
801 |
||
802 |
s := Socket newUNIXserverAt:'/tmp/foo'. |
|
803 |
s listenFor:5. |
|
804 |
s2 := s accept. |
|
805 |
" |
|
806 |
! ! |
|
807 |
||
464 | 808 |
!Socket class methodsFor:'queries'! |
0 | 809 |
|
126 | 810 |
domainOfProtocol:aProtocol |
811 |
"given a protocols name (i.e. tcp, udp etc) return the domain. |
|
812 |
This method needs more ... - or is there a way to get this from the system ?" |
|
813 |
||
814 |
" |
|
815 |
tcp/ip stuff |
|
816 |
" |
|
817 |
(aProtocol = 'tcp') ifTrue:[^ #inet]. |
|
818 |
(aProtocol = 'udp') ifTrue:[^ #inet]. |
|
819 |
(aProtocol = 'ip') ifTrue:[^ #inet]. |
|
820 |
" |
|
821 |
unix domain |
|
822 |
" |
|
823 |
(aProtocol = 'ud') ifTrue:[^ #unix]. |
|
824 |
||
825 |
" |
|
826 |
add x25 stuff (if any) here ... |
|
827 |
" |
|
828 |
" |
|
829 |
add appletalk stuff (if any) here ... |
|
830 |
" |
|
831 |
" |
|
832 |
add other stuff (if any) here ... |
|
833 |
" |
|
834 |
^ nil |
|
835 |
||
836 |
" |
|
837 |
Socket domainOfProtocol:'tcp' |
|
838 |
Socket domainOfProtocol:'ucp' |
|
839 |
Socket domainOfProtocol:(Socket protocolOfService:'nntp') |
|
840 |
Socket domainOfProtocol:(Socket protocolOfService:'echo') |
|
841 |
" |
|
842 |
! |
|
843 |
||
844 |
hostWithIpAddress:anAddress |
|
845 |
"return the hostname for an IP (internet-) address. |
|
846 |
The address is supposed to be a byteArray consisting of 4 bytes, |
|
847 |
the network bytes come first (no matter what the local byteorder is). |
|
848 |
This is the reverse operation to #ipAddressOfHost:." |
|
849 |
||
850 |
|b1 b2 b3 b4| |
|
851 |
||
852 |
b1 := anAddress at:1. |
|
853 |
b2 := anAddress at:2. |
|
854 |
b3 := anAddress at:3. |
|
855 |
b4 := anAddress at:4. |
|
856 |
%{ |
|
418 | 857 |
#ifndef NO_SOCKET |
126 | 858 |
struct sockaddr_in sa ; |
859 |
struct hostent *hp ; |
|
860 |
||
861 |
if (__bothSmallInteger(b1, b2) && __bothSmallInteger(b3, b4)) { |
|
862 |
bzero(&sa, sizeof(sa)) ; |
|
863 |
sa.sin_addr.s_addr = _intVal(b1) & 0xFF; |
|
864 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b2) & 0xFF); |
|
865 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b3) & 0xFF); |
|
866 |
sa.sin_addr.s_addr = (sa.sin_addr.s_addr << 8) | (_intVal(b4) & 0xFF); |
|
867 |
sa.sin_addr.s_addr = htonl(sa.sin_addr.s_addr); |
|
868 |
sa.sin_family = AF_INET; |
|
869 |
/* do we know the host's address? */ |
|
870 |
hp = gethostbyaddr((char *) &sa.sin_addr.s_addr, sizeof(sa.sin_addr.s_addr), AF_INET); |
|
871 |
if (hp == NULL) { |
|
872 |
DBGPRINTF(("SOCKET: unknown address: %d.%d.%d.%d\n", |
|
873 |
_intVal(b1), _intVal(b2), _intVal(b3), _intVal(b4))); |
|
874 |
} else { |
|
875 |
sa.sin_family = hp->h_addrtype; |
|
876 |
} |
|
877 |
} |
|
878 |
||
879 |
/* if the addressing family is not AF_INET, return nil */ |
|
880 |
if (sa.sin_family != AF_INET) { |
|
881 |
RETURN ( nil ); |
|
882 |
} |
|
883 |
if (hp != NULL) |
|
448
2203a4bb620d
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
884 |
RETURN (__MKSTRING(hp->h_name)); |
126 | 885 |
|
448
2203a4bb620d
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
886 |
RETURN (__MKSTRING(inet_ntoa(sa.sin_addr))); |
418 | 887 |
#else |
888 |
RETURN (nil); |
|
889 |
#endif |
|
126 | 890 |
%} |
891 |
||
892 |
" |
|
893 |
Socket ipAddressOfHost:'clam' |
|
894 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'clam') |
|
895 |
Socket ipAddressOfHost:'porty' |
|
896 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'porty') |
|
897 |
Socket hostWithIpAddress:#[1 2 3 4] |
|
898 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'1.2.3.4') |
|
899 |
" |
|
900 |
! |
|
901 |
||
6 | 902 |
ipAddressOfHost:aHostName |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
903 |
"return the IP (internet-) number for a hostname as a byteArray, |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
904 |
where the network bytes come first (no matter what the cpus byteOrder is). |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
905 |
If the host is unknown, return nil. |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
906 |
This is the reverse operation to #hostWithIpAddress:." |
6 | 907 |
|
908 |
|b1 b2 b3 b4| |
|
909 |
||
910 |
%{ |
|
418 | 911 |
#ifndef NO_SOCKET |
6 | 912 |
struct sockaddr_in sa ; |
913 |
struct hostent *hp ; |
|
914 |
long addr; |
|
915 |
||
21 | 916 |
if (__isString(aHostName)) { |
42 | 917 |
bzero(&sa, sizeof(sa)) ; |
918 |
if ((addr = inet_addr((char *) _stringVal(aHostName))) != -1) { |
|
919 |
/* is Internet addr in octet notation */ |
|
920 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)); |
|
921 |
sa.sin_family = AF_INET; |
|
922 |
} else { |
|
923 |
/* do we know the host's address? */ |
|
924 |
if ((hp = gethostbyname((char *) _stringVal(aHostName))) == NULL) { |
|
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
925 |
DBGPRINTF(("SOCKET: unknown host\n")); |
42 | 926 |
RETURN ( nil ); |
927 |
} |
|
928 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ; |
|
929 |
sa.sin_family = hp->h_addrtype; |
|
930 |
} |
|
6 | 931 |
} |
932 |
/* if the addressing family is not AF_INET, return nil */ |
|
933 |
if (sa.sin_family != AF_INET) { |
|
42 | 934 |
RETURN ( nil ); |
6 | 935 |
} |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
936 |
|
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
937 |
sa.sin_addr.s_addr = ntohl(sa.sin_addr.s_addr); |
217 | 938 |
b1 = __MKSMALLINT((sa.sin_addr.s_addr >> 24) & 0xFF); |
939 |
b2 = __MKSMALLINT((sa.sin_addr.s_addr >> 16) & 0xFF); |
|
940 |
b3 = __MKSMALLINT((sa.sin_addr.s_addr >> 8) & 0xFF); |
|
941 |
b4 = __MKSMALLINT((sa.sin_addr.s_addr >> 0) & 0xFF); |
|
418 | 942 |
#else |
943 |
RETURN (nil); |
|
944 |
#endif |
|
6 | 945 |
%} |
946 |
. |
|
947 |
^ ByteArray with:b1 with:b2 with:b3 with:b4 |
|
21 | 948 |
|
71 | 949 |
" |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
950 |
Socket ipAddressOfHost:'clam' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
951 |
Socket ipAddressOfHost:'porty' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
952 |
Socket ipAddressOfHost:'axept' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
953 |
Socket ipAddressOfHost:'axept' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
954 |
Socket ipAddressOfHost:'1.2.3.4' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
955 |
Socket ipAddressOfHost:'193.15.16.17' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
956 |
Socket ipAddressOfHost:'josef' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
957 |
Socket ipAddressOfHost:'styx.com' |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
958 |
Socket hostWithIpAddress:(Socket ipAddressOfHost:'localhost') |
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
959 |
Socket ipAddressOfHost:(Socket hostWithIpAddress:'127.0.0.1') |
71 | 960 |
" |
6 | 961 |
! |
962 |
||
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
963 |
networkLongOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
964 |
"return true, if the network-byte-order of longs is MSB. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
965 |
To be used as in: |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
966 |
'aStream nextPutLong:someValue MSB:(Socket networkLongOrderIsMSB)'." |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
967 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
968 |
%{ /* NOCONTEXT */ |
418 | 969 |
#ifndef NO_SOCKET |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
970 |
/* |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
971 |
* mhmh - do c-compilers optimize this away ? |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
972 |
*/ |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
973 |
if (htonl(0x76543210) == 0x76543210) { |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
974 |
RETURN (true); |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
975 |
} |
418 | 976 |
#endif |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
977 |
%}. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
978 |
^ false |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
979 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
980 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
981 |
Socket networkLongOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
982 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
983 |
! |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
984 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
985 |
networkShortOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
986 |
"return true, if the network-byte-order of shorts is MSB. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
987 |
To be used as in: |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
988 |
'aStream nextPutShort:someValue MSB:(Socket networkShortOrderIsMSB)'." |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
989 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
990 |
%{ /* NOCONTEXT */ |
418 | 991 |
#ifndef NO_SOCKET |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
992 |
/* |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
993 |
* mhmh - do c-compilers optimize this away ? |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
994 |
*/ |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
995 |
if (htons(0x3210) == 0x3210) { |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
996 |
RETURN (true); |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
997 |
} |
418 | 998 |
#endif |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
999 |
%}. |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1000 |
^ false |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1001 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1002 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1003 |
Socket networkShortOrderIsMSB |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1004 |
" |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1005 |
! |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
1006 |
|
0 | 1007 |
portOfService:aNameOrNumber |
1008 |
"returns the port-number for a given service |
|
1009 |
or nil if no such service exists; |
|
1010 |
- used to convert service names to portNumbers" |
|
1011 |
||
91 | 1012 |
^ self portOfService:aNameOrNumber protocol:nil |
1013 |
||
28 | 1014 |
" |
1015 |
Socket portOfService:'finger' |
|
74 | 1016 |
Socket portOfService:'nntp' |
1017 |
Socket portOfService:'echo' |
|
1018 |
Socket portOfService:'snmp' |
|
28 | 1019 |
" |
0 | 1020 |
! |
1021 |
||
91 | 1022 |
portOfService:aNameOrNumber protocol:aProtocol |
1023 |
"returns the port-number for a given service |
|
1024 |
or nil if no such service exists; |
|
1025 |
- used to convert service names to portNumbers" |
|
1026 |
||
1027 |
%{ |
|
418 | 1028 |
#ifndef NO_SOCKET |
91 | 1029 |
struct servent *servent = NULL; |
1030 |
char *protocol; |
|
1031 |
int tryBoth = 0; |
|
1032 |
||
92 | 1033 |
if (__isString(aProtocol)) { |
93 | 1034 |
protocol = __stringVal(aProtocol); |
92 | 1035 |
} else { |
93 | 1036 |
protocol = "tcp"; |
1037 |
tryBoth = 1; |
|
92 | 1038 |
} |
1039 |
||
91 | 1040 |
if (__isSmallInteger(aNameOrNumber)) { |
93 | 1041 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), protocol); |
1042 |
if (servent != NULL) { |
|
1043 |
RETURN ( aNameOrNumber ); |
|
1044 |
} |
|
1045 |
if (tryBoth) { |
|
1046 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "udp"); |
|
1047 |
if (servent != NULL) { |
|
1048 |
RETURN ( aNameOrNumber ); |
|
1049 |
} |
|
1050 |
} |
|
1051 |
RETURN ( aNameOrNumber ); |
|
91 | 1052 |
} |
1053 |
if (__isString(aNameOrNumber)) { |
|
93 | 1054 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), protocol); |
1055 |
if (servent != NULL) { |
|
217 | 1056 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
93 | 1057 |
} |
1058 |
if (tryBoth) { |
|
1059 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "udp"); |
|
1060 |
if (servent != NULL) { |
|
217 | 1061 |
RETURN ( __MKSMALLINT(ntohs(servent->s_port)) ); |
93 | 1062 |
} |
1063 |
} |
|
1064 |
RETURN ( nil ); |
|
91 | 1065 |
} |
418 | 1066 |
#endif |
91 | 1067 |
RETURN ( nil ); |
1068 |
%} |
|
1069 |
" |
|
1070 |
Socket portOfService:'echo' protocol:'udp' |
|
1071 |
Socket portOfService:'echo' protocol:'tcp' |
|
1072 |
" |
|
1073 |
! |
|
1074 |
||
0 | 1075 |
protocolOfService:aNameOrNumber |
1076 |
"returns the protocol (as string) for a given service |
|
28 | 1077 |
or nil if no such service exists." |
0 | 1078 |
%{ |
418 | 1079 |
#ifndef NO_SOCKET |
0 | 1080 |
struct servent *servent = NULL; |
1081 |
||
58 | 1082 |
if (__isSmallInteger(aNameOrNumber)) { |
42 | 1083 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "tcp") ; |
1084 |
if (servent == NULL) { |
|
1085 |
servent = getservbyport(htons(_intVal(aNameOrNumber)), "udp") ; |
|
1086 |
if (servent == NULL) { |
|
1087 |
RETURN ( nil ); |
|
1088 |
} |
|
1089 |
} |
|
0 | 1090 |
} else { |
42 | 1091 |
if (__isString(aNameOrNumber)) { |
1092 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "tcp"); |
|
1093 |
if (servent == NULL) { |
|
1094 |
servent = getservbyname((char *) _stringVal(aNameOrNumber), "udp"); |
|
1095 |
if (servent == NULL) { |
|
1096 |
RETURN ( nil ); |
|
1097 |
} |
|
1098 |
} |
|
1099 |
} |
|
0 | 1100 |
} |
1101 |
if (servent) { |
|
448
2203a4bb620d
removed all COMMA_CON / CON_COMMA uses
Claus Gittinger <cg@exept.de>
parents:
444
diff
changeset
|
1102 |
RETURN ( __MKSTRING(servent->s_proto) ); |
0 | 1103 |
} |
418 | 1104 |
#endif |
0 | 1105 |
RETURN ( nil ); |
1106 |
%} |
|
28 | 1107 |
" |
74 | 1108 |
Socket protocolOfService:'finger' |
1109 |
Socket protocolOfService:'nntp' |
|
28 | 1110 |
Socket protocolOfService:'xxx' |
1111 |
Socket protocolOfService:79 |
|
74 | 1112 |
Socket protocolOfService:'snmp' |
28 | 1113 |
" |
1114 |
! |
|
1115 |
||
1116 |
typeOfProtocol:aProtocol |
|
1117 |
"given a protocols name (i.e. tcp, udp etc) return the connection type. |
|
1118 |
This method needs more ... - or is there a way to get this from the system ?" |
|
1119 |
||
1120 |
(aProtocol = 'tcp') ifTrue:[^ #stream]. |
|
1121 |
(aProtocol = 'udp') ifTrue:[^ #datagram]. |
|
1122 |
(aProtocol = 'ip') ifTrue:[^ #raw]. |
|
1123 |
" |
|
71 | 1124 |
unix domain |
1125 |
" |
|
1126 |
(aProtocol = 'ud') ifTrue:[^ #stream]. |
|
1127 |
" |
|
91 | 1128 |
add x25 stuff (if any) here ... |
1129 |
" |
|
1130 |
" |
|
1131 |
add appletalk stuff (if any) here ... |
|
1132 |
" |
|
1133 |
" |
|
1134 |
add other stuff (if any) here ... |
|
28 | 1135 |
" |
1136 |
^ nil |
|
1137 |
||
1138 |
" |
|
1139 |
Socket typeOfProtocol:'tcp' |
|
1140 |
Socket typeOfProtocol:'ucp' |
|
1141 |
Socket typeOfProtocol:(Socket protocolOfService:'nntp') |
|
1142 |
Socket typeOfProtocol:(Socket protocolOfService:'echo') |
|
1143 |
" |
|
1144 |
! ! |
|
1145 |
||
126 | 1146 |
!Socket methodsFor:'ST-80 mimicri'! |
28 | 1147 |
|
126 | 1148 |
errorReporter |
1149 |
^ self |
|
28 | 1150 |
! |
1151 |
||
126 | 1152 |
notReadySignal |
1153 |
"for now - this is not yet raised" |
|
71 | 1154 |
|
126 | 1155 |
^ Signal new |
486 | 1156 |
! |
1157 |
||
1158 |
readStream |
|
1159 |
"ST-80 mimicry. |
|
1160 |
In ST-80, socket is not a stream, but referes to one. |
|
1161 |
ST-80 code therefore uses 'Socket readStream' to access |
|
1162 |
the actual stream. This method returns the receiver, for transparency" |
|
1163 |
||
1164 |
^ self |
|
1165 |
||
1166 |
"Created: 24.1.1997 / 23:52:57 / cg" |
|
1167 |
! |
|
1168 |
||
1169 |
writeStream |
|
1170 |
"ST-80 mimicry. |
|
1171 |
In ST-80, socket is not a stream, but referes to one. |
|
1172 |
ST-80 code therefore uses 'Socket writeStream' to access |
|
1173 |
the actual stream. This method returns the receiver, for transparency" |
|
1174 |
||
1175 |
^ self |
|
1176 |
||
1177 |
"Created: 24.1.1997 / 10:34:35 / cg" |
|
1178 |
"Modified: 24.1.1997 / 23:52:52 / cg" |
|
71 | 1179 |
! ! |
1180 |
||
126 | 1181 |
!Socket methodsFor:'datagram transmission'! |
28 | 1182 |
|
126 | 1183 |
receiveFrom:anAddressBuffer buffer:aDataBuffer |
1184 |
"receive datagramm data - put address of originating host into |
|
1185 |
anAddressBuffer, data into aBuffer. |
|
1186 |
Both must be ByteArray-like. The addressBuffer must |
|
1187 |
provide space for a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1188 |
Return the number of bytes received, or a negative number on error. |
|
1189 |
On error, the unix error code is left in the lastErrorNumber |
|
1190 |
instance variable." |
|
28 | 1191 |
|
126 | 1192 |
^ self receiveFrom:anAddressBuffer buffer:aDataBuffer start:1 for:(aDataBuffer size) |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1193 |
! |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1194 |
|
126 | 1195 |
receiveFrom:anAddressBuffer buffer:aDataBuffer start:startIndex for:nBytes |
1196 |
"receive datagramm data - put address of originating host into |
|
1197 |
anAddressBuffer, data into aBuffer. For ST-80 compatibility, |
|
1198 |
the addressBuffer may be a non-ByteArray; then, it must understand |
|
1199 |
the addressBytes-message (i.e. be a SocketAddress instance). |
|
1200 |
Return the number of bytes received, or a negative number on error. |
|
1201 |
On error, the unix error code is left in the lastErrorNumber |
|
1202 |
instance variable." |
|
1203 |
||
1204 |
|addrBytes addrLen nReceived| |
|
28 | 1205 |
|
126 | 1206 |
addrBytes := ByteArray new:100. |
1207 |
%{ |
|
418 | 1208 |
#ifndef NO_SOCKET |
126 | 1209 |
OBJ oClass; |
217 | 1210 |
OBJ fp = __INST(filePointer); |
126 | 1211 |
int nInstVars, nInstBytes, objSize; |
1212 |
int sock; |
|
1213 |
struct sockaddr_in sa ; |
|
1214 |
int alen; |
|
1215 |
int n; |
|
1216 |
char *cp; |
|
1217 |
int flags = 0; |
|
1218 |
||
1219 |
if (fp != nil) { |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1220 |
sock = fileno(__FILEVal(fp)); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1221 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1222 |
oClass = __Class(aDataBuffer); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1223 |
switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1224 |
case BYTEARRAY: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1225 |
case WORDARRAY: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1226 |
case LONGARRAY: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1227 |
case FLOATARRAY: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1228 |
case DOUBLEARRAY: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1229 |
break; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1230 |
default: |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1231 |
goto bad; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1232 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1233 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1234 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1235 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1236 |
objSize = _Size(aDataBuffer) - nInstBytes; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1237 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1238 |
if (__isSmallInteger(startIndex)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1239 |
cp += __intVal(startIndex); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1240 |
objSize -= __intVal(startIndex); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1241 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1242 |
if (__isSmallInteger(nBytes)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1243 |
if (__intVal(nBytes) < objSize) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1244 |
objSize = __intVal(nBytes); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1245 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1246 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1247 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1248 |
__BEGIN_INTERRUPTABLE__ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1249 |
do { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1250 |
if (addrBytes == nil) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1251 |
n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) 0, 0); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1252 |
} else { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1253 |
n = recvfrom(sock, cp, objSize, flags, (struct sockaddr *) &sa, &alen); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1254 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1255 |
} while ((n < 0) && (errno == EINTR)); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1256 |
__END_INTERRUPTABLE__ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1257 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1258 |
if (n >= 0) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1259 |
if (addrBytes != nil) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1260 |
oClass = __Class(addrBytes); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1261 |
if ((_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) != BYTEARRAY) |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1262 |
goto bad; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1263 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1264 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1265 |
objSize = _Size(addrBytes) - nInstBytes; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1266 |
cp = (char *)__InstPtr(addrBytes) + nInstBytes; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1267 |
if (objSize < alen) |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1268 |
goto bad; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1269 |
bcopy((char *)&sa, cp, alen); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1270 |
addrLen = __MKSMALLINT(alen); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1271 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1272 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1273 |
if (n < 0) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1274 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1275 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1276 |
nReceived = __MKSMALLINT(n); |
126 | 1277 |
} |
418 | 1278 |
#endif |
126 | 1279 |
bad: ; |
1280 |
%}. |
|
1281 |
nReceived notNil ifTrue:[ |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1282 |
nReceived < 0 ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1283 |
'Socket [warning]: ' infoPrint. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1284 |
(OperatingSystem errorTextForNumber:lastErrorNumber) infoPrintCR. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1285 |
]. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1286 |
addrLen notNil ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1287 |
anAddressBuffer class isBytes ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1288 |
anAddressBuffer replaceFrom:1 to:addrLen with:addrBytes |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1289 |
] ifFalse:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1290 |
"/ can be SocketAddress for ST-80 compatibility |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1291 |
anAddressBuffer addressBytes:(addrBytes copyTo:addrLen) |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1292 |
]. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1293 |
]. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1294 |
^ nReceived |
28 | 1295 |
]. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1296 |
" |
126 | 1297 |
arrive here if you try to receive into an invalid buffer |
1298 |
(i.e. not ByteArray-like), |
|
1299 |
or if the addressBuffer is nonNil AND not a ByteArray/String |
|
1300 |
or if the addressBuffer is nonNil AND too small. |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1301 |
" |
126 | 1302 |
self primitiveFailed |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1303 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1304 |
|
126 | 1305 |
sendTo:anAddressBuffer buffer:buffer |
1306 |
"send datagramm data - fetch address of destination host from |
|
1307 |
anAddressBuffer, data from aDataBuffer. |
|
1308 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
1309 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1310 |
Return the number of bytes transmitted, or a negative number on error. |
|
1311 |
On error, the unix error code is left in the lastErrorNumber |
|
1312 |
instance variable. |
|
1313 |
Flags is currently ignored; it is there for ST-80 compatibility." |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1314 |
|
126 | 1315 |
^ self sendTo:anAddressBuffer buffer:buffer start:1 for:buffer size flags:0 |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1316 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1317 |
|
126 | 1318 |
sendTo:anAddressBuffer buffer:aDataBuffer start:startIndex for:count flags:flags |
1319 |
"send datagramm data - fetch address of destination host from |
|
1320 |
anAddressBuffer, data from aDataBuffer starting at startIndex, |
|
1321 |
sending count bytes. |
|
1322 |
Both must be ByteArray-like. The bytes in the addressBuffer must |
|
1323 |
be a valid address for my domain (i.e. for inet, a 4-byte byteArray). |
|
1324 |
Return the number of bytes transmitted, or a negative number on error. |
|
1325 |
On error, the unix error code is left in the lastErrorNumber |
|
1326 |
instance variable." |
|
0 | 1327 |
|
126 | 1328 |
|addrBytes addrLen nReceived portNo| |
93 | 1329 |
|
126 | 1330 |
"/ addressBuffer can be a 6-byte byteArray (last 2 bytes are portNo, msb-first) |
1331 |
"/ or an instance of IPSocketAddress |
|
1332 |
"/ |
|
1333 |
anAddressBuffer class isBytes ifTrue:[ |
|
1334 |
addrBytes := anAddressBuffer copyFrom:1 to:4. |
|
1335 |
portNo := ((anAddressBuffer at:5) bitShift:8) |
|
1336 |
+ (anAddressBuffer at:6). |
|
1337 |
] ifFalse:[ |
|
1338 |
addrBytes := anAddressBuffer hostAddress. |
|
1339 |
portNo := anAddressBuffer port. |
|
28 | 1340 |
]. |
0 | 1341 |
%{ |
418 | 1342 |
#ifndef NO_SOCKET |
126 | 1343 |
OBJ oClass; |
217 | 1344 |
OBJ fp = __INST(filePointer); |
126 | 1345 |
int nInstVars, nInstBytes, objSize; |
1346 |
int sock; |
|
1347 |
struct sockaddr_in sa; |
|
1348 |
struct sockaddr *saPtr = (struct sockaddr *)&sa; |
|
1349 |
int alen = sizeof(sa); |
|
1350 |
int n; |
|
1351 |
char *cp; |
|
1352 |
int _flags = 0; |
|
1353 |
int offs, nBytes; |
|
1354 |
unsigned long norder; |
|
21 | 1355 |
|
126 | 1356 |
_flags = __longIntVal(flags); |
1357 |
||
1358 |
if ((fp != nil) |
|
1359 |
&& __isSmallInteger(startIndex) |
|
1360 |
&& __isSmallInteger(count)) { |
|
1361 |
sock = fileno(__FILEVal(fp)); |
|
0 | 1362 |
|
126 | 1363 |
if (addrBytes != nil) { |
1364 |
if (! __isByteArray(addrBytes)) goto bad; |
|
1365 |
cp = __ByteArrayInstPtr(addrBytes)->ba_element; |
|
1366 |
n = __byteArraySize(addrBytes); |
|
1367 |
if (alen < n) n = alen; |
|
1368 |
/* |
|
1369 |
printf("address is %d bytes ... %d.%d.%d.%d", n, cp[0], cp[1], cp[2], cp[3]); |
|
1370 |
*/ |
|
1371 |
bcopy(cp, &sa.sin_addr.s_addr, n); |
|
1372 |
sa.sin_family = AF_INET; |
|
1373 |
sa.sin_port = htons((u_short) __intVal(portNo)); |
|
1374 |
} else { |
|
1375 |
alen = 0; |
|
1376 |
saPtr = (struct sockaddr *)0; |
|
93 | 1377 |
} |
6 | 1378 |
|
126 | 1379 |
oClass = __Class(aDataBuffer); |
1380 |
switch (_intVal(_ClassInstPtr(oClass)->c_flags) & ARRAYMASK) { |
|
1381 |
case BYTEARRAY: |
|
1382 |
offs = __intVal(startIndex) - 1; |
|
1383 |
break; |
|
1384 |
case WORDARRAY: |
|
1385 |
offs = (__intVal(startIndex) - 1) * sizeof(short); |
|
1386 |
break; |
|
1387 |
case LONGARRAY: |
|
1388 |
offs = (__intVal(startIndex) - 1) * sizeof(long); |
|
1389 |
break; |
|
1390 |
case FLOATARRAY: |
|
1391 |
offs = (__intVal(startIndex) - 1) * sizeof(float); |
|
1392 |
break; |
|
1393 |
case DOUBLEARRAY: |
|
1394 |
offs = (__intVal(startIndex) - 1) * sizeof(double); |
|
1395 |
#ifdef NEED_DOUBLE_ALIGN |
|
1396 |
offs += sizeof(long); |
|
1397 |
#endif |
|
1398 |
break; |
|
1399 |
default: |
|
1400 |
goto bad; |
|
42 | 1401 |
} |
126 | 1402 |
nBytes = __intVal(count); |
71 | 1403 |
|
126 | 1404 |
nInstVars = _intVal(_ClassInstPtr(oClass)->c_ninstvars); |
1405 |
nInstBytes = OHDR_SIZE + nInstVars * sizeof(OBJ); |
|
1406 |
objSize = __qSize(aDataBuffer) - nInstBytes; |
|
217 | 1407 |
cp = (char *)__InstPtr(aDataBuffer) + nInstBytes; |
126 | 1408 |
cp += offs; |
1409 |
if ((offs + nBytes) > objSize) { |
|
1410 |
/* |
|
1411 |
printf("cut off ...\n"); |
|
1412 |
*/ |
|
1413 |
nBytes = objSize - offs; |
|
1414 |
} |
|
0 | 1415 |
|
126 | 1416 |
norder = htonl(sa.sin_addr.s_addr); |
1417 |
/* |
|
1418 |
printf("sending %d bytes ... to ", nBytes); |
|
1419 |
printf("%d.%d.%d.%d\n", |
|
1420 |
(norder >> 24) & 0xFF, |
|
1421 |
(norder >> 16) & 0xFF, |
|
1422 |
(norder >> 8) & 0xFF, |
|
1423 |
norder & 0xFF); |
|
1424 |
*/ |
|
0 | 1425 |
|
81 | 1426 |
__BEGIN_INTERRUPTABLE__ |
126 | 1427 |
do { |
1428 |
n = sendto(sock, cp, nBytes, _flags, saPtr, alen); |
|
1429 |
} while ((n < 0) && (errno == EINTR)); |
|
81 | 1430 |
__END_INTERRUPTABLE__ |
0 | 1431 |
|
126 | 1432 |
if (n < 0) { |
217 | 1433 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
42 | 1434 |
} |
217 | 1435 |
RETURN (__MKSMALLINT(n)); |
0 | 1436 |
} |
418 | 1437 |
#endif |
126 | 1438 |
bad: ; |
42 | 1439 |
%}. |
1440 |
" |
|
126 | 1441 |
arrive here if you try to send from an invalid buffer |
1442 |
(i.e. not ByteArray-like), |
|
1443 |
or if the addressBuffer is nonNil AND not a ByteArray/String |
|
1444 |
or if the addressBuffer is nonNil AND too small. |
|
42 | 1445 |
" |
126 | 1446 |
self primitiveFailed |
0 | 1447 |
! ! |
1448 |
||
1449 |
!Socket methodsFor:'low level'! |
|
1450 |
||
126 | 1451 |
accept |
1452 |
"create a new TCP socket from accepting on the receiver. |
|
1453 |
This method will suspend the current process if no connection is waiting. |
|
1454 |
For ST-80 compatibility" |
|
1455 |
||
1456 |
|newSock| |
|
1457 |
||
1458 |
newSock := self class new. |
|
1459 |
(newSock acceptOn:self) ifFalse:[^ nil]. |
|
1460 |
^ newSock |
|
1461 |
||
1462 |
" |
|
1463 |
|sock newSock| |
|
1464 |
||
1465 |
sock := Socket provide:8004. |
|
1466 |
sock listenFor:5. |
|
1467 |
newSock := sock accept. |
|
1468 |
" |
|
1469 |
! |
|
1470 |
||
1471 |
acceptOn:aSocket |
|
1472 |
"accept a connection on a server port (created with:'Socket>>onIPPort:') |
|
1473 |
usage is: (Socket basicNew acceptOn:(Socket onIPPort:9999)). |
|
207
e9f6913dc380
split: #accept is nonBlock; the old accept is not called #blockingAccept
Claus Gittinger <cg@exept.de>
parents:
206
diff
changeset
|
1474 |
This method will suspend the current process if no connection is waiting. |
e9f6913dc380
split: #accept is nonBlock; the old accept is not called #blockingAccept
Claus Gittinger <cg@exept.de>
parents:
206
diff
changeset
|
1475 |
Return the true if ok; false if not." |
e9f6913dc380
split: #accept is nonBlock; the old accept is not called #blockingAccept
Claus Gittinger <cg@exept.de>
parents:
206
diff
changeset
|
1476 |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1477 |
aSocket readWait. |
207
e9f6913dc380
split: #accept is nonBlock; the old accept is not called #blockingAccept
Claus Gittinger <cg@exept.de>
parents:
206
diff
changeset
|
1478 |
^ self blockingAcceptOn:aSocket |
126 | 1479 |
|
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1480 |
"Modified: 11.3.1996 / 14:21:31 / stefan" |
8 | 1481 |
! |
1482 |
||
507 | 1483 |
bindAnonymously |
1484 |
^ self |
|
1485 |
bindTo:0 |
|
1486 |
address:nil |
|
509 | 1487 |
reuseAddress:false |
1488 |
! |
|
1489 |
||
1490 |
bindAnonymouslyToAddress:addressString |
|
1491 |
^ self |
|
1492 |
bindTo:0 |
|
1493 |
address:addressString |
|
1494 |
reuseAddress:false |
|
507 | 1495 |
! |
1496 |
||
71 | 1497 |
bindTo:aSocketAddress |
425 | 1498 |
"ST80 compatible bind, expecting a socketAddress argument. |
1499 |
The socketAddress object (an instance of SocketAddress) |
|
1500 |
is supposed to respond to #portOrName and #address requests." |
|
71 | 1501 |
|
1502 |
^ self bindTo:(aSocketAddress portOrName) |
|
1503 |
address:(aSocketAddress address) |
|
425 | 1504 |
reuseAddress:true |
71 | 1505 |
! |
1506 |
||
425 | 1507 |
bindTo:portNrOrNameString address:addressString |
28 | 1508 |
"low level bind - returns true if ok, false otherwise. |
71 | 1509 |
Currently only non-address binding is supported; |
425 | 1510 |
i.e. the address must always be nil. |
71 | 1511 |
|
1512 |
The interpretation of portNrOrName depends on the domain: |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1513 |
inet domain uses (4byte) byteArray like internet numbers, |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1514 |
unix domain uses pathname strings, |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1515 |
others use whatever will come up in the future |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1516 |
" |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1517 |
|
425 | 1518 |
^ self |
1519 |
bindTo:portNrOrNameString |
|
1520 |
address:addressString |
|
1521 |
reuseAddress:true |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1522 |
! |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1523 |
|
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1524 |
bindTo:portNrOrName address:address reuseAddress:reuse |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1525 |
"low level bind - returns true if ok, false otherwise. |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1526 |
Currently only non-address binding is supported; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1527 |
i.e. address must always be nil. |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1528 |
|
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1529 |
The interpretation of portNrOrName depends on the domain: |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1530 |
inet domain uses (4byte) byteArray like internet numbers, |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1531 |
unix domain uses pathname strings, |
82 | 1532 |
others use whatever will come up in the future |
425 | 1533 |
|
1534 |
The reuse boolean argument controls if the SO_REUSEADDR socket option |
|
1535 |
is to be set (to avoid the 'bind: address in use' error). |
|
1536 |
" |
|
28 | 1537 |
|
1538 |
filePointer isNil ifTrue:[ |
|
42 | 1539 |
^ self error:'not a valid socket' |
28 | 1540 |
]. |
0 | 1541 |
%{ |
418 | 1542 |
#ifndef NO_SOCKET |
217 | 1543 |
OBJ t = __INST(filePointer); |
71 | 1544 |
OBJ myDomain; |
28 | 1545 |
int sock; |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1546 |
union { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1547 |
struct sockaddr_in in; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1548 |
struct sockaddr_un un; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1549 |
} sa; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1550 |
int sockaddr_size; |
6 | 1551 |
int ret; |
25 | 1552 |
int on = 1; |
28 | 1553 |
int ok; |
0 | 1554 |
|
217 | 1555 |
if (!__isString(__INST(domain)) && !__isSymbol(__INST(domain))) { |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1556 |
DBGPRINTF(("SOCKET: invalid domain arg\n")); |
42 | 1557 |
RETURN (false); |
28 | 1558 |
} |
1559 |
||
1560 |
ok = 0; |
|
217 | 1561 |
myDomain = __INST(domain); |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1562 |
# ifdef AF_INET |
71 | 1563 |
if (myDomain == @symbol(inet)) { |
42 | 1564 |
/* |
508 | 1565 |
* INET addresses - port must be a smallinteger or nil |
42 | 1566 |
*/ |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1567 |
sa.in.sin_family = AF_INET; |
508 | 1568 |
|
1569 |
if (portNrOrName == nil) { |
|
1570 |
sa.in.sin_port = 0; |
|
1571 |
} else { |
|
1572 |
if (! __isSmallInteger(portNrOrName)) { |
|
1573 |
DBGPRINTF(("SOCKET: invalid port arg\n")); |
|
1574 |
RETURN (false); |
|
1575 |
} |
|
1576 |
sa.in.sin_port = htons((u_short) _intVal(portNrOrName)); |
|
42 | 1577 |
} |
508 | 1578 |
|
1579 |
/* |
|
1580 |
* INET addresses - addr must be nil, integer or byteArray |
|
1581 |
*/ |
|
42 | 1582 |
if (address == nil) { |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1583 |
sa.in.sin_addr.s_addr = htonl(INADDR_ANY); |
42 | 1584 |
} else { |
82 | 1585 |
if (__isInteger(address)) { |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1586 |
sa.in.sin_addr.s_addr = htonl(__longIntVal(address)); |
82 | 1587 |
} else { |
508 | 1588 |
if (__isByteArray(address)) { |
1589 |
unsigned char *cp; |
|
1590 |
int n; |
|
1591 |
||
1592 |
cp = __ByteArrayInstPtr(address)->ba_element; |
|
1593 |
n = __byteArraySize(address); |
|
1594 |
if (n > 4) n = 4; |
|
1595 |
bcopy(cp, &sa.in.sin_addr.s_addr, n); |
|
1596 |
} else { |
|
1597 |
printf("SOCKET: address bind not yet supported\n"); |
|
1598 |
RETURN (false); |
|
1599 |
} |
|
82 | 1600 |
} |
42 | 1601 |
} |
508 | 1602 |
DBGPRINTF(("SOCKET: bind addr: %x port: %x\n", sa.in.sin_addr.s_addr, sa.in.sin_port)); |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1603 |
sockaddr_size = sizeof(struct sockaddr_in); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1604 |
ok = 1; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1605 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1606 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1607 |
# ifdef AF_UNIX |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1608 |
if (myDomain == @symbol(unix)) { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1609 |
char *pathName; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1610 |
int l; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1611 |
|
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1612 |
if (! __isString(portNrOrName)) { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1613 |
DBGPRINTF(("SOCKET: invalid port (pathname) arg\n")); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1614 |
RETURN (false); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1615 |
} |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1616 |
pathName = __stringVal(portNrOrName); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1617 |
l = strlen(pathName); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1618 |
if ((l + sizeof ( sa.un.sun_family )) > sizeof(struct sockaddr_un)) { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1619 |
DBGPRINTF(("SOCKET: pathname too long\n")); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1620 |
RETURN (false); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1621 |
} |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1622 |
|
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1623 |
strcpy(sa.un.sun_path, pathName); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1624 |
sa.un.sun_family = AF_UNIX; |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1625 |
sockaddr_size = l + sizeof ( sa.un.sun_family ); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1626 |
ok = 1; |
28 | 1627 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1628 |
# endif |
28 | 1629 |
/* |
1630 |
* XXXX add addressing stuff for other domains here ... |
|
1631 |
*/ |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1632 |
# ifdef AF_X25 |
71 | 1633 |
if (myDomain == @symbol(x25)) { |
28 | 1634 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1635 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1636 |
# ifdef AF_NS |
71 | 1637 |
if (myDomain == @symbol(ns)) { |
28 | 1638 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1639 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1640 |
# ifdef AF_APPLETALK |
71 | 1641 |
if (myDomain == @symbol(appletalk)) { |
28 | 1642 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1643 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1644 |
# ifdef AF_SNA |
93 | 1645 |
if (myDomain == @symbol(sna)) { |
1646 |
} |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1647 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1648 |
# ifdef AF_NS |
93 | 1649 |
if (myDomain == @symbol(xns)) { |
1650 |
} |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1651 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1652 |
# ifdef AF_RAW |
93 | 1653 |
if (myDomain == @symbol(raw)) { |
1654 |
} |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1655 |
# endif |
0 | 1656 |
|
28 | 1657 |
if (! ok) { |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1658 |
DBGPRINTF(("SOCKET: unsupported domain\n")); |
42 | 1659 |
RETURN (false); |
28 | 1660 |
} |
1661 |
||
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
1662 |
sock = fileno(__FILEVal(t)); |
28 | 1663 |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1664 |
# ifdef SO_REUSEADDR |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1665 |
if (reuse == true) { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1666 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1667 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1668 |
} |
28 | 1669 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1670 |
# endif /* SO_REUSEADDR */ |
28 | 1671 |
|
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
1672 |
__BEGIN_INTERRUPTABLE__ |
28 | 1673 |
do { |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1674 |
ret = bind(sock, (struct sockaddr *)&sa, sockaddr_size); |
28 | 1675 |
} while ((ret < 0) && (errno == EINTR)); |
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
1676 |
__END_INTERRUPTABLE__ |
28 | 1677 |
|
1678 |
if (ret < 0) { |
|
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
1679 |
DBGPRINTF(("SOCKET: bind failed errno=%d\n", errno)); |
217 | 1680 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
42 | 1681 |
RETURN (false); |
0 | 1682 |
} |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1683 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1684 |
__INST(port) = portNrOrName; __STORE(self, portNrOrName); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1685 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1686 |
# ifdef AF_INET |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1687 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1688 |
if (myDomain == @symbol(inet)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1689 |
if (! __isSmallInteger(portNrOrName) |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1690 |
|| (portNrOrName == __MKSMALLINT(0))) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1691 |
int p; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1692 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1693 |
/* |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1694 |
* INET anonymous port - get the actual portNr |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1695 |
*/ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1696 |
if (getsockname(sock, (struct sockaddr *)&sa, &sockaddr_size) < 0) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1697 |
DBGPRINTF(("SOCKET: cannot get peername\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1698 |
} else { |
508 | 1699 |
DBGPRINTF(("SOCKET: anon port=%x\n", sa.in.sin_port)); |
1700 |
p = ntohs(sa.in.sin_port); |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1701 |
__INST(port) = __MKSMALLINT(p); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1702 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1703 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1704 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1705 |
# endif |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1706 |
#else /* NO_SOCKET */ |
418 | 1707 |
RETURN (false); |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1708 |
#endif /* NO_SOCKET */ |
42 | 1709 |
%}. |
28 | 1710 |
^ true |
0 | 1711 |
|
28 | 1712 |
" |
1713 |
(Socket domain:#inet type:#stream) |
|
42 | 1714 |
bindTo:9999 |
1715 |
address:nil |
|
28 | 1716 |
" |
0 | 1717 |
! |
1718 |
||
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1719 |
blockingAcceptOn:aSocket |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1720 |
"accept a connection on a server port (created with:'Socket>>onIPPort:') |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1721 |
usage is: (Socket basicNew acceptOn:(Socket onIPPort:9999)). |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1722 |
Return the true if ok; false if not. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1723 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1724 |
NOTICE: this method will block, if no connection is already pending. |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1725 |
use readWait or Socket>>accept." |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1726 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1727 |
|serverSocketFd| |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1728 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1729 |
filePointer notNil ifTrue:[ |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1730 |
^ self error:'already connected' |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1731 |
]. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1732 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1733 |
domain := aSocket domain. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1734 |
socketType := aSocket type. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1735 |
serverSocketFd := aSocket fileDescriptor. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1736 |
serverSocketFd isNil ifTrue:[ |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1737 |
^ self error:'invalid server socket' |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1738 |
]. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1739 |
(serverSocketFd isMemberOf:SmallInteger) ifFalse:[ |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1740 |
^ self error:'invalid server socket' |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1741 |
]. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1742 |
%{ |
418 | 1743 |
#ifndef NO_SOCKET |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1744 |
FILE *fp; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1745 |
int flags; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1746 |
int sock, newSock; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1747 |
union { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1748 |
struct sockaddr_in in ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1749 |
struct sockaddr_un un ; |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1750 |
} sa; |
464 | 1751 |
int alen, alen0; |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1752 |
struct hostent *he ; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1753 |
char dotted[20] ; |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1754 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1755 |
sock = _intVal(serverSocketFd); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1756 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1757 |
#if defined(O_NDELAY) && defined(SET_NDELAY) |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1758 |
flags = ioctl(sock, F_GETFL, 0); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1759 |
ioctl(sock, F_SETFL, flags | O_NDELAY); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1760 |
#endif |
464 | 1761 |
|
1762 |
#ifdef AF_INET |
|
1763 |
if (__INST(domain) == @symbol(inet)) { |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1764 |
alen0 = sizeof(sa.in); |
464 | 1765 |
} |
1766 |
#endif |
|
1767 |
#ifdef AF_UNIX |
|
1768 |
if (__INST(domain) == @symbol(unix)) { |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1769 |
alen0 = sizeof(sa.un); |
464 | 1770 |
} |
1771 |
#endif |
|
1772 |
||
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1773 |
__BEGIN_INTERRUPTABLE__ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1774 |
do { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1775 |
alen = alen0; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1776 |
newSock = accept(sock, (struct sockaddr *) &sa, &alen); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1777 |
} while ((newSock < 0) && (errno == EINTR)); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1778 |
__END_INTERRUPTABLE__ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1779 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1780 |
#if defined(O_NDELAY) && defined(SET_NDELAY) |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1781 |
ioctl(sock, F_SETFL, flags); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1782 |
#endif |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1783 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1784 |
if (newSock < 0) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1785 |
DBGPRINTF(("SOCKET: accept call failed errno=%d\n", errno)); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1786 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1787 |
RETURN (false); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1788 |
} |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1789 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1790 |
/* |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1791 |
* extract the partners address |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1792 |
*/ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1793 |
#ifdef AF_INET |
217 | 1794 |
if (__INST(domain) == @symbol(inet)) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1795 |
he = gethostbyaddr((char *) &sa.in.sin_addr.s_addr, alen, AF_INET) ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1796 |
if (! he) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1797 |
unsigned long norder; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1798 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1799 |
norder = htonl(sa.in.sin_addr.s_addr) ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1800 |
sprintf(dotted, "%d.%d.%d.%d", |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1801 |
(norder >> 24) & 0xFF, |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1802 |
(norder >> 16) & 0xFF, |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1803 |
(norder >> 8) & 0xFF, |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1804 |
norder & 0xFF); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1805 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1806 |
DBGPRINTF(("SOCKET: accepted connection from host %s\n", (he ? he->h_name : dotted))) ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1807 |
__INST(peerName) = __MKSTRING((he ? he->h_name : dotted)); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1808 |
__STORESELF(peerName); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1809 |
} |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1810 |
#endif |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1811 |
#ifdef AF_UNIX |
217 | 1812 |
if (__INST(domain) == @symbol(unix)) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1813 |
DBGPRINTF(("SOCKET: accepted connection on unix socket\n")) ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1814 |
/* nothing to be done here */ |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1815 |
} |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1816 |
#endif |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1817 |
|
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1818 |
/* |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1819 |
* make it a FILE * |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1820 |
*/ |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1821 |
fp = fdopen(newSock, "r+"); |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1822 |
if (! fp) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1823 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1824 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1825 |
close(newSock); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1826 |
RETURN (false); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1827 |
} else { |
444 | 1828 |
#ifdef BUGGY_STDIO_LIB |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1829 |
setbuf(fp, NULL); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1830 |
__INST(buffered) = false; |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1831 |
#endif |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1832 |
__INST(filePointer) = __MKOBJ(fp); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1833 |
__STORESELF(filePointer); |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1834 |
} |
418 | 1835 |
#endif |
208
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1836 |
%}. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1837 |
mode := #readwrite. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1838 |
Lobby register:self. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1839 |
binary := false. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1840 |
port := aSocket port. |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1841 |
^ true |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1842 |
! |
c3eca2cc16dc
accept: do not wait for input on non-connected socket.
Stefan Vogel <sv@exept.de>
parents:
207
diff
changeset
|
1843 |
|
126 | 1844 |
closeFile |
1845 |
"low level close" |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1846 |
|
126 | 1847 |
%{ /* NOCONTEXT */ |
418 | 1848 |
#ifndef NO_SOCKET |
126 | 1849 |
|
1850 |
OBJ t; |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1851 |
|
217 | 1852 |
t = __INST(filePointer); |
126 | 1853 |
if (t != nil) { |
1854 |
FILE *fp; |
|
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1855 |
|
126 | 1856 |
fp = __FILEVal(t); |
1857 |
fflush(fp); |
|
1858 |
shutdown(fileno(fp), 2); |
|
1859 |
fclose(fp); |
|
217 | 1860 |
__INST(filePointer) = nil; |
126 | 1861 |
} |
418 | 1862 |
#endif |
126 | 1863 |
%} |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1864 |
! |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1865 |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1866 |
connectTo:hostOrPathName port:portNrOrName |
71 | 1867 |
"low level connect; connect to port, portNrOrName on host, hostName. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1868 |
For unix-domain sockets, the port argument is ignored and pathName is taken. |
115 | 1869 |
Other sockets are not yet implemented. |
71 | 1870 |
Return true if ok, false otherwise. |
99
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1871 |
Hostname must be a string, portNrOrName an integer port number (in inet domain). |
7d016cc30052
fixed & moved pingWalk from SNMPSession to here;
Claus Gittinger <cg@exept.de>
parents:
98
diff
changeset
|
1872 |
The current process will block (but not the whole Smalltalk) until the connection |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1873 |
is established. |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1874 |
See also: #connectTo:port:withTimeout: for a somewhat nicer interface." |
28 | 1875 |
|
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
1876 |
|isAsync| |
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
1877 |
|
28 | 1878 |
filePointer isNil ifTrue:[ |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1879 |
^ self error:'not a valid socket' |
28 | 1880 |
]. |
0 | 1881 |
%{ |
418 | 1882 |
#ifndef NO_SOCKET |
217 | 1883 |
OBJ t = __INST(filePointer); |
71 | 1884 |
OBJ myDomain; |
115 | 1885 |
union { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1886 |
struct sockaddr_in in ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1887 |
struct sockaddr_un un; |
115 | 1888 |
} sa; |
28 | 1889 |
struct hostent *hp ; |
1890 |
int a, sock ; |
|
1891 |
long addr; |
|
1892 |
FILE *fp; |
|
1893 |
int ret; |
|
1894 |
int on = 1; |
|
1895 |
int ok; |
|
115 | 1896 |
int sockaddr_size; |
28 | 1897 |
|
217 | 1898 |
if (!__isString(__INST(domain)) && !__isSymbol(__INST(domain))) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1899 |
DBGPRINTF(("SOCKET: invalid domain arg\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1900 |
RETURN (false); |
28 | 1901 |
} |
1902 |
||
1903 |
ok = 0; |
|
217 | 1904 |
myDomain = __INST(domain); |
115 | 1905 |
bzero((char *) &sa, sizeof(sa)) ; |
508 | 1906 |
|
28 | 1907 |
#ifdef AF_INET |
71 | 1908 |
if (myDomain == @symbol(inet)) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1909 |
char *hostName; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1910 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1911 |
if (! __isSmallInteger(portNrOrName)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1912 |
DBGPRINTF(("SOCKET: invalid port arg\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1913 |
RETURN (false); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1914 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1915 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1916 |
sa.in.sin_family = AF_INET; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1917 |
sa.in.sin_port = htons((u_short) _intVal(portNrOrName)) ; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1918 |
|
508 | 1919 |
if (__isByteArray(hostOrPathName)) { |
1920 |
unsigned char *cp; |
|
1921 |
int n; |
|
1922 |
||
1923 |
cp = __ByteArrayInstPtr(hostOrPathName)->ba_element; |
|
1924 |
n = __byteArraySize(hostOrPathName); |
|
1925 |
if (n > 4) n = 4; |
|
1926 |
bcopy(cp, &sa.in.sin_addr.s_addr, n); |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1927 |
} else { |
508 | 1928 |
if (! __isString(hostOrPathName)) { |
1929 |
DBGPRINTF(("SOCKET: invalid hostname arg\n")); |
|
1930 |
RETURN (false); |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1931 |
} |
508 | 1932 |
|
1933 |
hostName = (char *) _stringVal(hostOrPathName); |
|
1934 |
||
1935 |
if ((addr = inet_addr(hostName)) != -1) { |
|
1936 |
/* |
|
1937 |
* is Internet addr in octet notation |
|
1938 |
*/ |
|
1939 |
bcopy(&addr, (char *) &sa.in.sin_addr, sizeof(addr)) ; /* set address */ |
|
1940 |
} else { |
|
1941 |
/* |
|
1942 |
* do we know the host's address? |
|
1943 |
*/ |
|
1944 |
if ((hp = gethostbyname(hostName)) == NULL) { |
|
1945 |
DBGPRINTF(("SOCKET: unknown host:%s\n", hostName)); |
|
1946 |
RETURN (false); |
|
1947 |
} |
|
1948 |
bcopy(hp->h_addr, (char *) &sa.in.sin_addr, hp->h_length) ; |
|
1949 |
sa.in.sin_family = hp->h_addrtype; |
|
1950 |
} |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1951 |
} |
508 | 1952 |
|
1953 |
DBGPRINTF(("SOCKET: connect addr: %x port: %d\n", sa.in.sin_addr, sa.in.sin_port)); |
|
1954 |
||
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1955 |
sockaddr_size = sizeof(struct sockaddr_in); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1956 |
ok = 1; |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1957 |
} |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1958 |
#endif |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1959 |
#ifdef AF_UNIX |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
1960 |
if (myDomain == @symbol(unix)) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1961 |
char *pathName; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1962 |
int l; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1963 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1964 |
if (! __isString(hostOrPathName)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1965 |
DBGPRINTF(("SOCKET: invalid port (pathname) arg\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1966 |
RETURN (false); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1967 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1968 |
pathName = (char *) __stringVal(hostOrPathName); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1969 |
l = strlen(pathName); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1970 |
if ((l + sizeof ( sa.un.sun_family )) > sizeof(struct sockaddr_un)) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1971 |
DBGPRINTF(("SOCKET: pathname too long\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1972 |
RETURN (false); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1973 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1974 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1975 |
strcpy(sa.un.sun_path, pathName); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1976 |
sa.un.sun_family = AF_UNIX; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1977 |
sockaddr_size = l + sizeof ( sa.un.sun_family ); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
1978 |
ok = 1; |
28 | 1979 |
} |
1980 |
#endif |
|
1981 |
/* |
|
1982 |
* XXXX add addressing stuff for other domains here ... |
|
1983 |
*/ |
|
1984 |
#ifdef AF_X25 |
|
71 | 1985 |
if (myDomain == @symbol(x25)) { |
28 | 1986 |
} |
1987 |
#endif |
|
1988 |
#ifdef AF_NS |
|
71 | 1989 |
if (myDomain == @symbol(ns)) { |
28 | 1990 |
} |
1991 |
#endif |
|
1992 |
#ifdef AF_APPLETALK |
|
71 | 1993 |
if (myDomain == @symbol(appletalk)) { |
28 | 1994 |
} |
1995 |
#endif |
|
93 | 1996 |
#ifdef AF_NS |
1997 |
if (myDomain == @symbol(xns)) { |
|
1998 |
} |
|
1999 |
#endif |
|
2000 |
#ifdef AF_SNA |
|
2001 |
if (myDomain == @symbol(sna)) { |
|
2002 |
} |
|
2003 |
#endif |
|
2004 |
#ifdef AF_RAW |
|
2005 |
if (myDomain == @symbol(raw)) { |
|
2006 |
} |
|
2007 |
#endif |
|
28 | 2008 |
|
2009 |
if (! ok) { |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2010 |
DBGPRINTF(("SOCKET: unsupported domain\n")); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2011 |
RETURN (false); |
28 | 2012 |
} |
2013 |
||
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
2014 |
sock = fileno(__FILEVal(t)); |
28 | 2015 |
|
2016 |
/* |
|
2017 |
* connect |
|
2018 |
*/ |
|
81 | 2019 |
__BEGIN_INTERRUPTABLE__ |
28 | 2020 |
|
2021 |
do { |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2022 |
ret = connect(sock, (struct sockaddr *)&sa, sockaddr_size); |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2023 |
} while ((ret < 0) |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2024 |
&& ((errno == EINTR) |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2025 |
#ifdef EAGAIN |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2026 |
|| (errno == EAGAIN) |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2027 |
#endif |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2028 |
)); |
81 | 2029 |
__END_INTERRUPTABLE__ |
28 | 2030 |
|
2031 |
if (ret < 0) { |
|
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2032 |
#ifdef EINPROGRESS |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2033 |
if (errno == EINPROGRESS || errno == EALREADY) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2034 |
/* |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2035 |
* We were interrupted. Do not call connect again |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2036 |
* (we will get an EALREADY. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2037 |
* Do a select on write instead. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2038 |
*/ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2039 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2040 |
isAsync = true; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2041 |
} else |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2042 |
#endif |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2043 |
{ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2044 |
DBGPRINTF(("SOCKET: connect failed errno=%d\n", errno)); |
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
2045 |
#ifdef DUMP_ADDRESS |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2046 |
{ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2047 |
char *cp = (char *)(&sa); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2048 |
int i; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2049 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2050 |
printf("address data:\n"); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2051 |
for (i=0; i<sockaddr_size; i++) { |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2052 |
printf(" %02x\n", *cp++); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2053 |
} |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2054 |
} |
117
03cefad7e40b
UD socket example & interruptable bind
Claus Gittinger <cg@exept.de>
parents:
116
diff
changeset
|
2055 |
#endif |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2056 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2057 |
RETURN (false); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2058 |
} |
28 | 2059 |
} |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2060 |
#else /* NO_SOCKET */ |
418 | 2061 |
RETURN (false); |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2062 |
#endif /* NO_SOCKET */ |
42 | 2063 |
%}. |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2064 |
isAsync == true ifTrue:[ |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2065 |
self writeWait. |
430
12d736316f69
Fix #connectTo:port: Do not call connect() when we get EINPROGRESS
Stefan Vogel <sv@exept.de>
parents:
429
diff
changeset
|
2066 |
]. |
71 | 2067 |
port := portNrOrName. |
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2068 |
peerName := hostOrPathName. |
28 | 2069 |
^ true |
2070 |
! |
|
2071 |
||
126 | 2072 |
connectTo:hostOrPathName port:portNrOrName withTimeout:millis |
2073 |
"connect to port, portNrOrName on host, hostName. |
|
2074 |
Or path (UNIX socket), where portNrOrName is ignored. |
|
2075 |
Return true if ok, false otherwise. |
|
2076 |
Hostname must be a string, portNrOrName an integer port number (in inet domain). |
|
2077 |
If a non-nil timeout is given, stop trying after that time and return false as well." |
|
2078 |
||
2079 |
|stopSignal stopMe connection| |
|
2080 |
||
2081 |
millis isNil ifTrue:[ |
|
2082 |
^ self connectTo:hostOrPathName port:portNrOrName |
|
2083 |
]. |
|
2084 |
stopSignal := Signal new. |
|
2085 |
stopMe := [stopSignal raise]. |
|
2086 |
stopSignal handle:[:ex | |
|
2087 |
"/ 'timeout on connect' infoPrintNL. |
|
2088 |
^ false |
|
2089 |
] do:[ |
|
2090 |
Processor addTimedBlock:stopMe afterMilliseconds:millis. |
|
2091 |
connection := self connectTo:hostOrPathName port:portNrOrName. |
|
2092 |
Processor removeTimedBlock:stopMe. |
|
2093 |
]. |
|
2094 |
^ connection |
|
2095 |
||
2096 |
"Created: 31.10.1995 / 18:52:49 / cg" |
|
2097 |
! |
|
2098 |
||
2099 |
listenFor:aNumber |
|
2100 |
"same as listenWithBacklog: - for ST-80 compatibility" |
|
2101 |
||
2102 |
^ self listenWithBacklog:aNumber |
|
2103 |
! |
|
2104 |
||
28 | 2105 |
listenWithBacklog:aNumber |
2106 |
"start listening; return true if ok, false on error" |
|
2107 |
||
2108 |
filePointer isNil ifTrue:[ |
|
42 | 2109 |
^ self error:'not a valid socket' |
28 | 2110 |
]. |
2111 |
%{ |
|
418 | 2112 |
#ifndef NO_SOCKET |
217 | 2113 |
OBJ fp = __INST(filePointer); |
28 | 2114 |
int sock; |
6 | 2115 |
int ret; |
0 | 2116 |
|
58 | 2117 |
if (! __isSmallInteger(aNumber)) { |
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
2118 |
DBGPRINTF(("SOCKET: invalid arg\n")); |
42 | 2119 |
RETURN (false); |
28 | 2120 |
} |
2121 |
||
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
2122 |
sock = fileno(__FILEVal(fp)); |
28 | 2123 |
|
81 | 2124 |
__BEGIN_INTERRUPTABLE__ |
28 | 2125 |
do { |
42 | 2126 |
ret = listen(sock, _intVal(aNumber)); |
28 | 2127 |
} while ((ret < 0) && (errno == EINTR)); |
81 | 2128 |
__END_INTERRUPTABLE__ |
28 | 2129 |
|
2130 |
if (ret < 0) { |
|
98
5e787ba2cb31
use common byteOrder when representing IP numbers as byteArrays (always network-first)
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
2131 |
DBGPRINTF(("SOCKET: listen call failed errno=%d\n", errno)); |
217 | 2132 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
42 | 2133 |
RETURN (false); |
28 | 2134 |
} |
418 | 2135 |
#else |
2136 |
RETURN (false); |
|
2137 |
#endif |
|
42 | 2138 |
%}. |
28 | 2139 |
^ true |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2140 |
! |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2141 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2142 |
shutDown |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2143 |
"shutDown without flushing " |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2144 |
|
429
197dee9342a3
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
425
diff
changeset
|
2145 |
filePointer isNil ifTrue:[^ self]. |
197dee9342a3
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
425
diff
changeset
|
2146 |
Lobby unregister:self. |
197dee9342a3
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
425
diff
changeset
|
2147 |
|
197dee9342a3
when shutting down, also tell the Lobby
Claus Gittinger <cg@exept.de>
parents:
425
diff
changeset
|
2148 |
%{ |
418 | 2149 |
#ifndef NO_SOCKET |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2150 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2151 |
OBJ fp; |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2152 |
|
217 | 2153 |
fp = __INST(filePointer); |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2154 |
if (fp != nil) { |
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2155 |
FILE *f; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2156 |
int fd; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2157 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2158 |
__INST(filePointer) = nil; |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2159 |
f = __FILEVal(fp); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2160 |
fd = fileno(f); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2161 |
__BEGIN_INTERRUPTABLE__ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2162 |
shutdown(fd, 2); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2163 |
close(fd); |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2164 |
__END_INTERRUPTABLE__ |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2165 |
} |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2166 |
#endif |
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2167 |
%} |
25 | 2168 |
! ! |
2169 |
||
2170 |
!Socket methodsFor:'queries'! |
|
2171 |
||
126 | 2172 |
domain |
2173 |
"return the sockets addressing domain (i.e. #inet, #unix, #x25, #appletalk)" |
|
2174 |
||
2175 |
^ domain |
|
2176 |
! |
|
2177 |
||
2178 |
getName |
|
2179 |
"return the name; here, we return the ports name" |
|
2180 |
||
2181 |
^ port printString |
|
2182 |
! |
|
2183 |
||
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2184 |
getPeer |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2185 |
"ST-80 compatibility: return an IPSocketAddress instance representing |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2186 |
my hostname/port combination. |
155
fcf846c707b7
interest is written with one 'r' (shame on me)
Claus Gittinger <cg@exept.de>
parents:
126
diff
changeset
|
2187 |
If you are interested in the hostname, use getPeerName directly." |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2188 |
|
116
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2189 |
domain == #unix ifTrue:[ |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2190 |
^ UDSocketAddress name:peerName |
7789fb1c9fa2
more UD socket fixes (at least client side seems to work now)
Claus Gittinger <cg@exept.de>
parents:
115
diff
changeset
|
2191 |
]. |
102
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2192 |
^ IPSocketAddress hostAddress:(self class ipAddressOfHost:peerName) port:port |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2193 |
|
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2194 |
"Created: 2.11.1995 / 11:22:39 / cg" |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2195 |
! |
5dc264007513
added getPeer for ST-80 compatibility
Claus Gittinger <cg@exept.de>
parents:
99
diff
changeset
|
2196 |
|
25 | 2197 |
getPeerName |
2198 |
"return the peer name; thats the hostname (or dotted name) of the |
|
2199 |
partners host after an accept." |
|
2200 |
||
2201 |
^ peerName |
|
28 | 2202 |
! |
2203 |
||
126 | 2204 |
isActive |
2205 |
"return true, if the receiver has a connection" |
|
81 | 2206 |
|
126 | 2207 |
^ filePointer notNil |
81 | 2208 |
! |
2209 |
||
71 | 2210 |
port |
2211 |
"return the port number (or name for unix-sockets) to which the socket is bound" |
|
28 | 2212 |
|
506
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2213 |
|p| |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2214 |
|
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2215 |
port == 0 ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2216 |
p := self getPort. |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2217 |
p notNil ifTrue:[ |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2218 |
port := p |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2219 |
] |
11ef93d12870
support anonymous (OS_specified) ports. (for FTP-protocol)
Claus Gittinger <cg@exept.de>
parents:
486
diff
changeset
|
2220 |
]. |
71 | 2221 |
^ port |
28 | 2222 |
! |
2223 |
||
2224 |
type |
|
2225 |
"return the sockets connection type (i.e. #datagram, #stream etc)" |
|
2226 |
||
2227 |
^ socketType |
|
81 | 2228 |
! ! |
2229 |
||
126 | 2230 |
!Socket methodsFor:'socket setup'! |
2231 |
||
2232 |
domain:domainArg type:typeArg |
|
2233 |
"set up socket with domain and type. |
|
2234 |
This is a low level entry; no binding, listening or connect |
|
2235 |
is done. Both arguments must be symbols from one of |
|
2236 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp." |
|
2237 |
||
2238 |
^ self domain:domainArg type:typeArg protocol:0 |
|
2239 |
! |
|
2240 |
||
2241 |
domain:domainArg type:typeArg protocol:protocolNumber |
|
2242 |
"set up socket with domain, type and protocol number. |
|
2243 |
This is a low level entry; no binding, listening or connect |
|
2244 |
is done. Both arguments must be symbols from one of |
|
2245 |
#inet,#unix, #appletalk, #x25 .. and #stream, #datagram, #raw resp." |
|
2246 |
||
2247 |
|errorNr| |
|
2248 |
||
2249 |
filePointer notNil ifTrue:[ |
|
425 | 2250 |
^ self error:'already created' |
126 | 2251 |
]. |
2252 |
%{ |
|
418 | 2253 |
#ifndef NO_SOCKET |
126 | 2254 |
FILE *fp; |
2255 |
int dom, typ, pf, proto = 0, sock; |
|
2256 |
||
2257 |
if (! __isSymbol(domainArg)) { |
|
425 | 2258 |
DBGPRINTF(("SOCKET: bad domain\n")); |
2259 |
RETURN ( nil ); |
|
126 | 2260 |
} |
2261 |
if (! __isSymbol(typeArg)) { |
|
425 | 2262 |
DBGPRINTF(("SOCKET: bad type\n")); |
2263 |
RETURN ( nil ); |
|
126 | 2264 |
} |
2265 |
if (protocolNumber != nil) { |
|
425 | 2266 |
if (!__isSmallInteger(protocolNumber)) { |
2267 |
DBGPRINTF(("SOCKET: bad protocol\n")); |
|
2268 |
RETURN ( nil ); |
|
2269 |
} |
|
2270 |
proto = __intVal(protocolNumber); |
|
126 | 2271 |
} |
2272 |
||
81 | 2273 |
|
126 | 2274 |
/* |
2275 |
* get address and protocol-family |
|
2276 |
*/ |
|
2277 |
#ifdef AF_UNIX |
|
2278 |
if (domainArg == @symbol(unix)) { |
|
425 | 2279 |
dom = AF_UNIX; |
126 | 2280 |
} else |
2281 |
#endif |
|
2282 |
#ifdef AF_INET |
|
2283 |
if (domainArg == @symbol(inet)) { |
|
425 | 2284 |
dom = AF_INET; |
126 | 2285 |
} else |
2286 |
#endif |
|
2287 |
#ifdef AF_DECnet |
|
2288 |
if (domainArg == @symbol(DECnet)) { |
|
425 | 2289 |
dom = AF_DECnet; |
126 | 2290 |
} else |
2291 |
#endif |
|
2292 |
#ifdef AF_APPLETALK |
|
2293 |
if (domainArg == @symbol(appletalk)) { |
|
425 | 2294 |
dom = AF_APPLETALK; |
126 | 2295 |
} else |
2296 |
#endif |
|
2297 |
#ifdef AF_X25 |
|
2298 |
if (domainArg == @symbol(x25)) { |
|
425 | 2299 |
dom = AF_X25; |
126 | 2300 |
} else |
2301 |
#endif |
|
2302 |
#ifdef AF_NS |
|
2303 |
if (domainArg == @symbol(xns)) { |
|
425 | 2304 |
dom = AF_NS; |
126 | 2305 |
} else |
2306 |
#endif |
|
2307 |
#ifdef AF_SNA |
|
2308 |
if (domainArg == @symbol(sna)) { |
|
425 | 2309 |
dom = AF_SNA; |
126 | 2310 |
} else |
2311 |
#endif |
|
2312 |
#ifdef AF_RAW |
|
2313 |
if (domainArg == @symbol(raw)) { |
|
425 | 2314 |
dom = AF_RAW; |
126 | 2315 |
} else |
2316 |
#endif |
|
2317 |
{ |
|
425 | 2318 |
DBGPRINTF(("SOCKET: unknown domain <%s>\n", _stringVal(domainArg))); |
2319 |
RETURN ( nil ); |
|
126 | 2320 |
} |
2321 |
||
2322 |
#ifdef SOCK_STREAM |
|
2323 |
if (typeArg == @symbol(stream)) { |
|
425 | 2324 |
typ = SOCK_STREAM; |
126 | 2325 |
} else |
2326 |
#endif |
|
2327 |
#ifdef SOCK_DGRAM |
|
2328 |
if (typeArg == @symbol(datagram)) { |
|
425 | 2329 |
typ = SOCK_DGRAM; |
126 | 2330 |
} else |
2331 |
#endif |
|
2332 |
#ifdef SOCK_RAW |
|
2333 |
if (typeArg == @symbol(raw)) |
|
425 | 2334 |
typ = SOCK_RAW; |
126 | 2335 |
else |
2336 |
#endif |
|
2337 |
#ifdef SOCK_SEQPACKET |
|
2338 |
if (typeArg == @symbol(seqPacket)) |
|
425 | 2339 |
typ = SOCK_SEQPACKET; |
126 | 2340 |
else |
2341 |
#endif |
|
2342 |
{ |
|
425 | 2343 |
DBGPRINTF(("SOCKET: bad type <%s>\n", _stringVal(typeArg))); |
2344 |
RETURN ( nil ); |
|
126 | 2345 |
} |
2346 |
||
2347 |
__BEGIN_INTERRUPTABLE__ |
|
2348 |
do { |
|
425 | 2349 |
DBGPRINTF(("SOCKET: opening socket domain=%d type=%d proto=%d\n", dom, typ, proto)); |
2350 |
sock = socket(dom, typ, proto); |
|
126 | 2351 |
#if defined(EPROTONOSUPPORT) /* for SGI */ |
425 | 2352 |
if ((proto != 0) && (sock < 0) && (errno == EPROTONOSUPPORT)) { |
2353 |
DBGPRINTF(("SOCKET: retry with UNSPEC protocol\n")); |
|
2354 |
proto = 0; |
|
2355 |
sock = socket(dom, typ, 0); |
|
2356 |
} |
|
126 | 2357 |
#endif |
2358 |
} while ((sock < 0) && (errno == EINTR)); |
|
2359 |
__END_INTERRUPTABLE__ |
|
2360 |
||
2361 |
if (sock < 0) { |
|
425 | 2362 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=%d) call failed errno=%d\n", dom, typ, proto, errno)); |
2363 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
126 | 2364 |
} else { |
425 | 2365 |
/* |
2366 |
* make it a FILE * |
|
2367 |
*/ |
|
2368 |
fp = fdopen(sock, "r+"); |
|
2369 |
if (! fp) { |
|
2370 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
|
2371 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
|
2372 |
__BEGIN_INTERRUPTABLE__ |
|
2373 |
close(sock); |
|
2374 |
__END_INTERRUPTABLE__ |
|
2375 |
} else { |
|
2376 |
__INST(filePointer) = __MKOBJ(fp); |
|
2377 |
__STORESELF(filePointer); |
|
2378 |
} |
|
126 | 2379 |
} |
418 | 2380 |
#endif |
126 | 2381 |
%}. |
2382 |
||
2383 |
"all ok?" |
|
2384 |
filePointer notNil ifTrue:[ |
|
425 | 2385 |
domain := domainArg. |
2386 |
socketType := typeArg. |
|
2387 |
Lobby register:self. |
|
126 | 2388 |
] ifFalse:[ |
425 | 2389 |
^ nil |
126 | 2390 |
]. |
2391 |
||
2392 |
" |
|
2393 |
Socket new domain:#inet type:#stream |
|
2394 |
Socket new domain:#unix type:#stream |
|
2395 |
" |
|
81 | 2396 |
! |
2397 |
||
126 | 2398 |
for:hostName port:portNr |
2399 |
"setup for a TCP socket (i.e. inet domain, stream type) |
|
2400 |
If hostname is nil, a server port is opened, |
|
2401 |
otherwise a client port to the server on host. |
|
2402 |
||
2403 |
HISTORIC LEFTOVER: |
|
2404 |
This method will vanish, as soon as the low level |
|
2405 |
connect/bind works," |
|
2406 |
||
2407 |
self obsoleteMethodWarning. |
|
2408 |
||
2409 |
filePointer notNil ifTrue:[ |
|
2410 |
^ self error:'already created' |
|
2411 |
]. |
|
2412 |
(portNr isMemberOf:SmallInteger) ifFalse:[ |
|
2413 |
^ self error:'invalid portNr' |
|
2414 |
]. |
|
2415 |
%{ |
|
418 | 2416 |
#ifndef NO_SOCKET |
126 | 2417 |
struct sockaddr_in sa ; |
2418 |
struct hostent *hp ; |
|
2419 |
int a, sock ; |
|
2420 |
long addr; |
|
2421 |
FILE *fp; |
|
2422 |
int ret; |
|
2423 |
int on = 1; |
|
2424 |
||
2425 |
bzero((char *) &sa, sizeof(sa)) ; |
|
2426 |
sa.sin_family = AF_INET; |
|
2427 |
sa.sin_addr.s_addr = htonl(INADDR_ANY); |
|
2428 |
||
2429 |
if ((hostName != nil) && __isString(hostName)){ |
|
2430 |
bzero(&sa, sizeof(sa)) ; |
|
2431 |
if ((addr = inet_addr((char *) _stringVal(hostName))) != -1) { |
|
2432 |
/* |
|
2433 |
* is Internet addr in octet notation |
|
2434 |
*/ |
|
2435 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */ |
|
2436 |
} else { |
|
2437 |
/* |
|
2438 |
* do we know the host's address? |
|
2439 |
*/ |
|
2440 |
if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) { |
|
2441 |
DBGPRINTF(("SOCKET: unknown host\n")); |
|
2442 |
RETURN ( nil ); |
|
2443 |
} |
|
2444 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ; |
|
2445 |
sa.sin_family = hp->h_addrtype; |
|
2446 |
} |
|
2447 |
} |
|
2448 |
||
2449 |
/* |
|
2450 |
* create the socket |
|
2451 |
*/ |
|
2452 |
__BEGIN_INTERRUPTABLE__ |
|
2453 |
do { |
|
2454 |
sock = socket(sa.sin_family, SOCK_STREAM, 0); |
|
2455 |
} while ((sock < 0) && (errno == EINTR)); |
|
2456 |
__END_INTERRUPTABLE__ |
|
2457 |
||
2458 |
if (sock < 0) { |
|
2459 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_STREAM, errno)); |
|
217 | 2460 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2461 |
} else { |
2462 |
/* |
|
2463 |
* connect/bind |
|
2464 |
*/ |
|
2465 |
sa.sin_port = htons((u_short) _intVal(portNr)) ; |
|
2466 |
||
2467 |
__BEGIN_INTERRUPTABLE__ |
|
2468 |
if (hostName != nil) { |
|
2469 |
do { |
|
2470 |
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa)); |
|
2471 |
} while ((ret < 0) && (errno == EINTR)); |
|
2472 |
} else { |
|
2473 |
#ifdef SO_REUSEADDR |
|
2474 |
/* |
|
2475 |
* should I also do this for DGRAM sockets ? |
|
2476 |
*/ |
|
2477 |
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0) { |
|
2478 |
DBGPRINTF(("SOCKET: setsockopt - SO_REUSEADDR failed\n")); |
|
2479 |
} |
|
2480 |
#endif /* SO_REUSEADDR */ |
|
2481 |
sa.sin_addr.s_addr = htonl(INADDR_ANY); |
|
2482 |
do { |
|
2483 |
ret = bind(sock, (struct sockaddr *)&sa, sizeof(sa)); |
|
2484 |
} while ((ret < 0) && (errno == EINTR)); |
|
2485 |
} |
|
2486 |
__END_INTERRUPTABLE__ |
|
2487 |
||
2488 |
if (ret < 0) { |
|
2489 |
DBGPRINTF(("SOCKET: bind/connect call failed errno=%d\n", errno)); |
|
217 | 2490 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2491 |
__BEGIN_INTERRUPTABLE__ |
2492 |
close(sock) ; |
|
2493 |
__END_INTERRUPTABLE__ |
|
2494 |
} else { |
|
2495 |
/* |
|
2496 |
* make it a FILE * |
|
2497 |
*/ |
|
2498 |
fp = fdopen(sock, "r+"); |
|
2499 |
if (! fp) { |
|
2500 |
DBGPRINTF(("SOCKET: fdopen failed\n")); |
|
217 | 2501 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2502 |
__BEGIN_INTERRUPTABLE__ |
2503 |
close(sock); |
|
2504 |
__END_INTERRUPTABLE__ |
|
2505 |
} else { |
|
444 | 2506 |
#ifdef BUGGY_STDIO_LIB |
126 | 2507 |
setbuf(fp, NULL); |
217 | 2508 |
__INST(buffered) = false; |
126 | 2509 |
#endif |
217 | 2510 |
__INST(filePointer) = __MKOBJ(fp); |
425 | 2511 |
__STORESELF(filePointer); |
126 | 2512 |
} |
2513 |
} |
|
2514 |
} |
|
418 | 2515 |
#endif |
126 | 2516 |
%}. |
2517 |
filePointer isNil ifTrue:[ |
|
2518 |
^ nil |
|
2519 |
]. |
|
2520 |
mode := #readwrite. |
|
169 | 2521 |
Lobby register:self. |
126 | 2522 |
binary := false. |
2523 |
||
2524 |
domain := #inet. |
|
2525 |
socketType := #stream. |
|
2526 |
protocol := portNr. |
|
2527 |
peerName := hostName. |
|
81 | 2528 |
|
126 | 2529 |
" |
2530 |
Socket new for:'clam' port:(Socket portOfService:'echo') |
|
2531 |
||
2532 |
Socket new for:nil port:9999 |
|
2533 |
Socket new for:(OperatingSystem getHostName) port:9999 |
|
2534 |
" |
|
2535 |
! |
|
2536 |
||
2537 |
for:hostName udpPort:portNr |
|
2538 |
"setup for a UDP socket (i.e. inet domain, datagram type) |
|
2539 |
if hostname is nil, a server port is opened; |
|
2540 |
otherwise a client port to the server on host. |
|
2541 |
||
2542 |
HISTORIC LEFTOVER: |
|
2543 |
This method will vanish, as soon as the low level |
|
2544 |
connect/bind works," |
|
2545 |
||
2546 |
self obsoleteMethodWarning. |
|
2547 |
||
2548 |
filePointer notNil ifTrue:[ |
|
2549 |
^ self error:'already created' |
|
2550 |
]. |
|
2551 |
(portNr isMemberOf:SmallInteger) ifFalse:[ |
|
2552 |
^ self error:'invalid portNr' |
|
2553 |
]. |
|
2554 |
%{ |
|
418 | 2555 |
#ifndef NO_SOCKET |
126 | 2556 |
struct sockaddr_in sa ; |
2557 |
struct hostent *hp ; |
|
2558 |
int a, sock ; |
|
2559 |
long addr; |
|
2560 |
FILE *fp; |
|
2561 |
int ret; |
|
2562 |
||
2563 |
if (hostName != nil) { |
|
2564 |
bzero(&sa, sizeof(sa)) ; |
|
2565 |
if ((addr = inet_addr((char *) _stringVal(hostName))) != -1) { |
|
2566 |
/* |
|
2567 |
* is Internet addr in octet notation |
|
2568 |
*/ |
|
2569 |
bcopy(&addr, (char *) &sa.sin_addr, sizeof(addr)) ; /* set address */ |
|
2570 |
sa.sin_family = AF_INET ; |
|
2571 |
} else { |
|
2572 |
/* |
|
2573 |
* is hostname - |
|
2574 |
* do we know the host's address? |
|
2575 |
*/ |
|
2576 |
if ((hp = gethostbyname((char *) _stringVal(hostName))) == NULL) { |
|
2577 |
DBGPRINTF(("SOCKET: unknown host\n")); |
|
2578 |
RETURN ( nil ); |
|
2579 |
} |
|
2580 |
bcopy(hp->h_addr, (char *) &sa.sin_addr, hp->h_length) ; |
|
2581 |
sa.sin_family = hp->h_addrtype ; |
|
2582 |
} |
|
2583 |
} else |
|
2584 |
sa.sin_family = AF_INET; |
|
2585 |
||
2586 |
/* |
|
2587 |
* create the socket |
|
2588 |
*/ |
|
2589 |
__BEGIN_INTERRUPTABLE__ |
|
2590 |
do { |
|
2591 |
sock = socket(sa.sin_family, SOCK_DGRAM, 0); |
|
2592 |
} while ((sock < 0) && (errno == EINTR)); |
|
2593 |
__END_INTERRUPTABLE__ |
|
2594 |
||
2595 |
if (sock < 0) { |
|
2596 |
DBGPRINTF(("SOCKET: socket(dom=%d typ=%d proto=0) call failed errno=%d\n", sa.sin_family, SOCK_DGRAM, errno)); |
|
217 | 2597 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2598 |
} else { |
2599 |
/* |
|
2600 |
* ok, |
|
2601 |
* connect/bind |
|
2602 |
*/ |
|
2603 |
__BEGIN_INTERRUPTABLE__ |
|
2604 |
if (hostName == nil) { |
|
2605 |
sa.sin_addr.s_addr = htonl(INADDR_ANY); |
|
2606 |
do { |
|
2607 |
ret = bind(sock, (struct sockaddr *)&sa, sizeof(sa)); |
|
2608 |
} while ((ret < 0) && (errno == EINTR)); |
|
2609 |
} else { |
|
2610 |
sa.sin_port = htons((u_short) _intVal(portNr)) ; |
|
2611 |
do { |
|
2612 |
ret = connect(sock, (struct sockaddr *)&sa, sizeof(sa)); |
|
2613 |
} while ((ret < 0) && (errno == EINTR)); |
|
2614 |
} |
|
2615 |
__END_INTERRUPTABLE__ |
|
2616 |
||
2617 |
if (ret < 0) { |
|
2618 |
DBGPRINTF(("SOCKET: bind/connect call failed\n")); |
|
217 | 2619 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2620 |
__BEGIN_INTERRUPTABLE__ |
2621 |
close(sock) ; |
|
2622 |
__END_INTERRUPTABLE__ |
|
2623 |
} else { |
|
2624 |
/* |
|
2625 |
* make it a FILE * |
|
2626 |
*/ |
|
2627 |
fp = fdopen(sock, "r+"); |
|
2628 |
if (! fp) { |
|
2629 |
DBGPRINTF(("SOCKET: fdopen call failed\n")); |
|
217 | 2630 |
__INST(lastErrorNumber) = __MKSMALLINT(errno); |
126 | 2631 |
__BEGIN_INTERRUPTABLE__ |
2632 |
close(sock); |
|
2633 |
__END_INTERRUPTABLE__ |
|
2634 |
} else { |
|
444 | 2635 |
#ifdef BUGGY_STDIO_LIB |
126 | 2636 |
setbuf(fp, NULL); |
217 | 2637 |
__INST(buffered) = false; |
126 | 2638 |
#endif |
217 | 2639 |
__INST(filePointer) = __MKOBJ(fp); |
425 | 2640 |
__STORESELF(filePointer); |
126 | 2641 |
} |
2642 |
} |
|
2643 |
} |
|
418 | 2644 |
#endif |
126 | 2645 |
%}. |
2646 |
filePointer isNil ifTrue:[ |
|
2647 |
^ nil |
|
2648 |
]. |
|
2649 |
||
2650 |
mode := #readwrite. |
|
169 | 2651 |
Lobby register:self. |
126 | 2652 |
binary := false. |
2653 |
||
2654 |
domain := #inet. |
|
2655 |
socketType := #datagram. |
|
2656 |
protocol := portNr. |
|
2657 |
peerName := hostName. |
|
2658 |
||
2659 |
" |
|
2660 |
Socket new for:'clam' udpPort:(Socket portOfService:'echo') |
|
2661 |
" |
|
28 | 2662 |
! ! |
2663 |
||
2664 |
!Socket methodsFor:'specials'! |
|
2665 |
||
2666 |
receiveTimeout:seconds |
|
71 | 2667 |
"set the receive timeout - for special applications only. |
2668 |
Not all operatingSystems offer this functionality |
|
2669 |
(returns false, if unsupported)" |
|
28 | 2670 |
|
2671 |
|millis| |
|
2672 |
||
2673 |
filePointer isNil ifTrue:[ |
|
42 | 2674 |
^ self error:'not a valid socket' |
28 | 2675 |
]. |
2676 |
millis := (seconds * 1000) rounded. |
|
2677 |
%{ |
|
2678 |
#if defined(SO_RCVTIMEO) && defined(SOL_SOCKET) && defined(HZ) |
|
58 | 2679 |
if (__isSmallInteger(millis)) { |
217 | 2680 |
OBJ fp = __INST(filePointer); |
42 | 2681 |
int sock; |
2682 |
int opt; |
|
28 | 2683 |
|
110
7af19ae8693b
use new FILE* wrapper macros (based on externalAddress)
Claus Gittinger <cg@exept.de>
parents:
106
diff
changeset
|
2684 |
sock = fileno(__FILEVal(fp)); |
42 | 2685 |
opt = _intVal(millis) / (1000 / HZ); |
2686 |
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&opt, sizeof(int)); |
|
2687 |
RETURN(true); |
|
28 | 2688 |
} |
2689 |
#endif |
|
71 | 2690 |
%}. |
28 | 2691 |
^ false |
81 | 2692 |
! |
2693 |
||
126 | 2694 |
sendTimeout:seconds |
2695 |
"set the send timeout - for special applications only. |
|
2696 |
Not all operatingSystems offer this functionality |
|
2697 |
(returns false, if unsupported)" |
|
81 | 2698 |
|
126 | 2699 |
|millis| |
25 | 2700 |
|
126 | 2701 |
filePointer isNil ifTrue:[ |
2702 |
^ self error:'not a valid socket' |
|
2703 |
]. |
|
2704 |
millis := (seconds * 1000) rounded. |
|
2705 |
%{ |
|
2706 |
#if defined(SO_SNDTIMEO) && defined(SOL_SOCKET) && defined(HZ) |
|
2707 |
if (__isSmallInteger(millis)) { |
|
217 | 2708 |
OBJ fp = __INST(filePointer); |
126 | 2709 |
int sock; |
2710 |
int opt; |
|
81 | 2711 |
|
126 | 2712 |
sock = fileno(__FILEVal(fp)); |
2713 |
opt = _intVal(millis) / (1000 / HZ); |
|
2714 |
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&opt, sizeof(int)); |
|
2715 |
RETURN(true); |
|
2716 |
} |
|
2717 |
#endif |
|
2718 |
%}. |
|
2719 |
^ false |
|
2720 |
! ! |
|
82 | 2721 |
|
464 | 2722 |
!Socket class methodsFor:'documentation'! |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2723 |
|
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2724 |
version |
509 | 2725 |
^ '$Header: /cvs/stx/stx/libbasic2/Socket.st,v 1.80 1997-03-25 08:53:21 cg Exp $' |
206
77166a6b3ee6
For ST80 style socket creation: register socket in Lobby.
Stefan Vogel <sv@exept.de>
parents:
203
diff
changeset
|
2726 |
! ! |