My Project

libtelnet.h
Go to the documentation of this file.
1
42#if !defined(LIBTELNET_INCLUDE)
43#define LIBTELNET_INCLUDE 1
44
45/* standard C headers necessary for the libtelnet API */
46#include <stdarg.h>
47#include <stddef.h>
48
49/* C++ support */
50#if defined(__cplusplus)
51 extern "C" {
52#endif
53
54/* printf type checking feature in GCC and some other compilers */
55#if __GNUC__
56# define TELNET_GNU_PRINTF(f,a) __attribute__((format(printf, f, a)))
57# define TELNET_GNU_SENTINEL __attribute__((sentinel))
58#else
59# define TELNET_GNU_PRINTF(f,a)
60# define TELNET_GNU_SENTINEL
61#endif
62
63/* Disable environ macro for Visual C++ 2015. */
64#undef environ
65
67 typedef struct telnet_t telnet_t;
68
70 typedef union telnet_event_t telnet_event_t;
71
73 typedef struct telnet_telopt_t telnet_telopt_t;
74
78#define TELNET_IAC 255
79#define TELNET_DONT 254
80#define TELNET_DO 253
81#define TELNET_WONT 252
82#define TELNET_WILL 251
83#define TELNET_SB 250
84#define TELNET_GA 249
85#define TELNET_EL 248
86#define TELNET_EC 247
87#define TELNET_AYT 246
88#define TELNET_AO 245
89#define TELNET_IP 244
90#define TELNET_BREAK 243
91#define TELNET_DM 242
92#define TELNET_NOP 241
93#define TELNET_SE 240
94#define TELNET_EOR 239
95#define TELNET_ABORT 238
96#define TELNET_SUSP 237
97#define TELNET_EOF 236
103#define TELNET_TELOPT_BINARY 0
104#define TELNET_TELOPT_ECHO 1
105#define TELNET_TELOPT_RCP 2
106#define TELNET_TELOPT_SGA 3
107#define TELNET_TELOPT_NAMS 4
108#define TELNET_TELOPT_STATUS 5
109#define TELNET_TELOPT_TM 6
110#define TELNET_TELOPT_RCTE 7
111#define TELNET_TELOPT_NAOL 8
112#define TELNET_TELOPT_NAOP 9
113#define TELNET_TELOPT_NAOCRD 10
114#define TELNET_TELOPT_NAOHTS 11
115#define TELNET_TELOPT_NAOHTD 12
116#define TELNET_TELOPT_NAOFFD 13
117#define TELNET_TELOPT_NAOVTS 14
118#define TELNET_TELOPT_NAOVTD 15
119#define TELNET_TELOPT_NAOLFD 16
120#define TELNET_TELOPT_XASCII 17
121#define TELNET_TELOPT_LOGOUT 18
122#define TELNET_TELOPT_BM 19
123#define TELNET_TELOPT_DET 20
124#define TELNET_TELOPT_SUPDUP 21
125#define TELNET_TELOPT_SUPDUPOUTPUT 22
126#define TELNET_TELOPT_SNDLOC 23
127#define TELNET_TELOPT_TTYPE 24
128#define TELNET_TELOPT_EOR 25
129#define TELNET_TELOPT_TUID 26
130#define TELNET_TELOPT_OUTMRK 27
131#define TELNET_TELOPT_TTYLOC 28
132#define TELNET_TELOPT_3270REGIME 29
133#define TELNET_TELOPT_X3PAD 30
134#define TELNET_TELOPT_NAWS 31
135#define TELNET_TELOPT_TSPEED 32
136#define TELNET_TELOPT_LFLOW 33
137#define TELNET_TELOPT_LINEMODE 34
138#define TELNET_TELOPT_XDISPLOC 35
139#define TELNET_TELOPT_ENVIRON 36
140#define TELNET_TELOPT_AUTHENTICATION 37
141#define TELNET_TELOPT_ENCRYPT 38
142#define TELNET_TELOPT_NEW_ENVIRON 39
143#define TELNET_TELOPT_MSSP 70
144#define TELNET_TELOPT_COMPRESS 85
145#define TELNET_TELOPT_COMPRESS2 86
146#define TELNET_TELOPT_ZMP 93
147#define TELNET_TELOPT_EXOPL 255
148
149#define TELNET_TELOPT_MCCP2 86
155#define TELNET_TTYPE_IS 0
156#define TELNET_TTYPE_SEND 1
162#define TELNET_ENVIRON_IS 0
163#define TELNET_ENVIRON_SEND 1
164#define TELNET_ENVIRON_INFO 2
165#define TELNET_ENVIRON_VAR 0
166#define TELNET_ENVIRON_VALUE 1
167#define TELNET_ENVIRON_ESC 2
168#define TELNET_ENVIRON_USERVAR 3
174#define TELNET_MSSP_VAR 1
175#define TELNET_MSSP_VAL 2
181#define TELNET_FLAG_PROXY (1<<0)
182#define TELNET_FLAG_NVT_EOL (1<<1)
183
184/* Internal-only bits in option flags */
185#define TELNET_FLAG_TRANSMIT_BINARY (1<<5)
186#define TELNET_FLAG_RECEIVE_BINARY (1<<6)
187#define TELNET_PFLAG_DEFLATE (1<<7)
193 enum telnet_error_t {
194 TELNET_EOK = 0,
195 TELNET_EBADVAL,
196 TELNET_ENOMEM,
197 TELNET_EOVERFLOW,
198 TELNET_EPROTOCOL,
199 TELNET_ECOMPRESS
200};
201 typedef enum telnet_error_t telnet_error_t;
206 enum telnet_event_type_t {
207 TELNET_EV_DATA = 0,
208 TELNET_EV_SEND,
209 TELNET_EV_IAC,
210 TELNET_EV_WILL,
211 TELNET_EV_WONT,
212 TELNET_EV_DO,
213 TELNET_EV_DONT,
214 TELNET_EV_SUBNEGOTIATION,
215 TELNET_EV_COMPRESS,
216 TELNET_EV_ZMP,
217 TELNET_EV_TTYPE,
218 TELNET_EV_ENVIRON,
219 TELNET_EV_MSSP,
220 TELNET_EV_WARNING,
221 TELNET_EV_ERROR
222};
223 typedef enum telnet_event_type_t telnet_event_type_t;
228 struct telnet_environ_t {
229 unsigned char type;
230 char *var;
231 char *value;
232};
233
237 union telnet_event_t {
246 enum telnet_event_type_t type;
247
251 struct data_t {
252 enum telnet_event_type_t _type;
253 const char *buffer;
254 size_t size;
255 } data;
256
260 struct error_t {
261 enum telnet_event_type_t _type;
262 const char *file;
263 const char *func;
264 const char *msg;
265 int line;
266 telnet_error_t errcode;
267 } error;
268
272 struct iac_t {
273 enum telnet_event_type_t _type;
274 unsigned char cmd;
275 } iac;
276
280 struct negotiate_t {
281 enum telnet_event_type_t _type;
282 unsigned char telopt;
283 } neg;
284
288 struct subnegotiate_t {
289 enum telnet_event_type_t _type;
290 const char *buffer;
291 size_t size;
292 unsigned char telopt;
293 } sub;
294
298 struct zmp_t {
299 enum telnet_event_type_t _type;
300 const char **argv;
301 size_t argc;
302 } zmp;
303
307 struct ttype_t {
308 enum telnet_event_type_t _type;
309 unsigned char cmd;
310 const char* name;
311 } ttype;
312
316 struct compress_t {
317 enum telnet_event_type_t _type;
318 unsigned char state;
320 } compress;
321
325 struct environ_t {
326 enum telnet_event_type_t _type;
327 const struct telnet_environ_t *values;
328 size_t size;
329 unsigned char cmd;
330 } environ;
331
335 struct mssp_t {
336 enum telnet_event_type_t _type;
337 const struct telnet_environ_t *values;
338 size_t size;
339 } mssp;
340};
341
354 typedef void (*telnet_event_handler_t)(telnet_t *telnet,
355 telnet_event_t *event, void *user_data);
356
360 struct telnet_telopt_t {
361 short telopt;
362 unsigned char us;
363 unsigned char him;
364};
365
369 struct telnet_t;
370
384 extern telnet_t* telnet_init(const telnet_telopt_t *telopts,
385 telnet_event_handler_t eh, unsigned char flags, void *user_data);
386
396 extern void telnet_free(telnet_t *telnet);
397
410 extern void telnet_recv(telnet_t *telnet, const char *buffer,
411 size_t size);
412
419 extern void telnet_iac(telnet_t *telnet, unsigned char cmd);
420
432 extern void telnet_negotiate(telnet_t *telnet, unsigned char cmd,
433 unsigned char opt);
434
442 extern void telnet_send(telnet_t *telnet,
443 const char *buffer, size_t size);
444
453 extern void telnet_send_text(telnet_t *telnet,
454 const char *buffer, size_t size);
455
466 extern void telnet_begin_sb(telnet_t *telnet,
467 unsigned char telopt);
468
477#define telnet_finish_sb(telnet) telnet_iac((telnet), TELNET_SE)
478
492 extern void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt,
493 const char *buffer, size_t size);
494
507 extern void telnet_begin_compress2(telnet_t *telnet);
508
523 extern int telnet_printf(telnet_t *telnet, const char *fmt, ...)
524 TELNET_GNU_PRINTF(2, 3);
525
531extern int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va);
532
544extern int telnet_raw_printf(telnet_t *telnet, const char *fmt, ...)
545 TELNET_GNU_PRINTF(2, 3);
546
552extern int telnet_raw_vprintf(telnet_t *telnet, const char *fmt, va_list va);
553
567extern void telnet_begin_newenviron(telnet_t *telnet, unsigned char type);
568
580extern void telnet_newenviron_value(telnet_t* telnet, unsigned char type,
581 const char *string);
582
591#define telnet_finish_newenviron(telnet) telnet_finish_sb((telnet))
592
600 extern void telnet_ttype_send(telnet_t *telnet);
601
632 extern void telnet_ttype_is(telnet_t *telnet, const char* ttype);
633
641 extern void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv);
642
651 extern void telnet_send_zmpv(telnet_t *telnet, ...) TELNET_GNU_SENTINEL;
652
658extern void telnet_send_vzmpv(telnet_t *telnet, va_list va);
659
666extern void telnet_begin_zmp(telnet_t *telnet, const char *cmd);
667
674extern void telnet_zmp_arg(telnet_t *telnet, const char *arg);
675
684#define telnet_finish_zmp(telnet) telnet_finish_sb((telnet))
685
686/* C++ support */
687#if defined(__cplusplus)
688} /* extern "C" */
689#endif
690
691#endif /* !defined(LIBTELNET_INCLUDE) */
telnet_send_zmp
void telnet_send_zmp(telnet_t *telnet, size_t argc, const char **argv)
Send a ZMP command.
telnet_raw_printf
int telnet_raw_printf(telnet_t *telnet, const char *fmt,...) TELNET_GNU_PRINTF(2
Send formatted data (no newline escaping).
telnet_begin_compress2
void telnet_begin_compress2(telnet_t *telnet)
Begin sending compressed data.
telnet_send_text
void telnet_send_text(telnet_t *telnet, const char *buffer, size_t size)
telnet_send_zmpv
void telnet_send_zmpv(telnet_t *telnet,...) TELNET_GNU_SENTINEL
Send a ZMP command.
telnet_newenviron_value
void telnet_newenviron_value(telnet_t *telnet, unsigned char type, const char *string)
Send a NEW-ENVIRON variable name or value.
telnet_ttype_send
void telnet_ttype_send(telnet_t *telnet)
Send the TERMINAL-TYPE SEND command.
telnet_vprintf
int int telnet_vprintf(telnet_t *telnet, const char *fmt, va_list va)
Send formatted data.
telnet_begin_zmp
void telnet_begin_zmp(telnet_t *telnet, const char *cmd)
Begin sending a ZMP command.
telnet_negotiate
void telnet_negotiate(telnet_t *telnet, unsigned char cmd, unsigned char opt)
Send negotiation command.
telnet_event_type_t
telnet_event_type_t
Definition: libtelnet.h:206
TELNET_EV_WARNING
@ TELNET_EV_WARNING
Definition: libtelnet.h:220
TELNET_EV_ERROR
@ TELNET_EV_ERROR
Definition: libtelnet.h:221
TELNET_EV_IAC
@ TELNET_EV_IAC
Definition: libtelnet.h:209
TELNET_EV_WILL
@ TELNET_EV_WILL
Definition: libtelnet.h:210
TELNET_EV_COMPRESS
@ TELNET_EV_COMPRESS
Definition: libtelnet.h:215
TELNET_EV_TTYPE
@ TELNET_EV_TTYPE
Definition: libtelnet.h:217
TELNET_EV_DO
@ TELNET_EV_DO
Definition: libtelnet.h:212
TELNET_EV_ENVIRON
@ TELNET_EV_ENVIRON
Definition: libtelnet.h:218
TELNET_EV_ZMP
@ TELNET_EV_ZMP
Definition: libtelnet.h:216
TELNET_EV_WONT
@ TELNET_EV_WONT
Definition: libtelnet.h:211
TELNET_EV_DONT
@ TELNET_EV_DONT
Definition: libtelnet.h:213
TELNET_EV_SEND
@ TELNET_EV_SEND
Definition: libtelnet.h:208
TELNET_EV_SUBNEGOTIATION
@ TELNET_EV_SUBNEGOTIATION
Definition: libtelnet.h:214
TELNET_EV_MSSP
@ TELNET_EV_MSSP
Definition: libtelnet.h:219
TELNET_EV_DATA
@ TELNET_EV_DATA
Definition: libtelnet.h:207
telnet_send_vzmpv
void telnet_send_vzmpv(telnet_t *telnet, va_list va)
Send a ZMP command.
telnet_free
void telnet_free(telnet_t *telnet)
Free up any memory allocated by a state tracker.
telnet_begin_sb
void telnet_begin_sb(telnet_t *telnet, unsigned char telopt)
Begin a sub-negotiation command.
TELNET_GNU_PRINTF
#define TELNET_GNU_PRINTF(f, a)
Definition: libtelnet.h:59
telnet_recv
void telnet_recv(telnet_t *telnet, const char *buffer, size_t size)
Push a byte buffer into the state tracker.
telnet_begin_newenviron
void telnet_begin_newenviron(telnet_t *telnet, unsigned char type)
Begin a new set of NEW-ENVIRON values to request or send.
telnet_error_t
telnet_error_t
Definition: libtelnet.h:193
TELNET_ECOMPRESS
@ TELNET_ECOMPRESS
Definition: libtelnet.h:199
TELNET_EBADVAL
@ TELNET_EBADVAL
Definition: libtelnet.h:195
TELNET_EOVERFLOW
@ TELNET_EOVERFLOW
Definition: libtelnet.h:197
TELNET_EOK
@ TELNET_EOK
Definition: libtelnet.h:194
TELNET_EPROTOCOL
@ TELNET_EPROTOCOL
Definition: libtelnet.h:198
TELNET_ENOMEM
@ TELNET_ENOMEM
Definition: libtelnet.h:196
telnet_iac
void telnet_iac(telnet_t *telnet, unsigned char cmd)
Send a telnet command.
telnet_ttype_is
void telnet_ttype_is(telnet_t *telnet, const char *ttype)
Send the TERMINAL-TYPE IS command.
telnet_raw_vprintf
int int telnet_raw_vprintf(telnet_t *telnet, const char *fmt, va_list va)
Send formatted data (no newline escaping).
TELNET_GNU_SENTINEL
#define TELNET_GNU_SENTINEL
Definition: libtelnet.h:60
telnet_zmp_arg
void telnet_zmp_arg(telnet_t *telnet, const char *arg)
Send a ZMP command argument.
telnet_printf
int telnet_printf(telnet_t *telnet, const char *fmt,...) TELNET_GNU_PRINTF(2
Send formatted data.
telnet_send
void telnet_send(telnet_t *telnet, const char *buffer, size_t size)
telnet_t
struct telnet_t telnet_t
Definition: libtelnet.h:67
telnet_subnegotiation
void telnet_subnegotiation(telnet_t *telnet, unsigned char telopt, const char *buffer, size_t size)
Shortcut for sending a complete subnegotiation buffer.
telnet_event_handler_t
void(* telnet_event_handler_t)(telnet_t *telnet, telnet_event_t *event, void *user_data)
event handler
Definition: libtelnet.h:354
telnet_init
telnet_t * telnet_init(const telnet_telopt_t *telopts, telnet_event_handler_t eh, unsigned char flags, void *user_data)
Initialize a telnet state tracker.
telnet_environ_t
Definition: libtelnet.h:228
telnet_environ_t::type
unsigned char type
Definition: libtelnet.h:229
telnet_environ_t::value
char * value
Definition: libtelnet.h:231
telnet_environ_t::var
char * var
Definition: libtelnet.h:230
telnet_event_t::compress_t
Definition: libtelnet.h:316
telnet_event_t::compress_t::state
unsigned char state
Definition: libtelnet.h:318
telnet_event_t::compress_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:317
telnet_event_t::data_t
Definition: libtelnet.h:251
telnet_event_t::data_t::size
size_t size
Definition: libtelnet.h:254
telnet_event_t::data_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:252
telnet_event_t::data_t::buffer
const char * buffer
Definition: libtelnet.h:253
telnet_event_t::environ_t
Definition: libtelnet.h:325
telnet_event_t::environ_t::values
const struct telnet_environ_t * values
Definition: libtelnet.h:327
telnet_event_t::environ_t::cmd
unsigned char cmd
Definition: libtelnet.h:329
telnet_event_t::environ_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:326
telnet_event_t::environ_t::size
size_t size
Definition: libtelnet.h:328
telnet_event_t::error_t
Definition: libtelnet.h:260
telnet_event_t::error_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:261
telnet_event_t::error_t::line
int line
Definition: libtelnet.h:265
telnet_event_t::error_t::func
const char * func
Definition: libtelnet.h:263
telnet_event_t::error_t::file
const char * file
Definition: libtelnet.h:262
telnet_event_t::error_t::msg
const char * msg
Definition: libtelnet.h:264
telnet_event_t::error_t::errcode
telnet_error_t errcode
Definition: libtelnet.h:266
telnet_event_t::iac_t
Definition: libtelnet.h:272
telnet_event_t::iac_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:273
telnet_event_t::iac_t::cmd
unsigned char cmd
Definition: libtelnet.h:274
telnet_event_t::mssp_t
Definition: libtelnet.h:335
telnet_event_t::mssp_t::values
const struct telnet_environ_t * values
Definition: libtelnet.h:337
telnet_event_t::mssp_t::size
size_t size
Definition: libtelnet.h:338
telnet_event_t::mssp_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:336
telnet_event_t::negotiate_t
Definition: libtelnet.h:280
telnet_event_t::negotiate_t::telopt
unsigned char telopt
Definition: libtelnet.h:282
telnet_event_t::negotiate_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:281
telnet_event_t::subnegotiate_t
Definition: libtelnet.h:288
telnet_event_t::subnegotiate_t::buffer
const char * buffer
Definition: libtelnet.h:290
telnet_event_t::subnegotiate_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:289
telnet_event_t::subnegotiate_t::telopt
unsigned char telopt
Definition: libtelnet.h:292
telnet_event_t::subnegotiate_t::size
size_t size
Definition: libtelnet.h:291
telnet_event_t::ttype_t
Definition: libtelnet.h:307
telnet_event_t::ttype_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:308
telnet_event_t::ttype_t::cmd
unsigned char cmd
Definition: libtelnet.h:309
telnet_event_t::ttype_t::name
const char * name
Definition: libtelnet.h:310
telnet_event_t::zmp_t
Definition: libtelnet.h:298
telnet_event_t::zmp_t::argv
const char ** argv
Definition: libtelnet.h:300
telnet_event_t::zmp_t::_type
enum telnet_event_type_t _type
Definition: libtelnet.h:299
telnet_event_t::zmp_t::argc
size_t argc
Definition: libtelnet.h:301
telnet_telopt_t
Definition: libtelnet.h:360
telnet_telopt_t::us
unsigned char us
Definition: libtelnet.h:362
telnet_telopt_t::him
unsigned char him
Definition: libtelnet.h:363
telnet_telopt_t::telopt
short telopt
Definition: libtelnet.h:361
telnet_event_t
Definition: libtelnet.h:237
telnet_event_t::type
enum telnet_event_type_t type
Event type.
Definition: libtelnet.h:246