gonzui


Format: Advanced Search

t2ex/t2ex_source/kernel/sysmain/src/network_sample/net_show.cbare sourcepermlink (0.01 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: #include <basic.h>
   47: #include <stdio.h>
   48: #include <stdlib.h>
   49: #include <stdarg.h>
   50: #include <string.h>
   51: #include <t2ex/socket.h>
   52: 
   53: #include "network_sample/util.h"
   54: #include "network_sample/route.h"
   55: 
   56: static void net_show_inet(const struct ifaddrs *ifa)
   57: {
   58:         unsigned char str[32];
   59:         struct sockaddr_in *sin;
   60: 
   61:         sin = (struct sockaddr_in *)ifa->ifa_addr;
   62:         printf("    inet:      %s\n", 
   63:                inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
   64: 
   65:         if ((ifa->ifa_flags & IFF_BROADCAST) != 0) {
   66:                 sin = (struct sockaddr_in *)ifa->ifa_broadaddr;
   67:                 printf("    broadcast: %s\n", 
   68:                        inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
   69:         }
   70: 
   71:         sin = (struct sockaddr_in *)ifa->ifa_netmask;
   72:         printf("    netmask:   %s\n",
   73:                inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)));
   74: }
   75: 
   76: static void net_show_link(const struct ifaddrs *ifa)
   77: {
   78:         char buf[32];
   79:         int len;
   80: 
   81:         len = so_getnameinfo(ifa->ifa_addr, ifa->ifa_addr->sa_len,
   82:                              buf, sizeof(buf), NULL, 0, NI_NUMERICHOST, NULL);
   83:         if (len < 0) {
   84:                 return;
   85:         }
   86:         if (buf[0] == '\0') {
   87:                 return;
   88:         }
   89: 
   90:         printf("    address:   %s\n", buf);
   91: }
   92: 
   93: static void net_show_if(void)
   94: {
   95:         struct ifaddrs *ifa;
   96:         void *buf;
   97:         int len;
   98:         char lastname[IFNAMSIZ];
   99: 
  100:         len = so_getifaddrs(&ifa, NULL, 0);
  101:         if (len < 0) {
  102:                 printf("error: so_getifaddrs: %d(%d,%d)\n", len, MERCD(len), 
  103:                        SERCD(len));
  104:                 return;
  105:         }
  106:         buf = malloc(len);
  107:         if (buf == NULL) {
  108:                 return;
  109:         }
  110:         len = so_getifaddrs(&ifa, buf, len);
  111:         if (len < 0) {
  112:                 printf("error: so_getifaddrs: %d(%d,%d)\n", len, MERCD(len), 
  113:                        SERCD(len));
  114:                 free(buf);
  115:                 return;
  116:         }
  117: 
  118:         lastname[0] = '\0';
  119:         for( ; ifa != NULL; ifa=ifa->ifa_next) {
  120:                 if (strncmp(ifa->ifa_name, lastname, IFNAMSIZ) != 0) {
  121:                         printf("%s:\n", ifa->ifa_name);
  122:                         strncpy(lastname, ifa->ifa_name, IFNAMSIZ-1);
  123:                 }
  124:                 if (ifa->ifa_addr->sa_family == AF_LINK) {
  125:                         net_show_link(ifa);
  126:                 }
  127:                 else if (ifa->ifa_addr->sa_family == AF_INET) {
  128:                         net_show_inet(ifa);
  129:                 }
  130:         }
  131:         free(buf);
  132: }
  133: 
  134: static void net_show_dns(void)
  135: {
  136:         union {
  137:                 struct sockaddr *stop;
  138:                 char *ctop;
  139:                 UB c[256];
  140:         } buf;
  141:         int len, i;
  142:         struct sockaddr_in *sin;
  143:         struct sockaddr **res;
  144:         unsigned char rbuf[32];
  145:         char **domains;
  146: 
  147:         len = so_resctl(SO_RES_GET_SERVERS, &buf.stop, sizeof(buf));
  148:         if (len < 0) {
  149:                 printf("error: so_resctl()\n");
  150:                 return;
  151:         }
  152:         res = &buf.stop;
  153:         for( i=0; res[i] != NULL; i++ ) {
  154:                 sin = (struct sockaddr_in *)res[i];
  155:                 printf( "server: %s\n", 
  156:                         inet_ntop(AF_INET, &sin->sin_addr, rbuf, sizeof(rbuf)));
  157:         }
  158: 
  159:         len = so_resctl(SO_RES_GET_DOMAINS, &buf.ctop, sizeof(buf));
  160:         if (len < 0) {
  161:                 printf("error: so_resctl()\n");
  162:                 return;
  163:         }
  164:         domains = &buf.ctop;
  165:         for( i=0; domains[i] != NULL; i++ ) {
  166:                 printf( "domain: %s\n", domains[i] );
  167:         }
  168: }
  169: 
  170: static void net_show_hosttable(void) __attribute__((noinline));
  171: static void net_show_hosttable(void)
  172: {
  173:         union {
  174:                 struct hosttable top;
  175:                 UB c[256];
  176:         } buf;
  177:         int len, i;
  178:         struct hosttable *table;
  179:         unsigned char rbuf[32];
  180:         struct sockaddr_in *sin;
  181: 
  182:         len = so_resctl(SO_RES_GET_TABLES, &buf.top, sizeof(buf));
  183:         if (len < 0) {
  184:                 printf("error: so_resctl()\n");
  185:                 return;
  186:         }
  187:         table = &buf.top;
  188:         for(i=0; table[i].addr != NULL; i++) {
  189:                 sin = (struct sockaddr_in *)table[i].addr;
  190:                 printf("%-18s ", inet_ntop(AF_INET, &sin->sin_addr, rbuf, sizeof(rbuf)));
  191:                 printf("%s ", table[i].host );
  192:                 printf("%s\n", table[i].aliases != NULL ? table[i].aliases : "");
  193:         }
  194: }
  195: 
  196: void net_show(void)
  197: {
  198:         printf("\n--------------------------------\n");
  199:         printf("Network interface configurations\n");
  200:         printf("--------------------------------\n");
  201: 
  202:         net_show_if();
  203: 
  204:         printf("\n-----------------\n");
  205:         printf("DNS configuraions\n");
  206:         printf("-----------------\n");
  207: 
  208:         net_show_dns();
  209: 
  210:         printf("\n----------\n");
  211:         printf("Host table\n");
  212:         printf("----------\n");
  213: 
  214:         net_show_hosttable();
  215: 
  216:         printf("\n-------------------\n");
  217:         printf("Routing information\n");
  218:         printf("-------------------\n");
  219: 
  220:         dump_rtlist();
  221: 
  222:         printf("\n");
  223: }