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