1 
2 /* $Id: comserv.h,v 1.22 2002/06/20 01:52:48 bsd Exp $ */
3 
4 /*
5  * Copyright 2000, 2001, 2002 Brian S. Dean <bsd@bsdhome.com>
6  * All Rights Reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BRIAN S. DEAN ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL BRIAN S. DEAN BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
28  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29  * DAMAGE.
30  *
31  */
32 
33 #ifndef __comserv_h__
34 #define __comserv_h__
35 
36 #include <sys/types.h>
37 #include <sys/time.h>
38 #include <unistd.h>
39 #include <limits.h>
40 
41 #include "cmd.h"
42 
43 
44 /*
45  * debugging options, these are applied to the global 'debug_flags' to
46  * determine which debug messages should be output
47  */
48 #define D_BUFFER  0x0001
49 #define D_STATES  0x0002
50 #define D_CONNECT 0x0004
51 #define D_CMDS    0x0008
52 
53 #define DEBUG_BUFFER()  (debug_flags & D_BUFFER)
54 #define DEBUG_STATES()  (debug_flags & D_STATES)
55 #define DEBUG_CONNECT() (debug_flags & D_CONNECT)
56 #define DEBUG_CMDS()    (debug_flags & D_CMDS)
57 
58 extern unsigned int debug_flags;
59 
60 
61 /*
62  * ENABLE_TELNET_PORT allows incoming telnet connects to issue local
63  * mode commands.  This is dangerous and should only be used if you
64  * are in a secure environment or for debugging purposes.
65  */
66 
67 #if ENABLE_TELNET_PORT
68 
69 #define COMSERV "comserv"  /* /etc/services entry for incoming telnet
70                               command connections */
71 #endif
72 
73 
74 #define DEFAULT_CONFIG "/usr/local/etc/comservd.conf" /* default
75                                                          config file
76                                                          name */
77 
78 #define DEFAULT_LOGDIR "/usr/local/comserv/log" /* default directory
79                                                    for session log
80                                                    files */
81 
82 #define DEFAULT_DEVDIR "/usr/local/comserv/dev" /* default directory
83                                                    for device symlinks */
84 
85 
86 #define MAX_HOST     128  /* max hostname length */
87 
88 #define MAX_DEVID     64  /* max device id length  */
89 
90 #define MAX_SET      512  /* maximum var=value text length for the
91                              'set' command */
92 
93 
94 #ifndef max
95 #define max(a,b) ((a) >= (b) ? (a) : (b))
96 #endif
97 
98 
99 
100 /*
101  * states for the state machine, also serves as an identification of
102  * the connection type
103  */
104 enum {
105   ST_START,
106   ST_INCOMING,
107   ST_SERVICE,
108   ST_CMDINPUT,
109   ST_LOCAL,
110   ST_REMOTE,
111   ST_CONNECTING
112 };
113 
114 
115 /*
116  * Bit masks for modifying various operational behaviours of struct
117  * compserv_port instances.  These apply to the 'flags' field.
118  */
119 #define OPT_WAIT    0x0001 /* 1=don't connect to remote side until
120                               data is available on the local side */
121 #define OPT_LOGALL  0x0002 /* if logging is enabled, 1=log data from
122                               both sides of the connection; 0=log data
123                               only from the remote side */
124 #define OPT_LOGHEX  0x0004 /* if logging is enabled, 1=output log data
125                               in hex and ascii format and identify
126                               data coming from the remote vs the local
127                               side of the connection; 0=display data
128                               in ascii only and don't differentiate
129                               the source */
130 #define OPT_BLOCK  0x0008  /* 1=block the source if the receiver is
131                               not ready; 0=data is lost if the
132                               receiver is not ready, no data is lost
133                               from the log file */
134 
135 #define OPT_DEFAULT (OPT_BLOCK) /* default options */
136 
137 #define RFLAG_LISTEN 0x00001 /* remote endpoint is served via a listen
138                                 socket; the port is the TCP port
139                                 number of the listen socket and
140                                 localpath[] is the name of the /dev
141                                 serial port being served */
142 
143 #define RECONNECT_TIME_MAX (10*60) /* every 10 minutes */
144 
145 /*
146  * Data structure for associated pathnames with terminal server ports
147  */
148 struct comserv_port {
149   COMSERV_PORT * next;                      /* next in linked list */
150   char           devid[MAX_DEVID];          /* device id for this connection */
151   char           localpath [ PATH_MAX ]; /* path name to user connection */
152   char           host [ MAX_HOST ];         /* terminal server host name */
153   int            log;                       /* boolean: log or not */
154   char           logfile [ PATH_MAX ];      /* name of this port's log file */
155   int            logfd;                     /* log file fd */
156   int            serport;                   /* TS serial port number */
157   int            port;                      /* TS TCP/IP port */
158   int            control;                   /* 1=control connection, 0=not */
159   ENDPOINT     * le;                        /* local connection endpoint */
160   unsigned int   flags; /* options flags, see OPT_* #defines */
161   unsigned int   rflags;/* flags specific to the remote endpoint */
162   ENDPOINT     * re;    /* remote connection endpoint */
163   ENDPOINT     * listen;/* listen socket when serving local device */
164   unsigned long  n_le;  /* N bytes received from local endpoint */
165   unsigned long  n_re;  /* N bytes received from remote endpoint */
166   time_t         reconnect_time;
167   int            reconnect_time_incr;
168 };
169 
170 
171 #define ENABLE_RD(fd)    FD_SET(fd, &master_rd)
172 #define ISENABLED_RD(fd) FD_ISSET(fd, &master_rd)
173 #define ENABLE_WR(fd)    FD_SET(fd, &master_wr)
174 #define ISENABLED_WR(fd) FD_ISSET(fd, &master_wr)
175 #define DISABLE_RD(fd)   FD_CLR(fd, &master_rd)
176 #define DISABLE_WR(fd)   FD_CLR(fd, &master_wr)
177 
178 extern COMSERV_PORT * comserv_ports;   /* linked list of defined terminal
179                                           server ports that we know about */
180 
181 extern COMSERV_PORT global; /* default settings for new comserv ports */
182 
183 extern int maxfd;                    /* max file descriptor currently in use */
184 
185 extern fd_set master_rd;             /* the master set of file descriptor
186                                  bits that are selected for
187                                  readability */
188 extern fd_set master_wr;             /* the master set of file descriptor
189                                  bits that are selected for
190                                  writeability */
191 
192 extern int config_fd; /* file descriptor for the configuration file */
193 
194 extern char * progname;
195 
196 extern char config_file[]; /* name of the config file */
197 
198 
199 extern int verbose;
200 
201 #ifdef ENABLE_TELNET_PORT
202 extern int    lsock;          /* listening socket, listens for incoming
203                                  command connections */
204 #endif
205 
206 
207 
208 
209 #define CLEANUP(remote) cleanup(remote,&master_rd,&master_wr,&maxfd)
210 
211 
212 char         * statestr          (int state);
213 int            sock_bind_service (char * service, int srvport);
214 int            sock_bind_connect (char * host, char * service, int srvport,
215                                   int * ready);
216 int            lread             (int fd, char * buf, int len);
217 int            connect_server    (COMSERV_PORT * xp);
218 int            connect_user      (COMSERV_PORT * xp, int doretry);
219 void           reconnect         (int state, COMSERV_PORT * xp, int dopeer);
220 int            make_ctl_port     (COMSERV_PORT * xp);
221 COMSERV_PORT * new_comserv_port  (void);
222 void           free_comserv_port (COMSERV_PORT * xp);
223 
224 #endif
225 
226