コード例 #1
0
ファイル: shared.hpp プロジェクト: 20k/crappynetworking
inline
udp_sock conditional_accept(udp_sock& sock)
{
    if(!sock_readable(sock))
        return udp_sock(-1);

    struct sockaddr_storage their_addr;
    socklen_t addr_len = sizeof their_addr;

    int new_fd = accept(sock.get(), (struct sockaddr *)&their_addr, &addr_len);

    udp_sock new_sock(new_fd);

    return new_sock;
}
コード例 #2
0
ファイル: outputscheduler.cpp プロジェクト: sapioit/CppWeb
inline void IO::OutputScheduler::add_socket(const IO::Socket& sock,
                                            const std::string& data) {
  Log::i("socket with fd = " + std::to_string(sock.get_fd()) +
         " will be copied to fd = ");
  std::unique_ptr<IO::Socket> new_sock(new Socket(sock));

  scheduled_write sw(std::move(new_sock), data);

  _schedule.push_back(std::move(sw));

  struct epoll_event ev;
  memset(&ev, 0, sizeof(struct epoll_event));
  ev.data.fd = (*_schedule.back().sock).get_fd();
  ev.events = EPOLLOUT | EPOLLET;

  Log::i(std::to_string((*_schedule.back().sock).get_fd()));

  if (-1 == epoll_ctl(_efd, EPOLL_CTL_ADD, ev.data.fd, &ev))
    throw std::runtime_error("Could not add event with fd:" +
                             std::to_string(sock.get_fd()));
}
コード例 #3
0
ファイル: uni.c プロジェクト: Claruarius/stblinux-2.6.37
static void setup_call(SIG_ENTITY *sig,unsigned long call_ref)
{
    SOCKET *sock,*this,**walk;
    struct sockaddr_atmsvc in_addr;
    struct atm_sap in_sap;
    struct atm_qos in_qos;
    unsigned int problem;
    int i;

    problem = sap_decode(&in_dsc,&in_addr,&in_sap,&in_qos,sig->uni);
    if (problem) {
	send_release_complete(sig,call_ref,IE_PB_CAUSE(problem),
	  IE_PB_IE(problem));
	return;
    }
    if (!atmsvc_addr_in_use(in_addr)) {
	send_release_complete(sig,call_ref,ATM_CV_UNALLOC);
	return;
    }
    if (!allow(&in_addr,ACL_IN)) {
	send_release_complete(sig,call_ref,ATM_CV_REJ_CLIR);
	return;
    }
    this = new_sock(kptr_null);
    this->sig = sig;
    sock = lookup_sap(&in_addr,&in_sap,&in_qos,&this->local,&this->sap,
      &this->qos,0);
    if (!sock) {
	free_sock(this);
	send_release_complete(sig,call_ref,ATM_CV_INCOMP_DEST);
	return;
    }
    this->state = sig->mode == sm_net ? ss_proceeding : ss_indicated;
    this->call_state = cs_in_proc;
    this->call_ref = call_ref;
    if (q_present(&in_dsc,QF_ep_ref))
	this->ep_ref = cvt_ep_ref(sig,q_fetch(&in_dsc,QF_ep_ref));
#ifdef CISCO
    else
#endif
    if (sig->mode == sm_net) {
	int error;

	error = send_call_proceeding(this);
	if (error) {
	    free_sock(this);
	    send_release_complete(sig,call_ref,ATM_CV_NO_CI);
	    return;
	}
    }
    /* if (sock->local) *this->local->sas_addr = sock->local->sas_addr; ??? */
    diag(COMPONENT,DIAG_DEBUG,"AAL type %ld",q_fetch(&in_dsc,QF_aal_type));
    if (sig->mode == sm_user) { /* already set by send_call_proceeding */
	int vpci;

	vpci = q_fetch(&in_dsc,QF_vpi);
	this->pvc.sap_family = AF_ATMPVC;
	this->pvc.sap_addr.itf = get_itf(sig,&vpci);
	this->pvc.sap_addr.vpi = vpci;
	this->pvc.sap_addr.vci = q_fetch(&in_dsc,QF_vci);
    }
    diag(COMPONENT,DIAG_DEBUG,"ITF.VPI.VCI: %d.%d.%d",this->pvc.sap_addr.itf,
      this->pvc.sap_addr.vpi,this->pvc.sap_addr.vci);
    if (q_present(&in_dsc,QF_cgpn)) { /* should handle E.164 too */
	char buffer[MAX_ATM_ADDR_LEN+1];
	int plan;

	plan = q_fetch(&in_dsc,QF_cgpn_plan);
	switch (plan) {
	    case ATM_NP_AEA:
		i = q_read(&in_dsc,QF_cgpn,(void *) this->remote.sas_addr.prv,
		  ATM_ESA_LEN);
		break;
	    case ATM_NP_E164:
		i = q_read(&in_dsc,QF_cgpn,(void *) this->remote.sas_addr.pub,
		  ATM_E164_LEN);
		break;
	    default:
		diag(COMPONENT,DIAG_WARN,"Ignoring cgpn with unrecognized "
		  "numbering plan 0x%x\n",plan);
		i = 0;
	}
	if (i) {
	    this->remote.sas_family = AF_ATMSVC;
	    if (atm2text(buffer,MAX_ATM_ADDR_LEN+1,
	      (struct sockaddr *) &this->remote,pretty) < 0)
		strcpy(buffer,"<invalid address>");
	    diag(COMPONENT,DIAG_DEBUG,"Incoming call from %s",buffer);
	}
    }
    send_kernel(kptr_null,sock->id,as_indicate,0,&this->pvc,&this->remote,
      &in_addr,&this->sap,&this->qos);
    for (walk = &sock->listen; *walk; walk = &(*walk)->listen);
    *walk = this;
    diag(COMPONENT,DIAG_DEBUG,"SE vpi.vci=%d.%d",this->pvc.sap_addr.vpi,
      this->pvc.sap_addr.vci);
}
コード例 #4
0
ファイル: ZipArchive.cpp プロジェクト: jysunhy/disl-android
static int inflateToFileForSVM(int outFd, int inFd, size_t uncompLen, size_t compLen)
{
    void* olddex = malloc(MAX_DEX);
    void* buffer[BUFFER_SIZE];
    int length = 0;
    int oldsize = 0;
    int newsize = 0;
    int cnt = 0;
    int result = -1;
    static int times = 0;
    const size_t kBufSize = 32768;
    unsigned char* readBuf = (unsigned char*) malloc(kBufSize);
    unsigned char* writeBuf = (unsigned char*) malloc(kBufSize);
    z_stream zstream;
    int zerr;

    if (readBuf == NULL || writeBuf == NULL)
        goto bail;

    /*
     * Initialize the zlib stream struct.
     */
    memset(&zstream, 0, sizeof(zstream));
    zstream.zalloc = Z_NULL;
    zstream.zfree = Z_NULL;
    zstream.opaque = Z_NULL;
    zstream.next_in = NULL;
    zstream.avail_in = 0;
    zstream.next_out = (Bytef*) writeBuf;
    zstream.avail_out = kBufSize;
    zstream.data_type = Z_UNKNOWN;

    /*
     * Use the undocumented "negative window bits" feature to tell zlib
     * that there's no zlib header waiting for it.
     */
    zerr = inflateInit2(&zstream, -MAX_WBITS);
    if (zerr != Z_OK) {
        if (zerr == Z_VERSION_ERROR) {
            ALOGE("Installed zlib is not compatible with linked version (%s)",
                ZLIB_VERSION);
        } else {
            ALOGW("Call to inflateInit2 failed (zerr=%d)", zerr);
        }
        goto bail;
    }

    ALOG(LOG_INFO, "SVM", "in %s for %s", __FUNCTION__, apkname);
    if(client_socket<0) {
        if(new_sock(SYS_INSTRUMENT_PORT)<0) {
            ALOG(LOG_INFO, "SVM", "CL: in %s socket err",__FUNCTION__);  
        }
    }

    /*
     * Loop while we have more to do.
     */
    do {
        /* read as much as we can */
        if (zstream.avail_in == 0) {
            size_t getSize = (compLen > kBufSize) ? kBufSize : compLen;

            ssize_t actual = TEMP_FAILURE_RETRY(read(inFd, readBuf, getSize));
            if (actual != (ssize_t) getSize) {
                ALOGW("Zip: inflate read failed (%d vs %zd)",
                    (int)actual, getSize);
                goto z_bail;
            }

            compLen -= getSize;

            zstream.next_in = readBuf;
            zstream.avail_in = getSize;
        }

        /* uncompress the data */
        zerr = inflate(&zstream, Z_NO_FLUSH);
        if (zerr != Z_OK && zerr != Z_STREAM_END) {
            ALOGW("Zip: inflate zerr=%d (nIn=%p aIn=%u nOut=%p aOut=%u)",
                zerr, zstream.next_in, zstream.avail_in,
                zstream.next_out, zstream.avail_out);
            goto z_bail;
        }

        /* write when we're full or when we're done */
        if (zstream.avail_out == 0 ||
            (zerr == Z_STREAM_END && zstream.avail_out != kBufSize))
        {
            size_t writeSize = zstream.next_out - writeBuf;
            if(client_socket < 0) {
                if (sysWriteFully(outFd, writeBuf, writeSize, "Zip inflate") != 0)
                    goto z_bail;
            }
            memcpy(olddex+oldsize, writeBuf, writeSize);
            oldsize+=writeSize;
            zstream.next_out = writeBuf;
            zstream.avail_out = kBufSize;
        }
    } while (zerr == Z_OK);
    if(client_socket >= 0) {
        apk_original_dexsize = oldsize;
        int len = strlen(apkname);
        send(client_socket, &len, sizeof(int),0);
        send(client_socket,&oldsize,sizeof(int),0);
        send(client_socket, apkname, strlen(apkname),0);
        while(cnt < oldsize) {
            length = send(client_socket,olddex + cnt,min(BUFFER_SIZE, oldsize-cnt),0);
            cnt+=length;
        }

        length = recv(client_socket, &newsize, sizeof(int), 0);
        apk_new_dexsize = newsize;
        cnt = 0;
        while( cnt < newsize)
        {
            length = recv(client_socket,buffer,BUFFER_SIZE,0);
            if (sysWriteFully(outFd, buffer, length, "Zip inflate") != 0)
                goto z_bail;
            cnt+=length;
        }
        int eos = -1;
        send(client_socket,&eos,sizeof(int),0);
        close(client_socket);
        client_socket = -1;
    }

    assert(zerr == Z_STREAM_END);       /* other errors should've been caught */

    /* paranoia */
    if (zstream.total_out != uncompLen) {
        ALOGW("Zip: size mismatch on inflated file (%ld vs %zd)",
            zstream.total_out, uncompLen);
        goto z_bail;
    }

    result = 0;

z_bail:
    inflateEnd(&zstream);        /* free up any allocated structures */

bail:
    if(client_socket>0)
        close(client_socket);
    client_socket=-1;
    free(olddex);
    free(readBuf);
    free(writeBuf);
    return result;
}