view smpp-trx-sa/main.c @ 272:c78b8d6ce885

doc/Number-database: should be complete for now
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 26 Nov 2023 19:13:52 -0800
parents 1bf989f60aa3
children
line wrap: on
line source

/*
 * This C file is the main module for smpp-trx-sa - a standalone bind_trx
 * client (ESME role) that writes all incoming messages into a log file
 * and also allows outgoing messages to be sent via a UNIX domain local
 * socket (datagram).
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>

extern int tcpsock, localsock;

FILE *logF;
time_t curtime;
char fmt_time[32];
int init_done;

static void
format_time()
{
	struct tm *tm;

	tm = gmtime(&curtime);
	sprintf(fmt_time, "%d-%02d-%02dT%02d:%02d:%02dZ",
		tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
		tm->tm_hour, tm->tm_min, tm->tm_sec);
}

main(argc, argv)
	char **argv;
{
	struct sockaddr_in server_sin;
	fd_set fds;
	int max_fd, rc;

	if (argc < 6 || argc > 7) {
		fprintf(stderr,
"usage: %s server-ip system-id password log-file socket-pathname [enq-link]\n",
			argv[0]);
		exit(1);
	}
	server_sin.sin_family = AF_INET;
	server_sin.sin_addr.s_addr = inet_addr(argv[1]);
	if (server_sin.sin_addr.s_addr == INADDR_NONE) {
		fprintf(stderr, "error: invalid IP address argument \"%s\"\n",
			argv[1]);
		exit(1);
	}
	server_sin.sin_port = htons(2775);
	if (strlen(argv[2]) > 15) {
		fprintf(stderr, "error: system-id string is too long\n");
		exit(1);
	}
	if (strlen(argv[3]) > 8) {
		fprintf(stderr, "error: password string is too long\n");
		exit(1);
	}
	logF = fopen(argv[4], "a");
	if (!logF) {
		perror(argv[4]);
		exit(1);
	}
	if (argv[6])
		log_enquire_link_open(argv[6]);
	create_local_socket(argv[5]);
	max_fd = localsock;
	open_tcp_conn(&server_sin);
	if (tcpsock > max_fd)
		max_fd = tcpsock;
	time(&curtime);
	format_time();
	send_bind_req(argv[2], argv[3]);
	init_done = 1;
	/* main select loop */
	for (;;) {
		FD_ZERO(&fds);
		FD_SET(tcpsock, &fds);
		FD_SET(localsock, &fds);
		rc = select(max_fd+1, &fds, 0, 0, 0);
		if (rc < 0) {
			if (errno == EINTR)
				continue;
			perror("select");
			log_fatal_error("select syscall error");
			exit(1);
		}
		time(&curtime);
		format_time();
		if (FD_ISSET(tcpsock, &fds))
			tcpsock_select_handler();
		if (FD_ISSET(localsock, &fds))
			localsock_select_handler();
		fflush(logF);
	}
}