--- libaitcli/example/tnet.c 2011/03/16 18:37:15 1.1 +++ libaitcli/example/tnet.c 2011/05/29 22:49:25 1.2 @@ -0,0 +1,93 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +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); + 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", code, str, cli_telnet_Get_SubOpt(&a[0], &code, str, (char*) &ret)); + 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(caps, sizeof caps, A, 2, &a, &ret)); + cli_telnet_DumpAttrs(a, ret); + 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); + 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], &code, str, (char*) &ret)); + 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); + 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)); + 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); + free(a); + + printf("str=%s\n", str); + } + + close(c); + close(s); + return 0; +}