PR_IMPLEMENT(PRStatus) _MD_gethostname(char *name, int namelen) { OSStatus err; InetInterfaceInfo info; /* * On a Macintosh, we donÕt have the concept of a local host name. * We do though have an IP address & everyone should be happy with * a string version of that for a name. * The alternative here is to ping a local DNS for our name, they * will often know it. This is the cheap, easiest, and safest way out. */ /* Make sure the string is as long as the longest possible address */ if (namelen < strlen("123.123.123.123")) { err = kEINVALErr; goto ErrorExit; } err = OTInetGetInterfaceInfo(&info, kDefaultInetInterface); if (err != kOTNoError) goto ErrorExit; OTInetHostToString(info.fAddress, name); return PR_SUCCESS; ErrorExit: macsock_map_error(err); return PR_FAILURE; }
static pascal OSStatus DoIncomingBind(OTMPEndpointRef ep, InetPort portNum, OTQLen qlen) { OSStatus err; TBind req; TBind ret; InetAddress reqAddr; InetAddress retAddr; OTInitInetAddress(&reqAddr, portNum, kOTAnyInetAddress); OTMemzero(&req, sizeof(req)); req.addr.len = sizeof(reqAddr); req.addr.buf = (UInt8 *) &reqAddr; req.qlen = qlen; OTMemzero(&ret, sizeof(ret)); ret.addr.maxlen = sizeof(retAddr); ret.addr.buf = (UInt8 *) &retAddr; err = OTMPXBind(ep, &req, &ret); if (err == noErr) { char addrAsStr[32]; OTInetHostToString(retAddr.fHost, addrAsStr); MPLogPrintf("\n"); MPLogPrintf("Bound to = %s:%d with qlen of %d\n", addrAsStr, retAddr.fPort, ret.qlen); } return err; }
char *inet_ntoa( struct in_addr addr) { static char addrString[INET_ADDRSTRLEN + 1 /* '\0' */]; OTInetHostToString( addr.s_addr, addrString); return (addrString); }
void otudp_tellmeeverything(OTUDP *x) { Boolean sane; int free, pending, outgoing; post("otudp_tellmeeverything: You asked for it"); version(x); post(" Thanks: James McCartney, Adrian Freed, David Zicarelli, Chris Brown, Keith McMillen, etc."); post(" This object is a %s", x->o_writer ? "writer" : "reader"); post(" This object error does %sreport errors", x->o_errorreporting ? "" : "not "); if (x->o_udp_ep == 0) { post(" This object has not created an endpoint yet."); } else { post(" This object's endpoint reference is %p", x->o_udp_ep); if (x->o_ready) { post(" And the endpoint has been bound, so we're ready to go."); } else { post(" But the endpoint has not been bound, so we're not ready."); } post(" Endpoint info: addr %ld, options %ld, tsdu %ld, etsdu %ld, connect %ld", x->epinfo.addr, x->epinfo.options, x->epinfo.tsdu, x->epinfo.etsdu, x->epinfo.connect); post(" discon %ld, servtype %lu, flags %lu", x->epinfo.discon, x->epinfo.servtype, x->epinfo.flags); } if (x->o_writer) { char hostnameBuf[100]; post(" You asked to write to internet host \"%s\"", x->o_inetHostName); OTInetHostToString(x->o_inetHost, hostnameBuf); post(" Writing to internet host %s", hostnameBuf); post(" Writing to port %ld", (long)( x->o_inetPort)); } post(" This object reads from port %ld", (long) x->o_receiveInetPort); sane = BufferSanityCheck(x, &free, &pending, &outgoing); post(" Buffer sanity check %s", sane ? "passed" : "failed"); post(" Buffers are %ld bytes", x->bufsize); post(" Allocated %ld buffers:", x->nbufs); post(" %ld currently available buffers", free); post(" %ld received-but-not-yet-outputted buffers", pending); post(" %ld Waiting-until-non-interrupt-time to be output buffers", outgoing); if (x->o_ever_dropped_a_packet) { if (x->o_num_dropped_packets) { post(" %d packets have been dropped since the last tellmeeverything", x->o_num_dropped_packets); x->o_num_dropped_packets = 0; } else { post(" Packets have been dropped, but not since the last tellmeeverything"); } } else { post(" No packets have been dropped by this object"); } }
PR_IMPLEMENT(struct hostent *) gethostbyaddr(const void *addr, int addrlen, int type) { PR_ASSERT(type == AF_INET); PR_ASSERT(addrlen == sizeof(struct in_addr)); OTInetHostToString((InetHost)addr, sHostInfo.name); return (gethostbyname(sHostInfo.name)); }
void PostUDERR(char *source, EndpointRef ep) { OSStatus err; TUDErr errBlock; struct InetAddress addr; #define OPTIONS_LENGTH 2048 unsigned char RoomForStupidOptions[OPTIONS_LENGTH]; errBlock.addr.buf = (void *) &addr; errBlock.addr.maxlen = sizeof(addr); #ifdef IGNORE_OPTIONS errBlock.opt.len = 0; errBlock.opt.maxlen = 0; errBlock.opt.buf = 0; #else errBlock.opt.len = 0; errBlock.opt.maxlen = OPTIONS_LENGTH; errBlock.opt.buf = RoomForStupidOptions; #endif err = OTRcvUDErr(ep, &errBlock); post("OTUDP: %s \"protocol-dependent\" error code %ld", source, errBlock.error); { char hostNameString[255]; OTInetHostToString(addr.fHost, hostNameString); post(" Addr. assoc w/ error is %s, port %d.", hostNameString, addr.fPort); } if (errBlock.error == 49) { post("Error 49 seems to mean that you're sending to a machine that's listening to"); post("UDP but not on the port you're sending to."); } if (err == kOTNoError ) { // No further error; don't worry about it } else if (err == kOTLookErr) { OTResult r = OTLook(ep); post("Got kOTLookErr; OTLook returned %d", r); } else if (err == kOTFlowErr) { post("Flow control error."); } else if (err == kOTBufferOverflowErr) { post("Lame Open Transport says it doesn't have enough memory0 to tell me what my problem is."); } else { post("OTRcvUDErr returned %ld", err); } }
static OSStatus ListenTest(OTMPEndpointRef ep) { OSStatus err; OSStatus junk; TCall call; InetAddress remoteAddr; OTMPEndpointRef worker; worker = NULL; err = SetFourByteOption(ep, INET_IP, IP_REUSEADDR, 0); if (err == noErr) { err = DoIncomingBind(ep, 1024, 10); } if (err == noErr) { OTMemzero(&remoteAddr, sizeof(remoteAddr)); OTMemzero(&call, sizeof(call)); call.addr.buf = (UInt8 *) &remoteAddr; call.addr.maxlen = sizeof(remoteAddr); err = OTMPXListen(ep, &call); if (err == noErr) { char addrAsStr[100]; OTInetHostToString(remoteAddr.fHost, addrAsStr); printf("\n"); printf("Call from %s:%d\n", addrAsStr, remoteAddr.fPort); worker = OTMPXOpenEndpointQInContext("tcp", 0, NULL, &err, NULL); } if (err == noErr) { err = OTMPXAccept(ep, worker, &call); } } if (err == noErr) { err = OTMPXSndDisconnect(worker, NULL); } if (err == noErr) { err = OTMPXUnbind(ep); } if (worker != NULL) { junk = OTMPXCloseProvider(worker); assert(junk == noErr); } return err; }
static pascal OSStatus DoOutgoingBind(OTMPEndpointRef ep) { OSStatus err; TBind ret; InetAddress boundAddr; OTMemzero(&ret, sizeof(ret)); ret.addr.maxlen = sizeof(boundAddr); ret.addr.buf = (UInt8 *) &boundAddr; err = OTMPXBind(ep, NULL, &ret); if (err == noErr) { char addrAsStr[32]; OTInetHostToString(boundAddr.fHost, addrAsStr); printf("\n"); printf("boundAddr = %s:%d\n", addrAsStr, boundAddr.fPort); } return err; }
NMErr NMGetConfig( NMConfigRef inConfig, char *outConfigStr, NMSInt16 *ioConfigStrLen) { DEBUG_ENTRY_EXIT("NMGetConfig"); NMIPConfigPriv *theConfig = (NMIPConfigPriv *) inConfig; char hostName[kMaxHostNameLen + 1]; NMBoolean putToken; NMUInt32 port; NMUInt32 tokenLen; NMErr status = kNMNoError; op_vassert_return((inConfig != NULL),"Config ref is NULL!",kNMParameterErr); op_vassert_return((outConfigStr != NULL),"outConfigStr is NULL!",kNMParameterErr); op_vassert_return((theConfig->type == kModuleID),"Config ref doesn't belong to this module!",kNMInvalidConfigErr); op_vassert_return((theConfig->version <= kVersion),"Config ref belongs to a later version of this module!",kNMNewerVersionErr); op_vassert_return((ioConfigStrLen != NULL),"ioConfigStrLen is NULL!",kNMParameterErr); //Try_ { *outConfigStr = '\0'; // Write the type tokenLen = sizeof (NMType); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigModuleType, LONG_DATA, &theConfig->type, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the version tokenLen = sizeof (NMUInt32); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigModuleVersion, LONG_DATA, &theConfig->version, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the game id tokenLen = sizeof (NMUInt32); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigGameID, LONG_DATA, &theConfig->gameID, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the name tokenLen = strlen(theConfig->name); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigGameName, STRING_DATA, &theConfig->name, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the connection mode tokenLen = sizeof (NMUInt32); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigEndpointMode, LONG_DATA, &theConfig->connectionMode, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the NetSprocket mode tokenLen = sizeof (NMBoolean); putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigNetSprocketMode, BOOLEAN_DATA, &theConfig->netSprocketMode, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the custom data, if any tokenLen = theConfig->customEnumDataLen; if (tokenLen) { putToken = put_token(outConfigStr, *ioConfigStrLen, kConfigCustomData, BINARY_DATA, &theConfig->customEnumData, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); } // Convert out host addr into a string OTInetHostToString(theConfig->address.fHost, hostName); // Write the name tokenLen = strlen(hostName); putToken = put_token(outConfigStr, *ioConfigStrLen, kIPConfigAddress, STRING_DATA, hostName, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); // Write the port port = theConfig->address.fPort; tokenLen = sizeof (NMUInt32); putToken = put_token(outConfigStr, *ioConfigStrLen, kIPConfigPort, LONG_DATA, &port, tokenLen); op_vassert_return((putToken),"put_token returned false. We need a bigger config string!",kNMInvalidConfigErr); *ioConfigStrLen = strlen(outConfigStr); return kNMNoError; } //Catch_(code) error: if (status) { NMErr code = status; return code; } return status; }
NMErr NMSetupDialog(NMDialogPtr dialog, short frame, short inBaseItem,NMConfigRef config) { DEBUG_ENTRY_EXIT("NMSetupDialog"); NMErr status = kNMNoError; Handle ourDITL; NMIPConfigPriv *theConfig = (NMIPConfigPriv *) config; SetupLibraryState duh; // Make sure we're accessing the right resource fork Str255 hostName; Str255 portText; NMSInt16 kind; Handle h; Rect r; SetTempPort port(dialog); op_vassert_return((theConfig != NULL),"Config ref is NULL!",kNMParameterErr); op_vassert_return((dialog != NULL),"Dialog ptr is NULL!",kNMParameterErr); gBaseItem = inBaseItem; // Try to load in our DITL. If we fail, we should bail ourDITL = Get1Resource('DITL', kDITLID); if (ourDITL == NULL) { status = kNMBadStateErr; goto error; } if (ourDITL == NULL) { NMSInt16 err = ResError(); return kNMResourceErr; } // Append our DITL relative to the frame by passing the negative of the frame's id AppendDITL(dialog, ourDITL, -frame); ReleaseResource(ourDITL); // Setup our dialog info. if (theConfig->address.fHost != 0) { // Try to get the canonical name status = OTUtils::MakeInetNameFromAddress(theConfig->address.fHost, (char *) hostName); // if that fails, just use the string version of the dotted quad if (status != kNMNoError) OTInetHostToString(theConfig->address.fHost, (char *) hostName); c2pstr((char *) hostName); } else { doCopyPStr("\p0.0.0.0", hostName); } // get the port NumToString(theConfig->address.fPort, portText); GetDialogItem(dialog, gBaseItem + kHostText, &kind, &h, &r); SetDialogItemText(h, hostName); GetDialogItem(dialog, gBaseItem + kPortText, &kind, &h, &r); SetDialogItemText(h, portText); error: return status; }
PR_IMPLEMENT(char *) inet_ntoa(struct in_addr addr) { OTInetHostToString((InetHost)addr.s_addr, sHostInfo.name); return sHostInfo.name; }
void ottcp_tellmeeverything(OTTCP *x) { OTResult r; EnterCallback(); post("ottcp_tellmeeverything: You asked for it"); version(x); post(" This object error does %sreport errors", x->o_errorreporting ? "" : "not "); post(" state: %s", StateName(x->o_state)); if (x->o_state == OUTPUTTING) { post(" whatToOutput: %s", StateName(x->o_whatToOutput)); post(" nextRequest: %s", StateName(x->o_nextRequest)); } else if (x->o_state == GET_NBYTES) { post(" Getting %ld bytes.", x->o_numBytesWeWant); } else if (x->o_state == GET_DELIM) { post(" Looking for %ld-byte delimiterthat starts with \"%c\"", x->o_delimiterLen, x->o_delimiterBytes[0]); } if (x->o_datawaiting) { post(" There is data waiting in TCP's buffer"); } else { post(" There doesn't seem to be any data waiting in TCP's buffer"); } if (x->o_tcp_ep == 0) { post(" This object has not created an endpoint yet."); } else { post(" This object's endpoint reference is %p", x->o_tcp_ep); post(" Endpoint info: addr %ld, options %ld, tsdu %ld, etsdu %ld, connect %ld", x->epinfo.addr, x->epinfo.options, x->epinfo.tsdu, x->epinfo.etsdu, x->epinfo.connect); post(" discon %ld, servtype %lu, flags %lu", x->epinfo.discon, x->epinfo.servtype, x->epinfo.flags); } r = OTGetEndpointState(x->o_tcp_ep); if (r == T_UNINIT) { post(" Endpoint state: T_UNINIT"); } else if (r == T_UNBND) { post(" Endpoint state: T_UNBND"); } else if (r == T_IDLE) { post(" Endpoint state: T_IDLE"); } else if (r == T_OUTCON) { post(" Endpoint state: T_OUTCON"); } else if (r == T_INCON) { post(" Endpoint state: T_INCON"); } else if (r == T_DATAXFER) { post(" Endpoint state: T_DATAXFER"); } else if (r == T_OUTREL) { post(" Endpoint state: T_OUTREL"); } else if (r == T_INREL) { post(" Endpoint state: T_INREL"); } else { post(" еее Unrecognized endpoints state %ld!", r); } { char hostnameBuf[100]; post(" You asked to write to internet host \"%s\"", x->o_inetHostName); OTInetHostToString(x->o_inetHost, hostnameBuf); post(" Writing to internet host %s", hostnameBuf); post(" Writing to port %ld", (long)( x->o_inetPort)); } post(" This object reads from port %ld", (long) x->o_receiveInetPort); post(" Read buffers: size %ld, %ld bytes in current buffer, %ld bytes in next buffer", x->o_ReadBufSize, x->o_bytesRead, x->o_bytesReadForNextTime); post(" Write buffer: size %ld, read position %ld, write position %ld", x->o_WriteBufSize, x->o_WBReadPos, x->o_WBWritePos); ExitCallback(); }
static OSStatus RunOneServer(InetHost ipAddr) // This routine is the main line of the thread that runs // an HTTP server. ipAddr is the address on which the // server is listening. // // The routine uses a directory whose name is the // dotted decimal string representation of ipAddr as the // root directory of the HTTP server. { OSStatus err; OSStatus junk; Str255 ipAddrString; FSSpec dirSpec; CInfoPBRec cpb; ServerContext *context; MPTaskID junkServerThread; // Allocate a context for the preemptive thread. err = noErr; context = (ServerContext *) MPAllocateAligned(sizeof(*context), kMPAllocateDefaultAligned, kNilOptions); if (context == NULL) { err = memFullErr; } // Fill out the context. We do this here because it needs // to use services that aren't MP-safe. if (err == noErr) { context->ipAddr = ipAddr; // Get ipAddr as a dotted decimal Pascal string. OTInetHostToString(ipAddr, ((char *) ipAddrString) + 1); ipAddrString[0] = OTStrLength(((char *) ipAddrString) + 1); // Find the associated dirID, creating the directory // if necessary. junk = MoreProcGetCurrentProcessFSSpec(&dirSpec); assert(junk == noErr); (void) FSMakeFSSpec(dirSpec.vRefNum, dirSpec.parID, ipAddrString, &dirSpec); context->rootVRefNum = dirSpec.vRefNum; err = FSpGetCatInfo(&dirSpec, 0, &cpb); if (err == noErr && ( (cpb.hFileInfo.ioFlAttrib & (1 << 4)) != 0) ) { context->rootDirID = cpb.hFileInfo.ioDirID; } else { err = FSpDirCreate(&dirSpec, 0, (SInt32 *) &context->rootDirID); } } // Start a preemptive thread to run an HTTP server on the IP address. if (err == noErr) { err = MPCreateTask(HTTPServerProc, context, 65536, kInvalidID, NULL, NULL, kNilOptions, &junkServerThread); if (err == noErr) { context = NULL; // it's now the task's responsibility } } if (context != NULL) { MPFree(context); } return err; }
static OSStatus MiscEndpointTest(OTMPEndpointRef ep) { OSStatus err; OSStatus junk; TEndpointInfo info; char addrAsStr[100]; err = OTMPXGetEndpointInfo(ep, &info); if (err == noErr) { printf("addr = %ld\n", info.addr); printf("options = %ld\n", info.options); printf("tsdu = %ld\n", info.tsdu); printf("etsdu = %ld\n", info.etsdu); printf("connect = %ld\n", info.connect); printf("discon = %ld\n", info.discon); printf("servtype = %ld\n", info.servtype); printf("flags = %ld\n", info.flags); } if (err == noErr) { err = OTMPXGetEndpointState(ep); if (err >= noErr) { printf("state1 = %ld\n", err); err = noErr; } } if (err == noErr) { err = OTMPXBind(ep, NULL, NULL); } if (err == noErr) { err = OTMPXGetEndpointState(ep); if (err >= noErr) { printf("state2 = %ld\n", err); err = noErr; } if (err == noErr) { TBind local; InetAddress localAddr; OTMemzero(&localAddr, sizeof(localAddr)); OTMemzero(&local, sizeof(local)); local.addr.maxlen = sizeof(localAddr); local.addr.buf = (UInt8 *) &localAddr; err = OTMPXGetProtAddress(ep, &local, NULL); if (err == noErr) { OTInetHostToString(localAddr.fHost, addrAsStr); printf("localAddr = %s:%d\n", addrAsStr, localAddr.fPort); } } if (err == noErr) { TBind dns; TBind inet; DNSAddress dnsAddr; InetAddress inetAddr; OTMemzero(&dns, sizeof(dns)); OTMemzero(&inet, sizeof(inet)); OTMemzero(&inetAddr, sizeof(inetAddr)); dns.addr.len = OTInitDNSAddress(&dnsAddr, "1.2.3.4:80"); dns.addr.buf = (UInt8 *) &dnsAddr; inet.addr.maxlen = sizeof(inetAddr); inet.addr.buf = (UInt8 *) &inetAddr; err = OTMPXResolveAddress(ep, &dns, &inet, 1000); if (err == noErr) { OTInetHostToString(inetAddr.fHost, addrAsStr); printf("inetAddr = %s:%d\n", addrAsStr, inetAddr.fPort); } } junk = OTMPXUnbind(ep); assert(junk == noErr); } if (err == noErr) { OTByteCount byteCount; byteCount = 666; err = OTMPXCountDataBytes(ep, &byteCount); assert(err == kOTNoDataErr); assert(byteCount == 0); if (err == kOTNoDataErr) { printf("byteCount = %ld\n", byteCount); err = noErr; } else { err = -1; } } return err; }