File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / example / tnet.c
Revision 1.4: download - view: text, annotated - select for diffs - revision graph
Thu Aug 18 09:27:36 2016 UTC (8 years, 2 months ago) by misho
Branches: MAIN
CVS tags: cli4_6, cli4_5, cli4_4, cli4_3, cli4_2, cli4_1, HEAD, CLI4_5, CLI4_4, CLI4_3, CLI4_2, CLI4_1, CLI4_0
version 4.0

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/select.h>
#ifndef __linux__
#include <sys/filio.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/telnet.h>
#include <elwix.h>
#include <aitcli.h>


int main(int argc, char **argv)
{
	int ret, s, c;
	struct sockaddr_in sin = { sizeof sin, AF_INET, htons(2000), .sin_addr.s_addr = 0x100007f };
	struct telnetAttrs *a, A[2];
	char str[256], code;

	s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
	bind(s, (struct sockaddr*) &sin, sizeof sin);
	listen(s, 1);
	c = accept(s, (struct sockaddr*) &sin, (socklen_t*) &ret);

	cli_telnet_SetCmd(&A[0], DO, TELOPT_TTYPE);
	if (argc > 1) {
		cli_telnet_Set_SubOpt(&A[1], TELOPT_TTYPE, TELQUAL_SEND, NULL, 0);
//		cli_telnet_DumpAttrs(A, 2);
		printf("cli_telnetSend()=%d bytes\n", cli_telnetSend(c, &A[0], 1, NULL, 0, 1));
		printf("errno=%d %s ret=%d telnetRecv()=%d attributes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		e_free(a);

		printf("str=%s\n", str);

		printf("telnetSend()=%d bytes\n", cli_telnetSend(c, &A[1], 1, "brym\n", 5, 0));
		printf("errno=%d %s ret=%d telnetRecv()=%d attributes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		ret = 255;
		printf("term=%X str=%s get_subopt=%d\n", (u_char) code, str, 
				cli_telnet_Get_SubOpt(&a[0], (u_char*) &code, str, (u_char*) &ret));
		e_free(a);
	} else {
		cli_telnet_SetCmd(&A[1], GA, 0);
//		cli_telnet_DumpAttrs(A, 2);

		char caps[] = { TELOPT_BINARY, TELOPT_TTYPE, TELOPT_STATUS };
		printf("a=%p ret=%d telnet_Answer()=%d\n", a, ret, 
				cli_telnet_Answer((u_char*) caps, sizeof caps, A, 2, &a, &ret));
//		cli_telnet_DumpAttrs(a, ret);
		e_free(a);

		printf("telnetSend()=%d bytes\n", cli_telnetSend(c, A, 2, "piuk\n", 5, 0));
		printf("errno=%d %s ret=%d telnetRecv()=%d bytes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		e_free(a);

		printf("str=%s\n", str);

		cli_telnet_Set_SubOpt(&A[1], TELOPT_TTYPE, TELQUAL_SEND, NULL, 0);
		printf("telnetSend()=%d bytes\n", cli_telnetSend(c, &A[1], 1, NULL, 0, 1));
		printf("errno=%d %s ret=%d telnetRecv()=%d bytes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		printf("str=%s\n", str);
		ret = 255;
		printf("term=%X str=%s get_subopt=%d\n", code, str, 
				cli_telnet_Get_SubOpt(&a[0], (u_char*) &code, str, (u_char*) &ret));
		e_free(a);

		printf("errno=%d %s ret=%d telnetRecv()=%d bytes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		e_free(a);

		printf("str=%s\n", str);

		printf("telnetSend()=%d bytes\n", cli_telnetSend(c, NULL, 0, "ALA-BALA\n", 9, 0));
		printf("errno=%d %s ret=%d telnetRecv()=%d bytes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, NULL, NULL, str, 255));
		e_free(a);
		printf("str=%s\n", str);
		printf("errno=%d %s ret=%d telnetRecv()=%d bytes\n", cli_GetErrno(), cli_GetError(), 
				ret, cli_telnetRecv(c, &a, &ret, str, 255));
//		cli_telnet_DumpAttrs(a, ret);
		e_free(a);

		printf("str=%s\n", str);
	}

	close(c);
	close(s);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>