gonzui


Format: Advanced Search

t2ex/t2ex_source/kernel/sysmain/src/network_sample/net_test.cbare sourcepermlink (0.04 seconds)

Search this content:

    1: /*
    2:  *----------------------------------------------------------------------
    3:  *    T2EX Software Package
    4:  *
    5:  *    Copyright 2012 by Ken Sakamura.
    6:  *    This software is distributed under the latest version of T-License 2.x.
    7:  *----------------------------------------------------------------------
    8:  *
    9:  *    Released by T-Engine Forum(http://www.t-engine.org/) at 2012/12/12.
   10:  *    Modified by TRON Forum(http://www.tron.org/) at 2015/06/04.
   11:  *
   12:  *----------------------------------------------------------------------
   13:  */
   14: /*
   15:  * This software package is available for use, modification, 
   16:  * and redistribution in accordance with the terms of the attached 
   17:  * T-License 2.x.
   18:  * If you want to redistribute the source code, you need to attach 
   19:  * the T-License 2.x document.
   20:  * There's no obligation to publish the content, and no obligation 
   21:  * to disclose it to the TRON Forum if you have modified the 
   22:  * software package.
   23:  * You can also distribute the modified source code. In this case, 
   24:  * please register the modification to T-Kernel traceability service.
   25:  * People can know the history of modifications by the service, 
   26:  * and can be sure that the version you have inherited some 
   27:  * modification of a particular version or not.
   28:  *
   29:  *    http://trace.tron.org/tk/?lang=en
   30:  *    http://trace.tron.org/tk/?lang=ja
   31:  *
   32:  * As per the provisions of the T-License 2.x, TRON Forum ensures that 
   33:  * the portion of the software that is copyrighted by Ken Sakamura or 
   34:  * the TRON Forum does not infringe the copyrights of a third party.
   35:  * However, it does not make any warranty other than this.
   36:  * DISCLAIMER: TRON Forum and Ken Sakamura shall not be held
   37:  * responsible for any consequences or damages caused directly or
   38:  * indirectly by the use of this software package.
   39:  *
   40:  * The source codes in bsd_source.tar.gz in this software package are 
   41:  * derived from NetBSD or OpenBSD and not covered under T-License 2.x.
   42:  * They need to be changed or redistributed according to the 
   43:  * representation of each source header.
   44:  */
   45: 
   46: /*
   47:  *      @(#)net_test.c
   48:  *
   49:  */
   50: 
   51: #include <tk/tkernel.h>
   52: 
   53: #include <t2ex/socket.h>
   54: #include <t2ex/errno.h>
   55: 
   56: #include <stdio.h>
   57: #include <stdlib.h>
   58: #include <strings.h>
   59: 
   60: #include "dhclient.h"
   61: #include "route.h"
   62: #include "ping.h"
   63: #include "util.h"
   64: #include "httpclient.h"
   65: 
   66: 
   67: #define IP_T_ENGINE_ORG "202.32.0.87"
   68: #define IP_GOOGLE_CO_JP "74.125.235.152"
   69: 
   70: #define HTTP_BUFSIZE 8192
   71: 
   72: static ID semid;
   73: static ID semid2;
   74: static ID server_tskid;
   75: static ID client_tskid;
   76: 
   77: static void test_http(void)
   78: {
   79:         int re;
   80:         char* buf;
   81: 
   82:         printf("[http] start\n");
   83: 
   84:         buf = malloc(HTTP_BUFSIZE);
   85:         bzero(buf, HTTP_BUFSIZE);
   86:         re = http_get("t-engine.org", "/", buf, HTTP_BUFSIZE);
   87:         DEBUG_PRINT(("server_task: http_get = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
   88:         if ( re < 0 ) {
   89:                 printf("[http] FAILED\n");
   90:                 return;
   91:         }
   92:         printf(buf);
   93:         free(buf);
   94: 
   95:         printf("[http] OK\n");
   96: }
   97: 
   98: static void test_getaddrinfo(void)
   99: {
  100:         const char* hostname = "www.t-engine.org";//"localhost";
  101:         struct addrinfo hints, *res;
  102:         struct in_addr addr;
  103:         char* buf;
  104:         int re;
  105:         int size;
  106:         char rbuf[18];
  107: 
  108:         printf("[getaddrinfo] start\n");
  109: 
  110:         bzero(&hints, sizeof hints);
  111:         hints.ai_socktype = SOCK_STREAM;
  112:         hints.ai_family = AF_INET;
  113: 
  114:         size = so_getaddrinfo(hostname, NULL, &hints, &res, NULL, 0, NULL);
  115:         DEBUG_PRINT(("test_getaddrinfo: so_getaddrinfo = %d(%d, %d)\n", size, MERCD(size), SERCD(size)));
  116:         if ( size < 0 ) {
  117:                 printf("[getaddrinfo] FAILED\n");
  118:                 return;
  119:         }
  120: 
  121:         buf = malloc(size);
  122:         re = so_getaddrinfo(hostname, NULL, &hints, &res, buf, size, NULL);
  123:         DEBUG_PRINT(("test_getaddrinfo: so_getaddrinfo = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  124:         if ( re < 0 ) {
  125:                 printf("[getaddrinfo] FAILED\n");
  126:                 return;
  127:         }
  128:         addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
  129:         printf("test_getaddrinfo: %s => %s\n", hostname, inet_ntop(AF_INET, &addr, rbuf, sizeof(rbuf)));
  130:         free(buf);
  131: 
  132:         printf("[getaddrinfo] OK\n");
  133: }
  134: 
  135: static void test_getnameinfo(void)
  136: {
  137:         struct sockaddr_in sa;
  138:         char buf[NI_MAXHOST];
  139:         char buf2[NI_MAXSERV];
  140:         int re;
  141:         char rbuf[18];
  142: 
  143:         printf("[getnameinfo] start\n");
  144: 
  145:         bzero(&sa, sizeof sa);
  146:         sa.sin_len = sizeof sa;
  147:         sa.sin_family = AF_INET;
  148:         sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  149:         sa.sin_port = htons(12345);
  150: 
  151:         re = so_getnameinfo((struct sockaddr*)&sa, sizeof sa, buf, sizeof buf, buf2, sizeof buf2, 0, NULL);
  152:         DEBUG_PRINT(("test_getnameinfo: so_getnameinfo = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  153:         if ( re < 0 ) {
  154:                 printf("[getnameinfo] FAILED\n");
  155:                 return;
  156:         }
  157:         printf("test_getnameinfo: %s:%d => %s:%s\n", inet_ntop(AF_INET, &sa.sin_addr, rbuf, sizeof(rbuf)), ntohs(sa.sin_port), buf, buf2);
  158: 
  159:         sa.sin_addr.s_addr = inet_addr(IP_T_ENGINE_ORG);
  160:         sa.sin_port = htons(80);
  161:         re = so_getnameinfo((struct sockaddr*)&sa, sizeof sa, buf, sizeof buf, buf2, sizeof buf2, 0, NULL);
  162:         DEBUG_PRINT(("test_getnameinfo: so_getnameinfo = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  163:         if ( re < 0 ) {
  164:                 printf("[getnameinfo] FAILED\n");
  165:                 return;
  166:         }
  167:         printf("test_getnameinfo: %s:%d => %s:%s\n", inet_ntop(AF_INET, &sa.sin_addr, rbuf, sizeof(rbuf)), ntohs(sa.sin_port), buf, buf2);
  168: 
  169:         printf("[getnameinfo] OK\n");
  170: }
  171: 
  172: static void wait_data(int sd)
  173: {
  174:         int re;
  175:         fd_set fdset;
  176: 
  177:         FD_ZERO(&fdset);
  178:         FD_SET(sd, &fdset);
  179: 
  180:         re = so_select(sd+1, &fdset, NULL, NULL, NULL);
  181:         DEBUG_PRINT(("wait_data: so_select = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  182: }
  183: 
  184: static void test_tcp_server(void)
  185: {
  186:         int re;
  187:         int sd;
  188:         int reader = 0;
  189:         char buf[5];
  190:         struct sockaddr_in sa;
  191:         struct sockaddr_in sa2;
  192:         socklen_t sa_len;
  193: 
  194:         printf("[tcp(server)] start\n");
  195: 
  196:         sd = so_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  197:         if ( sd < 0 ) {
  198:                 goto error;
  199:         }
  200:         DEBUG_PRINT(("server_task: so_socket = %d(%d, %d)\n", sd, MERCD(sd), SERCD(sd)));
  201: 
  202:         bzero(&sa, sizeof sa);
  203:         sa.sin_family = AF_INET;
  204:         sa.sin_addr.s_addr = htonl(INADDR_ANY);
  205:         sa.sin_port = htons(12345);
  206:         re = so_bind(sd, (struct sockaddr*)&sa, sizeof sa);
  207:         DEBUG_PRINT(("server_task: so_bind = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  208:         if ( re < 0 ) {
  209:                 goto error;
  210:         }
  211: 
  212:         re = so_listen(sd, 5);
  213:         DEBUG_PRINT(("server_task: so_listen = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  214:         if ( re < 0 ) {
  215:                 goto error;
  216:         }
  217: 
  218:         tk_sig_sem(semid, 1);
  219:         DEBUG_PRINT(("server_task: server semaphore signaled 1\n"));
  220: 
  221:         reader = so_accept(sd, (struct sockaddr*)&sa2, &sa_len);
  222:         DEBUG_PRINT(("server_task: so_accept = %d(%d, %d)\n", reader, MERCD(reader), SERCD(reader)));
  223:         if ( reader < 0 ) {
  224:                 goto error;
  225:         }
  226: 
  227:         wait_data(reader);
  228: 
  229:         bzero(buf, sizeof buf);
  230:         re = so_sockatmark(reader);
  231:         DEBUG_PRINT(("server_task: so_sockatmark = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  232:         if ( re < 0 ) {
  233:                 goto error;
  234:         }
  235:         re = so_read(reader, buf, 4);
  236:         DEBUG_PRINT(("server_task: so_read = %d(%d, %d), buf = %s\n", re, MERCD(re), SERCD(re), buf));
  237:         if ( re < 0 || memcmp(buf, "1234", 4) != 0 ) {
  238:                 goto error;
  239:         }
  240: 
  241:         wait_data(reader);
  242: 
  243:         bzero(buf, sizeof buf);
  244:         re = so_sockatmark(reader);
  245:         DEBUG_PRINT(("server_task: so_sockatmark = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  246:         if ( re < 0 ) {
  247:                 goto error;
  248:         }
  249:         re = so_recv(reader, buf, 4, MSG_OOB);
  250:         DEBUG_PRINT(("server_task: so_recv = %d(%d, %d), buf = %s\n", re, MERCD(re), SERCD(re), buf));
  251:         if ( re < 0 || buf[0] != 'a' ) {
  252:                 goto error;
  253:         }
  254: 
  255:         tk_sig_sem(semid2, 1);
  256:         DEBUG_PRINT(("server_task: server semaphore for break signaled 2\n"));
  257: 
  258:         DEBUG_PRINT(("server_task: pre-accept for break\n"));
  259:         re = so_accept(sd, (struct sockaddr*)&sa2, &sa_len);
  260:         DEBUG_PRINT(("server_task: so_accept = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  261:         if ( re != EX_INTR ) {
  262:                 goto error;
  263:         }
  264: 
  265:         so_close(reader);
  266:         so_close(sd);
  267: 
  268:         printf("[tcp(server)] OK\n");
  269:         return;
  270: 
  271: error:
  272:         printf("[tcp(server)] FAILED\n");
  273:         if ( sd > 0 ) {
  274:                 so_close(sd);
  275:         }
  276:         if ( reader > 0 ) {
  277:                 so_close(reader);
  278:         }
  279:         tk_del_sem(semid2);
  280:         return;
  281: }
  282: 
  283: static void test_tcp_client(void)
  284: {
  285:         int sd;
  286:         int re;
  287:         struct sockaddr_in sa;
  288: 
  289:         printf("[tcp(client)] start\n");
  290: 
  291:         sd = so_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  292:         DEBUG_PRINT(("so_socket = %d(%d, %d)\n", sd, MERCD(sd), SERCD(sd)));
  293:         if ( sd < 0 ) {
  294:                 goto error2;
  295:         }
  296: 
  297:         bzero(&sa, sizeof sa);
  298:         sa.sin_family = AF_INET;
  299:         sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  300:         sa.sin_port = htons(12345);
  301:         re = so_connect(sd, (struct sockaddr*)&sa, sizeof sa);
  302:         printf("so_connect = %d(%d, %d)\n", re, MERCD(re), SERCD(re));
  303:         if ( re < 0 ) {
  304:                 goto error2;
  305:         }
  306: 
  307:         re = so_write(sd, "1234", 4);
  308:         DEBUG_PRINT(("so_write = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  309:         if ( re < 0 ) {
  310:                 goto error2;
  311:         }
  312: 
  313:         re = so_send(sd, "a", 1, MSG_OOB);
  314:         DEBUG_PRINT(("so_send = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  315:         if ( re < 0 ) {
  316:                 goto error2;
  317:         }
  318: 
  319:         so_close(sd);
  320: 
  321:         tk_wai_sem(semid2, 1, TMO_FEVR);
  322:         re = so_break(server_tskid);
  323:         DEBUG_PRINT(("so_break = %d(%d, %d)\n", re, MERCD(re), SERCD(re)));
  324:         if ( re < 0 ) {
  325:                 goto error2;
  326:         }
  327: 
  328:         printf("[tcp(client)] OK\n");
  329:         return;
  330: 
  331: error2:
  332:         if ( sd > 0 ) {
  333:                 so_close(sd);
  334:         }
  335:         so_break(server_tskid);
  336:         printf("[tcp(client)] FAILED\n");
  337: }
  338: 
  339: static void server_task(INT stacd, VP exinf)
  340: {
  341:         DEBUG_PRINT(("server task started\n"));
  342: 
  343:         ping(inet_addr(IP_T_ENGINE_ORG)); // t-engine.org
  344:         traceroute(inet_addr(IP_T_ENGINE_ORG)); // t-engine.org
  345:         //traceroute(inet_addr(IP_GOOGLE_CO_JP)); // google.co.jp
  346: 
  347:         test_tcp_server();
  348: 
  349:         test_getaddrinfo();
  350:         test_getnameinfo();
  351: 
  352:         test_http();
  353: 
  354:         tk_sig_sem(semid, 1);
  355: 
  356:         tk_exd_tsk();
  357: }
  358: 
  359: void client_task(INT stacd, VP exinf)
  360: {
  361:         DEBUG_PRINT(("client task started\n"));
  362: 
  363:         test_tcp_client();
  364: 
  365:         tk_sig_sem(semid, 1);
  366: 
  367:         tk_exd_tsk();
  368: }
  369: 
  370: void net_test(void)
  371: {
  372:         T_CSEM csem;
  373:         T_CTSK ctsk;
  374: 
  375:         printf(" == net test == \n");
  376: 
  377:         net_conf(NET_CONF_EMULATOR, NET_CONF_DHCP);
  378:         net_show();
  379: 
  380:         csem.maxsem = 100;
  381:         csem.isemcnt = 0;
  382:         csem.sematr = TA_TFIFO | TA_FIRST;
  383:         semid = tk_cre_sem(&csem);
  384:         semid2 = tk_cre_sem(&csem);
  385: 
  386:         bzero(&ctsk, sizeof ctsk);
  387:         ctsk.tskatr = TA_HLNG | TA_RNG0;
  388:         ctsk.task = server_task;
  389:         ctsk.itskpri = 100;
  390:         ctsk.stksz = 32 * 1024 * 2;
  391:         server_tskid = tk_cre_tsk(&ctsk);
  392:         DEBUG_PRINT(("start server task %d\n", server_tskid));
  393:         tk_sta_tsk(server_tskid, 0);
  394: 
  395:         DEBUG_PRINT(("wait server semaphore\n"));
  396:         tk_wai_sem(semid, 1, TMO_FEVR);
  397: 
  398:         bzero(&ctsk, sizeof ctsk);
  399:         ctsk.tskatr = TA_HLNG | TA_RNG0;
  400:         ctsk.task = client_task;
  401:         ctsk.itskpri = 101;
  402:         ctsk.stksz = 4 * 1024 * 2;
  403:         client_tskid = tk_cre_tsk(&ctsk);
  404:         DEBUG_PRINT(("start client task %d\n", client_tskid));
  405:         tk_sta_tsk(client_tskid, 0);
  406: 
  407:         DEBUG_PRINT(("waiting for server and client semaphore\n"));
  408:         tk_wai_sem(semid, 2, TMO_FEVR);
  409: 
  410:         printf(" == net test end == \n");
  411: 
  412:         tk_del_sem(semid);
  413:         tk_del_sem(semid2);
  414: }
  415: