summaryrefslogtreecommitdiffstats
path: root/cfe/cfe/ui/ui_tcpcmds.c
blob: e092021a63bc9e714f5d16444469ec38bcd8d302 (plain)
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
/*  *********************************************************************
    *  Broadcom Common Firmware Environment (CFE)
    *  
    *  TCP protocol commands			File: ui_tcpcmds.c
    *  
    *  This file contains commands that make use of the TCP protocol
    *  in CFE, assuming it's configured.
    *  
    *  Author:  Mitch Lichtenberg (mpl@broadcom.com)
    *  
    *********************************************************************  
    *
    *  Copyright 2000,2001,2002,2003
    *  Broadcom Corporation. All rights reserved.
    *  
    *  This software is furnished under license and may be used and 
    *  copied only in accordance with the following terms and 
    *  conditions.  Subject to these conditions, you may download, 
    *  copy, install, use, modify and distribute modified or unmodified 
    *  copies of this software in source and/or binary form.  No title 
    *  or ownership is transferred hereby.
    *  
    *  1) Any source code used, modified or distributed must reproduce 
    *     and retain this copyright notice and list of conditions 
    *     as they appear in the source file.
    *  
    *  2) No right is granted to use any trade name, trademark, or 
    *     logo of Broadcom Corporation.  The "Broadcom Corporation" 
    *     name may not be used to endorse or promote products derived 
    *     from this software without the prior written permission of 
    *     Broadcom Corporation.
    *  
    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT 
    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN 
    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR 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), EVEN IF ADVISED OF 
    *     THE POSSIBILITY OF SUCH DAMAGE.
    ********************************************************************* */


#include "lib_types.h"
#include "lib_string.h"
#include "lib_queue.h"
#include "lib_malloc.h"
#include "lib_printf.h"

#include "cfe_timer.h"

#include "cfe_error.h"
#include "cfe_console.h"

#include "ui_command.h"
#include "cfe.h"

#include "cfe_iocb.h"
#include "cfe_devfuncs.h"

#include "bsp_config.h"

#if CFG_TCP
#include "net_ebuf.h"
#include "net_api.h"
#endif


/*  *********************************************************************
    *  Configuration
    ********************************************************************* */

/*  *********************************************************************
    *  prototypes
    ********************************************************************* */

#if CFG_TCP

int ui_init_tcpcmds(void);

static int ui_cmd_rlogin(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_connect(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_listen(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_tcpconstest(ui_cmdline_t *cmd,int argc,char *argv[]);
static int ui_cmd_ttcp(ui_cmdline_t *cmd,int argc,char *argv[]);
#define isdigit(d) (((d) >= '0') && ((d) <= '9'))


/*  *********************************************************************
    *  ui_init_tcpcmds()
    *  
    *  Add TCP-specific commands to the command table
    *  
    *  Input parameters: 
    *  	   nothing
    *  	   
    *  Return value:
    *  	   0
    ********************************************************************* */


int ui_init_tcpcmds(void)
{

    cmd_addcmd("rlogin",
	       ui_cmd_rlogin,
	       NULL,
	       "mini rlogin client.",
	       "rlogin hostname [username]\n\n"
	       "Connects to a remote system using the RLOGIN protocol.  The remote"
	       "system must have appropriate permissions in place (usually via the"
	       "file '.rhosts') for CFE to connect.  To terminate the session, type"
	       "a tilde (~) character followed by a period (.)",
	       "");

    cmd_addcmd("tcp connect",
	       ui_cmd_connect,
	       NULL,
	       "TCP connection test.",
	       "tcp connect hostname [portnum]",
	       "-q;sink output, don't display on terminal|"
	       "-d;Send junk data to discard|"
	       "-nodelay;set nodelay option on socket|"
	       "-srcport=*;Specify the source port");

    cmd_addcmd("tcp listen",
	       ui_cmd_listen,
	       NULL,
	       "port listener.",
	       "tcp listen portnum",
	       "-q;sink output, don't display on terminal|"
	       "-d;Send junk data to discard|"
	       "-nodelay;set nodelay option on socket");


    cmd_addcmd("tcp constest",
	       ui_cmd_tcpconstest,
	       NULL,
	       "tcp console test.",
	       "tcp constest device",
	       "");

    cmd_addcmd("ttcp",
	       ui_cmd_ttcp,
	       NULL,
	       "TCP test command.",
	       "ttcp -t [-options] host\n"
	       "ttcp -r [-options]\n\n",
	       "-t;Source a pattern to the network|"
	       "-r;Sink (discard) data from the network|"
	       "-D;Don't buffer TCP writes (TCP_NODELAY)|"
	       "-n=*;Number of buffers to send (-t only) (default 2048)|"
	       "-l=*;Size of buffer to send/receive (default 2048)|"
	       "-p=*;Port number to use (default 5001)");

    return 0;
}





static unsigned long rand(void)
{
    static unsigned long seed = 1;
    long x, hi, lo, t;

    x = seed;
    hi = x / 127773;
    lo = x % 127773;
    t = 16807 * lo - 2836 * hi;
    if (t <= 0) t += 0x7fffffff;
    seed = t;
    return t;
}


static int ui_cmd_rlogin(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int s;
    uint8_t hostaddr[IP_ADDR_LEN];
    char *host;
    int res;
    int connflag;
    int rxdata;
    int sport;
    uint8_t data[100];
    int tilde;
    char *username;
    uint8_t *p;

    /*
     * Process args
     */

    host = cmd_getarg(cmd,0);
    if (!host) return ui_showusage(cmd);

    username = cmd_getarg(cmd,1);
    if (!username) username = "";

    /*
     * Look up remote host
     */

    if (isdigit(*host)) {
	if (parseipaddr(host,hostaddr) < 0) {
	    xprintf("Invalid IP address: %s\n",host);
	    return -1;
	    }
	}
    else {
	res = dns_lookup(host,hostaddr);
	if (res < 0) {
	    return ui_showerror(res,"Could not resolve IP address of host %s",host);
	    }
	}

    /*
     * Create TCP socket and bind to a port number less than 1023
     * See RFC1282 for more info about this
     */

    s = tcp_socket();

    if (s < 0) {
	return ui_showerror(s,"Could not create TCP socket");
	}

    res = 0;
    tilde = 0;
    for (sport = 1023; sport > 513; sport--) {
	res = tcp_bind(s,sport);
	if (res == 0) break;
	}

    if (sport == 513) {
	ui_showerror(res,"No ports available for RLOGIN");
	return res;
	}

    /*
     * Establish a connection.  Our sockets default to nonblocking
     * so we want to switch to blocking temporarily to
     * let the tcp_connect routine do this by itself.
     */

    tcp_setflags(s,0);
    res = tcp_connect(s,hostaddr,513);
    if (res < 0) {
	ui_showerror(res,"Could not connect to host %I",hostaddr);
	tcp_close(s);
	return res;
	}


    /*
     * Construct the initial RLOGIN sequence to include
     * our user name and terminal type
     */

    p = data;
    *p++ = '\0';
    p += sprintf(p,"%s",username) + 1;
    p += sprintf(p,"%s",username) + 1;
    p += sprintf(p,"vt100/38400") + 1;
    
    tcp_send(s,data,p-&data[0]);

    res = tcp_recv(s,data,1);			/* receive result code */
    if (res <= 0) {
	goto remdisc;
	}

    /*
     * Switch back to nonblocking I/O for the loop
     */

    tcp_setflags(s,TCPFLG_NBIO);

    /*
     * Begin processing loop
     */

    connflag = TRUE;
    for (;;) {

	/*
	 * Test connection status
	 */

	tcp_status(s,&connflag,&rxdata,NULL);
	if (connflag != TCPSTATUS_CONNECTED) {
	    goto remdisc;
	    }

	/*
	 * Process received data
	 */

	if (rxdata != 0) {
	    res = tcp_recv(s,data,sizeof(data));
	    if (res > 0) {
		console_write(data,res);
		}
	    if (res < 0) {
		ui_showerror(res,"TCP read error");
		break;
		}
	    }

	/*
	 * Process transmitted data
	 */

	if (console_status()) {
	    console_read(data,1);
	    if (tilde == 1) {
		if (data[0] == '.') break;
		tcp_send(s,data,1);
		}
	    else {
		if (data[0] == '~') tilde = 1;
		else tcp_send(s,data,1);
		}
	    }

	/*
	 * Give the background a chance
	 */

	POLL();
	}

    printf("Disconnecting...");
    tcp_close(s);
    printf("done.\n");
    return 0;

remdisc:    
    printf("Remote host is no longer connected.\n");
    tcp_close(s);
    return 0;
}



static int ui_cmd_connect(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int s;
    uint8_t hostaddr[IP_ADDR_LEN];
    char *host;
    char *port;
    int res;
    int connflag;
    int rxdata;
    uint8_t data[100];
    int quiet;
    int discard;
    int total = 0;
    int total2 = 0;
    char b = 0;
    cfe_timer_t t;
    char *bigbuf;
    int nodelay;
    char *sport = NULL;

    bigbuf = KMALLOC(4096,0);
    for (res = 0; res < 4096; res++) bigbuf[res] = 'A'+(res%26);

    quiet = cmd_sw_isset(cmd,"-q");
    discard = cmd_sw_isset(cmd,"-d");
    nodelay = cmd_sw_isset(cmd,"-nodelay");
    
    host = cmd_getarg(cmd,0);
    if (!host) return -1;

    port = cmd_getarg(cmd,1);
    if (!port) port = "23";

    if (strcmp(port,"discard") == 0) port = "9";
    else if (strcmp(port,"chargen") == 0) port = "19";
    else if (strcmp(port,"echo") == 0) port = "7";

    if (isdigit(*host)) {
	if (parseipaddr(host,hostaddr) < 0) {
	    xprintf("Invalid IP address: %s\n",host);
	    return -1;
	    }
	}
    else {
	res = dns_lookup(host,hostaddr);
	if (res < 0) {
	    return ui_showerror(res,"Could not resolve IP address of host %s",host);
	    }
	}


    s = tcp_socket();

    if (s < 0) {
	return ui_showerror(s,"Could not create TCP socket");
	}

    if (cmd_sw_value(cmd,"-srcport",&sport)) {
	res = tcp_bind(s,atoi(sport));
	if (res < 0) {
	    ui_showerror(res,"Could not bind to port %s",sport);
	    tcp_close(s);
	    return res;
	    }
	}

    res = tcp_connect(s,hostaddr,atoi(port));
    if (res < 0) {
	ui_showerror(res,"Could not connect to host %I",hostaddr);
	tcp_close(s);
	return res;
	}

    TIMER_SET(t,CFE_HZ*30);
    connflag = 0;
    while (!TIMER_EXPIRED(t)) {
	POLL();
	tcp_status(s,&connflag,NULL,NULL);
	if (connflag == TCPSTATUS_CONNECTING) continue;
	break;
	}

    if (connflag != TCPSTATUS_CONNECTED) {
	printf("Could not connect to remote host\n");
	tcp_close(s);
	return -1;
	}
    else {
	printf("Connected to remote host.\n");
	}
    

    if (nodelay) tcp_setflags(s,TCPFLG_NODELAY);

    connflag = TRUE;
    for (;;) {
	tcp_status(s,&connflag,&rxdata,NULL);
	if (connflag != TCPSTATUS_CONNECTED) {
	    printf("Remote host is no longer connected.\n");
	    break;
	    }
	if (rxdata != 0) {
	    res = tcp_recv(s,data,sizeof(data));
	    if (res > 0) {
		if (quiet) {
		    total += res;
		    if (total > 1000000) {
			total -= 1000000;
			printf(".");
			}
		    }
		else {
		    console_write(data,res);
		    }
		}
	    if (res < 0) {
		ui_showerror(res,"TCP read error");
		}
	    }
	if (console_status()) {
	    console_read(data,1);
	    if (data[0] == 1) break;
	    else if (data[0] == 3) break;
	    else if (data[0] == 4) {
		for (res = 0; res < 100; res++) data[res] = 'A'+(res%26);
		tcp_send(s,data,100);
		}
	    else if (data[0] == 5) tcp_send(s,bigbuf,2048);
	    else if (data[0] == 2) tcp_debug(s,0);
	    else tcp_send(s,data,1);
	    }
	if (discard) {
	    res = rand() % sizeof(data);
	    memset(data,b,res);
	    b++;
	    res = tcp_send(s,data,res);
	    if (res > 0) {
		total2 += res;
		if (total2 > 1000000) {
		    total2 -= 1000000;
		    printf("+");
		    }
		}

	    }

	POLL();
	}

    printf("Disconnecting...");
    tcp_close(s);
    printf("done.\n");
    
    KFREE(bigbuf);

    return 0;
}


static int ui_cmd_listen(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int s;
    char *port;
    int res;
    int connflag;
    int rxdata;
    uint8_t data[100];
    int quiet;
    int discard;
    int total = 0;
    int total2 = 0;
    char b = 0;
    char *bigbuf;
    uint16_t p;
    uint16_t remport;
    uint8_t remaddr[IP_ADDR_LEN];
    int nodelay;

    bigbuf = KMALLOC(4096,0);
    for (res = 0; res < 4096; res++) bigbuf[res] = 'A'+(res%26);

    quiet = cmd_sw_isset(cmd,"-q");
    discard = cmd_sw_isset(cmd,"-d");
    nodelay = cmd_sw_isset(cmd,"-nodelay");
    
    port = cmd_getarg(cmd,0);
    if (!port) port = "1234";
    p = atoi(port);

    s = tcp_socket();

    if (s < 0) {
	return ui_showerror(s,"Could not create TCP socket");
	}

    res = tcp_listen(s,p);
    if (res < 0) {
	ui_showerror(res,"Could not set socket to listen");
	tcp_close(s);
	return res;
	}

    printf("Listening...");
    connflag = FALSE;
    for (;;) {
	if (console_status()) break;
	tcp_status(s,&connflag,NULL,NULL);
	if (connflag == TCPSTATUS_CONNECTED) break;
	POLL();
	}

    if (connflag != TCPSTATUS_CONNECTED) {
	printf("No connection received from remote host\n");
	tcp_close(s);
	return -1;
	}

    tcp_peeraddr(s,remaddr,&remport);
    printf("Connection from port %u on %I\n",remport,remaddr);

    if (nodelay) tcp_setflags(s,TCPFLG_NODELAY);

    connflag = TRUE;
    for (;;) {
	tcp_status(s,&connflag,&rxdata,NULL);
	if (connflag != TCPSTATUS_CONNECTED) {
	    printf("Remote host is no longer connected.\n");
	    break;
	    }
	if (rxdata != 0) {
	    res = tcp_recv(s,data,sizeof(data));
	    if (res > 0) {
		if (quiet) {
		    total += res;
		    if (total > 1000000) {
			total -= 1000000;
			printf(".");
			}
		    }
		else {
		    console_write(data,res);
		    }
		}
	    if (res < 0) {
		ui_showerror(res,"TCP read error");
		}
	    }
	if (console_status()) {
	    console_read(data,1);
	    if (data[0] == 1) break;
	    if (data[0] == 3) break;
	    if (data[0] == 4) {
		for (res = 0; res < 100; res++) data[res] = 'A'+(res%26);
		tcp_send(s,data,100);
		}
	    if (data[0] == 5) tcp_send(s,bigbuf,2048);
	    if (data[0] == 2) tcp_debug(s,0);
	    else tcp_send(s,data,1);
	    }
	if (discard) {
	    res = rand() % sizeof(data);
	    memset(data,b,res);
	    b++;
	    res = tcp_send(s,data,res);
	    if (res > 0) {
		total2 += res;
		if (total2 > 1000000) {
		    total2 -= 1000000;
		    printf("+");
		    }
		}

	    }

	POLL();
	}


    printf("Disconnecting...");
    tcp_close(s);
    printf("done.\n");
    
    KFREE(bigbuf);

    return 0;
}


static int ui_cmd_tcpconstest(ui_cmdline_t *cmd,int argc,char *argv[])
{
    char *x;
    int fh;
    int res;
    uint8_t data[100];

    x = cmd_getarg(cmd,0);
    if (!x) return ui_showusage(cmd);

    fh = cfe_open(x);
    if (fh < 0) return ui_showerror(fh,"Could not open device %s",x);
    
    for (;;) {
	if (console_status()) break;
	res = cfe_read(fh,data,sizeof(data));
	if (res < 0) {
	    ui_showerror(res,"could not read data"); 
	    break;
	    }
	console_write(data,res);
	}

    cfe_close(fh);

    return 0;
}

static int ui_cmd_ttcp(ui_cmdline_t *cmd,int argc,char *argv[])
{
    int s;
    uint8_t hostaddr[IP_ADDR_LEN];
    char *host;
    int res;
    int totalbytes = 0;
    int totalbufs = 0;
    cfe_timer_t start_time;
    cfe_timer_t stop_time;
    cfe_timer_t t;
    int connflag;
    char *bigbuf;
    int nodelay;
    int numbuf;
    int buflen;
    int txmode,rxmode;
    uint16_t port;

    char *x;

    if (cmd_sw_value(cmd,"-n",&x)) numbuf = atoi(x);
    else numbuf = 2048;

    if (cmd_sw_value(cmd,"-l",&x)) buflen = atoi(x);
    else buflen = 2048;

    if (cmd_sw_value(cmd,"-p",&x)) port = atoi(x);
    else port = 5001;

    if ((numbuf == 0) || (buflen == 0)) return ui_showusage(cmd);


    bigbuf = KMALLOC(buflen,0);
    for (res = 0; res < buflen; res++) bigbuf[res] = 'A'+(res%26);

    txmode = cmd_sw_isset(cmd,"-t");
    rxmode = cmd_sw_isset(cmd,"-r");

    if (!(txmode ^ rxmode)) {
	return ui_showerror(-1,"You must specify one of -t or -r");
	}

    nodelay = cmd_sw_isset(cmd,"-D");

    if (txmode) {
	host = cmd_getarg(cmd,0);
	if (!host) return ui_showusage(cmd);

	if (isdigit(*host)) {
	    if (parseipaddr(host,hostaddr) < 0) {
		return ui_showerror(-1,"Invalid IP address: %s\n",host);
		}
	    }
	else {
	    res = dns_lookup(host,hostaddr);
	    if (res < 0) {
		return ui_showerror(res,"Could not resolve IP address of host %s",host);
		}
	    }
	}


    s = tcp_socket();

    if (s < 0) {
	return ui_showerror(s,"Could not create TCP socket");
	}


    if (txmode) {
	res = tcp_connect(s,hostaddr,port);
	if (res < 0) {
	    ui_showerror(res,"Could not connect to host %I",hostaddr);
	    tcp_close(s);
	    return res;
	    }

	TIMER_SET(t,CFE_HZ*30);
	connflag = 0;
	while (!TIMER_EXPIRED(t)) {
	    POLL();
	    tcp_status(s,&connflag,NULL,NULL);
	    if (connflag == TCPSTATUS_CONNECTING) continue;
	    break;
	    }

	if (connflag != TCPSTATUS_CONNECTED) {
	    printf("Could not connect to remote host\n");
	    tcp_close(s);
	    return -1;
	    }
	else {
	    printf("Connected to remote host.\n");
	    }
	}

    if (rxmode) {
	printf("Waiting for connection on port %d: ",port);
	tcp_listen(s,port);
	for (;;) {
	    if (console_status()) break;
	    tcp_status(s,&connflag,NULL,NULL);
	    if (connflag == TCPSTATUS_CONNECTED) break;
	    POLL();
	    }
	if (connflag != TCPSTATUS_CONNECTED) {
	    printf("No connection received from remote host\n");
	    tcp_close(s);
	    return -1;
	    }
	printf("done.\n");
	}


    if (nodelay) tcp_setflags(s,TCPFLG_NODELAY);	/* also sets blocking */
    else tcp_setflags(s,0);

    start_time = cfe_ticks;

    if (rxmode) {
	while (1) {
	    POLL();
	    res = tcp_recv(s,bigbuf,buflen);
	    if (res != buflen) break;
	    totalbytes += res;
	    totalbufs++;
	    }
	}
    else {
	while (numbuf > 0) {
	    POLL();
	    res = tcp_send(s,bigbuf,buflen);
	    if (res != buflen) break;
	    numbuf--;
	    totalbytes += res;
	    totalbufs++;
	    }
	}

    stop_time = cfe_ticks;

    tcp_close(s);

    if ((res < 0) && !rxmode) {
	ui_showerror(res,"Network I/O error");
	}
    else {
	printf("%d bytes transferred via %d calls in %lld ticks\n",
	       totalbytes,totalbufs,stop_time-start_time);
	}
    

    KFREE(bigbuf);

    return 0;

}

#endif	 /* CFG_TCP */