| 1 | /* |
| 2 | * Copyright (c) 2003 Ben Harris |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person |
| 6 | * obtaining a copy of this software and associated documentation |
| 7 | * files (the "Software"), to deal in the Software without |
| 8 | * restriction, including without limitation the rights to use, |
| 9 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following |
| 12 | * conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be |
| 15 | * included in all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR |
| 21 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 22 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | * SOFTWARE. |
| 25 | */ |
| 26 | /* |
| 27 | * mtcpnet.c - MacTCP interface |
| 28 | */ |
| 29 | |
| 30 | #include <MacTypes.h> |
| 31 | #include <Devices.h> |
| 32 | #include <Endian.h> |
| 33 | #include <Folders.h> |
| 34 | #include <MacTCP.h> |
| 35 | #include <MixedMode.h> |
| 36 | #include <Resources.h> |
| 37 | |
| 38 | #include <assert.h> |
| 39 | |
| 40 | #define DEFINE_PLUG_METHOD_MACROS |
| 41 | #include "putty.h" |
| 42 | #include "network.h" |
| 43 | #include "mac.h" |
| 44 | |
| 45 | /* |
| 46 | * The following structures are documented as being in |
| 47 | * <AddressXlation.h>, but that isn't shipped with Universal |
| 48 | * Interfaces, and it's easier to define them here than to require |
| 49 | * people to download yet another SDK. |
| 50 | */ |
| 51 | |
| 52 | static OSErr OpenResolver(char *); |
| 53 | static OSErr CloseResolver(void); |
| 54 | |
| 55 | enum { |
| 56 | OPENRESOLVER = 1, |
| 57 | CLOSERESOLVER, |
| 58 | STRTOADDR, |
| 59 | ADDRTOSTR, |
| 60 | ENUMCACHE, |
| 61 | ADDRTONAME, |
| 62 | HXINFO, |
| 63 | MXINFO |
| 64 | }; |
| 65 | |
| 66 | #define NUM_ALT_ADDRS 4 |
| 67 | |
| 68 | typedef struct hostInfo { |
| 69 | int rtnCode; |
| 70 | char cname[255]; |
| 71 | unsigned long addr[NUM_ALT_ADDRS]; |
| 72 | }; |
| 73 | |
| 74 | typedef CALLBACK_API(void, ResultProcPtr)(struct hostInfo *, char *); |
| 75 | typedef STACK_UPP_TYPE(ResultProcPtr) ResultUPP; |
| 76 | enum { uppResultProcInfo = kPascalStackBased |
| 77 | | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(struct hostInfo*))) |
| 78 | | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *))) |
| 79 | }; |
| 80 | #define NewResultUPP(userRoutine) \ |
| 81 | (ResultUPP)NewRoutineDescriptor((ProcPtr)(userRoutine), \ |
| 82 | uppResultProcInfo, \ |
| 83 | GetCurrentArchitecture()) |
| 84 | #define DisposeResultUPP(userUPP) DisposeRoutineDescriptor(userUPP) |
| 85 | |
| 86 | static OSErr StrToAddr(char *, struct hostInfo *, ResultUPP *, char *); |
| 87 | |
| 88 | typedef CALLBACK_API_C(OSErr, OpenResolverProcPtr)(UInt32, char *); |
| 89 | typedef STACK_UPP_TYPE(OpenResolverProcPtr) OpenResolverUPP; |
| 90 | enum { uppOpenResolverProcInfo = kCStackBased |
| 91 | | RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
| 92 | | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(UInt32))) |
| 93 | | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char*))) |
| 94 | }; |
| 95 | #define InvokeOpenResolverUPP(selector, fileName, userUPP) \ |
| 96 | CALL_TWO_PARAMETER_UPP((userUPP), uppOpenResolverProcInfo, \ |
| 97 | (selector), (fileName)) |
| 98 | |
| 99 | typedef CALLBACK_API_C(OSErr, CloseResolverProcPtr)(UInt32); |
| 100 | typedef STACK_UPP_TYPE(CloseResolverProcPtr) CloseResolverUPP; |
| 101 | enum { uppCloseResolverProcInfo = kCStackBased |
| 102 | | RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
| 103 | | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(UInt32))) |
| 104 | }; |
| 105 | #define InvokeCloseResolverUPP(selector, userUPP) \ |
| 106 | CALL_ONE_PARAMETER_UPP((userUPP), uppCloseResolverProcInfo, (selector)) |
| 107 | |
| 108 | typedef CALLBACK_API_C(OSErr, StrToAddrProcPtr)(UInt32, char *, |
| 109 | struct hostInfo *, ResultUPP, |
| 110 | char *); |
| 111 | typedef STACK_UPP_TYPE(StrToAddrProcPtr) StrToAddrUPP; |
| 112 | enum { uppStrToAddrProcInfo = kCStackBased |
| 113 | | RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
| 114 | | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(UInt32))) |
| 115 | | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *))) |
| 116 | | STACK_ROUTINE_PARAMETER(3, SIZE_CODE(sizeof(struct hostInfo *))) |
| 117 | | STACK_ROUTINE_PARAMETER(4, SIZE_CODE(sizeof(ResultUPP))) |
| 118 | | STACK_ROUTINE_PARAMETER(5, SIZE_CODE(sizeof(char *))) |
| 119 | }; |
| 120 | #define InvokeStrToAddrUPP(selector, hostName, hostInfoPtr, ResultProc, \ |
| 121 | userDataPtr, userUPP) \ |
| 122 | CALL_FIVE_PARAMETER_UPP((userUPP), uppStrToAddrProcInfo, (selector),\ |
| 123 | (hostName), (hostInfoPtr), (ResultProc), \ |
| 124 | (userDataPtr)) |
| 125 | #define StrToAddr(hostName, hostInfoPtr, ResultProc, userDataPtr) \ |
| 126 | InvokeStrToAddrUPP(STRTOADDR, hostName, hostInfoPtr, ResultProc, \ |
| 127 | userDataPtr, (StrToAddrUPP)*mactcp.dnr_handle) |
| 128 | |
| 129 | typedef CALLBACK_API_C(OSErr, AddrToStrProcPtr)(UInt32, unsigned long, char *); |
| 130 | typedef STACK_UPP_TYPE(AddrToStrProcPtr) AddrToStrUPP; |
| 131 | enum { uppAddrToStrProcInfo = kCStackBased |
| 132 | | RESULT_SIZE(SIZE_CODE(sizeof(OSErr))) |
| 133 | | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(unsigned long))) |
| 134 | | STACK_ROUTINE_PARAMETER(2, SIZE_CODE(sizeof(char *))) |
| 135 | }; |
| 136 | #define InvokeAddrToStrUPP(selector, addr, addrStr, userUPP) \ |
| 137 | CALL_THREE_PARAMETER_UPP((userUPP), uppAddrToStrProcInfo, (selector),\ |
| 138 | (addr), (addrStr)) |
| 139 | #define AddrToStr(addr, addrStr) \ |
| 140 | InvokeAddrToStrUPP(ADDRTOSTR, addr, addrStr, \ |
| 141 | (AddrToStrUPP)*mactcp.dnr_handle) |
| 142 | |
| 143 | /* End of AddressXlation.h bits */ |
| 144 | |
| 145 | struct Socket_tag { |
| 146 | struct socket_function_table *fn; |
| 147 | /* the above variable absolutely *must* be the first in this structure */ |
| 148 | StreamPtr s; |
| 149 | OSErr err; |
| 150 | Plug plug; |
| 151 | void *private_ptr; |
| 152 | bufchain output_data; |
| 153 | int connected; |
| 154 | int writable; |
| 155 | int frozen; /* this causes readability notifications to be ignored */ |
| 156 | int frozen_readable; /* this means we missed at least one readability |
| 157 | * notification while we were frozen */ |
| 158 | int localhost_only; /* for listening sockets */ |
| 159 | char oobdata[1]; |
| 160 | int sending_oob; |
| 161 | int oobpending; /* is there OOB data available to read? */ |
| 162 | int oobinline; |
| 163 | int pending_error; /* in case send() returns error */ |
| 164 | int listener; |
| 165 | struct Socket_tag *next; |
| 166 | struct Socket_tag **prev; |
| 167 | }; |
| 168 | |
| 169 | /* |
| 170 | * We used to typedef struct Socket_tag *Socket. |
| 171 | * |
| 172 | * Since we have made the networking abstraction slightly more |
| 173 | * abstract, Socket no longer means a tcp socket (it could mean |
| 174 | * an ssl socket). So now we must use Actual_Socket when we know |
| 175 | * we are talking about a tcp socket. |
| 176 | */ |
| 177 | typedef struct Socket_tag *Actual_Socket; |
| 178 | |
| 179 | struct SockAddr_tag { |
| 180 | int resolved; |
| 181 | struct hostInfo hostinfo; |
| 182 | char hostname[512]; |
| 183 | }; |
| 184 | |
| 185 | /* Global variables */ |
| 186 | static struct { |
| 187 | Handle dnr_handle; |
| 188 | int initialised; |
| 189 | short refnum; |
| 190 | ProcessSerialNumber self; |
| 191 | Actual_Socket socklist; |
| 192 | } mactcp; |
| 193 | |
| 194 | static pascal void mactcp_lookupdone(struct hostInfo *hi, char *cookie); |
| 195 | static pascal void mactcp_asr(StreamPtr, unsigned short, Ptr, unsigned short, |
| 196 | struct ICMPReport *); |
| 197 | static Plug mactcp_plug(Socket, Plug); |
| 198 | static void mactcp_flush(Socket); |
| 199 | static void mactcp_close(Socket); |
| 200 | static int mactcp_write(Socket, char *, int); |
| 201 | static int mactcp_write_oob(Socket, char *, int); |
| 202 | static void mactcp_set_private_ptr(Socket, void *); |
| 203 | static void *mactcp_get_private_ptr(Socket); |
| 204 | static char *mactcp_socket_error(Socket); |
| 205 | static void mactcp_set_frozen(Socket, int); |
| 206 | |
| 207 | static void mactcp_recv(Actual_Socket s, size_t len); |
| 208 | |
| 209 | /* |
| 210 | * Initialise MacTCP. |
| 211 | * This should be called once before any TCP connection is opened. |
| 212 | */ |
| 213 | |
| 214 | OSErr mactcp_init(void) |
| 215 | { |
| 216 | OSErr err; |
| 217 | |
| 218 | /* |
| 219 | * IM:Devices describes a convoluted way of finding a spare unit |
| 220 | * number to open a driver on before calling OpenDriver. Happily, |
| 221 | * the MacTCP INIT ensures that .IPP is already open (and hence |
| 222 | * has a valid unit number already) so we don't need to go through |
| 223 | * all that. (MacTCP Programmer's Guide p6) |
| 224 | */ |
| 225 | err = OpenDriver("\p.IPP", &mactcp.refnum); |
| 226 | if (err != noErr) return err; |
| 227 | err = OpenResolver(NULL); |
| 228 | if (err != noErr) return err; |
| 229 | |
| 230 | mactcp.initialised = TRUE; |
| 231 | return noErr; |
| 232 | } |
| 233 | |
| 234 | void mactcp_shutdown(void) |
| 235 | { |
| 236 | |
| 237 | CloseResolver(); |
| 238 | mactcp.initialised = FALSE; |
| 239 | } |
| 240 | |
| 241 | static ResultUPP mactcp_lookupdone_upp; |
| 242 | |
| 243 | SockAddr sk_namelookup(char *host, char **canonicalname) |
| 244 | { |
| 245 | SockAddr ret = smalloc(sizeof(struct SockAddr_tag)); |
| 246 | OSErr err; |
| 247 | volatile int done = FALSE; |
| 248 | char *realhost; |
| 249 | |
| 250 | /* Clear the structure. */ |
| 251 | memset(ret, 0, sizeof(struct SockAddr_tag)); |
| 252 | if (mactcp_lookupdone_upp == NULL) |
| 253 | mactcp_lookupdone_upp = NewResultUPP(&mactcp_lookupdone); |
| 254 | err = StrToAddr(host, &ret->hostinfo, mactcp_lookupdone_upp, |
| 255 | (char *)&done); |
| 256 | /* |
| 257 | * PuTTY expects DNS lookups to be synchronous (see bug |
| 258 | * "async-dns"), so we pretend they are. |
| 259 | */ |
| 260 | if (err == cacheFault) |
| 261 | while (!done) |
| 262 | continue; |
| 263 | ret->resolved = TRUE; |
| 264 | |
| 265 | if (ret->hostinfo.rtnCode == noErr) |
| 266 | realhost = ret->hostinfo.cname; |
| 267 | else |
| 268 | realhost = ""; |
| 269 | *canonicalname = smalloc(1+strlen(realhost)); |
| 270 | strcpy(*canonicalname, realhost); |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static pascal void mactcp_lookupdone(struct hostInfo *hi, char *cookie) |
| 275 | { |
| 276 | volatile int *donep = (int *)cookie; |
| 277 | |
| 278 | *donep = TRUE; |
| 279 | } |
| 280 | |
| 281 | SockAddr sk_nonamelookup(char *host) |
| 282 | { |
| 283 | SockAddr ret = smalloc(sizeof(struct SockAddr_tag)); |
| 284 | |
| 285 | ret->resolved = FALSE; |
| 286 | ret->hostinfo.rtnCode = noErr; |
| 287 | ret->hostname[0] = '\0'; |
| 288 | strncat(ret->hostname, host, lenof(ret->hostname) - 1); |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | void sk_getaddr(SockAddr addr, char *buf, int buflen) |
| 293 | { |
| 294 | char mybuf[16]; |
| 295 | OSErr err; |
| 296 | |
| 297 | if (addr->resolved) { |
| 298 | /* XXX only return first address */ |
| 299 | err = AddrToStr(addr->hostinfo.addr[0], mybuf); |
| 300 | buf[0] = '\0'; |
| 301 | if (err != noErr) |
| 302 | strncat(buf, mybuf, buflen - 1); |
| 303 | } else { |
| 304 | buf[0] = '\0'; |
| 305 | strncat(buf, addr->hostname, buflen - 1); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /* I think "local" here really means "loopback" */ |
| 310 | |
| 311 | int sk_hostname_is_local(char *name) |
| 312 | { |
| 313 | |
| 314 | return !strcmp(name, "localhost"); |
| 315 | } |
| 316 | |
| 317 | int sk_address_is_local(SockAddr addr) |
| 318 | { |
| 319 | int i; |
| 320 | |
| 321 | if (addr->resolved) |
| 322 | for (i = 0; i < NUM_ALT_ADDRS; i++) |
| 323 | if (addr->hostinfo.addr[i] & 0xff000000 == 0x7f000000) |
| 324 | return TRUE; |
| 325 | return FALSE; |
| 326 | } |
| 327 | |
| 328 | int sk_addrtype(SockAddr addr) |
| 329 | { |
| 330 | |
| 331 | if (addr->resolved) |
| 332 | return ADDRTYPE_IPV4; |
| 333 | return ADDRTYPE_NAME; |
| 334 | } |
| 335 | |
| 336 | void sk_addrcopy(SockAddr addr, char *buf) |
| 337 | { |
| 338 | |
| 339 | /* XXX only return first address */ |
| 340 | memcpy(buf, &addr->hostinfo.addr[0], 4); |
| 341 | } |
| 342 | |
| 343 | void sk_addr_free(SockAddr addr) |
| 344 | { |
| 345 | |
| 346 | sfree(addr); |
| 347 | } |
| 348 | |
| 349 | static Plug mactcp_plug(Socket sock, Plug p) |
| 350 | { |
| 351 | Actual_Socket s = (Actual_Socket) sock; |
| 352 | Plug ret = s->plug; |
| 353 | |
| 354 | if (p) |
| 355 | s->plug = p; |
| 356 | return ret; |
| 357 | } |
| 358 | |
| 359 | static void mactcp_flush(Socket s) |
| 360 | { |
| 361 | |
| 362 | fatalbox("sk_tcp_flush"); |
| 363 | } |
| 364 | |
| 365 | Socket sk_register(void *sock, Plug plug) |
| 366 | { |
| 367 | |
| 368 | fatalbox("sk_register"); |
| 369 | } |
| 370 | |
| 371 | static TCPNotifyUPP mactcp_asr_upp; |
| 372 | |
| 373 | Socket sk_new(SockAddr addr, int port, int privport, int oobinline, |
| 374 | int nodelay, Plug plug) |
| 375 | { |
| 376 | static struct socket_function_table fn_table = { |
| 377 | mactcp_plug, |
| 378 | mactcp_close, |
| 379 | mactcp_write, |
| 380 | mactcp_write_oob, |
| 381 | mactcp_flush, |
| 382 | mactcp_set_private_ptr, |
| 383 | mactcp_get_private_ptr, |
| 384 | mactcp_set_frozen, |
| 385 | mactcp_socket_error |
| 386 | }; |
| 387 | TCPiopb pb; |
| 388 | UDPiopb upb; |
| 389 | Actual_Socket ret; |
| 390 | ip_addr dstaddr; |
| 391 | size_t buflen; |
| 392 | |
| 393 | /* |
| 394 | * Create Socket structure. |
| 395 | */ |
| 396 | ret = smalloc(sizeof(struct Socket_tag)); |
| 397 | ret->s = 0; |
| 398 | ret->fn = &fn_table; |
| 399 | ret->err = noErr; |
| 400 | ret->plug = plug; |
| 401 | bufchain_init(&ret->output_data); |
| 402 | ret->connected = 0; /* to start with */ |
| 403 | ret->writable = 0; /* to start with */ |
| 404 | ret->sending_oob = 0; |
| 405 | ret->frozen = 0; |
| 406 | ret->frozen_readable = 0; |
| 407 | ret->localhost_only = 0; /* unused, but best init anyway */ |
| 408 | ret->pending_error = 0; |
| 409 | ret->oobinline = oobinline; |
| 410 | ret->oobpending = FALSE; |
| 411 | ret->listener = 0; |
| 412 | |
| 413 | dstaddr = addr->hostinfo.addr[0]; /* XXX should try all of them */ |
| 414 | /* |
| 415 | * Create a TCP stream. |
| 416 | * |
| 417 | * MacTCP requires us to provide it with some buffer memory. Page |
| 418 | * 31 of the Programmer's Guide says it should be a minimum of |
| 419 | * 4*MTU+1024. Page 36 says a minimum of 4096 bytes. Assume |
| 420 | * they're both correct. |
| 421 | */ |
| 422 | assert(addr->resolved); |
| 423 | upb.ioCRefNum = mactcp.refnum; |
| 424 | upb.csCode = UDPMaxMTUSize; |
| 425 | upb.csParam.mtu.remoteHost = dstaddr; |
| 426 | upb.csParam.mtu.userDataPtr = NULL; |
| 427 | ret->err = PBControlSync((ParmBlkPtr)&upb); |
| 428 | if (ret->err != noErr) return (Socket)ret; |
| 429 | |
| 430 | buflen = upb.csParam.mtu.mtuSize * 4 + 1024; |
| 431 | if (buflen < 4096) buflen = 4096; |
| 432 | if (mactcp_asr_upp == NULL) |
| 433 | mactcp_asr_upp = NewTCPNotifyUPP(&mactcp_asr); |
| 434 | GetCurrentProcess(&mactcp.self); |
| 435 | pb.ioCRefNum = mactcp.refnum; |
| 436 | pb.csCode = TCPCreate; |
| 437 | pb.csParam.create.rcvBuff = smalloc(buflen); |
| 438 | pb.csParam.create.rcvBuffLen = buflen; |
| 439 | pb.csParam.create.notifyProc = mactcp_asr_upp; |
| 440 | pb.csParam.create.userDataPtr = (Ptr)ret; |
| 441 | ret->err = PBControlSync((ParmBlkPtr)&pb); |
| 442 | if (ret->err != noErr) return (Socket)ret; |
| 443 | ret->s = pb.tcpStream; |
| 444 | |
| 445 | /* |
| 446 | * Open the connection. |
| 447 | */ |
| 448 | pb.ioCRefNum = mactcp.refnum; |
| 449 | pb.csCode = TCPActiveOpen; |
| 450 | pb.tcpStream = ret->s; |
| 451 | pb.csParam.open.validityFlags = 0; |
| 452 | pb.csParam.open.remoteHost = dstaddr; |
| 453 | pb.csParam.open.remotePort = port; |
| 454 | pb.csParam.open.localPort = privport ? 1023 : 0; |
| 455 | pb.csParam.open.dontFrag = FALSE; |
| 456 | pb.csParam.open.timeToLive = 0; |
| 457 | pb.csParam.open.security = 0; |
| 458 | pb.csParam.open.optionCnt = 0; |
| 459 | pb.csParam.open.userDataPtr = (Ptr)ret; |
| 460 | while (1) { |
| 461 | ret->err = PBControlSync((ParmBlkPtr)&pb); |
| 462 | if (!privport || ret->err != duplicateSocket) |
| 463 | break; |
| 464 | pb.csParam.open.localPort--; |
| 465 | if (pb.csParam.open.localPort == 0) |
| 466 | break; |
| 467 | } |
| 468 | |
| 469 | if (ret->err != noErr) return (Socket)ret; |
| 470 | |
| 471 | ret->connected = TRUE; |
| 472 | ret->writable = TRUE; |
| 473 | |
| 474 | /* Add this to the list of all sockets */ |
| 475 | ret->next = mactcp.socklist; |
| 476 | ret->prev = &mactcp.socklist; |
| 477 | ret->next->prev = &ret->next; |
| 478 | mactcp.socklist = ret; |
| 479 | |
| 480 | return (Socket)ret; |
| 481 | } |
| 482 | |
| 483 | Socket sk_newlistener(char *srcaddr, int port, Plug plug, int local_host_only) |
| 484 | { |
| 485 | |
| 486 | fatalbox("sk_newlistener"); |
| 487 | } |
| 488 | |
| 489 | static void mactcp_close(Socket sock) |
| 490 | { |
| 491 | Actual_Socket s = (Actual_Socket)sock; |
| 492 | TCPiopb pb; |
| 493 | |
| 494 | /* |
| 495 | * TCPClose is equivalent to shutdown(fd, SHUT_WR), and hence |
| 496 | * leaves the Rx side open, while TCPAbort seems rather vicious, |
| 497 | * throwing away Tx data that haven't been ACKed yet. We do both |
| 498 | * in succession. |
| 499 | */ |
| 500 | pb.ioCRefNum = mactcp.refnum; |
| 501 | pb.csCode = TCPClose; |
| 502 | pb.tcpStream = s->s; |
| 503 | pb.csParam.close.validityFlags = 0; |
| 504 | pb.csParam.close.userDataPtr = (Ptr)s; |
| 505 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 506 | /* Not much we can do about an error anyway. */ |
| 507 | |
| 508 | pb.ioCRefNum = mactcp.refnum; |
| 509 | pb.csCode = TCPAbort; |
| 510 | pb.tcpStream = s->s; |
| 511 | pb.csParam.abort.userDataPtr = (Ptr)s; |
| 512 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 513 | /* Even less we can do about an error here. */ |
| 514 | |
| 515 | pb.ioCRefNum = mactcp.refnum; |
| 516 | pb.csCode = TCPRelease; |
| 517 | pb.tcpStream = s->s; |
| 518 | pb.csParam.create.userDataPtr = (Ptr)s; |
| 519 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 520 | if (s->err == noErr) |
| 521 | sfree(pb.csParam.create.rcvBuff); |
| 522 | |
| 523 | /* Unhitch from list of sockets */ |
| 524 | *s->prev = s->next; |
| 525 | if (s->next != NULL) |
| 526 | s->next->prev = s->prev; |
| 527 | |
| 528 | sfree(s); |
| 529 | } |
| 530 | |
| 531 | static int mactcp_write(Socket sock, char *buf, int len) |
| 532 | { |
| 533 | Actual_Socket s = (Actual_Socket) sock; |
| 534 | wdsEntry wds[2]; |
| 535 | TCPiopb pb; |
| 536 | |
| 537 | wds[0].length = len; |
| 538 | wds[0].ptr = buf; |
| 539 | wds[1].length = 0; |
| 540 | |
| 541 | pb.ioCRefNum = mactcp.refnum; |
| 542 | pb.csCode = TCPSend; |
| 543 | pb.tcpStream = s->s; |
| 544 | pb.csParam.send.validityFlags = 0; |
| 545 | pb.csParam.send.pushFlag = TRUE; /* XXX we want it to return. */ |
| 546 | pb.csParam.send.urgentFlag = 0; |
| 547 | pb.csParam.send.wdsPtr = (Ptr)wds; |
| 548 | pb.csParam.send.userDataPtr = (Ptr)s; |
| 549 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | static int mactcp_write_oob(Socket sock, char *buf, int len) |
| 554 | { |
| 555 | |
| 556 | fatalbox("mactcp_write_oob"); |
| 557 | } |
| 558 | |
| 559 | static pascal void mactcp_asr(StreamPtr str, unsigned short event, Ptr cookie, |
| 560 | unsigned short termin_reason, |
| 561 | struct ICMPReport *icmp) |
| 562 | { |
| 563 | |
| 564 | WakeUpProcess(&mactcp.self); |
| 565 | } |
| 566 | |
| 567 | /* |
| 568 | * Called from our event loop if there's work to do. |
| 569 | */ |
| 570 | void mactcp_poll(void) |
| 571 | { |
| 572 | Actual_Socket s; |
| 573 | TCPiopb pb; |
| 574 | |
| 575 | for (s = mactcp.socklist; s != NULL; s = s->next) { |
| 576 | /* XXX above can't handle sockets being deleted. */ |
| 577 | pb.ioCRefNum = mactcp.refnum; |
| 578 | pb.csCode = TCPStatus; |
| 579 | pb.tcpStream = s->s; |
| 580 | pb.csParam.status.userDataPtr = (Ptr)s; |
| 581 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 582 | if (s->err != noErr) |
| 583 | continue; |
| 584 | if (pb.csParam.status.amtUnreadData > 0) |
| 585 | mactcp_recv(s, pb.csParam.status.amtUnreadData); |
| 586 | /* Should check connectionState in case remote has closed */ |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | static void mactcp_recv(Actual_Socket s, size_t len) |
| 591 | { |
| 592 | rdsEntry rds[2]; |
| 593 | TCPiopb pb; |
| 594 | |
| 595 | if (s->frozen) return; |
| 596 | |
| 597 | while (len > 0) { |
| 598 | pb.ioCRefNum = mactcp.refnum; |
| 599 | pb.csCode = TCPNoCopyRcv; |
| 600 | pb.tcpStream = s->s; |
| 601 | pb.csParam.receive.commandTimeoutValue = 0; |
| 602 | pb.csParam.receive.rdsPtr = (Ptr)rds; |
| 603 | pb.csParam.receive.rdsLength = lenof(rds) - 1; |
| 604 | pb.csParam.receive.userDataPtr = (Ptr)s; |
| 605 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 606 | if (s->err != noErr) |
| 607 | return; |
| 608 | plug_receive(s->plug, 0, rds[0].ptr, rds[0].length); |
| 609 | len -= rds[0].length; |
| 610 | pb.csCode = TCPRcvBfrReturn; |
| 611 | s->err = PBControlSync((ParmBlkPtr)&pb); |
| 612 | if (s->err != noErr) |
| 613 | return; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | * Each socket abstraction contains a `void *' private field in |
| 619 | * which the client can keep state. |
| 620 | */ |
| 621 | static void mactcp_set_private_ptr(Socket sock, void *ptr) |
| 622 | { |
| 623 | Actual_Socket s = (Actual_Socket) sock; |
| 624 | s->private_ptr = ptr; |
| 625 | } |
| 626 | |
| 627 | static void *mactcp_get_private_ptr(Socket sock) |
| 628 | { |
| 629 | Actual_Socket s = (Actual_Socket) sock; |
| 630 | return s->private_ptr; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Special error values are returned from sk_namelookup and sk_new |
| 635 | * if there's a problem. These functions extract an error message, |
| 636 | * or return NULL if there's no problem. |
| 637 | */ |
| 638 | char *sk_addr_error(SockAddr addr) |
| 639 | { |
| 640 | static char buf[64]; |
| 641 | |
| 642 | switch (addr->hostinfo.rtnCode) { |
| 643 | case noErr: |
| 644 | return NULL; |
| 645 | case nameSyntaxErr: |
| 646 | return "Name syntax error"; |
| 647 | case noNameServer: |
| 648 | return "No name server found"; |
| 649 | case authNameErr: |
| 650 | return "Domain name does not exist"; |
| 651 | case noAnsErr: |
| 652 | return "No answer from domain name server"; |
| 653 | case dnrErr: |
| 654 | return "Domain name server returned an error"; |
| 655 | case outOfMemory: |
| 656 | return "Out of memory"; |
| 657 | default: |
| 658 | sprintf(buf, "Unknown DNR error %d", addr->hostinfo.rtnCode); |
| 659 | return buf; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | static char *mactcp_socket_error(Socket sock) |
| 664 | { |
| 665 | static char buf[64]; |
| 666 | Actual_Socket s = (Actual_Socket) sock; |
| 667 | |
| 668 | switch (s->err) { |
| 669 | case noErr: |
| 670 | return NULL; |
| 671 | case insufficientResources: |
| 672 | return "Insufficient resources to open TCP stream"; |
| 673 | case duplicateSocket: |
| 674 | return "Duplicate socket"; |
| 675 | case openFailed: |
| 676 | return "Connection failed while opening"; |
| 677 | default: |
| 678 | sprintf(buf, "Unknown MacTCP error %d", s->err); |
| 679 | return buf; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | static void mactcp_set_frozen(Socket sock, int is_frozen) |
| 684 | { |
| 685 | Actual_Socket s = (Actual_Socket) sock; |
| 686 | |
| 687 | if (s->frozen == is_frozen) |
| 688 | return; |
| 689 | s->frozen = is_frozen; |
| 690 | } |
| 691 | |
| 692 | /* |
| 693 | * Bits below here would usually be in dnr.c, shipped with the MacTCP |
| 694 | * SDK, but its convenient not to require that, and since we assume |
| 695 | * System 7 we can actually simplify things a lot. |
| 696 | */ |
| 697 | |
| 698 | static OSErr OpenResolver(char *hosts_file) |
| 699 | { |
| 700 | short vrefnum; |
| 701 | long dirid; |
| 702 | HParamBlockRec pb; |
| 703 | Str255 filename; |
| 704 | OSErr err; |
| 705 | int fd; |
| 706 | Handle dnr_handle; |
| 707 | |
| 708 | if (mactcp.dnr_handle != NULL) |
| 709 | return noErr; |
| 710 | |
| 711 | err = FindFolder(kOnSystemDisk, kControlPanelFolderType, FALSE, &vrefnum, |
| 712 | &dirid); |
| 713 | if (err != noErr) return err; |
| 714 | |
| 715 | /* |
| 716 | * Might be better to use PBCatSearch here, but it's not always |
| 717 | * available. |
| 718 | */ |
| 719 | pb.fileParam.ioCompletion = NULL; |
| 720 | pb.fileParam.ioNamePtr = filename; |
| 721 | pb.fileParam.ioVRefNum = vrefnum; |
| 722 | pb.fileParam.ioFDirIndex = 1; |
| 723 | pb.fileParam.ioDirID = dirid; |
| 724 | fd = -1; |
| 725 | |
| 726 | while (PBHGetFInfoSync(&pb) == noErr) { |
| 727 | if (pb.fileParam.ioFlFndrInfo.fdType == 'cdev' && |
| 728 | pb.fileParam.ioFlFndrInfo.fdCreator == 'ztcp') { |
| 729 | fd = HOpenResFile(vrefnum, dirid, filename, fsRdPerm); |
| 730 | if (fd == -1) continue; |
| 731 | dnr_handle = Get1IndResource('dnrp', 1); |
| 732 | if (dnr_handle != NULL) |
| 733 | break; |
| 734 | CloseResFile(fd); |
| 735 | fd = -1; |
| 736 | } |
| 737 | pb.fileParam.ioDirID = dirid; |
| 738 | pb.fileParam.ioFDirIndex++; |
| 739 | } |
| 740 | if (fd == -1) |
| 741 | return fnfErr; |
| 742 | |
| 743 | DetachResource(dnr_handle); |
| 744 | CloseResFile(fd); |
| 745 | |
| 746 | MoveHHi(dnr_handle); |
| 747 | HLock(dnr_handle); |
| 748 | |
| 749 | err = InvokeOpenResolverUPP(OPENRESOLVER, hosts_file, |
| 750 | (OpenResolverUPP)*dnr_handle); |
| 751 | if (err != noErr) { |
| 752 | HUnlock(dnr_handle); |
| 753 | DisposeHandle(dnr_handle); |
| 754 | return err; |
| 755 | } |
| 756 | |
| 757 | mactcp.dnr_handle = dnr_handle; |
| 758 | return noErr; |
| 759 | } |
| 760 | |
| 761 | OSErr CloseResolver(void) |
| 762 | { |
| 763 | Handle dnr_handle = mactcp.dnr_handle; |
| 764 | OSErr err; |
| 765 | |
| 766 | if (mactcp.dnr_handle == NULL) |
| 767 | return notOpenErr; |
| 768 | |
| 769 | err = InvokeCloseResolverUPP(CLOSERESOLVER, |
| 770 | (CloseResolverUPP)*mactcp.dnr_handle); |
| 771 | if (err != noErr) |
| 772 | return err; |
| 773 | |
| 774 | mactcp.dnr_handle = NULL; |
| 775 | HUnlock(dnr_handle); |
| 776 | DisposeHandle(dnr_handle); |
| 777 | return noErr; |
| 778 | } |
| 779 | |
| 780 | /* MacTCP doesn't have a services database. */ |
| 781 | int net_service_lookup(char *service) |
| 782 | { |
| 783 | |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | |
| 788 | /* |
| 789 | * Local Variables: |
| 790 | * c-file-style: "simon" |
| 791 | * End: |
| 792 | */ |