예제 #1
0
string basic_auth(const char *user, const char *pass)
{
    string auth = user;
    auth += ":";
    if (pass)
        auth += pass;
    return tobase64(auth.c_str());
}
예제 #2
0
파일: proxy.c 프로젝트: stevestreza/bitlbee
static gboolean http_canwrite(gpointer data, gint source, b_input_condition cond)
{
	char cmd[384];
	struct PHB *phb = data;
	unsigned int len;
	int error = ETIMEDOUT;
	if (phb->inpa > 0)
		b_event_remove(phb->inpa);
	len = sizeof(error);
	if (getsockopt(source, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
		close(source);
		phb->func(phb->data, -1, GAIM_INPUT_READ);
		g_free(phb->host);
		g_free(phb);
		return FALSE;
	}
	sock_make_blocking(source);

	g_snprintf(cmd, sizeof(cmd), "CONNECT %s:%d HTTP/1.1\r\nHost: %s:%d\r\n", phb->host, phb->port,
		   phb->host, phb->port);
	if (send(source, cmd, strlen(cmd), 0) < 0) {
		close(source);
		phb->func(phb->data, -1, GAIM_INPUT_READ);
		g_free(phb->host);
		g_free(phb);
		return FALSE;
	}

	if (strlen(proxyuser) > 0) {
		char *t1, *t2;
		t1 = g_strdup_printf("%s:%s", proxyuser, proxypass);
		t2 = tobase64(t1);
		g_free(t1);
		g_snprintf(cmd, sizeof(cmd), "Proxy-Authorization: Basic %s\r\n", t2);
		g_free(t2);
		if (send(source, cmd, strlen(cmd), 0) < 0) {
			close(source);
			phb->func(phb->data, -1, GAIM_INPUT_READ);
			g_free(phb->host);
			g_free(phb);
			return FALSE;
		}
	}

	g_snprintf(cmd, sizeof(cmd), "\r\n");
	if (send(source, cmd, strlen(cmd), 0) < 0) {
		close(source);
		phb->func(phb->data, -1, GAIM_INPUT_READ);
		g_free(phb->host);
		g_free(phb);
		return FALSE;
	}

	phb->inpa = b_input_add(source, GAIM_INPUT_READ, http_canread, phb);
	
	return FALSE;
}
예제 #3
0
파일: sasl.c 프로젝트: mrdon/bitlbee
xt_status sasl_pkt_challenge( struct xt_node *node, gpointer data )
{
	struct im_connection *ic = data;
	struct jabber_data *jd = ic->proto_data;
	struct xt_node *reply_pkt = NULL;
	char *nonce = NULL, *realm = NULL, *cnonce = NULL;
	unsigned char cnonce_bin[30];
	char *digest_uri = NULL;
	char *dec = NULL;
	char *s = NULL, *reply = NULL;
	xt_status ret = XT_ABORT;
	
	if( node->text_len == 0 )
		goto error;
	
	dec = frombase64( node->text );
	
	if( jd->flags & JFLAG_SASL_FB )
	{
		/* New-style Facebook OAauth2 support. Instead of sending a refresh
		   token, they just send an access token that should never expire. */
		GSList *p_in = NULL, *p_out = NULL;
		char time[33];
		
		oauth_params_parse( &p_in, dec );
		oauth_params_add( &p_out, "nonce", oauth_params_get( &p_in, "nonce" ) );
		oauth_params_add( &p_out, "method", oauth_params_get( &p_in, "method" ) );
		oauth_params_free( &p_in );
		
		g_snprintf( time, sizeof( time ), "%lld", (long long) ( gettime() * 1000 ) );
		oauth_params_add( &p_out, "call_id", time );
		oauth_params_add( &p_out, "api_key", oauth2_service_facebook.consumer_key );
		oauth_params_add( &p_out, "v", "1.0" );
		oauth_params_add( &p_out, "format", "XML" );
		oauth_params_add( &p_out, "access_token", jd->oauth2_access_token );
		
		reply = oauth_params_string( p_out );
		oauth_params_free( &p_out );
	}
	else if( !( s = sasl_get_part( dec, "rspauth" ) ) )
	{
		/* See RFC 2831 for for information. */
		md5_state_t A1, A2, H;
		md5_byte_t A1r[16], A2r[16], Hr[16];
		char A1h[33], A2h[33], Hh[33];
		int i;
		
		nonce = sasl_get_part( dec, "nonce" );
		realm = sasl_get_part( dec, "realm" );
		
		if( !nonce )
			goto error;
		
		/* Jabber.Org considers the realm part optional and doesn't
		   specify one. Oh well, actually they're right, but still,
		   don't know if this is right... */
		if( !realm )
			realm = g_strdup( jd->server );
		
		random_bytes( cnonce_bin, sizeof( cnonce_bin ) );
		cnonce = base64_encode( cnonce_bin, sizeof( cnonce_bin ) );
		digest_uri = g_strdup_printf( "%s/%s", "xmpp", jd->server );
		
		/* Generate the MD5 hash of username:realm:password,
		   I decided to call it H. */
		md5_init( &H );
		s = g_strdup_printf( "%s:%s:%s", jd->username, realm, ic->acc->pass );
		md5_append( &H, (unsigned char *) s, strlen( s ) );
		g_free( s );
		md5_finish( &H, Hr );
		
		/* Now generate the hex. MD5 hash of H:nonce:cnonce, called A1. */
		md5_init( &A1 );
		s = g_strdup_printf( ":%s:%s", nonce, cnonce );
		md5_append( &A1, Hr, 16 );
		md5_append( &A1, (unsigned char *) s, strlen( s ) );
		g_free( s );
		md5_finish( &A1, A1r );
		for( i = 0; i < 16; i ++ )
			sprintf( A1h + i * 2, "%02x", A1r[i] );
		
		/* A2... */
		md5_init( &A2 );
		s = g_strdup_printf( "%s:%s", "AUTHENTICATE", digest_uri );
		md5_append( &A2, (unsigned char *) s, strlen( s ) );
		g_free( s );
		md5_finish( &A2, A2r );
		for( i = 0; i < 16; i ++ )
			sprintf( A2h + i * 2, "%02x", A2r[i] );
		
		/* Final result: A1:nonce:00000001:cnonce:auth:A2. Let's reuse H for it. */
		md5_init( &H );
		s = g_strdup_printf( "%s:%s:%s:%s:%s:%s", A1h, nonce, "00000001", cnonce, "auth", A2h );
		md5_append( &H, (unsigned char *) s, strlen( s ) );
		g_free( s );
		md5_finish( &H, Hr );
		for( i = 0; i < 16; i ++ )
			sprintf( Hh + i * 2, "%02x", Hr[i] );
		
		/* Now build the SASL response string: */
		reply = g_strdup_printf( "username=\"%s\",realm=\"%s\",nonce=\"%s\",cnonce=\"%s\","
		                         "nc=%08x,qop=auth,digest-uri=\"%s\",response=%s,charset=%s",
		                         jd->username, realm, nonce, cnonce, 1, digest_uri, Hh, "utf-8" );
	}
	else
	{
		/* We found rspauth, but don't really care... */
		g_free( s );
	}
	
	s = reply ? tobase64( reply ) : NULL;
	reply_pkt = xt_new_node( "response", s, NULL );
	xt_add_attr( reply_pkt, "xmlns", XMLNS_SASL );
	
	if( !jabber_write_packet( ic, reply_pkt ) )
		goto silent_error;
	
	ret = XT_HANDLED;
	goto silent_error;

error:
	imcb_error( ic, "Incorrect SASL challenge received" );
	imc_logout( ic, FALSE );

silent_error:
	g_free( digest_uri );
	g_free( cnonce );
	g_free( nonce );
	g_free( reply );
	g_free( realm );
	g_free( dec );
	g_free( s );
	xt_free_node( reply_pkt );
	
	return ret;
}
예제 #4
0
void HttpRequest::connect_ready()
{
    if (state == WaitConnect){
        state = Connected;
#ifdef USE_OPENSSL
        bool isSecure = m_proxy->isSecure();
        if (isSecure){
            SSLClient *ssl = new SSLClient(m_sock);
            if (!ssl->initHTTPS()){
                delete ssl;
                ssl = NULL;
                error_state(ErrorProxyConnect);
                return;
            }
            ssl->setNotify(this);
            ssl->connect();
            ssl->process();
            m_sock = ssl;
            return;
        }
#endif
    }

    const char *h = host();
    HttpPacket *p = packet();

    Buffer bOut;
    bOut << (p ? "POST" : "GET") << " http://" << h << uri() << " HTTP/1.1\r\n" <<
    "Host: " << h << "\r\n"
    "User-agent: Mozilla/4.08 [en] (WinNT; U ;Nav)\r\n"
    "Cache-control: no-store, no-cache\r\n"
    "Connection: close\r\n"
    "Pragma: no-cache\r\n";

    if (p){
        char b[15];
        snprintf(b, sizeof(b), "%u", p->size + 14);
        bOut << "Content-Length: " << b << "\r\n";
    }

    if (m_proxy->m_user.length()){
        string s;
        s = m_proxy->m_user.c_str();
        s += ":";
        s += m_proxy->m_passwd.c_str();
        s = tobase64(s.c_str());
        bOut << "Proxy-Auth: basic ";
        bOut << s.c_str();
        bOut << "\r\n";
    }
    bOut << "\r\n";
    if (p){
        unsigned short len = p->size + 12;
        bOut
        << len
        << HTTP_PROXY_VERSION
        << p->type
        << 0x00000000L
        << p->nSock;
        if (p->size)
            bOut.pack(p->data, p->size);
        m_proxy->queue.remove(p);
        delete p;
    }
    dumpPacket(bOut, 0, "Proxy write");
    m_sock->write(bOut.Data(0), bOut.size());
    bOut.init(0);
}
예제 #5
0
void
msn_slp_session_request_user_display(MsnSlpSession *slpsession,
									 MsnUser *local_user,
									 MsnUser *remote_user,
									 const MsnObject *obj)
{
	MsnMessage *invite_msg;
	long session_id;
	char *msnobj_data;
	char *msnobj_base64;
	char *branch;
	char *content;
	char *body;

	g_return_if_fail(slpsession  != NULL);
	g_return_if_fail(local_user  != NULL);
	g_return_if_fail(remote_user != NULL);
	g_return_if_fail(obj         != NULL);

	msnobj_data = msn_object_to_string(obj);
	msnobj_base64 = tobase64(msnobj_data, strlen(msnobj_data));
	g_free(msnobj_data);

#if 0
	if ((c = strchr(msnobj_base64, '=')) != NULL)
		*c = '\0';
#endif

	session_id = rand() % 0xFFFFFF00 + 4;

	branch = g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111,
							 rand() % 0xAAFF + 0x1111);

	slpsession->call_id = g_strdup_printf("%4X%4X-%4X-%4X-%4X-%4X%4X%4X",
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111,
										  rand() % 0xAAFF + 0x1111);

	content = g_strdup_printf(
		"EUF-GUID: {A4268EEC-FEC5-49E5-95C3-F126696BDBF6}\r\n"
		"SessionID: %lu\r\n"
		"AppID: 1\r\n"
		"Context: %s",
		session_id,
		msnobj_base64);

	g_free(msnobj_base64);

	body = g_strdup_printf(
		"INVITE MSNMSGR:%s MSNSLP/1.0\r\n"
		"To: <msnmsgr:%s>\r\n"
		"From: <msnmsgr:%s>\r\n"
		"Via: MSNSLP/1.0/TLP ;branch={%s}\r\n"
		"CSeq: 0\r\n"
		"Call-ID: {%s}\r\n"
		"Max-Forwards: 0\r\n"
		"Content-Type: application/x-msnmsgr-sessionreqbody\r\n"
		"Content-Length: %d\r\n"
		"\r\n"
		"%s"
		"\r\n\r\n",
		msn_user_get_passport(remote_user),
		msn_user_get_passport(remote_user),
		msn_user_get_passport(local_user),
		branch,
		slpsession->call_id,
		strlen(content) + 5,
		content);

	g_free(content);
	g_free(branch);

	gaim_debug_misc("msn", "Message = {%s}\n", body);

	invite_msg = msn_message_new_msnslp();

	msn_message_set_sender(invite_msg, local_user);
	msn_message_set_receiver(invite_msg, remote_user);

	msn_message_set_body(invite_msg, body);

	g_free(body);

	msn_slp_session_send_msg(slpsession, invite_msg);

	msn_message_destroy(invite_msg);
}
예제 #6
0
파일: main.cpp 프로젝트: Chris112/sep
int main(int argc, char **argv)
{
    if(argc != 6) {
        std::cout << "wrong args" << std::endl;
        return 1;
    }
    const char *sensorDeviceName = argv[1];
    const char *actuatorDeviceName = argv[2];
    const int cameraDeviceNum = atoi(argv[3]);
    const int port = atoi(argv[4]);
    const char *mode = argv[5];

    bool showThermal = false;
    bool showRGB = false;
    if(mode[0] == 't') {
        showThermal = true;
    } else if(mode[0] == 'r') {
        showRGB = true;
    } else {
        throw "wtf";
    }

    std::cout << "blah" << std::endl;

    std::shared_ptr<ApplicationCore> core = ApplicationCore::instantiate();

    auto sc = std::make_shared<ThermalSensorController>(core, sensorDeviceName, 115200);
    auto rc = std::make_shared<RgbController>(core, cameraDeviceNum);
    auto ac = std::make_shared<ActuatorController>("/dev/tty.usbserial-A9S3VTXD");
    auto ns = std::make_shared<NetService>(core);

    sc->init();
    rc->init();
    ac->init();
    ns->init(port);

    boost::asio::deadline_timer timer(*core->getIOService());
    std::function<void(const boost::system::error_code&)> captureStuff;
    GyroReading gyroReading;
    ThermoReading thermoReading;
    captureStuff = [&](const boost::system::error_code& /*e*/) { 
        //
        cv::Vec2d pos = ac->getCurrentPosition();

        rc->captureFrame();
        auto rgbFrame = rc->popFrame();

        if(showRGB && rgbFrame->rows > 0) {
            rapidjson::Document doc;
            auto &aloc = doc.GetAllocator();
            doc.SetObject();

            cv::Mat imgMat(rgbFrame->rows, rgbFrame->cols, CV_8UC4, cv::Scalar::all(0.0));
            cv::cvtColor(*rgbFrame, imgMat, CV_BGR2RGBA, 4); 

            cv::Size size(rgbFrame->cols*0.2, rgbFrame->rows*0.2);
            cv::resize(imgMat, imgMat, size);

            std::string imgDataB64 = tobase64(imgMat.data, imgMat.total()*4*sizeof(byte));
            rapidjson::Value val;
            val.SetString(imgDataB64.c_str(), doc.GetAllocator());
            doc.AddMember("data", val, aloc);

            doc.AddMember("type", "rgb_data", aloc);
            doc.AddMember("yaw", -pos[0], aloc);
            doc.AddMember("pitch", -pos[1], aloc);
            doc.AddMember("dataWidth", imgMat.cols, aloc);
            doc.AddMember("dataHeight", imgMat.rows, aloc);
            doc.AddMember("yawSize", 63.625, aloc);
            doc.AddMember("pitchSize", 35.789, aloc);

            ns->sendWSDoc(doc);
        }


        /*if(sc->popGyroReading(gyroReading)) {
            printf("Roll: %f, Pitch: %f, Yaw: %f.\n",
                gyroReading.roll, gyroReading.pitch, gyroReading.yaw
            );
        }*/
        sc->requestThermoReading();

        std::cout << "tick: " << timer.expires_at() << std::endl;

        if(showThermal && sc->popThermoReading(thermoReading)){
            rapidjson::Document doc;
            auto &aloc = doc.GetAllocator();
            doc.SetObject();
            doc.AddMember("type", "thermo_data", aloc);
            doc.AddMember("yaw", -pos[0], aloc);
            doc.AddMember("pitch", -pos[1], aloc);

            cv::Mat imgMat(4, 16, CV_8UC4, cv::Scalar::all(0.0));
            cv::Mat mat = thermoReading.img;

            for(int i = 0; i < mat.total(); i++) {
                int y = 3-(i%4);
                int x = i/4;
                double temp = mat.at<float>(0, i);

                if(
                        (x == 11 && y == 2)
                    ||  (x == 11 && y == 3)
                    ||  (x == 12 && y == 2)
                ) {
                    temp += 10.0;
                }

                //std::cout << (int)temp << " ";

                cv::Vec4b col = hsv(
                    300-300.0*(std::max(temp, 14.0)-14.0)/(40.0-14.0),
                    1, 1
                );
                if(temp <= 11.0) {
                    col = cv::Vec4b(30, 30, 50, 255);
                } else if(temp > 40.0) {
                    col = cv::Vec4b(255, 255, 255, 255);
                }
                imgMat.at<cv::Vec4b>(y, x) = col;
                //std::cout << std::endl;
            }

            std::string imgDataB64 = tobase64(imgMat.data, imgMat.total()*4*sizeof(byte));
            rapidjson::Value val;
            val.SetString(imgDataB64.c_str(), doc.GetAllocator());
            doc.AddMember("data", val, aloc);

            ns->sendWSDoc(doc);
        }

        timer.expires_from_now(boost::posix_time::milliseconds(interval));
        timer.async_wait(captureStuff);
    };
    timer.expires_from_now(boost::posix_time::milliseconds(interval));
    timer.async_wait(captureStuff);

    ns->registerCallback("move_actuator", [&](const rapidjson::Document &doc) {
        ac->stop();
        ActuatorMoveOrder order;
        order.posDeg = cv::Vec2d(
            std::max(-150.0, std::min(150.0, -doc["yaw"  ].GetDouble()/M_PI*180)),
            std::max(- 90.0, std::min( 90.0, -doc["pitch"].GetDouble()/M_PI*180))
        );
        order.duration = 3.5;
        ac->queueMove(order);
    });
    
    std::cout << "run" << std::endl;
    core->run();
}