GophHub - go4retro/tcpser/src/line.c


Raw File

    1	#include "debug.h"
    2	#include "modem_core.h"
    3	#include "phone_book.h"
    4	#include "ip.h"
    5	#include "bridge.h"
    6	#include "line.h"
    7	
    8	
    9	void reset_config(line_config *cfg) {
   10	  cfg->fd = -1;
   11	  cfg->is_telnet = FALSE;
   12	  cfg->is_data_received = FALSE;
   13	  cfg->is_connected = FALSE;
   14	  nvt_init_config(&cfg->nvt_data);
   15	}
   16	
   17	void line_init_config(line_config *cfg) {
   18	  reset_config(cfg);
   19	}
   20	
   21	int line_read(line_config *cfg, unsigned char *data, int len) {
   22	
   23	  // should do escaping in here, like we do for writes below
   24	  return ip_read(cfg->fd, data, len);
   25	}
   26	
   27	int line_write(line_config *cfg, unsigned char* data, int len) {
   28	  int retval;
   29	  int i = 0;
   30	  int double_iac = FALSE;
   31	  unsigned char text[1024];
   32	  int text_len = 0;
   33	  int mask = 0x7f;
   34	
   35	  if(cfg->is_telnet) {
   36	    if(cfg->nvt_data.binary_xmit) {
   37	      mask = 0xff;
   38	    }
   39	    retval = 0;
   40	    while(i < len) {
   41	      if (double_iac) {
   42	        text[text_len++] = NVT_IAC;
   43	        double_iac = FALSE;
   44	        i++;
   45	      } else {
   46	        if(NVT_IAC == data[i]) {
   47	          text[text_len++] = NVT_IAC;
   48	          double_iac = TRUE;
   49	        } else {
   50	          text[text_len++] = data[i++] & mask;
   51	        }
   52	      }
   53	      if(text_len == sizeof(text)) {
   54	        retval = ip_write(cfg->fd, text, text_len);
   55	        text_len = 0;
   56	      }
   57	    }
   58	    if(text_len) {
   59	      retval = ip_write(cfg->fd, text, text_len);
   60	    }
   61	    return retval;
   62	  }
   63	
   64	  return ip_write(cfg->fd, data, len);
   65	}
   66	
   67	int line_listen(line_config *cfg) {
   68	  return 0;
   69	}
   70	
   71	int line_accept(line_config *cfg) {
   72	  cfg->fd = ip_accept(cfg->sfd);
   73	  if(cfg->fd > -1) {
   74	    LOG(LOG_ALL, "Connection accepted");
   75	    cfg->is_connected = TRUE;
   76	    return 0;
   77	  }
   78	  LOG(LOG_ALL, "Could not accept connection");
   79	  return -1;
   80	}
   81	
   82	int line_off_hook(line_config *cfg) {
   83	  return 0;
   84	}
   85	
   86	int line_connect(line_config *cfg, char *number) {
   87	  char address[PH_ENTRY_SIZE];
   88	
   89	  LOG(LOG_INFO, "Connecting line");
   90	  pb_search(number, address);
   91	  cfg->fd = ip_connect(address);
   92	  if(cfg->fd > -1) {
   93	    LOG(LOG_ALL, "Connected to %s", address);
   94	    cfg->is_connected = TRUE;
   95	    return 0;
   96	  } else {
   97	    LOG(LOG_ALL, "Could not connect to %s",address);
   98	    return -1;
   99	  }
  100	}
  101	
  102	int line_disconnect(line_config *cfg) {
  103	  LOG(LOG_INFO, "Disconnecting line");
  104	  if(cfg->is_connected == TRUE) {
  105	    ip_disconnect(cfg->fd);
  106	  }
  107	  reset_config(cfg);
  108	  return 0;
  109	}
  110	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.