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