예제 #1
0
파일: qUAck-daemon.cpp 프로젝트: rjp/qUAck
bool ProxyHost(EDF *pEDF)
{
   STACKTRACE
   int iHostname = 0, iAddress = 0;
   socklen_t iSockLen = 0;
   char szHostname[NI_MAXHOST], szAddress[NI_MAXSERV], pBuffer[BUFSIZ]; //, *szReturn = NULL;
   struct sockaddr pSockAddr;

   debug(DEBUGLEVEL_INFO, "ProxyHost entry %p\n", pEDF);

   iSockLen = sizeof(pSockAddr);
   if(getpeername(fileno(stdin), (struct sockaddr *)&pSockAddr, &iSockLen) < 0)
   {
      debug(DEBUGLEVEL_ERR, "ProxyHost getpeername failed, %s\n", strerror(errno));

      iSockLen = sizeof(pSockAddr);
      if(recvfrom(fileno(stdin), pBuffer, sizeof(pBuffer), MSG_PEEK, (struct sockaddr *)&pSockAddr, &iSockLen) < 0)
      {
         debug(DEBUGLEVEL_INFO, "ProxyHost exit false, recvfrom failed\n");
         return false;
      }
   }

   mappedtov4(&pSockAddr);

   szHostname[0] = '\0';
   szAddress[0] = '\0';

   iHostname = getnameinfo(&pSockAddr, sizeof(struct sockaddr), szHostname, sizeof(szHostname), NULL, 0, NI_NAMEREQD);
   iAddress = getnameinfo(&pSockAddr, sizeof(struct sockaddr), szAddress, sizeof(szAddress), NULL, 0, NI_NUMERICHOST);

   debug(DEBUGLEVEL_INFO, "ProxyHost '%s'(%d) / '%s'(%d)\n", szHostname, iHostname, szAddress, iAddress);

   if(strlen(szHostname) > 0)
   {
      pEDF->AddChild("hostname", szHostname);
      if(strlen(szAddress) > 0 && ProtocolVersion("2.5") >= 0)
      {
         pEDF->AddChild("address", szAddress);
      }
   }
   else if(strlen(szAddress) > 0)
   {
      pEDF->AddChild(ProtocolVersion("2.5") >= 0 ? "address" : "hostname", szAddress);
   }

   // debugEDFPrint("ProxyHost fields", pEDF);

   debug(DEBUGLEVEL_INFO, "ProxyHost exit true\n");
   return true;
}
예제 #2
0
파일: tcp-env.c 프로젝트: kp-org/eQmail
int main(int argc,char *argv[])
//int argc;
//char *argv[];
{
  int dummy;
  char *proto;
  int opt;
  int flagremoteinfo;
  unsigned long timeout;
// struct sockaddr_in *v4;

  sig_pipeignore();

  flagremoteinfo = 1;
  timeout = 30;
  while ((opt = getopt(argc,argv,"rRt:")) != opteof)
    switch(opt)
    {
     case 'r': flagremoteinfo = 1; break;
     case 'R': flagremoteinfo = 0; break;
     case 't': scan_ulong(subgetoptarg,&timeout); break;
    }

  argv += subgetoptind;
  argc -= subgetoptind;

  if (argc < 1) die();
  if (!env_init()) die();

  proto = env_get("PROTO");
  if (!proto || str_diff(proto,"TCP"))
  {
    if (!env_puts("PROTO=TCP")) die();

    dummy = sizeof(salocal);
    if (getsockname(0,(struct sockaddr *) &salocal, (socklen_t *) &dummy) == -1) die();
    mappedtov4(&salocal);
    switch(salocal.sa.sa_family) {
    case AF_INET:
      localport = ntohs(salocal.sa4.sin_port);
      temp[fmt_ulong(temp,localport)] = 0;
    if (!env_put("TCPLOCALPORT",temp)) die();
      temp[ip4_fmt(temp,(char *) &salocal.sa4.sin_addr)] = 0;
    if (!env_put("TCPLOCALIP",temp)) die();

    switch(dns_ptr(&localname,&salocal.sa4.sin_addr)) {
      case DNS_MEM: die();
      case DNS_SOFT:
        if (!stralloc_copys(&localname,"softdnserror")) die();
      case 0:
        if (!stralloc_0(&localname)) die();
        case_lowers(localname.s);
        if (!env_put("TCPLOCALHOST",localname.s)) die();
        break;
      default:
        if (!env_unset("TCPLOCALHOST")) die();
    }
    break;
#ifdef INET6
    case AF_INET6:
      localport = ntohs(salocal.sa6.sin6_port);
      temp[fmt_ulong(temp,localport)] = 0;
      if (!env_put("TCPLOCALPORT",temp)) die();
      temp[ip6_fmt(temp,(char *) &salocal.sa6.sin6_addr)] = 0;
      if (!env_put("TCPLOCALIP",temp)) die();
      switch(dns_ptr6(&localname,&salocal.sa6.sin6_addr)) {
        case DNS_MEM: die();
        case DNS_SOFT:
             if (!stralloc_copys(&localname,"softdnserror")) die();
        case 0:
             if (!stralloc_0(&localname)) die();
          case_lowers(localname.s);
          if (!env_put("TCPLOCALHOST",localname.s)) die();
          break;
        default:
             if (!env_unset("TCPLOCALHOST")) die();
      }
      break;
#endif
    default:
      die();
    }

    dummy = sizeof(saremote);
    if (getpeername(0,(struct sockaddr *) &saremote, (socklen_t *) &dummy) == -1) die();
      mappedtov4(&saremote);

    switch(saremote.sa.sa_family) {
    case AF_INET:
      remoteport = ntohs(saremote.sa4.sin_port);
      temp[fmt_ulong(temp,remoteport)] = 0;
    if (!env_put("TCPREMOTEPORT",temp)) die();

    temp[ip4_fmt(temp,(char *)&saremote.sa4.sin_addr)] = 0;
    if (!env_put("TCPREMOTEIP",temp)) die();

    switch(dns_ptr(&remotename,&saremote.sa4.sin_addr)) {
    case DNS_MEM: die();
    case DNS_SOFT:
      if (!stralloc_copys(&remotename,"softdnserror")) die();
    case 0:
      if (!stralloc_0(&remotename)) die();
      case_lowers(remotename.s);
      if (!env_put("TCPREMOTEHOST",remotename.s)) die();
      break;
    default:
      if (!env_unset("TCPREMOTEHOST")) die();
    }
    break;
#ifdef INET6
    case AF_INET6:
    remoteport = ntohs(saremote.sa6.sin6_port);
      temp[fmt_ulong(temp,remoteport)] = 0;
    if (!env_put("TCPREMOTEPORT",temp)) die();
    temp[ip6_fmt(temp,(char *) &saremote.sa6.sin6_addr)] = 0;
    if (!env_put("TCPREMOTEIP",temp)) die();
    switch(dns_ptr6(&remotename,&saremote.sa6.sin6_addr)) {
     case DNS_MEM: die();
     case DNS_SOFT:
       if (!stralloc_copys(&remotename,"softdnserror")) die();
     case 0:
       if (!stralloc_0(&remotename)) die();
       case_lowers(remotename.s);
       if (!env_put("TCPREMOTEHOST",remotename.s)) die();
       break;
     default:
       if (!env_unset("TCPREMOTEHOST")) die();
    }
    break;
#endif
    default:
      die();
    }

    if (!env_unset("TCPREMOTEINFO")) die();

    if (flagremoteinfo)
    {
      char *rinfo;
      rinfo = remoteinfo_get(&saremote, &salocal,(int) timeout);
      if (rinfo)
        if (!env_put("TCPREMOTEINFO",rinfo)) die();
    }
  }

  sig_pipedefault();
  execvp(*argv,argv);
  die();
  return(0);  /* never reached */
}