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