File: solterm.c

package info (click to toggle)
openipmi 2.0.25-2.1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 11,560 kB
  • sloc: ansic: 150,609; python: 8,801; sh: 5,752; perl: 1,356; makefile: 561
file content (909 lines) | stat: -rw-r--r-- 22,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
/*
 * solterm.c
 *
 * IPMI Serial-over-LAN Terminal application
 *
 * Author: Cyclades Australia Pty. Ltd.
 *         Darius Davis <dariusd@users.sourceforge.net>
 *
 * Copyright 2005 Cyclades Australia Pty. Ltd.
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this program; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <netdb.h>
#include <ctype.h>
#include <time.h>
#include <signal.h>

#include <OpenIPMI/ipmiif.h>
#include <OpenIPMI/ipmi_smi.h>
#include <OpenIPMI/ipmi_err.h>
#include <OpenIPMI/ipmi_auth.h>
#include <OpenIPMI/ipmi_lan.h>
#include <OpenIPMI/ipmi_posix.h>

#include <OpenIPMI/internal/ipmi_int.h>
#include <OpenIPMI/ipmi_sol.h>
#include <OpenIPMI/ipmi_debug.h>


#undef USE_SUSPEND

/*
 * Usage:
 *
 *	solterm -U username -P password [-Ra auth] [-Ri integ] [-Rc conf] hostname [-e escape_char]
 *		[-notencrypted] [-notauthenticated] [-bitrate (9600|19200|38400|57600|115200)]
 *		[-alerts (succeed|defer|fail)] [-holdoff] [-v] [-q]
 *
 *	-Ra auth	Specify authentication algorithm to use.
 *	-Ri integ	Specify integrity algorithm to use.
 *	-Rc conf	Specify confidentiality algorithm to use.
 *	-e escape_char	The escape character to use (Default: ~).
 *	-notencrypted	Specify that SoL packets should not be encrypted.
 *	-notauthenticated Specify that SoL packets should not be authenticated.
 *	-bitrate xxx	Specify bit rate to use.  BMCs are not required to
 *			support all bit rates.  Make sure this matches the bit
 *			rate at which the baseboard is communicating!
 *	-alerts succeed	Specify that serial/modem alerts are to succeed while SoL is active.
 *	-alerts deferred	Serial/modem alerts are to be deferred for the duration of the SoL session.
 *	-alerts fail	Serial/modem alerts automatically fail during the SoL session.
 *	-holdoff	Specifies that CTS, DTR and DSR are to be deasserted at the start of the SoL session,
 *			so that the configuration may be modified before the handshake is released.
 */


typedef enum {
	condition_running,
	condition_exiting,
	condition_exit_now
} exit_condition_t;

typedef enum {
	escape_waiting_for_CR,
	escape_waiting_for_escape,
	escape_next
} escape_status_t;

char			escape_char = '~';
escape_status_t		escape_status = escape_waiting_for_escape;

struct termios orig_attr;
static const char *progname;

exit_condition_t	exit_condition;
ipmi_sol_conn_t		*active_connection = NULL;
int			connection_up = 0;
int verbosity;

#define INITIAL_VERBOSITY 10

typedef struct {
	int authenticated, encrypted;
	int bit_rate;
	ipmi_sol_serial_alert_behavior alert_behavior;
	int holdoff;
	int ACK_timeout_usec;
	int ACK_retries;
} sol_configuration_t;



static void
my_vlog(os_handler_t *handler, const char *format,
	enum ipmi_log_type_e log_type, va_list ap)
{
    int do_nl = 1;

    switch(log_type)
    {
	case IPMI_LOG_INFO:
		if (verbosity < 3) return;
	    printf("INFO: ");
	    break;

	case IPMI_LOG_WARNING:
		if (verbosity < 1) return;
	    printf("WARN: ");
	    break;

	case IPMI_LOG_SEVERE:
	    printf("SEVR: ");
	    break;

	case IPMI_LOG_FATAL:
	    printf("FATL: ");
	    break;

	case IPMI_LOG_ERR_INFO:
		if (verbosity < 2) return;
	    printf("EINF: ");
	    break;

	case IPMI_LOG_DEBUG_START:
		if (verbosity < 2) return;
	    do_nl = 0;
	    /* FALLTHROUGH */
	case IPMI_LOG_DEBUG:
		if (verbosity < 2) return;
	    printf("DEBG: ");
	    break;

	case IPMI_LOG_DEBUG_CONT:
		if (verbosity < 2) return;
	    do_nl = 0;
	    /* FALLTHROUGH */
	case IPMI_LOG_DEBUG_END:
		if (verbosity < 2) return;
	    break;
    }

    vprintf(format, ap);

    if (do_nl)
	printf("\n");
}

void con_usage(const char *name, const char *help, void *cb_data)
{
    if (strcmp(name, "lan") != 0)
	/* Only supports LAN. */
	return;
    printf("\n%s%s\n", name, help);
}

static void
usage(void)
{
    printf("Usage:\n"
	   "  %s <conparms>\n"
"	[-e escape_char] [-notencrypted] [-notauthenticated]\n"
"	[-bitrate (9600|19200|38400|57600|115200)]\n"
"	[-alerts (succeed|defer|fail)] [-holdoff] [-ack-retries n]\n"
"	[-ack-timeout usec] [-v] [-q]\n\n"
"-e escape_char	The escape character to use.  Default is ~.\n"
"-notencrypted	Specify that SoL packets should not be encrypted.\n"
"-notauthenticated Specify that SoL packets should not be authenticated.\n"
"-bitrate xxx	Specify bit rate to use.  BMCs are not required to support all\n"
"		bit rates.  Make sure this matches the bit rate at which the\n"
"		baseboard is communicating!  Defaults to the BMC's configured\n"
"		nonvolatile bit rate.\n"
"-alerts succeed  Specify that serial/modem alerts are to succeed while\n"
"               SoL is active.\n"
"-alerts deferred  Serial/modem alerts are to be deferred for the duration\n"
"               of the SoL session.\n"
"-alerts fail	Serial/modem alerts automatically fail during the SoL session.\n"
"               This is the default.\n"
"-holdoff	Specifies that CTS, DTR and DSR are to be deasserted at the\n"
"               start of the SoL session,so that the configuration may be\n"
"               modified before the handshake is released.\n"
"-v		Be more verbose.  May be specified multiple times.\n"
"-q		Be quieter.  Opposite of -v.\n"
"\n<conparms> specified connection parameters for solterm.  Note that only\n"
"lan connection are supported for solterm.  These parms are:\n", progname);
    ipmi_parse_args_iter_help(con_usage, NULL);
}

static void show_buffer_text(const void *data, size_t count)
{
	unsigned int i;

	printf("[");
	for (i = 0; i < count; ++i)
	{
		char c = ((unsigned char *)data)[i];
		if (isprint(c))
			putchar(c);
		else
			putchar('.');
	}

	printf("]");
	fflush(stdout);
}

static void show_buffer_hex(const void *data, size_t count)
{
	unsigned int i;

	printf("[");
	for (i = 0; i < count; ++i)
		printf("%02x", ((unsigned char *)data)[i]);

	printf("]");
	fflush(stdout);
}

static int data_received(ipmi_sol_conn_t *conn, const void *data, size_t count, void *user_data)
{
	unsigned int i;
	if (verbosity > 3)
		show_buffer_text(data, count);
	if (verbosity > 5)
		show_buffer_hex(data, count);

	for (i = 0; i < count; ++i)
		printf("%c", ((unsigned char *)data)[i]);
	fflush(stdout);
	return 0;
}


static void break_detected(ipmi_sol_conn_t *conn, void *user_data)
{
	tcsendbreak(1, 0);
}

static void bmc_transmit_overrun(ipmi_sol_conn_t *conn, void *user_data)
{
	if (verbosity < 0)
		return;
	fprintf(stderr, "[BMC-BUFFER-OVERRUN]");
	fflush(stderr);
}

/*typedef enum { ipmi_sol_state_closed, ipmi_sol_state_connecting, ipmi_sol_state_connected,
	ipmi_sol_state_connected_ctu, ipmi_sol_state_closing } ipmi_sol_state;*/
char *state_names[5] = {"Closed", "Connecting", "Connected", "Connected (Char Trans Unavail)", "Closing"};


#define ERROR_STRING_LEN 50
static void connection_state(ipmi_sol_conn_t *conn, ipmi_sol_state state, int error, void *cb_data)
{
	char error_string[ERROR_STRING_LEN];

	ipmi_get_error_string(error,
			error_string,
			ERROR_STRING_LEN);

	if ((verbosity > 2) && (state == ipmi_sol_state_closed))
		printf("===============================================================================\n");

	ipmi_log((error ? IPMI_LOG_SEVERE : IPMI_LOG_INFO),
		"Connection state changed: %s; Reason: %s", state_names[state], error_string);

	if ((verbosity > 2) && (state == ipmi_sol_state_connected || state == ipmi_sol_state_connected_ctu))
		printf("===============================================================================\n");

	if ((state == ipmi_sol_state_connected || state == ipmi_sol_state_connected_ctu) && !connection_up)
	{
		if (verbosity >= 0)
		{
			printf("Connected.  Escape character is %c.  %c? for help; %c. to disconnect.\n", escape_char, escape_char, escape_char);
			fflush(stdout);
		}
		connection_up = 1;
	}

	if (state == ipmi_sol_state_closed)
		exit_condition = condition_exit_now;

	fflush(stdout);
}

static void transmit_complete(ipmi_sol_conn_t *conn, int error, void *user_data)
{
	if (!error)
	{
		if (verbosity > 5)
		{
			printf("[TC]");
			fflush(stdout);
		}
		return;
	}

	if (IPMI_IS_SOL_ERR(error) && (IPMI_GET_SOL_ERR(error) == IPMI_SOL_UNCONFIRMABLE_OPERATION))
	{
		if (verbosity > 1)
			fprintf(stderr, "[Unconfirmable]");
	}
	else
	{
		/*
		 * Transmission failed
		 */
		char buf[50];
		fprintf(stderr, "[Pkt lost: %s]", ipmi_get_error_string(error, buf, 50));
		fflush(stderr);
	}
}

static void flush_complete(ipmi_sol_conn_t *conn, int error, int queues_flushed, void *user_data)
{
	transmit_complete(conn, error, user_data);
}

static void sol_send(const char *text, int count)
{
	int rv;

	if (verbosity > 5)
		show_buffer_hex(text, count);

	rv = ipmi_sol_write(active_connection, text, count, transmit_complete, NULL);

	if (rv)
	{
		char buf[50];
		fprintf(stderr, "[TX err: %s]", ipmi_get_error_string(rv, buf, 50));
	}
}

static void sol_send_break()
{
	int rv = ipmi_sol_send_break(active_connection, transmit_complete, NULL);

	if (rv)
	{
		char buf[50];
		fprintf(stderr, "[TX err: %s]", ipmi_get_error_string(rv, buf, 50));
	}
}

static void sol_do_flush()
{
	int rv = ipmi_sol_flush(active_connection, IPMI_SOL_ALL_QUEUES, flush_complete, NULL);

	if (rv)
	{
		char buf[50];
		fprintf(stderr, "[TX err: %s]", ipmi_get_error_string(rv, buf, 50));
	}
}

static void unconfigure_terminal()
{
	tcsetattr(STDIN_FILENO, TCSANOW, &orig_attr);
	signal(SIGINT, SIG_DFL);
	signal(SIGPIPE, SIG_DFL);
}
	
static void signal_handler(int signal)
{
	unconfigure_terminal();
	exit(1);
}

static void configure_terminal()
{
	struct termios attr;
	tcgetattr(STDIN_FILENO, &attr);
	tcgetattr(STDIN_FILENO, &orig_attr);

	attr.c_iflag &= ~(/*IGNBRK|BRKINT|PARMRK|*/ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXANY|IXOFF);
/*	attr.c_oflag &= ~OPOST;*/
	attr.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN | ECHOE | ECHOK);
	attr.c_cflag &= ~(CSIZE|PARENB);
	attr.c_cflag |= CS8;
	attr.c_cc[VTIME] = 0;
	attr.c_cc[VMIN] = 0;

	tcsetattr(STDIN_FILENO, TCSANOW, &attr);
	signal(SIGINT, signal_handler);
	signal(SIGPIPE, signal_handler);
}

static int configure_sol()
{
	int rv;

	rv = ipmi_sol_register_data_received_callback(active_connection, data_received, NULL);
	if (rv)
		ipmi_log(IPMI_LOG_SEVERE, "Error registering for data_received event: %s.", strerror(rv));

	rv = ipmi_sol_register_break_detected_callback(active_connection, break_detected, NULL);
	if (rv)
		ipmi_log(IPMI_LOG_SEVERE, "Error registering for break_detected event: %s.", strerror(rv));

	rv = ipmi_sol_register_bmc_transmit_overrun_callback(active_connection, bmc_transmit_overrun, NULL);
	if (rv)
		ipmi_log(IPMI_LOG_SEVERE, "Error registering for bmc_transmit_overrun event: %s.", strerror(rv));

	rv = ipmi_sol_register_connection_state_callback(active_connection, connection_state, NULL);
	if (rv)
		ipmi_log(IPMI_LOG_SEVERE, "Error registering for connection_state event: %s.", strerror(rv));

	ipmi_sol_set_ACK_retries(active_connection, 10);
	ipmi_sol_set_ACK_timeout(active_connection, 1000000);

	return 0;
}

static void conn_changed(ipmi_con_t   *ipmi,
		  int          err,
		  unsigned int port_num,
		  int          any_port_up,
		  void         *cb_data)
{
	static int last_any_port_up = -1;

	int rv = err;
	if (any_port_up == last_any_port_up)
		return;

	last_any_port_up = any_port_up;

	if (any_port_up)
	{
		if (verbosity > 2)
			ipmi_log(IPMI_LOG_INFO, "IPMI connection is up.  Bringing up SoL connection.");

		ipmi_sol_open(active_connection);
	}
	else
	{
		char buf[50];
		ipmi_log(IPMI_LOG_SEVERE, "No connection to BMC is available.  %s.", ipmi_get_error_string(rv, buf, 50));
		ipmi_sol_force_close(active_connection);
		exit_condition = condition_exit_now;
	}
}

static void solterm_disconnect()
{
	if (!connection_up)
	{
		exit_condition = condition_exit_now;
	}
	else
	/*
	 * If we aren't already shutting down, do a forcible shut down!
	 */
	if (exit_condition == condition_running)
	{
		exit_condition = condition_exiting;
		ipmi_sol_close(active_connection);
	}
	else
	{
		ipmi_log(IPMI_LOG_WARNING, "Forcing SoL connection closed.");
		exit_condition = condition_exit_now;
	}
}

static void show_escape(unsigned char c)
{
	if (isprint(c))
		printf("[%c%c]", escape_char, c);
	else if ((c >= 1) && (c <= 26))
		printf("[%c^%c]", escape_char, (c | 0x40));
	else
		printf("[%c\\0x%02x]", escape_char, c);
}

static void show_help()
{
	char hostname[80];

	printf("\nThis is solterm");
	if (0 == gethostname(&hostname[0], 80))
	{
		hostname[79] = 0;
		printf(" on \"%s\"", &hostname[0]);
	}

	printf(	".  Supported escape sequences:\n"
		"%c.  - terminate connection\n"
		"%cB  - send a BREAK to the managed baseboard\n"
		"%cF  - flush local and remote buffers\n\n"

		"Baseboard RS232 Control signals:\n"
		"\t%cR/%cr  - ASSERT/deassert RI (Ring Indicator) at managed baseboard\n"
		"\t%cD/%cd  - ASSERT/deassert DCD and DSR at managed baseboard\n"
		"\t%cC/%cc  - CTS PAUSE/place cts under BMC control\n\n",
		escape_char, escape_char, escape_char,
		escape_char, escape_char, escape_char,
		escape_char, escape_char, escape_char
	);

#ifdef USE_SUSPEND
	printf(	"%c^Z - suspend solterm\n", escape_char);
#endif

	printf(	"%c?  - this message\n"
		"%c%c  - send the escape character by typing it twice\n"
		"(Note that escapes are only recognized immediately after newline.)\n",
		escape_char, escape_char, escape_char
	);
}

static int did_handle_escape(unsigned char c)
{
	switch (c)
	{
		case '.':
			/* Disconnect */
			show_escape(c);
			solterm_disconnect();
			return 1;

		case 'b':
		case 'B':
			/* Send Break */
			show_escape(c);
			sol_send_break();
			return 1;

		case 'f':
		case 'F':
			/* Flush everything */
			show_escape(c);
			sol_do_flush();
			return 1;

		case 'R':
		case 'r':
			show_escape(c);
			if (connection_up)
				ipmi_sol_set_RI_asserted(active_connection, (c == 'R'), transmit_complete, NULL);
			return 1;

		case 'D':
		case 'd':
			show_escape(c);
			if (connection_up)
				ipmi_sol_set_DCD_DSR_asserted(active_connection, (c == 'D'), transmit_complete, NULL);
			return 1;

		case 'C':
		case 'c':
			show_escape(c);
			if (connection_up)
				ipmi_sol_set_CTS_assertable(active_connection, (c == 'c'), transmit_complete, NULL);
			return 1;

#ifdef USE_SUSPEND
		case 'Z' & 0x1f: /* ^Z */
			show_escape(c); fflush(stdout);
			unconfigure_terminal();
			kill(getpid(), SIGTSTP);
			configure_terminal();
			printf("Resuming SoL session...\n"); fflush(stdout);

			return 1;
#endif

		case '?':
			show_escape(c);
			show_help();
			return 1;
	}

	return 0;
}


static void stdin_data_avail(int fd, void *cb_data, os_hnd_fd_id_t *id)
{
	struct timeval tv;
	char c;
	fd_set theSet;

	tv.tv_sec = 0;
	tv.tv_usec = 0;

	FD_ZERO(&theSet);
	FD_SET(STDIN_FILENO, &theSet);

	if (!connection_up)
		ipmi_log(IPMI_LOG_WARNING, "Trying to transmit with no active connection!");

	while (select(1, &theSet, NULL, NULL, &tv))
	{
		if (0 == read(STDIN_FILENO, &c, 1))
		{
			solterm_disconnect();
			break;
		}

		else if ((escape_status == escape_next) && (c != escape_char) && did_handle_escape(c))
		{
			/* If it was handled, we just go back to waiting for another escape char.
			 * We should do this so the user can do ~R~r to assert then deassert RI.
			 */
			escape_status = escape_waiting_for_escape;
			fflush(stdout);
		}

		else if ((escape_status == escape_waiting_for_escape) && (escape_char == c))
			escape_status = escape_next;

		else if ((escape_status == escape_waiting_for_CR) && ('\r' == c))
		{
			if (connection_up)
				sol_send(&c, 1);
			escape_status = escape_waiting_for_escape;
		}
		else if (connection_up)
		{
			/* First, send the bottled-up escape char if it wasn't actually used as an escape char! */
			if ((escape_status == escape_next) && (escape_char != c))
				sol_send(&escape_char, 1);

			sol_send(&c, 1);
			if ('\r' == c)
				escape_status = escape_waiting_for_escape;
			else
				escape_status = escape_waiting_for_CR;
		}
	}
}

static os_handler_t *os_hnd;

ipmi_open_option_t domain_open_options = { IPMI_OPEN_OPTION_ALL, { ival: 0 }};

static int parse_sol_bit_rate(char *src, int *bit_rate)
{
	if (0 == strcmp(src, "9600"))
		*bit_rate = IPMI_SOL_BIT_RATE_9600;
	else if (0 == strcmp(src, "19200"))
		*bit_rate = IPMI_SOL_BIT_RATE_19200;
	else if (0 == strcmp(src, "38400"))
		*bit_rate = IPMI_SOL_BIT_RATE_38400;
	else if (0 == strcmp(src, "57600"))
		*bit_rate = IPMI_SOL_BIT_RATE_57600;
	else if (0 == strcmp(src, "115200"))
		*bit_rate = IPMI_SOL_BIT_RATE_115200;
	else
		return 0;

	return 1;
}

static int parse_sol_alerts(char *src, ipmi_sol_serial_alert_behavior *behavior)
{
	if (0 == strcasecmp(src, "fail"))
		*behavior = ipmi_sol_serial_alerts_fail;
	else if (0 == strcasecmp(src, "defer"))
		*behavior = ipmi_sol_serial_alerts_deferred;
	else if (0 == strcasecmp(src, "deferred"))
		*behavior = ipmi_sol_serial_alerts_deferred;
	else if (0 == strcasecmp(src, "succeed"))
		*behavior = ipmi_sol_serial_alerts_succeed;
	else
		return 0;

	return 1;
}

#define ARG (argv[curr_arg])
#define NEXT_ARG \
	{\
		curr_arg++; \
		if (curr_arg >= argc) { \
			ipmi_log(IPMI_LOG_FATAL, "Expected parameter after argument \"%s\"", argv[argc - 1]); \
			exit(1); \
		} \
	}

int main(int argc, char *argv[])
{
	int         rv;
	int         curr_arg = 1;
	ipmi_args_t *args;
	ipmi_con_t  *ipmi;
	char *name = "remote system";
	os_hnd_fd_id_t *stdin_id;
	sol_configuration_t sol_configuration;

	memset(&sol_configuration, 0, sizeof(sol_configuration));

	/* Enable authentication and encryption by default. */
	sol_configuration.authenticated = 1;
	sol_configuration.encrypted = 1;

	exit_condition = condition_running;
	verbosity = INITIAL_VERBOSITY;

	progname = argv[0];

	/* OS handler allocated first. */
	os_hnd = ipmi_posix_setup_os_handler();
	if (!os_hnd) {
		fprintf(stderr, "main: Unable to allocate os handler\n");
		exit(1);
	}
	os_hnd->set_log_handler(os_hnd, my_vlog);

	/* Initialize the OpenIPMI library. */
	ipmi_init(os_hnd);

	if (argc < 2) {
		usage();
		exit(1);
	}

	/* Now we make sure "lan" is the first argument so we get the
	   right connection type... */
	if (strcmp(argv[1], "lan") != 0) {
		fprintf(stderr, "main: %s only supports lan connections\n",
			progname);
		exit(1);
	}

	curr_arg = 1;
	rv = ipmi_parse_args2(&curr_arg, argc, argv, &args);
	if (rv) {
		ipmi_log(IPMI_LOG_FATAL, "Error parsing command arguments, argument %d: %s\n",
			curr_arg, strerror(rv));
		usage();
		exit(1);
	}

	while (curr_arg < argc)
	{
		if (0 == strcmp(ARG, "-notencrypted"))
			sol_configuration.encrypted = 0;
		else if (0 == strcmp(ARG, "-notauthenticated"))
			sol_configuration.authenticated = 0;
		else if (0 == strcmp(ARG, "-holdoff"))
			sol_configuration.holdoff = 1;
		else if (0 == strncmp(ARG, "-bitrate=", 9))
		{
			if (!parse_sol_bit_rate(&ARG[9], &sol_configuration.bit_rate))
				break;
		}
		else if (0 == strcmp(ARG, "-bitrate"))
		{
			NEXT_ARG;
			if (!parse_sol_bit_rate(ARG, &sol_configuration.bit_rate))
				break;
		}
		else if (0 == strncmp(ARG, "-alerts=", 8))
		{
			if (!parse_sol_alerts(&ARG[8], &sol_configuration.alert_behavior))
				break;
		}
		else if (0 == strncmp(ARG, "-alert=", 7))
		{
			if (!parse_sol_alerts(&ARG[7], &sol_configuration.alert_behavior))
				break;
		}
		else if	((0 == strcmp(ARG, "-alerts")) || (0 == strcmp(ARG, "-alert")))
		{
			NEXT_ARG;
			if (!parse_sol_alerts(ARG, &sol_configuration.alert_behavior))
				break;
		}
		else if (0 == strcmp(ARG, "-ack-retries"))
		{
			NEXT_ARG;
		//	if (!parse_int(ARG
		}
		else if (0 == strcmp(ARG, "-e"))
		{
			NEXT_ARG;
			if (strlen(argv[curr_arg]) != 1)
				break;
			escape_char = argv[curr_arg][0];
		}
		else if (0 == strcmp(ARG, "-v"))
			verbosity++;
		else if (0 == strcmp(ARG, "-q"))
			verbosity--;
		else break;

		curr_arg++;
	}

	if (curr_arg < argc)
	{			
		ipmi_log(IPMI_LOG_FATAL, "Unknown arg: %s", argv[curr_arg]);
		usage();
		exit(1);
	}

	if (sol_configuration.encrypted && !sol_configuration.authenticated)
	{
		ipmi_log(IPMI_LOG_FATAL, "Encryption cannot be enabled unless authentication is also enabled.");
		exit(1);
	}

	verbosity -= INITIAL_VERBOSITY;


	rv = ipmi_args_setup_con(args, os_hnd, NULL, &ipmi);
	if (rv) {
	        ipmi_log(IPMI_LOG_FATAL, "ipmi_ip_setup_con: %s", strerror(rv));
		exit(1);
	}

	if (ipmi->name)
		name = ipmi->name;

	rv = ipmi_sol_create(ipmi, &active_connection);
	if (rv)
	{
		ipmi_log(IPMI_LOG_FATAL, "Unable to create sol_conn. Error 0x%08x", rv);
		exit(1);
	}

	rv = configure_sol();
	if (rv)
	{
		ipmi_log(IPMI_LOG_SEVERE, "Unable to configure sol_conn. Error 0x%08x", rv);
	}

	/*
	 * Copy the parsed configuration into the sol_conn
	 */
	ipmi_sol_set_use_encryption(active_connection, sol_configuration.encrypted);
	ipmi_sol_set_use_authentication(active_connection, sol_configuration.authenticated);
	ipmi_sol_set_bit_rate(active_connection, sol_configuration.bit_rate);
	ipmi_sol_set_shared_serial_alert_behavior(active_connection,
		sol_configuration.alert_behavior);
	ipmi_sol_set_deassert_CTS_DCD_DSR_on_connect(active_connection,
		sol_configuration.holdoff);
	

	if (verbosity > 4)
		DEBUG_MSG_ENABLE();
	if (verbosity > 5)
		DEBUG_RAWMSG_ENABLE();

	if (verbosity >= 0)
	{
		printf(	"Connecting to %s...\n", name);
		fflush(stdout);
	}

	configure_terminal();

	rv = ipmi->add_con_change_handler(ipmi, conn_changed, NULL);
	ipmi->start_con(ipmi);

	os_hnd->add_fd_to_wait_for(os_hnd,
		STDIN_FILENO,
		stdin_data_avail, /* os_data_ready_t */
		NULL, /* cb_data */
		NULL, /*os_fd_data_freed_t */
		&stdin_id);

	while (exit_condition != condition_exit_now) {
		os_hnd->perform_one_op(os_hnd, NULL);
	}

	unconfigure_terminal();
	if (active_connection)
	{
		ipmi_sol_force_close(active_connection);
		ipmi_sol_free(active_connection);
	}

	if (connection_up)
		printf("Connection to %s closed.\n", name);

	os_hnd->free_os_handler(os_hnd);

	return 0;
}