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