gonzui


Format: Advanced Search

t2ex/t2ex_source/kernel/sysmain/src/network_sample/net_conf.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 <stdarg.h>
   49: #include <t2ex/socket.h>
   50: 
   51: #include "network_sample/util.h"
   52: #include "network_sample/dhclient.h"
   53: #include "network_sample/route.h"
   54: 
   55: #define IPADDR  "192.168.0.2"
   56: #define NETMASK "255.255.255.0"
   57: #define GATEWAY "192.168.0.1"
   58: 
   59: #define DNSSERVER1 "127.0.0.1"
   60: //#define DNSSERVER2 "127.0.0.2"
   61: //#define DNSSERVER3 "127.0.0.3"
   62: #define DNSDOMAIN "localhost.localdomain"
   63: 
   64: #define DEBUG
   65: 
   66: #ifdef DEBUG
   67: static void NETDBG(const char *format, ...)
   68: {
   69:         va_list ap;
   70: 
   71:         printf("[NET] ");
   72:         va_start(ap, format);
   73:         vfprintf(stdout, format, ap);
   74:         va_end(ap);
   75:         printf("\n");
   76: }
   77: #else
   78: #define NETDBG(x, ...)
   79: #endif
   80: 
   81: int net_conf_dhcp(void)
   82: {
   83:         /*
   84:          * Configuration of network addresses using DHCP.
   85:          *
   86:          */
   87:         NETDBG("Assign INADDR_ANY to Neta before transmitting DHCP packets.");
   88:         set_ifaddr("Neta", htonl(INADDR_ANY), htonl(INADDR_ANY));
   89: 
   90:         NETDBG("Retrieve network information from a DHCP server.");
   91:         dhclient("Neta");
   92: 
   93:         return 0;
   94: }
   95: 
   96: int net_conf_static(void)
   97: {
   98:         int index;
   99:         struct sockaddr_in sin;
  100: 
  101:         /*
  102:          * Configuration for the network interface card Neta.
  103:          *
  104:          *  - Assign a IP address and a broadcast address to Neta.
  105:          *  - Bring up lo0.
  106:          */
  107:         NETDBG("Assign a IP address to Neta.");
  108:         set_ifaddr("Neta", inet_addr(IPADDR), inet_addr(NETMASK));
  109:         NETDBG("Bring up Neta.");
  110:         if_updown("Neta", 1);
  111: 
  112:         /*
  113:          * Configuration for a default gateway.
  114:          */
  115:         NETDBG("Set a default gateway.");
  116:         index = so_ifnametoindex("Neta");
  117:         route_add(INADDR_ANY, inet_addr(GATEWAY), INADDR_ANY, index, 0);
  118: 
  119:         /*
  120:          * Configuration for DNS servers.
  121:          */
  122: #if defined(DNSSERVER1) || defined(DNSSERVER2) || defined(DNSSERVER3)
  123:         NETDBG("Configure DNS.");
  124:         memset(&sin, 0, sizeof(sin));
  125:         sin.sin_len = sizeof(sin);
  126:         sin.sin_family = AF_INET;
  127: #endif
  128: #ifdef DNSSERVER1
  129:         sin.sin_addr.s_addr = inet_addr(DNSSERVER1);
  130:         so_resctl(SO_RES_ADD_SERVER, &sin, sizeof(sin));
  131: #endif
  132: #ifdef DNSSERVER2
  133:         sin.sin_addr.s_addr = inet_addr(DNSSERVER2);
  134:         so_resctl(SO_RES_ADD_SERVER, &sin, sizeof(sin));
  135: #endif
  136: #ifdef DNSSERVER3
  137:         sin.sin_addr.s_addr = inet_addr(DNSSERVER3);
  138:         so_resctl(SO_RES_ADD_SERVER, &sin, sizeof(sin));
  139: #endif
  140: 
  141: #ifdef DNSDOMAIN
  142:         so_resctl(SO_RES_ADD_DOMAIN, DNSDOMAIN, sizeof(DNSDOMAIN)+1);
  143: #endif
  144: 
  145:         return 0;
  146: }
  147: 
  148: /*
  149:  * This function configures 
  150:  *  - IP addresses of the loopback device and the network interface card Neta,
  151:  *  - a default gateway, and 
  152:  *  - DNS servers.
  153:  *
  154:  * argument:
  155:  *   emu: 
  156:  *     - zero:     initialization for em1d
  157:  *     - non-zero: initialization for emulator
  158:  *   dhcp:
  159:  *     - zero:     static 
  160:  *     - non-zero: DHCP
  161:  * 
  162:  */
  163: int net_conf(int emu, int dhcp)
  164: {
  165:         ER ercd;
  166: 
  167:         NETDBG("Network initialization.");
  168: 
  169:         /*
  170:          * Configuration for the emulator.
  171:          *
  172:          * The LAN driver passes packets to upper layers whose size are larger
  173:          * than 60 bytes by default, otherwise the driver discards them.  The
  174:          * minimum size 60 bytes is the minimum ethernet frame size without
  175:          * CRC. So small-sized packets are padded to bring them to this length
  176:          * before transmission.
  177:          *
  178:          * But it can be possible to receive packets whose size smaller than
  179:          * 60 bytes by using a TUN device, because paddings are usually
  180:          * inserted into those small packets by a network adapter and packets
  181:          * do not go thorugh a network adapter when they are directly received
  182:          * from or sent to a TUN device.
  183:          *
  184:          * So we have to change the minimum packet size then we use the QEMU
  185:          * emulator which uses a TUN device.
  186:          */
  187:         if (emu != 0) {
  188:                 NETDBG("Set the minium packet size to 42 byte for using emulator.");
  189:                 netdrv_set_minpktsz("Neta", 42);
  190:         }
  191: 
  192:         /*
  193:          * Configuration for the loopback device lo0.
  194:          *
  195:          *  - Assign a local address 127.0.0.1 to lo0.
  196:          *  - Bring up lo0.
  197:          */
  198:         NETDBG("Assign 127.0.0.1 to lo0.");
  199:         set_ifaddr("lo0", htonl(INADDR_LOOPBACK), inet_addr("255.0.0.0"));
  200:         NETDBG("Bring up lo0.");
  201:         if_updown("lo0", 1);
  202: 
  203:         /*
  204:          * Configuration for the network interface card Neta.
  205:          *
  206:          * - Attach a device driver Neta.
  207:          * - Assign an IP address to Neta.
  208:          * - Set a default gateway address.
  209:          * - Set DNS servers.
  210:          */
  211:         NETDBG("Attach a device driver Neta.");
  212:         ercd = so_ifattach("Neta");
  213:         if (ercd < 0) {
  214:                 return ercd;
  215:         }
  216:         if (dhcp) {
  217:                 net_conf_dhcp();
  218:         }
  219:         else {
  220:                 net_conf_static();
  221:         }
  222: 
  223:         /*
  224:          * Configuration for the host table.
  225:          *
  226:          * - Add an entry for "localhost" whose IP address is 127.0.0.1.
  227:          */
  228:         NETDBG("Add an entry for localhost into the host name table.");
  229:         add_hosttable("localhost", htonl(INADDR_LOOPBACK));
  230:         add_hosttable("test", inet_addr("127.0.0.2"));
  231: 
  232:         return 0;
  233: }