Exemple #1
0
void
ack_received( tNode* pNode ) {
    tPDU* pPDU = pNode->pWaitingForAckPDU;
    pNode->pWaitingForAckPDU = NULL;

    /* notify user that the node is no longer waiting for ack */
    if (pNode->pAnnexE->events.AnnexEEvWriteable != NULL) {
        pNode->nRef++; /* protect node */
        pNode->pAnnexE->events.AnnexEEvWriteable(
                AsHANNEXE( pNode->pAnnexE ),
                pNode->pAnnexE->hAppAnnexE,
                pNode->RemoteHost.nIP,
                pNode->RemoteHost.nPort
            );
        if (pNode->nRef == 1) {
            del_node( pNode );
        }
        pNode->nRef--; /* unprotect node */
    }
    /* start IMA timer  */
    pNode->nRetry = 0;
    start_retransmit_or_ima_timer( pNode );
    /* free PDU! */
    free_pdu( pNode->pAnnexE, pPDU );
}
int main() {
    char *message = "1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100.";
    //char* message = "1, 2, 3, 4, 5, 6, 7, 8, 9 and 10, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, !!";
    //char* message1 = "ئهجگچج";  //Should be 07916998051030F2040B916998775706F80008410192810153610C06260647062C06AF0686062C
    //in encoding 2 0011000891797765800008A70CD8A6D987D8ACDAAFDA86D8AC
    //char* message1 = "\x06\xA3";
    char *message1 = "\x06\x33\x06\x44\x06\x4a\x06\x45";
    //const wchar_t *name = L"r\u00e9sum\u00e9";
    //setlocale(LC_CTYPE, "en_US.UTF-8");
    //wprintf(L"name is %ls\n", name);
    char *to = "97775608";
    int alphabet = 0;
    int flash = 0;
//    int with_udh = 0;
//    char *udh_data = "";
    char *mode = "new";
    int report = 0;
    int validity = 167; // 1 day
    int system_msg = 0;
    int replace_msg = 0;
    int to_type = 1;
    char *smsc = "";
    size_t msgs;

    char **pdu = encode_to_pdu(&msgs, to, message, strlen(message), alphabet, flash, report, mode, validity,
                               replace_msg, system_msg, to_type, smsc);

    for (int i = 0; i < msgs; i++) {
        printf("pdu%d = %s\n", i, pdu[i]);
    }

    if (pdu) free_pdu(&msgs, pdu);


    char **pdu1 = encode_to_pdu(&msgs, to, message1, strlen(message1), alphabet, flash, report, mode, validity,
                                replace_msg, system_msg, to_type, smsc);

    for (int j = 0; j < msgs; j++) {
        printf("pdu1%d = %s\n", j, pdu1[j]);
    }

    if (pdu1) free_pdu(&msgs, pdu1);

    return (0);
}
//*
//*---------------------------------------------------------------------------------
//* Function Name: free_request_list
//* Description : Release outstanding pdus (snmp messages)
//* Return Value : NONE
//* Calling To  : free_pdu
//* Called by   : snmp_close
//*---------------------------------------------------------------------------------
//*
int free_request_list(request_list      *req)
{
    free_pdu (req->pdu) ;
    if (req) {
            free (req) ;
            req = NULL ;
    }
    return 0 ;
}
Exemple #4
0
void
send_pdu( tAnnexE* pAnnexE, tPDU* pPDU ) {
    if (pPDU->fPDUIsWaitingForSend)
        return;
    if ((!IsListEmpty( &pAnnexE->WaitingForTransmissionList )) || (!pAnnexE->fReadyToSend)) {
        pPDU->fPDUIsWaitingForSend = TRUE;
        InsertTailList( &pAnnexE->WaitingForTransmissionList, &pPDU->lPDULink );
        pPDU = NULL;
    }
    if (pAnnexE->fReadyToSend) {
        if (pAnnexE->fReadyToSend) {
            int res;
            if (pPDU == NULL) {
                PLIST_ENTRY plink;
                if (IsListEmpty( &pAnnexE->WaitingForTransmissionList ))
                    return;
                plink = RemoveHeadList( &pAnnexE->WaitingForTransmissionList );
                pPDU = CONTAINING_RECORD( plink, tPDU, lPDULink );
                pPDU->fPDUIsWaitingForSend = FALSE;
            }
            res = liUdpSend(
                    pAnnexE->sock,
                    &pPDU->PDU[0],
                    pPDU->nSize,
                    pPDU->nIP,
                    pPDU->nPort
                );
            if (res < 0) {
                pPDU->fPDUIsWaitingForSend = TRUE;
                InsertHeadList( &pAnnexE->WaitingForTransmissionList, &pPDU->lPDULink );
                pAnnexE->fReadyToSend = FALSE;
                return;
            }
            if (AEHGet_A( pPDU->PDU ) == 0)
                free_pdu( pAnnexE, pPDU );

            pPDU = NULL;
        }
    }
}
int             snmp_send_trap(snmp_session     *session, snmp_pdu      *pdu)
{
        uint8 data[SNMP_MAX_TRAP_LEN];
        request_list  *rp ;
        int       length = SNMP_MAX_TRAP_LEN;

        if (session == NULL)
        return(FALSE);   //------SNMP: Send message on NULL Session.-----------

    if (pdu == NULL)
        return(FALSE);   //------SNMP: Intend to send a null pdu.--------------

    if (pdu->r_addr.sin_addr.s_addr == SNMP_DEFAULT_ADDRESS) {
        if (session->r_addr.sin_addr.s_addr != SNMP_DEFAULT_ADDRESS)
              bcopy((char *)&session->r_addr,(char *)&pdu->r_addr,sizeof(pdu->r_addr));
        else
              return(FALSE);  //-------SNMP: Without specify remote ip address.
    }
    switch (pdu->command) {
        case SNMP_GET:
             snmpOutGetRequests++ ;
             break ;
        case SNMP_GET_NEXT:
             snmpOutGetNexts++ ;
                break ;
        case SNMP_SET:
                snmpOutSetRequests++ ;
             break;
        case SNMP_GET_RESPONSE:
             snmpOutGetResponses++ ;
             break;
        case SNMP_TRAP:
             snmpOutTraps++ ;
             break ;
        default:
             break ;
    }
    switch (pdu->command) {
        case SNMP_GET:
        case SNMP_GET_NEXT:
        case SNMP_SET:
                if (pdu->reqid == 0)
                         pdu->reqid     = ++Reqid ;
//------------- pdu->errstatus = 0 ; -------------------------------------
//------------- pdu->errindex  = 0 ; -------------------------------------
                rp = (request_list *)calloc(1,sizeof(request_list)) ;
                if (rp == NULL)
                 return(FALSE) ;
                session->requests = rp ;
                rp->pdu           = pdu ;
                rp->reqid         = pdu->reqid ;
//              rp->retries       = session->retries ;
//              rp->timeout       = session->timeout ;
                break;
        case SNMP_GET_RESPONSE:
                break;
        case SNMP_TRAP:
                pdu->reqid = 1; // trap do not need request id
                break;
        default:
                return(FALSE);  //-----------SNMP: Unknown message type.-------
    }
        if(!snmp_build_trap(session, pdu, data, &length))
                return(FALSE);  //------SNMP: Fail to build snmp message.------
        snmpOutPkts++ ;
        if (!snmp_udp_send(session, data, length))
                return(FALSE);  //------SNMP: Fail to send snmp message.-------
        if (pdu->command == SNMP_GET_RESPONSE || pdu->command == SNMP_TRAP)
                free_pdu (pdu) ;
    return(TRUE);
}
void
test_send_pdu(struct proc *p, iscsi_test_send_pdu_parameters_t *par)
{
	static uint8_t pad_bytes[4] = { 0 };
	test_pars_t *tp;
	connection_t *conn;
	pdu_t *pdu;
	uint32_t psize = par->pdu_size;
	void *pdu_ptr = par->pdu_ptr;
	struct uio *uio;
	uint32_t i, pad, dsl, size;
	int s;

	if ((tp = find_test_id(par->test_id)) == NULL) {
		par->status = ISCSI_STATUS_INVALID_ID;
		return;
	}
	if (!psize || pdu_ptr == NULL ||
		((par->options & ISCSITEST_SFLAG_UPDATE_FIELDS) && psize < BHS_SIZE)) {
		par->status = ISCSI_STATUS_PARAMETER_INVALID;
		return;
	}
	if ((conn = tp->connection) == NULL) {
		par->status = ISCSI_STATUS_TEST_INACTIVE;
		return;
	}
	if ((pdu = get_pdu(conn, TRUE)) == NULL) {
		par->status = ISCSI_STATUS_TEST_CONNECTION_CLOSED;
		return;
	}
	DEB(1, ("Test Send PDU, id %d\n", par->test_id));

	if ((par->status = map_databuf(p, &pdu_ptr, psize)) != 0) {
		free_pdu(pdu);
		return;
	}

	i = 1;
	if (!par->options) {
		pdu->io_vec[0].iov_base = pdu_ptr;
		pdu->io_vec[0].iov_len = size = psize;
	} else {
		memcpy(&pdu->pdu, pdu_ptr, BHS_SIZE);

		if (!(pdu->pdu.Opcode & OP_IMMEDIATE))
			conn->session->CmdSN++;
		pdu->pdu.p.command.CmdSN = htonl(conn->session->CmdSN);

		dsl = psize - BHS_SIZE;
		size = BHS_SIZE;

		hton3(dsl, pdu->pdu.DataSegmentLength);

		if (conn->HeaderDigest &&
			!(par->options & ISCSITEST_SFLAG_NO_HEADER_DIGEST)) {
			pdu->pdu.HeaderDigest = gen_digest(&pdu->pdu, BHS_SIZE);
			size += 4;
		}

		pdu->io_vec[0].iov_base = &pdu->pdu;
		pdu->io_vec[0].iov_len = size;

		if (dsl) {
			pdu->io_vec[1].iov_base = &pdu_ptr[BHS_SIZE];
			pdu->io_vec[1].iov_len = dsl;
			i++;
			size += dsl;

			/* Pad to next multiple of 4 */
			pad = (par->options & ISCSITEST_SFLAG_NO_PADDING) ? 0 : size & 0x03;

			if (pad) {
				pad = 4 - pad;
				pdu->io_vec[i].iov_base = pad_bytes;
				pdu->io_vec[i].iov_len = pad;
				i++;
				size += pad;
			}

			if (conn->DataDigest &&
				!(par->options & ISCSITEST_SFLAG_NO_DATA_DIGEST)) {
				pdu->data_digest = gen_digest_2(&pdu_ptr[BHS_SIZE], dsl,
												pad_bytes, pad);
				pdu->io_vec[i].iov_base = &pdu->data_digest;
				pdu->io_vec[i].iov_len = 4;
				i++;
				size += 4;
			}
		}
	}
	uio = &pdu->uio;
	uio->uio_iov = pdu->io_vec;
	UIO_SETUP_SYSSPACE(uio);
	uio->uio_rw = UIO_WRITE;
	uio->uio_iovcnt = i;
	uio->uio_resid = size;

	pdu->disp = PDUDISP_SIGNAL;
	pdu->flags = PDUF_BUSY | PDUF_NOUPDATE;

	s = splbio();
	/* Enqueue for sending */
	if (pdu->pdu.Opcode & OP_IMMEDIATE)
		TAILQ_INSERT_HEAD(&conn->pdus_to_send, pdu, send_chain);
	else
		TAILQ_INSERT_TAIL(&conn->pdus_to_send, pdu, send_chain);

	wakeup(&conn->pdus_to_send);
	tsleep(pdu, PINOD, "test_send_pdu", 0);
	splx(s);

	unmap_databuf(p, pdu_ptr, psize);
	par->status = ISCSI_STATUS_SUCCESS;
	if (par->options & ISCSITEST_KILL_CONNECTION)
		kill_connection(conn, ISCSI_STATUS_TEST_CONNECTION_CLOSED, NO_LOGOUT,
						TRUE);
}
Exemple #7
0
int __cdecl main(int argc, char **argv)
{
	WSADATA wsaData;
	SOCKET ConnectSocket = INVALID_SOCKET;
	struct addrinfo *result = NULL,
		*ptr = NULL,
		hints;
	
	char sendbuf[DEFAULT_BUFLEN];
	int sendbuflen = DEFAULT_BUFLEN;
	
	char recvbuf[DEFAULT_BUFLEN];
	int recvbuflen = DEFAULT_BUFLEN;
	
	int iResult;
	
	// init log config
	sprintf_s(LOG_PATH_FILENAME, _countof(LOG_PATH_FILENAME), "%s%s", LOG_PATH, LOG_FILENAME);
	sprintf_s(LOG_PATH_INDEX, _countof(LOG_PATH_INDEX), "%s%s", LOG_PATH, LOG_INDEX);

	signal(SIGINT, sigintHandler);

	// Initialize Winsock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		printf("WSAStartup failed with error: %d\n", iResult);
		return 1;
	}

	ZeroMemory(&hints, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

	char connection_out_port[DEFAULT_PORTLEN];
	sprintf_s(connection_out_port, DEFAULT_PORTLEN, "%d", global_config.connection_out.port);

	// Resolve the server address and port
	iResult = getaddrinfo(global_config.connection_out.ip, connection_out_port, &hints, &result);
	if (iResult != 0) {
		printf("getaddrinfo failed with error: %d\n", iResult);
		WSACleanup();
		return 1;
	}

	// Attempt to connect to an address until one succeeds
	for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {

		// Create a SOCKET for connecting to server
		ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
		if (ConnectSocket == INVALID_SOCKET) {
			printf("socket failed with error: %ld\n", WSAGetLastError());
			WSACleanup();
			return 1;
		}

		// Connect to server.
		iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
		if (iResult == SOCKET_ERROR) {
			closesocket(ConnectSocket);
			ConnectSocket = INVALID_SOCKET;
			continue;
		}
		break;
	}

	freeaddrinfo(result);

	if (ConnectSocket == INVALID_SOCKET) {
		printf("Unable to connect to server!\n");
		WSACleanup();
		return 1;
	}


	asn1SccSint sendInvokeid = 0;
	
	char sendMsg[] = "this is a test request";
	InputType sendType;
	strcpy_s(sendType.command.arr, DEFAULT_MSGLEN, sendMsg);
	sendType.command.nCount = strlen(sendMsg) + 1;

	OutputType recvType;

	do {
		sendType.invokeid = ++sendInvokeid;

		// Encode message to send
		pdu_ptr send_pdu = NULL;
		int sendEncodeErr = encode_out_pdu(&send_pdu, &sendType);
		if (sendEncodeErr)
			return 1;

		// Create a pdu to send
		int sendbufptr = DEFAULT_BUFLEN;
		write_pdu_to_send_buffer(send_pdu, sendbuf, sendbuflen, &sendbufptr);

		// Send a pdu
		iResult = send(ConnectSocket, sendbuf, sendbufptr, 0);
		if (iResult == SOCKET_ERROR) {
			printf("send failed with error: %d\n", WSAGetLastError());
			closesocket(ConnectSocket);
			WSACleanup();
			return 1;
		}
		 
		printf("Bytes Sent: %d\n", iResult);
		
		free_pdu(send_pdu);

		// Receive a pdu
		MEMZERO(recvbuf, recvbuflen);
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0) {
			queue_t pdus = { .head = NULL, .tail = NULL };
			queue_entry_ptr qe = NULL;
			pdu_ptr p = NULL;

			int recvbufptr = DEFAULT_BUFLEN;
			read_pdus_from_recv_buffer(recvbuf, recvbuflen, &recvbufptr, &pdus);

			qe = pdus.head;
			while (qe != NULL) {
				p = (pdu_ptr)(qe->ref);

				// Decode received message
				pdu_t recv_pdu;
				memcpy(recv_pdu.buffer, p->buffer, p->length);
				recv_pdu.length = p->length;
				int recvEncodeErr = decode_in_pdu(&recvType, &recv_pdu);
				if (recvEncodeErr)
					return 1;

				printf("Bytes received: %d (id: %d msg: %s) \n", iResult, (int)recvType.invokeid, recvType.command.arr);

				free_pdu(p);

				qe = qe->next;
			}

		}
		else {
			printf("recv failed with error: %d\n", WSAGetLastError());
			closesocket(ConnectSocket);
			WSACleanup();
			return 1;
		}
	
		Sleep(1000);

	} while (isActive);