modemu2k
Library that provides telnet capability to a comm program
modemu2k.h
Go to the documentation of this file.
1/*
2 *
3 * Copyright 2018 Andy Alt <andy400-dev@yahoo.com>
4 *
5 * modemu2k is a fork of modemu
6 * Originally developed by Toru Egashira
7 * Copyright (c) 1995, 1996
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02110-1301, USA.
23 *
24 *
25 */
26
32#include <stdio.h> /*stderr,(sscanf,sprintf) */
33#include <string.h> /*(strncpy) */
34#include <stdbool.h>
35#include <sys/types.h>
36#include <sys/time.h>
37
38#ifdef TERMNET
39#include <termnet.h>
40#endif
41
42#ifdef USE_FILIO_H
43#include <sys/filio.h>
44#else
45#include <sys/ioctl.h>
46#endif
47
48#include "cmdarg.h"
49
50/* TODO: the API shouldn't be localized and really shouldn't contain any strings;
51 * but rather the functions should return values to the application can print
52 * any messages to stdout or stderr
53 */
54#define ENABLE_NLS 1
55#include <locale.h>
56#include "gettext.h"
57#define _(String) gettext (String)
58#define gettext_noop(String) String
59#define N_(String) gettext_noop (String)
60
61/* socket read buffer size */
62#define SOCKBUFR_SIZE 1024
63
64/* tty read buffer size */
65#define TTYBUFR_SIZE 1024
66
67/* line (!char) mode line-length */
68#define LINEBUF_SIZE 256
69
70/* command mode line-length (w/o null) */
71#define CMDBUF_MAX 255
72
73typedef unsigned char uchar;
74
75#if defined(__GLIBC__) || defined(SVR4)
76#define HAVE_GRANTPT
77#endif
78
79#define DEFAULT_PORT 23
80
81// sock
82
83typedef struct st_sock
84{
86 int fd;
87
89 int alive;
90
92 struct addrinfo *rp;
93} st_sock;
94
95//extern st_sock sock;
96
97void sockInit (struct st_sock *sock);
98int sockClose (st_sock *sock);
99int sockShutdown (st_sock *sock);
100
101
109int m2k_sockDial (st_sock *sock);
110
111// atcmd
112
113typedef enum
114{
115 ATDA_NUM,
116 ATDA_STR
117} AtdAType;
118
119typedef enum
120{
121 ATDP_NUL,
122 ATDP_NUM,
123 ATDP_STR
124} AtdPType;
125
126#define ADDR_MAX 63
127#define PORT_MAX 63
128#define PT_MAX 40
129#define SREG_MAX 12
130
131typedef struct
132{
133 struct
134 {
135 struct
136 {
137 char str[ADDR_MAX + 1];
138 AtdAType type;
139 } addr;
140 struct
141 {
142 char str[PORT_MAX + 1];
143 AtdPType type;
144 } port;
145 } d;
146 int f;
147 unsigned char s[SREG_MAX + 1];
148 int pb[2];
149 int pd;
150 int pl;
151 int pr;
152 struct
153 {
154 char str[PT_MAX + 1];
155 int len;
156 int wont;
157 } pt;
158 int pv;
159} Atcmd;
160
161extern Atcmd atcmd;
162extern Atcmd atcmdNV;
163
164#define CHAR_ESC (atcmd.s[2])
165#define CHAR_CR (atcmd.s[3])
166#define CHAR_LF (atcmd.s[4])
167#define CHAR_BS (atcmd.s[5])
168
169void atcmdInit (struct st_cmdarg *cmdarg, st_sock *sock);
170
171
184void m2k_atcmdD (const char *s, AtdAType at, AtdPType pt);
185
186
187int atcmdFake (const char *s, const char *vals);
188int atcmdH (const char *s, st_sock *sock);
189int atcmdI (const char *s);
190int atcmdSQuery (const char *s);
191int atcmdSSet (const char *s);
192void atcmdZ (st_sock *sock);
193void atcmdAW (void);
194int atcmdPB (const char *s);
195int atcmdPD (const char *s);
196int atcmdPL (const char *s);
197void atcmdPQ (st_sock *sock);
198int atcmdPR (const char *s);
199int atcmdPT (const char *s);
200int atcmdPTSet (const char *s);
201int atcmdPV (const char *s);
202
203// commx
204#ifdef HAVE_GRANTPT
205void commxForkExec (const char *cmd, char *ptyslave);
206#else
207void commxForkExec (const char *cmd, char c10, char c01);
208#endif
209
210// sockbuf
211
212
213/* reading socket */
214
215extern struct st_sockBufR
216{
217 uchar buf[SOCKBUFR_SIZE];
218 uchar *ptr;
219 uchar *end;
220} sockBufR;
221
222
223void
224sockBufRReset(void);
225
226int
227getSock1(void);
228
229void sockBufRead (st_sock *sock);
230
231
232/* writing socket */
233
234#define SOCKBUFW_SIZE (2 * TTYBUFR_SIZE) /* this seems to be any number */
235#define SOCKBUFW_SIZE_A (SOCKBUFW_SIZE + TTYBUFR_SIZE) /* important */
236
237extern struct st_sockBufW
238{
239 uchar buf[SOCKBUFW_SIZE_A];
240 uchar *top;
241 uchar *ptr;
242 int stop;
243} sockBufW;
244
245void
246sockBufWReset(void);
247
248bool
249sockBufWHasData(void);
250
251bool
252sockBufWReady(void);
253
254void sockBufWrite (st_sock *sock);
255void putSock1 (uchar c);
256void putSockN (const uchar * cp, int n);
257
258// stty
259
260void setTty (void);
261
262// telopt
263
264/* requirements for a telnet option */
265typedef enum
266{
267 TOR_MUSTNOT, /* disable the opt or disconnect */
268 TOR_BETTERNOT, /* disable the opt or enable unwillingly */
269 TOR_NEUTRAL, /* modemu doesn't initiate any action for the opt.
270 {en,dis}able the opt as the peer requests. */
271 TOR_BETTER, /* enable the opt or disable unwillingly */
272 TOR_MUST /* enable the opt or disconnect */
273} TelOptReq;
274
275typedef struct
276{
277 TelOptReq req; /* requirement for the opt */
278 int state; /* current state (enabled:1 or disabled:0) */
279 int pending; /* state is pending (requested but no reply yet) */
281
282typedef struct
283{
284 int opt; /* an telnet option. TELOPT_XXX */
285 TelOptState local; /* local state of the option */
286 TelOptState remote; /* remote state of the option */
288
289extern struct st_telOpt
290{
291 int binsend; /* local binary opt is enabled */
292 int binrecv; /* remote binary opt is enabled */
293 int sgasend; /* local SGA opt is enabled (char-at-a-time mode) */
294 int sentReqs; /* have sent option requests to the peer
295 or skip sending them */
296 TelOptStates **stTab; /* = stTab[] in telopt.c */
297} telOpt;
298
299#define putOptCmd(s,c) { putSock1(IAC); putSock1(s); putSock1(c); }
300
301void telOptReset (void);
302void telOptInit (void);
303void telOptPrintCmd (const char *str, int cmd);
304void telOptSendReqs (void);
305int telOptHandle (int cmd, int opt);
306int telOptSBHandle (int opt);
307
308// timeval
309
310void timevalSet10ms (struct timeval *ap, int b);
311void timevalAdd (struct timeval *ap, const struct timeval *bp);
312void timevalSub (struct timeval *ap, const struct timeval *bp);
313int timevalCmp (const struct timeval *ap, const struct timeval *bp);
314
315// ttybuf
316
317struct st_tty
318{
319 int rfd;
320 int wfd;
321};
322
323extern struct st_tty tty;
324
325
326/* reading tty */
327
329{
330 unsigned char buf[TTYBUFR_SIZE];
331 unsigned char *ptr;
332 unsigned char *end;
333 struct timeval newT;
334 struct timeval prevT;
335};
336
337extern struct st_ttyBufR ttyBufR;
338
339void
340ttyBufRReset(void);
341
342int
343getTty1(void);
344
345void ttyBufRead (st_sock *sock);
346
347
348/* writing tty */
349
350#define TTYBUFW_SIZE (2 * SOCKBUFR_SIZE) /* this seems to be any number */
351#define TTYBUFW_SIZE_A (TTYBUFW_SIZE + SOCKBUFR_SIZE) /* important */
352
354{
355 uchar buf[TTYBUFW_SIZE_A];
356 uchar *top;
357 uchar *ptr;
358 int stop;
359};
360
361extern struct st_ttyBufW ttyBufW;
362
363#define ttyBufWReset() { \
364 ttyBufW.ptr = ttyBufW.top = ttyBufW.buf; \
365 ttyBufW.stop = 0; \
366}
367#define ttyBufWHasData() (ttyBufW.ptr > ttyBufW.buf)
368#define ttyBufWReady() (!ttyBufW.stop)
369#define putTtyStr(s) putTtyN(s, sizeof(s)-1)
370
371void ttyBufWrite (st_sock *sock);
372void putTty1 (unsigned char c);
373void putTtyN (const char *cp, int n);
374
375// utils
376
377void *chk_alloc (void *ptr);
378
379// verbose
380
381#define VERB_MISC 1
382#define VERB_TELOPT 2
383
384void verboseOut (int mask, const char *format, ...);
385void verbosePerror (int mask, const char *s);
386
387// cmdlex
388
389typedef enum
390{
391 CMDST_OK,
392 CMDST_ERROR,
393 CMDST_CONNECT,
394 CMDST_NOCARRIER,
395 CMDST_NOAT,
396 CMDST_ATD,
397 CMDST_ATO
398} Cmdstat;
399
400Cmdstat cmdLex (const char *ptr, st_sock *sock);
401
int m2k_sockDial(st_sock *sock)
Definition: sock.c:64
void m2k_atcmdD(const char *s, AtdAType at, AtdPType pt)
Definition: atcmd.c:77
Definition: modemu2k.h:132
Definition: modemu2k.h:276
Definition: modemu2k.h:283
Definition: cmdarg.h:32
Definition: modemu2k.h:216
Definition: modemu2k.h:238
Definition: modemu2k.h:84
struct addrinfo * rp
Definition: modemu2k.h:92
int alive
Definition: modemu2k.h:89
int fd
Definition: modemu2k.h:86
Definition: modemu2k.h:290
Definition: modemu2k.h:329
Definition: modemu2k.h:354
Definition: modemu2k.h:318