GophHub - go4retro/tcpser/src/tcpser.c


Raw File

    1	#include <stdio.h>
    2	#include <stdlib.h>
    3	#include <signal.h>
    4	#include <unistd.h>
    5	#include <sys/time.h>
    6	
    7	#include <sys/param.h>
    8	#include <pthread.h>
    9	
   10	#include "bridge.h"
   11	#include "debug.h"
   12	#include "init.h"
   13	#include "ip.h"
   14	#include "modem_core.h"
   15	#include "phone_book.h"
   16	#include "util.h"
   17	
   18	const char MDM_BUSY[] = "BUSY\n";
   19	
   20	#define MAX_MODEMS 16
   21	
   22	int main(int argc, char *argv[]) {
   23	  modem_config cfg[MAX_MODEMS];
   24	  int modem_count;
   25	  char *ip_addr = NULL;
   26	
   27	  char all_busy[255];
   28	  int i;
   29	  int rc = 0;
   30	  int sSocket = 0;
   31	  fd_set readfs;
   32	  int max_fd = 0;
   33	  int accept_pending = FALSE;
   34	  unsigned char buf[255];
   35	  int cSocket;
   36	
   37	  log_init();
   38	
   39	  LOG_ENTER();
   40	
   41	  log_set_level(LOG_FATAL);
   42	
   43	  mdm_init();
   44	
   45	  pb_init();
   46	  
   47	  signal(SIGIO, SIG_IGN); /* Some Linux variant term on SIGIO by default */
   48	
   49	  modem_count = init(argc, argv, cfg, MAX_MODEMS, &ip_addr, all_busy, sizeof(all_busy));
   50	  sSocket = ip_init_server_conn(ip_addr, 6400);
   51	  if(-1 == sSocket) {
   52	    ELOG(LOG_FATAL, "Could not listen on %s", ip_addr);
   53	    exit (-1);
   54	  }
   55	
   56	  for(i = 0; i < modem_count; i++) {
   57	    LOG(LOG_INFO, "Creating modem #%d", i);
   58	    if(-1 == pipe(cfg[i].mp[0])) {
   59	      ELOG(LOG_FATAL, "Bridge task incoming IPC pipe could not be created");
   60	      exit(-1);
   61	    }
   62	    if(-1 == pipe(cfg[i].mp[1])) {
   63	      ELOG(LOG_FATAL, "Bridge task outgoing IPC pipe could not be created");
   64	      exit(-1);
   65	    }
   66	    cfg[i].line_data.sfd = sSocket;
   67	
   68	    spawn_thread(*bridge_task, (void *)&cfg[i], "BRIDGE");
   69	
   70	  }
   71	
   72	  for(;;) {
   73	    FD_ZERO(&readfs);
   74	    max_fd = 0;
   75	    for(i = 0; i < modem_count; i++) {
   76	      FD_SET(cfg[i].mp[0][0], &readfs);
   77	      max_fd=MAX(max_fd, cfg[i].mp[0][0]);
   78	    }
   79	    if(accept_pending == FALSE) {
   80	      max_fd=MAX(max_fd, sSocket);
   81	      FD_SET(sSocket, &readfs); 
   82	    }
   83	    LOG(LOG_ALL, "Waiting for incoming connections and/or indicators");
   84	    select(max_fd+1, &readfs, NULL, NULL, NULL);
   85	    for(i = 0; i < modem_count; i++) {
   86	      if (FD_ISSET(cfg[i].mp[0][0], &readfs)) {  // child pipe
   87	        rc = read(cfg[i].mp[0][0], buf, sizeof(buf) -1);
   88	        if(rc > -1) {
   89	          buf[rc] = 0;
   90	          LOG(LOG_DEBUG, "modem core #%d sent response '%s'", i, buf);
   91	          accept_pending = FALSE;
   92	        }
   93	      }
   94	    }
   95	    if (FD_ISSET(sSocket, &readfs)) {  // IP traffic
   96	      if(!accept_pending) {
   97	        LOG(LOG_DEBUG, "Incoming connection pending");
   98	        // first try for a modem that is listening.
   99	        for(i = 0; i < modem_count; i++) {
  100	          if(cfg[i].s[0] != 0 && cfg[i].is_off_hook == FALSE) {
  101	            // send signal to pipe saying pick up...
  102	            LOG(LOG_DEBUG, "Sending incoming connection to listening modem #%d", i);
  103	            writePipe(cfg[i].mp[1][1], MSG_CALLING);
  104	            accept_pending = TRUE;
  105	            break;
  106	          }
  107	        }
  108	        // now, send to any non-active modem.
  109	        for(i = 0; i < modem_count; i++) {
  110	          if(cfg[i].is_off_hook == FALSE) {
  111	            // send signal to pipe saying pick up...
  112	            LOG(LOG_DEBUG, "Sending incoming connection to non-connected modem #%d", i);
  113	            writePipe(cfg[i].mp[1][1], MSG_CALLING);
  114	            accept_pending = TRUE;
  115	            break;
  116	          }
  117	        }
  118	        if(i == modem_count) {
  119	          LOG(LOG_DEBUG, "No open modem to send to, send notice and close");
  120	          // no connections.., accept and print error
  121	          cSocket = ip_accept(sSocket);
  122	          if(cSocket > -1) {
  123	            // No tracing on this data output
  124	            if(strlen(all_busy) < 1) {
  125	              ip_write(cSocket, (unsigned char *)MDM_BUSY, strlen(MDM_BUSY));
  126	            } else {
  127	              writeFile(all_busy, cSocket);
  128	            }
  129	            close(cSocket);
  130	          }
  131	        }
  132	      }
  133	    }
  134	  }
  135	  LOG_EXIT();
  136	  return rc;
  137	}
  138	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.