Пример #1
0
/* get host name
 */
void NATCPgethost(na_tcpreadp *callback, void *context)
{
	int id;
	tcpinfo *tcp;
	struct IPParamBlock *ippb;
	na_win *win;
	
	if ((*tcpstate)->localhost[0]) {
		win = NAlockWindow((na_win **) tcpstate);
		(*callback)(context, -1, NATCP_connect, strlen(tstate->localhost),
			tstate->localhost);
		NAunlockWindowh((na_win **) tcpstate, win);
	} else if ((tcp = getTCPbuf(callback, context, &id)) != NULL) {
		/* here we make the assumption that an IP param block is smaller than
		 * a TCP param block.  This seems like a safe assumption to me.
		 */
		ippb = (struct IPParamBlock *) &tcp->pb;
		/* get IP address */
		ippb->ioCRefNum = (*tcpstate)->tcp_driver;
		ippb->csCode = ipctlGetAddr;
		PBControl((ParmBlkPtr)ippb, false);
		if (ippb->ioResult != 0) {
			(*callback)(context, -1, NATCP_notcpbuf, ippb->ioResult, NULL);
			DisposPtr((Ptr) tcp);
			(*tcpstate)->tcpbufs[id] = NULL;
		} else {
			/* begin IP address lookup */
			tcp->dnrdone = 0;
			AddrToName(ippb->ourAddress, &tcp->host, addrproc, (char *) tcp);
			tcp->state = TCP_GETHOST;
			tcp->gethost = 1;
		}
	}
}
Пример #2
0
int IpAddr2Name(char *hostname)
{
#if !TARGET_API_MAC_CARBON
  struct hostInfo hInfoMacTCP;
  OSErr err;
  int cnt, tmp;
  char *cptr;
  Boolean done;
#endif
  OSStatus lStatus;
  InetHost lHostAddr;

  if (!slNetChecked) {
    slNetPresent = OpenNetwork();
    slNetChecked = 1;
  }

  if (slNetPresent == 1) {

    /* turn ascii with periods into a long */
    lStatus = OTInetStringToHost(hostname, &lHostAddr);
    if (lStatus != noErr) return 0;

    /* turn the long into a reverse-resolved name */
    sSvcRef.done=false;
    lStatus=OTInetAddressToName(sSvcRef.ref,lHostAddr,hostname);
    if (!lStatus) {
      do {
	MacIdle();
      } while (!sSvcRef.done);
      lStatus=sSvcRef.result;
    }
    if (!lStatus) {
      if (hostname[strlen(hostname)-1]=='.') hostname[strlen(hostname)-1]=0;
      return(1);
    }
  }
#if !TARGET_API_MAC_CARBON
  else if (slNetPresent==2) {
    lHostAddr=0;
    cptr=hostname;
    for (cnt=0; cnt<4; ++cnt) {
      if (!ISDIGIT(*cptr)) return(0);
      tmp=atoi(cptr);
      if (tmp<0 || tmp>255) return(0);
      lHostAddr=(lHostAddr<<8)|tmp;
      while (ISDIGIT(*cptr)) ++cptr;
      if (cnt!=3 && *cptr!='.') return(0);
      ++cptr;
    }
    memset(&hInfoMacTCP, 0, sizeof(hInfoMacTCP));
    done=false;
    err = AddrToName(lHostAddr, &hInfoMacTCP, gMacTCPDNRResultProcUPP,
		     (char*)&done);
    if (err == cacheFault) {
      while (!done) MacIdle();
      err = hInfoMacTCP.rtnCode;
    }
    if (err == noErr) {
      hInfoMacTCP.cname[254] = 0;
      (void)strcpy(hostname, hInfoMacTCP.cname);
      if (hostname[strlen(hostname)-1]=='.') hostname[strlen(hostname)-1]=0;
      return(1);
    }
  }
#endif
  return 0;
} /* end IpAddr2Name() */