示例#1
0
void qObjProto::EvalXTCP(qCtx *ctx, qStr *out, qArgAry *args)
{
    if (args->Count() < 1) {
        ctx->Throw(out, 655, "USAGE: %connect-tcp(host,body[,timeout]])");
        return;
    }

    CStrAry hosts;

    CStr serv = (*args)[0];

    CStr bodyStr = args->GetAt(1);

    double timeout = ParseDbl((*args)[2]);

    if (serv.IsEmpty())
        return;

    int port = 0;

    char * p = strchr((const char *)serv, ':');

    if (p) {
        port = atoi(p+1);
        *p = '\0';
        ++p;
    }

    if (!port)
        port = IPPORT_TELNET;


    int sock_rval;
    Sock sock;

    CStr body, dom, ref;

    qCtxTmp tmpCtx(ctx);

    if (!bodyStr.IsEmpty()) {
        tmpCtx.MapObj(&sock, EvalXTCPSend,"send");
        tmpCtx.MapObj(&sock, EvalXTCPRecv,"recv");
    }

    PROTO_OPEN_SOCK();

    if (timeout > 1)
        sock.SetTimeout((float) timeout);

    tmpCtx.Parse(bodyStr, out);

    sock.Close();

    return;
}
示例#2
0
void qObjProto::EvalWhois(qCtx *ctx, qStr *out, qArgAry *args)
{
    if (args->Count() < 1) {
        ctx->Throw(out, 655, "USAGE: %whois(host[;host;host...][,body[,server[,bindip]]])");
        return;
    }

    CStrAry hosts;
    CStr hostStr = (*args)[0];
    CStr bodyStr = (*args)[1];
    CStr serv = (*args)[2];
    CStr bindip = (*args)[3];

    ProtoParseHosts(hosts, hostStr);

    if (hosts.Count() == 0)
        return;

    int port;

    if(serv.IsEmpty())

        serv = DEFAULT_WHOIS_HOST;

    port = IPPORT_WHOIS;

    int sock_rval, i;
    Sock sock;

    if(!bindip.IsEmpty())
        sock.Bind(bindip);

    CStr body, dom, ref, reg, url;

    qCtxTmp tmpCtx(ctx);

    if (!bodyStr.IsEmpty()) {
        tmpCtx.MapObj(&body, "results");
        tmpCtx.MapObj(&body, "body");

        tmpCtx.MapObj(&dom,  "domain");
        tmpCtx.MapObj(&ref,  "refer");

        tmpCtx.MapObj(&dom,  "domain-name");
        tmpCtx.MapObj(&reg,  "registrar");
        tmpCtx.MapObj(&ref,  "whois-server");
        tmpCtx.MapObj(&url,  "referral-url");
    }

    for (i = 0; i < hosts.Count(); ++i) {
        PROTO_OPEN_SOCK();

        body = CStr();
        ref = CStr();
        dom = hosts[i];
        sock.Write(hosts[i]<<"\r\n", hosts[i].Length()+2);

        do {
            if ((sock_rval = sock.Read(SOCK_DEFAULT_BUF_SIZE)) < 0) {
                if (sock_rval == Sock::ERR_TIMEOUT)
                    ctx->ThrowF(out, 705, "Time out while reading from host %s:%d, %y", (const char *) serv, port, GetLastError());
                else if (sock_rval)
                    ctx->ThrowF(out, 706, "Error reading from host %s:%d, %y", (const char *) serv, port, GetLastError());
                return;
            }
            if (!bodyStr.IsEmpty())
                body << CStr(sock.GetBuf(), sock_rval);
            else
                out->PutS(sock.GetBuf(), sock_rval);
        }
        while( sock_rval > 0);	//  If something was received

        if (!bodyStr.IsEmpty()) {
            const char *p;
            if ((p = stristr(body.GetBuffer(),"Whois Server:"))) {
                p += 14;
                while (isspace(*p)) ++p;
                char *e = strchr(p, '\n');
                ref = CStr(p, e-p);
            }
            if ((p = stristr(body.GetBuffer(),"Registrar:"))) {
                p += 14;
                while (isspace(*p)) ++p;
                char *e = strchr(p, '\n');
                reg = CStr(p, e-p);
            }
            if ((p = stristr(body.GetBuffer(),"Referral URL:"))) {
                p += 14;
                while (isspace(*p)) ++p;
                char *e = strchr(p, '\n');
                url = CStr(p, e-p);

            }
            tmpCtx.Parse(bodyStr, out);
        }
        sock.Close();
    }

    return;
}