Esempio n. 1
0
/**
 * Function name:  send_csw
 * Description:    Sends a command status wrapper (CSW) request to the host.
 *                 All SCSI commands must finish processing by calling this
 *                 function.
 * Parameters: 
 *     @fd:      (IN) FD context
 *     @tag:     (IN) Tag received in the matching CBW
 *     @residue: (IN) Amount of data (in bytes) that was NOT transferred in
 *                    the data stage
 *     @status:  (IN) Command status
 *
 * Return value: None
 * Scope:        Global
 **/
void send_csw(fd_storage_t *fd, juint32_t tag, juint32_t residue, juint8_t status)
{
    jresult_t rc;
    struct bulk_cs_wrap *csw = (struct bulk_cs_wrap *)
        (fd->cmd_request->buffer.vaddr);

    if (!command_in_progress(fd))
        return;

    if (status)
    {
        DBG_V(DSLAVE_MS_USB, ("MASS: Sending CSW [Tag: %x], Status 0x%x, "
            "Residue %d.\n", tag, status, residue));
    }

    csw->Signature      = htole32(USB_BULK_CS_SIG);
    csw->Tag            = htole32(tag);
    csw->Residue        = htole32(residue);
    csw->Status         = status;

    rc = post_write(fd, fd->cmd_request, USB_BULK_CS_WRAP_LEN, 
        send_csw_callback, fd);
    if (rc)
        fatal_processing_error(fd);
}
Esempio n. 2
0
NET_API void
net_socket_close(struct net_service* service, net_socket nd, char send_rest)
{
	unsigned short index;
	struct net_session* session;
	sb_tree_value v;

	if(!service)
	{
		return;
	}
	if(!nd)
	{
		return;
	}

	index = ffid_index(service->socket_ids, nd);
	net_lock(&service->session_lock[index]);
	session = service->sessions[index];
	if(!session || session->id != nd)
	{
		net_unlock(&service->session_lock[index]);
		return;
	}
	// epoll_ctl(service->net_service_fd, EPOLL_CTL_DEL, session->fd, 0);
	clean_epoll_op(service, session);
	if(send_rest)
	{
		post_write(service, session);
	}
	
	service->sessions[index] = 0;
	// release read session
	release_read_session(session->rsession);
	session->rsession = 0;

	if(session->wsession && session->wsession->op == OP_NET_WRITE)
	{
		session->wsession->send_rest = send_rest;
		shutdown(session->fd, 0);
		release_listen_session(session->lsession);
		session->lsession = 0;
		// join to close_root sb_tree
		net_lock(&service->close_lock);
		v.ptr = session;
		sb_tree_insert(&service->close_root , nd, v);
		net_unlock(&service->close_lock);
	}
	else
	{
		net_close_fd(session->fd);
		release_net_session(session);
	}
	net_unlock(&service->session_lock[index]);

	net_lock(&service->id_lock);
	ffid_del_id(service->socket_ids, nd);
	net_unlock(&service->id_lock);
}
Esempio n. 3
0
void
handle_write(struct net_service* service, int ret, int err, struct write_session* wsession, size_t bytes)
{
    struct net_session* session;
    unsigned short index;
    unsigned int events;
    ffid_vtype fd;

    if(!wsession)
    {
        return;
    }
    fd = wsession->id;
    if(fd == 0)
    {
        release_write_session(wsession);
        return;
    }

    index = ffid_index(service->socket_ids, fd);
    net_lock(&service->session_lock[index]);
    session = service->sessions[index];
    wsession->op = OP_NET_NONE;
    if(ret && bytes >= 0)
    {
        send_buff_consume(wsession->sbuff, bytes);
    }

    if(!session || session->id != fd)
    {
        if(ret && wsession->send_rest && post_rest_write(service, wsession) > 0)
        {
            net_unlock(&service->session_lock[index]);
            return;
        }
        release_write_session(wsession);
        net_unlock(&service->session_lock[index]);
        return;
    }

    ret = post_write(service, session);
    events = 0;
    if(ret < 0)
    {
        events |= Eve_Error;
        print_error();
    }

    if (!events || push_queue(service, session, events) > 0)
    {
        net_unlock(&service->session_lock[index]);
        return;
    }
    net_unlock(&service->session_lock[index]);
    net_socket_close(service, fd, 0);
}
Esempio n. 4
0
void storeL(_u32 address, _u32 data)
{
    address &= 0xFFFFFF;
    _u32* ptr = translate_address_write(address);

    //Write
    if (ptr)
    {
        *ptr = data;
        post_write(address);
    }
}
Esempio n. 5
0
NET_API void
net_socket_close(struct net_service* service, net_socket nd, char send_rest)
{
    unsigned short index;
    struct net_session* session;

    if(!service)
    {
        return;
    }
    if(!nd)
    {
        return;
    }

    index = ffid_index(service->socket_ids, nd);
    net_lock(&service->session_lock[index]);
    session = service->sessions[index];
    if(!session || session->id != nd)
    {
        net_unlock(&service->session_lock[index]);
        return;
    }
    if(send_rest)
    {
        post_write(service, session);
    }

    service->sessions[index] = 0;

    if(session->wsession && session->wsession->op == OP_NET_WRITE)
    {
        shutdown(session->fd, 0);
        session->wsession->fd = session->fd;
        session->wsession->send_rest = send_rest;
    }
    else
    {
        shutdown(session->fd, SD_BOTH);
        net_close_fd(session->fd);
    }
    release_net_session(session);
    net_unlock(&service->session_lock[index]);

    net_lock(&service->id_lock);
    ffid_del_id(service->socket_ids, nd);
    net_unlock(&service->id_lock);
}
Esempio n. 6
0
static void post_syscall(void *drcontext, int sysnum)
{
	switch (sysnum) {
	case SYS_open:
		post_open(drcontext);
		break;
	case SYS_close:
		post_close(drcontext);
		break;
	case SYS_read:
		post_read(drcontext);
		break;
	case SYS_write:
		post_write(drcontext);
		break;
	case SYS_pwrite64:
		post_pwrite64(drcontext);
		break;
	}
}
Esempio n. 7
0
int main()
{
    char *some_memory[10]; /* an arrray of memory pointers */
    int exit_code = EXIT_FAILURE; /* set up exit code for failure */
    int i, j;

/* Call to mtrace routines in glibc library */
#ifdef MTRACE
    mtrace();  /* Turn on mtrace function */
#endif
    lmm_init();

    printf("Start of general malloc testing\n\n");

    printf("Start allocating 10 1k pieces of memory\n");
    for (i=0; i<10; i++)
    {
    some_memory[i] = (char *)malloc(ONE_K);
    printf ("Allocated a piece at 0x%x\n", (int)some_memory[i]);
    }
    printf("We have allocated some memory\n");
    printf("Now lets free part some memory\n");

    for (j=0; j<5; j++)
    {
    if (some_memory[j*2] != NULL) 
        {
        free(some_memory[j*2]);

        free(some_memory[j*2]);

        printf ("Freed a piece at 0x%x\n", (int)some_memory[j*2]);
        exit_code = EXIT_SUCCESS ;
        }
    }

    printf("\n\nNow let us check other functions\n\n");

#ifndef DMALLOC

    printf("Read after the end of the allocated string\n");

    post_read(); 

#endif

    printf("\nWrite after the end of the allocated string\n");

    post_write(); 

    printf ("\nRead before the allocated string\n");

    read_before(); 

#ifndef MTRACE

    printf("\nWrite before allocated area\n");

    write_before(); 

#endif

	lmm_dump_info();

    printf("\nMissed memory free of allocated string\n");

    miss_free();

#if !defined(MTRACE) && !defined(MEMWATCH) && !defined(DMALLOC)
    printf("\nUsing uninitialized memory\n");

    uninit_mem();

#endif

#ifdef MTRACE
    muntrace();  /* Turn off mtrace function */
#endif
	while(1);

    exit(exit_code);
}
Esempio n. 8
0
//---------------------------------------------------------------------------
int get_message(CConceptClient *OWNER, TParameters *PARAM, SOCKET CLIENT_SOCKET, char *LOCAL_PRIVATE_KEY, PROGRESS_API notify_parent, bool idle_call) {
    unsigned int size2          = 0;
    int          size           = 0;
    int          received       = 0;
    int          result         = 0;
    int          rec_count      = 0;
    int          buf_size       = 0;
    char         *buffer        = 0;
    char         *output        = 0;
    int          filemarker     = 0;
    int          MSG_ID         = 0;
    int          remaining      = 0;
    int          decrypt_result = 0;
    char         buf_temp[0xFFFF];

    if (BufferedMessages.Count()) {
        TParameters *clone = (TParameters *)BufferedMessages.Remove(0);
        PARAM->Sender = clone->Sender;
        PARAM->ID     = clone->ID;
        PARAM->Target = clone->Target;
        PARAM->Value  = clone->Value;
        //PARAM->Owner=clone->Owner;
        delete clone;
        return 1;
    }
    if (OWNER->RTSOCKET != INVALID_SOCKET) {
        if (sock_eof_timeout(OWNER->RTSOCKET, 0) == 0) {
            received = recv2(OWNER->RTSOCKET, buf_temp, sizeof(buf_temp), 0);
            size     = *(unsigned int *)buf_temp;
            if (!LOCAL_PRIVATE_KEY) {
                size2 = ntohl(*(unsigned int *)&size);
                if (size2 & 0xF0000000) {
                    size = size2 & 0xFFFF;
                } else {
                    size  = ntohl(size);
                    size2 = 0;
                }
            } else
                size = ntohl(size);

            if ((received <= 0) || (size <= 0) /*|| (size>0xFFFF)*/) {
                PARAM->Sender = (char *)"";
                PARAM->ID     = 0;
                PARAM->Target = (char *)"";
                PARAM->Value  = (char *)"";

                return 1;
            }
            if (size > received - 4)
                size = received - 4;

            if (size)
                buffer = buf_temp + sizeof(int);
            char *out_content = 0;

            if ((LOCAL_PRIVATE_KEY) && (!OWNER->is_http)) {
                out_content    = new char[size * 2];
                buffer[size]   = 0;
                decrypt_result = AES_decrypt(buffer, size, out_content, size, LOCAL_PRIVATE_KEY, 16, 0, false);

                out_content[decrypt_result] = 0;
                if (!decrypt_result) {
                    delete[] out_content;
                    out_content = 0;
                } else
                    size = decrypt_result;
            }

            if (out_content)
                DeSerializeBuffer(out_content, size, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value, size2);
            else
                DeSerializeBuffer(buffer, size, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value, size2);
            // just for safety
            switch (MSG_ID) {
                case 0x1001:
                    // event on buffer
                    if (PARAM->Target == (char *)"350") {
                        MSG_ID        = 0x110;
                        PARAM->Target = (char *)"103";
                    } else
                        MSG_ID = -0x1001;
                    break;

                case 0x110:
                    if ((PARAM->Target != (char *)"1003") && (PARAM->Target != (char *)"1007"))
                        MSG_ID = -0x110;
                    break;

                default:
                    MSG_ID = -0x1000;
            }
            PARAM->ID = MSG_ID;
            if (out_content)
                delete[] out_content;
            return size;
        }
    }

    do {
        //received   = recv(CLIENT_SOCKET, (char *)&size + rec_count, sizeof(int) - rec_count, 0);
        received = OWNER->Recv((char *)&size + rec_count, sizeof(int) - rec_count);
        if (received > 0) {
            rec_count += received;
        } else {
#ifdef _WIN32
            if (WSAGetLastError() == WSAETIMEDOUT)
#else
            if (errno == ETIMEDOUT)
#endif
                return -2;
            CHECK_RECONNECT(received);
        }
    } while ((rec_count < sizeof(int)) && (received > 0));

    if ((!LOCAL_PRIVATE_KEY) || (OWNER->is_http)) {
        size2 = ntohl(*(unsigned int *)&size);
        if (size2 & 0xF0000000) {
            size = size2 & 0xFFFF;
        } else {
            size  = ntohl(size);
            size2 = 0;
        }
    } else
        size = ntohl(size);

    if (received <= 0) {
        if (PostFile)
            post_done();
        return 0;
    }

    buf_size = size;
    char *out_content2 = 0;
    int  initial_buf   = 0;

    if (size) {
        if (PostFile) {
            buf_size = size > FILE_MESSAGE ? FILE_MESSAGE : size;
            if (size > FILE_MESSAGE)
                initial_buf = buf_size + 5;
            else
                initial_buf = buf_size;
            if (LOCAL_PRIVATE_KEY)
                out_content2 = new char[buf_size * 2];
        }
        buffer = new char[buf_size + 1];
    }

    //----------------------------------------//
    if ((size >= BIG_MESSAGE) && (notify_parent))
        // notify big message !
        notify_parent(-1, 3, idle_call);
    //----------------------------------------//
    rec_count = 0;
    do {
        if (PostFile) {
            if (filemarker) {
                remaining = size - rec_count;
                if (remaining > buf_size)
                    remaining = buf_size;
                if (LOCAL_PRIVATE_KEY) {
                    received = AES_recv_exact(OWNER, CLIENT_SOCKET, buffer, remaining, 0, LOCAL_PRIVATE_KEY);
                } else
                    received = OWNER->Recv(buffer, remaining);
                //received = recv(CLIENT_SOCKET, buffer, remaining, 0);
            } else
                received = OWNER->Recv(buffer + rec_count, initial_buf - rec_count);
            //received = recv(CLIENT_SOCKET, buffer + rec_count, initial_buf - rec_count, 0);
            rec_count += received;
            if (received > 0) {
                if (filemarker)
                    post_write(buffer, received);
                else
                if (rec_count == initial_buf) {
                    PARAM->Sender = (char *)"";
                    PARAM->Target = (char *)"";
                    if (LOCAL_PRIVATE_KEY) {
                        buffer[rec_count] = 0;
                        decrypt_result    = AES_decrypt(buffer, rec_count, out_content2, rec_count, LOCAL_PRIVATE_KEY, 16, 0, true);
                        if (decrypt_result) {
                            //decrypt_result -= 5;//rec_count - 5;
                            out_content2[decrypt_result] = 0;
                        }
                    }
                    int fpos = 0;
                    if (decrypt_result > 0) {
                        fpos = DeSerializeBuffer(out_content2, decrypt_result, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value);
                        if ((fpos > 0) && (fpos < decrypt_result))
                            post_write(out_content2 + fpos, decrypt_result - fpos);
                        delete[] out_content2;
                        out_content2 = 0;
                    } else {
                        fpos = DeSerializeBuffer(buffer, rec_count, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value);
                        if ((fpos > 0) && (fpos < rec_count))
                            post_write(buffer + fpos, rec_count - fpos);
                    }
                    PARAM->ID = MSG_ID;
                    PARAM->Value.LoadBuffer(0, 0);
                    filemarker = 1;
                }
            } else {
                CHECK_RECONNECT(received);
            }
        } else {
            //received   = CConceptClient::timedout_recv(CLIENT_SOCKET, buffer + rec_count, size - rec_count, 0);
            received = OWNER->RecvTimeout(buffer + rec_count, size - rec_count);
            if (received > 0) {
                rec_count += received;
            } else {
                CHECK_RECONNECT(received);
            }
        }

        if ((size >= BIG_MESSAGE) && (notify_parent))
            notify_parent((int)(((double)rec_count / size) * 100), 0, idle_call);
    } while ((received > 0) && (rec_count < size));

    if (out_content2) {
        delete[] out_content2;
        out_content2 = 0;
    }

    if ((size >= BIG_MESSAGE) && (notify_parent))
        notify_parent(101, 0, idle_call);
    // done big message !

    if (received <= 0) {
        if (buffer)
            delete[] buffer;
        return 0;
    }

    if (PostFile)
        post_done();
    else {
        char *out_content = 0;

        if ((LOCAL_PRIVATE_KEY) && (!OWNER->is_http)) {
            out_content    = new char[size * 2];
            buffer[size]   = 0;
            decrypt_result = AES_decrypt(buffer, size, out_content, size, LOCAL_PRIVATE_KEY, 16, 0, true);

            out_content[decrypt_result] = 0;
            if (!decrypt_result) {
                delete[] out_content;
                out_content = 0;
            } else
                size = decrypt_result;
        }

        if (out_content)
            DeSerializeBuffer(out_content, size, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value, size2);
        else
            DeSerializeBuffer(buffer, size, &PARAM->Sender, &MSG_ID, &PARAM->Target, &PARAM->Value, size2);
        PARAM->ID = MSG_ID;

        if (out_content)
            delete[] out_content;
    }
    if (buffer)
        delete[] buffer;

    return size;
}