view smpp-trx-sa/pdu_out.c @ 227:a349ae9d90fa

number db v2: implement themwi-dump-numdb2
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 10:18:50 -0800
parents 9d6e8d99d2b1
children
line wrap: on
line source

/*
 * This module implements all functions related to sending outgoing PDUs
 * to the SMPP server.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>

extern int tcpsock;

static unsigned req_out_seq = 1;

static void
pdu_to_tcp(pdu, pdulen)
	u_char *pdu;
	unsigned pdulen;
{
	int cc;

	cc = write(tcpsock, pdu, pdulen);
	if (cc != pdulen) {
		perror("write to TCP socket");
		log_fatal_error("error writing to TCP socket");
		exit(1);
	}
}

void
send_bind_req(system_id, password)
	char *system_id, *password;
{
	u_char bind_req[64], *dp;
	unsigned bind_req_len, slen;

	dp = bind_req + 4;	/* length will be filled last */
	/* command_id */
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 0x09;		/* bind_transceiver */
	/* empty command_status */
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 0;
	/* sequence_number */
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 0;
	*dp++ = 1;
	/* system_id */
	slen = strlen(system_id) + 1;
	bcopy(system_id, dp, slen);
	dp += slen;
	/* password */
	slen = strlen(password) + 1;
	bcopy(password, dp, slen);
	dp += slen;
	/* system_type */
	strcpy(dp, "SMPP");
	dp += 5;
	/* interface_version */
	*dp++ = 0x34;
	/* addr_ton */
	*dp++ = 0;
	/* addr_npi */
	*dp++ = 0;
	/* address_range */
	*dp++ = 0;
	bind_req_len = dp - bind_req;
	bind_req[0] = bind_req_len >> 24;
	bind_req[1] = bind_req_len >> 16;
	bind_req[2] = bind_req_len >> 8;
	bind_req[3] = bind_req_len;
	pdu_to_tcp(bind_req, bind_req_len);
	log_sent_pdu(bind_req, bind_req_len);
}

void
send_enq_link_resp(rx_hdr)
	u_char *rx_hdr;
{
	u_char resp[16];

	/* command_length */
	resp[0] = 0;
	resp[1] = 0;
	resp[2] = 0;
	resp[3] = 16;
	/* command_id */
	resp[4] = rx_hdr[4] | 0x80;
	resp[5] = rx_hdr[5];
	resp[6] = rx_hdr[6];
	resp[7] = rx_hdr[7];
	/* command_status */
	resp[8] = 0;
	resp[9] = 0;
	resp[10] = 0;
	resp[11] = 0;
	/* sequence_number */
	resp[12] = rx_hdr[12];
	resp[13] = rx_hdr[13];
	resp[14] = rx_hdr[14];
	resp[15] = rx_hdr[15];
	/* good to go */
	pdu_to_tcp(resp, 16);
}

void
send_message_resp(rx_hdr)
	u_char *rx_hdr;
{
	u_char resp[17];

	/* command_length */
	resp[0] = 0;
	resp[1] = 0;
	resp[2] = 0;
	resp[3] = 17;
	/* command_id */
	resp[4] = rx_hdr[4] | 0x80;
	resp[5] = rx_hdr[5];
	resp[6] = rx_hdr[6];
	resp[7] = rx_hdr[7];
	/* command_status */
	resp[8] = 0;
	resp[9] = 0;
	resp[10] = 0;
	resp[11] = 0;
	/* sequence_number */
	resp[12] = rx_hdr[12];
	resp[13] = rx_hdr[13];
	resp[14] = rx_hdr[14];
	resp[15] = rx_hdr[15];
	/* empty message_id */
	resp[16] = 0;
	/* good to go */
	pdu_to_tcp(resp, 17);
	log_sent_pdu(resp, 17);
}

void
send_pdu_from_localsock(pdu, pdulen)
	u_char *pdu;
	unsigned pdulen;
{
	/* assign incrementing sequence number */
	req_out_seq++;
	if (req_out_seq > 0x7FFFFFFF)
		req_out_seq = 1;
	pdu[12] = req_out_seq >> 24;
	pdu[13] = req_out_seq >> 16;
	pdu[14] = req_out_seq >> 8;
	pdu[15] = req_out_seq;
	/* good to go */
	pdu_to_tcp(pdu, pdulen);
	log_sent_pdu(pdu, pdulen);
}