Exemplo n.º 1
0
void SubmitData::CommSendData2Web(QMap<QString,QString> need2SubmtData,QString serverName,QString action,bool tianjiao /*= false*/)
{
	loop = new QEventLoop(this);                                           
	//http = new QHttp(this);        
	http = new QIHttp(this, getHostAddr(tianjiao), getHostPort(tianjiao));     
	connect(http, SIGNAL(done(bool)),this, SLOT(on_http_done(bool)));
	//http->setHost(getHostAddr(),getHostPort());
	//IpaType
	QHttpRequestHeader header("POST", "http://" + getHostAddr(tianjiao) + ":" + QString::number(getHostPort(tianjiao)) + "/" + serverName) ;
	header.setValue("Host", getHostAddr(tianjiao)) ;
	header.setContentType("text/xml");
	QString data;

	data.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
	data.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
	data.append("<soap:Body>");
	data.append("<" + action + "  xmlns=\"http://tempuri.org/\">");

	data.append("<request>");
	QMapIterator<QString, QString> it(need2SubmtData);
	while (it.hasNext()) 
	{
		it.next();
		data.append("<" + it.key() +">" + it.value() + "</" + it.key() + ">");
	}
	data.append("</request>");

	data.append("</" + action + ">");
	data.append("</soap:Body>");
	data.append("</soap:Envelope>");

	http->request(header, data.toUtf8()) ;
	loop->exec();
}
Exemplo n.º 2
0
int main(int argc, char *argv[])
{

    int ret_status;
    int i, res;
    char local_ip[IP4_MAX_LEN], remote_ip[IP4_MAX_LEN];
    uint16_t local_port = PORT_NUM, remote_port;
    uint16_t send_port, recv_port, fax_rem_port;
    uint32_t fax_rem_ip;
    char buffer[512];
    char tiff_filename[64], recv_filename[64];
    int buflen;
    int len;
    int ses_count = 0;
    int sock;
    char call_id_str[FAX_MAX_SESSION_CNT];
    sig_message_t *msg_req = NULL;
    sig_message_t *msg_resp = NULL;
    fax_session_t *f_session[FAX_MAX_SESSION_CNT][2];
    pthread_t      f_thread[FAX_MAX_SESSION_CNT][2];
    struct stat st;

    if (argc < 5) {
        printf("Not enough arguments: [REMOTE_HOST] [REMOTE_PORT] [SESSION_COUNT] [TIFF_FILE_TO_SEND]\n");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    ses_count = strtoul(argv[3], NULL, 10);

    if (ses_count > FAX_MAX_SESSION_CNT) {
        printf("Too many sessions! No more then %d supported\n", FAX_MAX_SESSION_CNT);
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strncpy(tiff_filename, argv[4], sizeof(tiff_filename));
    if (stat(tiff_filename, &st)) {
        printf("Tiff file error ('%s'): %s\n", tiff_filename, strerror(errno));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(local_ip, getLocalIP("eth0"));

    printf("Local IP is '%s:%d'\n", local_ip, local_port);

    if ((sock = socket_init(local_ip, local_port)) < 0) {
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if ((res = getHostAddr(argv[1], argv[2], &remote_addr))) {
        printf("getaddrinfo() error: %s\n", gai_strerror(res));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }


    strcpy(remote_ip, inet_ntoa(remote_addr.sin_addr));
    remote_port = ntohs(remote_addr.sin_port);

    printf("Remote IP is '%s:%d'\n", remote_ip, remote_port);

    for(i = 0; i < ses_count; i++)
    {
        sprintf(call_id_str, "0000000%d", i + 1);

        send_port = FAX_SEND_PORT_START + i;
        recv_port = FAX_RECV_PORT_START + i;

        f_session[i][0] = f_session[i][1] = NULL;

        sig_msgCreateSetup(call_id_str,
                           ntohl(inet_addr(local_ip)), send_port,
                           ntohl(inet_addr(local_ip)), recv_port,
                           FAX_MODE_GW_GW, (sig_message_setup_t **)(&msg_req));


        sig_msgCompose(msg_req, buffer, sizeof(buffer));

        buflen = strlen(buffer);
        printf("buffer_len: %d buffer: '%.*s'", buflen, buflen, buffer);

        if ((len = sendPacket((uint8_t *)buffer, buflen)) != -1) {
            printf("Data sent to %s:%d: len=%d buf='%s'\n",
                   inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port),
                   buflen, buffer);
        } else {
            perror("sendPacket() failed");
            ret_status = EXIT_FAILURE;
            goto _exit;
        }

        if ((len = recvPacket((uint8_t *)buffer, sizeof(buffer))) != -1) {
            printf("Data received form %s:%d len=%d buf='%s'\n",
                   inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port),
                   len, buffer);
        } else {
            perror("recvfrom() failed");
            ret_status = EXIT_FAILURE;
        }

        sig_msgParse(buffer, &msg_resp);
        sig_msgPrint(msg_resp, buffer, sizeof(buffer));

        printf("MSG Response: %s\n", buffer);

        fax_rem_ip = ((sig_message_ok_t *)msg_resp)->ip;
        fax_rem_port = ((sig_message_ok_t *)msg_resp)->port;

        /* Sending thread */
        f_thread[i][0] = start_fax_session(SEND, call_id_str, send_port,
                                           fax_rem_ip, fax_rem_port,
                                           tiff_filename,
                                           &(f_session[i][0]));

        /* If we do without fork() - received filed will be distorted,
         * don't know why.
         *
         * Most of all - spandsp bug, not sure
         */
        if(!fork())
        {
            if(msg_resp->type != FAX_MSG_OK) exit(1);


            sprintf(recv_filename, "received_fax_%02d.tif", i);

            /* Receiving thread */
            f_thread[i][1] = start_fax_session(RECV, call_id_str, recv_port, 0,
                                               0, recv_filename,
                                               &f_session[i][0]);

            pthread_join(f_thread[i][1], NULL);
            if(f_session[i][1]) free(f_session[i][1]);

            exit(0);
        }

        sig_msgDestroy(msg_req);
        sig_msgDestroy(msg_resp);

        msg_req = NULL;
        msg_resp = NULL;
    }

    for(i = 0; i < ses_count; i++)
    {
        pthread_join(f_thread[i][0], NULL);
    }

    for(i = 0; i < ses_count; i++)
    {
        if(f_session[i][0]) free(f_session[i][0]);
    }

_exit:
    if (sock > -1) {
        shutdown(sock, SHUT_RDWR);
        close(sock);
    }

    return ret_status;
}
Exemplo n.º 3
0
void reception::traitement(char * messageStr, int size) {

    QHostAddress hoteCourant;
    quint16 portCourant;

    int numKey;

    //const static unsigned char key[]={0xB0,0xA1,0x73,0x37,0xA4,0x5B,0xF6,0x72,0x87,0x92,0xFA,0xEF,0x7C,0x2D,0x3D,0x4D, 0x60,0x3B,0xC5,0xBA,0x4B,0x47,0x81,0x93,0x54,0x09,0xE1,0xCB,0x7B,0x9E,0x17,0x88};

    hoteCourant = getHostAddr();
    portCourant = getHostPort();

    cout << "Génération du token..." << endl;
    string token = m_ts->GenToken(hoteCourant,portCourant);
    cout << "Token généré." << endl;

    cout << "Got somethin" << endl;

    Requete req;

    string input = mess->decrypt((unsigned char*) messageStr, size);

    if(mess->getEChangeKey()) {

        clientFront cli;
        cli.socBind();
        cli.emission(mess->getToSendArray(), sizeof(char)*258);



    } else {

        numKey = mess->getNumClient();



        cout << "input : " << input << endl;

        if(req.decoupage(input)) {
            //input = token + "*" + input + "*";
            cout << "tata" << endl;
            cout << "lenom ici : " << req.getNom() << endl;

            cout << "lenom ici : " << req.getNom() << endl;
            req.construction();
            cout << "lenom ici : " << req.getNom() << endl;

            cout << "lenom ici : " << req.getNom() << endl;
            cout << "tata" << endl;

            if(req.getPourBdd()) {

                m_bdd->emission(req.getRequete());

                m_bdd->attendLecture(300);

                string messBdd = m_bdd->getMsg();

                list<string> listReponseBdd;
                listReponseBdd.push_back(messBdd);

                req.tri(listReponseBdd);
            }
            else
            {
                cout << "lenom ici : " << req.getNom() << endl;

                cout << "lenom ici : " << req.getNom() << endl;

                string traitement = req.getAffectation();

                string option = req.getOption();
                string parametre = req.getParametre();

                if(option.compare("-n")==0) { /* traitement est une affectation */
                    m_netinf->send_look(traitement, token);
                    cout << "ici envoi" << endl;
                }
                else if(option.compare("-e")==0) {
                    m_netinf->send_exist(traitement, token);
                }
                if(option.compare("-r")==0) {
                    m_netinf->send_lookrec(parametre,traitement, token);
                }

                list<string> * listReponse = m_ts->startTimer(token, 5000);
                cout << "lenom ici : " << req.getNom() << endl;

                if(!listReponse->empty()) {
                    string option = req.getOption();

                    if(req.tri(*listReponse) == 1 && !option.compare("-r")) {

                        string pubR = req.getPub();
                        string requeteP = req.getRequete();

                        token = m_ts->GenToken(hoteCourant,portCourant);

                        m_netinf->send_pull(requeteP, pubR, token);

                        list<string> * listReponsePull = m_ts->startTimer(token, 5000);
                        req.tri(*listReponsePull);


                    }

                }
                else {
                    cout << "Temps maximum écoulé, pas de réponse" << endl;
                    req.setResultat("N*empty*");
                }

            }

            QString retour;
            retour = QString("%1").arg(req.getResultat().c_str());

            //msg2.entete();
            //msg2.chiffrement(key);

            cout << "resultat : " << req.getResultat() << endl;

            string resultat =req.getResultat();
            cout << numKey << endl;

            char * chiffrer = mess->crypt((unsigned char*) resultat.data(), resultat.size(), numKey);

            //string toto = msg2.getMsg().toStdString();
            cout << chiffrer << endl;

            clientFront cli;
            cli.socBind();
            cli.emission(chiffrer, req.getResultat().size()+16-(req.getResultat().size()%16));//, hoteCourant, portCourant);

        }
    }
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
    int sock = -1;
    int ret_status = 0;
    struct sockaddr_in local_addr;
    struct sockaddr_in remote_addr;
    int reuse = 0;
    int syncnt = 1;
    int i, res;
    char local_ip[IP4_MAX_LEN], remote_ip[IP4_MAX_LEN];
    unsigned local_port = PORT_NUM, remote_port;
    char buf[1024] = {0};
    int len;

    if (argc < 3) {
        printf("Not enough arguments: [REMOTE_HOST] [REMOTE_PORT]\n");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(local_ip, getLocalIP("eth0"));

    printf("Local IP is '%s:%d'\n", local_ip, local_port);

    local_addr.sin_family = AF_INET;
    local_addr.sin_addr.s_addr = inet_addr(local_ip);
    local_addr.sin_port = htons(local_port);

    if ((res = getHostAddr(argv[1], argv[2], &remote_addr))) {
        printf("getaddrinfo() error: %s\n", gai_strerror(res));
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(remote_ip, inet_ntoa(remote_addr.sin_addr));
    remote_port = ntohs(remote_addr.sin_port);

    printf("Remote IP is '%s:%d'\n", remote_ip, remote_port);

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

//    fcntl(sock, F_SETFL, O_NONBLOCK);

    signal(SIGINT, &sigint_handler);

    gl_sock = sock;

    printf("Client socket created. fd: %d\n", sock);

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
        perror("setsockopt() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (setsockopt(sock, IPPROTO_TCP, TCP_SYNCNT, &syncnt, sizeof(syncnt)) < 0) {
        perror("setsockopt() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (bind(sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) {
        perror("bind() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    if (connect(sock, (struct sockaddr *)&remote_addr, sizeof(remote_addr)) < 0) {
        perror("connect() failed");
        ret_status = EXIT_FAILURE;
        goto _exit;
    }

    strcpy(buf, "Test message from client.");

    for(i = 0; i < 5; i++)
    {
        if ((len = send(sock, buf, strlen(buf), 0)) != -1) {
            printf("Data sent to %s:%d: len=%d buf='%s'\n", remote_ip, remote_port,
                   len, buf);
        } else {
            perror("send() failed");
            ret_status = EXIT_FAILURE;
            goto _exit;
        }
        sleep(2);
    }

_exit:
    if (sock > -1) {
        shutdown(sock, SHUT_RDWR);
        close(sock);
    }

    return ret_status;
}
Exemplo n.º 5
0
/**
 * Formulate the Http Request and send it.
 * Read the response for meta-information and parse the Server Configuration information.
 * Params : URL , port no and the method (HEAD or GET)
 */
int HttpSocket::sendHttpRequest(HttpParams *hq)
{
	CString strurl, useragent, httpRequest;
	HTTP_METHOD method;
	CONN_TYPE connType;
	int port=0, proxyport=0;
	char strPortno[6] = {0};

	strurl		= hq->getURI();
	method		= hq->getHttpMethod();
	useragent	= hq->getUserAgent();
	connType	= hq->getConnType();
	port		= hq->getHttpPort();

	if((sessionErrorCode = parseURL(strurl)) != 0)
		return sessionErrorCode;
	
	/** If the port is specified in the URI
	 *  that gets the priority over what is 
	 *  specified in the HTTP port edit box.
	 */
	if(isPortSpecified()==true)
		port		= portNo;
	else
		portNo		= port;
	/** If the port is not specified by the 
	 *	user in the HTTP port edit control
	 *	then use the default port 80
	 */

	if(isValidPortRange(port)==false)
	{
		sessionErrorCode = InvalidHTTPPortNo;
		return sessionErrorCode;
	}

	/** Construct the HTTP request to be sent to the
	 *	HTTP Server.
	 */
	if(method == HEAD)
		httpRequest = "HEAD ";
	else 
		httpRequest = "GET ";

	/** If the HTTP connection is through the proxy, the 
	 *  absolute URI of the resource requested needs to 
	 *  be send as a parmeter of the HTTP method in HTTP 
	 *  header.
	 */
	if(connType == THRU_PROXY)
	{
		/** The resource with http port needs to be given
		 *	as specified in the requested URI.
		 */
		if(isPortSpecified()==true)
		{
			itoa(port,strPortno,10);
			httpRequest = httpRequest	+	scheme + host + ":" + CString(strPortno) + resource	+ " HTTP/1.1\r\n";
		}
		else
			httpRequest = httpRequest	+	scheme + host + resource + " HTTP/1.1\r\n";
	}
	else
		httpRequest = httpRequest	+	resource				+ " HTTP/1.1\r\n";

	httpRequest = httpRequest	+	"Host: "				+ host + "\r\n";
	httpRequest = httpRequest	+	"User-Agent: "			+  useragent + "\r\n";
	httpRequest = httpRequest	+	"Accept: */*\r\n";
	httpRequest = httpRequest	+	"Accept-Language: en-us,en;q=0.5\r\n";
	httpRequest = httpRequest	+	"Connection: close\r\n"	+ "\r\n";

	
    struct sockaddr_in hostaddr;
	/**
	 * If connection is through HTTP Proxy then 
	 * TCP connection should be established with 
	 * it and then then HTTP requset needs to be
	 * sent on the connection.
	 */
	if(connType == THRU_PROXY)
	{
		if(getHostAddr(hq->getProxyIP(),hostaddr) != 0)
		{
			return sessionErrorCode;
		}
		hostaddr.sin_port = htons(hq->getProxyPort());
	}
	else
	{
		/** Connect to the host HTTP Server on the
		 * specified port no
		 */
		if(getHostAddr((LPCSTR)host,hostaddr) != 0)
		{
			return sessionErrorCode;
		}
		hostaddr.sin_port = htons(portNo);
	}

	if(connect(sock, (struct sockaddr *)&hostaddr, sizeof(hostaddr)) == SOCKET_ERROR)
	{
		if(connType == THRU_PROXY)
			sessionErrorCode = HTTPProxyConnErr;			
		else
			sessionErrorCode = HTTPSrvrConnErr;

        return sessionErrorCode;
	}
	
	// Send an HTTP request to the host at the specified port no
	int writtenBytes = send(sock, (LPCSTR) httpRequest, httpRequest.GetLength(),0);
	int readBytes    = 0;

	memset(strHttpResponse,0,RESLEN); // Clear the receive buffer

	if(writtenBytes <=0)
	{
		sessionErrorCode = ZeroBytesSent;
        return sessionErrorCode;
	}
	
	if(IsSocketReadible(sock)) {
		readBytes           = recvfrom(sock, strHttpResponse, RESLEN-1, 0, 0, 0);
		
		if(readBytes > 0) {
			strHttpResponse[readBytes] = '\0';
		}
	}
	
    return 0;
}