Mercurial > flash_v2
comparison packages/net/tcpip/current/src/lib/network_support.c @ 103:95f3e12a6327 ecos-sw-2000-06-23
Merge from eCos master repository on 2000-06-23-16:41:10-BST
| author | jlarmour |
|---|---|
| date | Fri, 23 Jun 2000 17:06:31 +0000 |
| parents | ced4577552cd |
| children | 84e4bde58b26 |
comparison
equal
deleted
inserted
replaced
| 102:6409b6d94dd7 | 103:95f3e12a6327 |
|---|---|
| 40 //####BSDCOPYRIGHTEND#### | 40 //####BSDCOPYRIGHTEND#### |
| 41 //========================================================================== | 41 //========================================================================== |
| 42 //#####DESCRIPTIONBEGIN#### | 42 //#####DESCRIPTIONBEGIN#### |
| 43 // | 43 // |
| 44 // Author(s): gthomas | 44 // Author(s): gthomas |
| 45 // Contributors: gthomas | 45 // Contributors: gthomas, sorin@netappi.com ("Sorin Babeanu"), hmt |
| 46 // Date: 2000-01-10 | 46 // Date: 2000-01-10 |
| 47 // Purpose: | 47 // Purpose: |
| 48 // Description: | 48 // Description: |
| 49 // | 49 // |
| 50 // | 50 // |
| 68 #include <net/route.h> | 68 #include <net/route.h> |
| 69 | 69 |
| 70 #include <cyg/infra/diag.h> | 70 #include <cyg/infra/diag.h> |
| 71 #include <cyg/kernel/kapi.h> | 71 #include <cyg/kernel/kapi.h> |
| 72 | 72 |
| 73 #include <stdio.h> // for 'sprintf()' | |
| 74 | |
| 73 #include <bootp.h> | 75 #include <bootp.h> |
| 74 #include <network.h> | 76 #include <network.h> |
| 75 #include <arpa/inet.h> | 77 #include <arpa/inet.h> |
| 76 | 78 |
| 77 #ifdef CYGHWR_NET_DRIVER_ETH0 | 79 #ifdef CYGHWR_NET_DRIVER_ETH0 |
| 85 const char *eth1_name = "eth1"; | 87 const char *eth1_name = "eth1"; |
| 86 #endif | 88 #endif |
| 87 | 89 |
| 88 #define _string(s) #s | 90 #define _string(s) #s |
| 89 #define string(s) _string(s) | 91 #define string(s) _string(s) |
| 92 | |
| 93 | |
| 94 #ifdef CYGPKG_NET_NLOOP | |
| 95 #if 0 < CYGPKG_NET_NLOOP | |
| 96 // | |
| 97 // Initialize loopback interface ---------- Added by sorin@netappi.com | |
| 98 // | |
| 99 cyg_bool_t init_loopback_interface(int lo) | |
| 100 { | |
| 101 struct sockaddr_in *addrp; | |
| 102 struct ifreq ifr; | |
| 103 int s; | |
| 104 int one = 1; | |
| 105 struct ecos_rtentry route; | |
| 106 struct in_addr netmask, gateway; | |
| 107 | |
| 108 //diag_printf("Init_loopback_interface\n"); | |
| 109 s = socket(AF_INET, SOCK_DGRAM, 0); | |
| 110 if (s < 0) { | |
| 111 perror("socket"); | |
| 112 return false; | |
| 113 } | |
| 114 if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &one, sizeof(one))) { | |
| 115 perror("setsockopt"); | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 addrp = (struct sockaddr_in *) &ifr.ifr_addr; | |
| 120 memset(addrp, 0, sizeof(*addrp)); | |
| 121 addrp->sin_family = AF_INET; | |
| 122 addrp->sin_len = sizeof(*addrp); | |
| 123 addrp->sin_port = 0; | |
| 124 // Make an address 127.0.<lo>.1 to manage multiple loopback ifs. | |
| 125 // (There is normally only 1, so it's the standard 127.0.0.1) | |
| 126 addrp->sin_addr.s_addr = htonl((0x100 * lo) + INADDR_LOOPBACK) ; | |
| 127 | |
| 128 // Init the one we were told to | |
| 129 sprintf(ifr.ifr_name, "lo%d", lo); | |
| 130 | |
| 131 if (ioctl(s, SIOCSIFADDR, &ifr)) { | |
| 132 perror("SIOCIFADDR"); | |
| 133 return false; | |
| 134 } | |
| 135 | |
| 136 #if 1 < CYGPKG_NET_NLOOP | |
| 137 // We cheat to make different nets for multiple loopback devs | |
| 138 addrp->sin_addr.s_addr = netmask.s_addr = htonl(IN_CLASSC_NET); | |
| 139 #else | |
| 140 // | |
| 141 addrp->sin_addr.s_addr = netmask.s_addr = htonl(IN_CLASSA_NET); | |
| 142 #endif | |
| 143 if (ioctl(s, SIOCSIFNETMASK, &ifr)) { | |
| 144 perror("SIOCSIFNETMASK"); | |
| 145 return false; | |
| 146 } | |
| 147 ifr.ifr_flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING; | |
| 148 if (ioctl(s, SIOCSIFFLAGS, &ifr)) { | |
| 149 perror("SIOCSIFFLAGS"); | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 gateway.s_addr = htonl(INADDR_LOOPBACK); | |
| 154 memset(&route, 0, sizeof(route)); | |
| 155 addrp->sin_family = AF_INET; | |
| 156 addrp->sin_port = 0; | |
| 157 addrp->sin_addr.s_addr = htonl((0x100 * lo) + INADDR_LOOPBACK) & netmask.s_addr; | |
| 158 memcpy(&route.rt_dst, addrp, sizeof(*addrp)); | |
| 159 addrp->sin_addr = netmask; | |
| 160 memcpy(&route.rt_genmask, addrp, sizeof(*addrp)); | |
| 161 addrp->sin_addr = gateway; | |
| 162 memcpy(&route.rt_gateway, addrp, sizeof(*addrp)); | |
| 163 | |
| 164 route.rt_dev = ifr.ifr_name; | |
| 165 route.rt_flags = RTF_UP|RTF_GATEWAY; | |
| 166 route.rt_metric = 0; | |
| 167 | |
| 168 if (ioctl(s, SIOCADDRT, &route)) { | |
| 169 diag_printf("Route - dst: %s", inet_ntoa(((struct sockaddr_in *)&route.rt_dst)->sin_addr)); | |
| 170 diag_printf(", mask: %s", inet_ntoa(((struct sockaddr_in *)&route.rt_genmask)->sin_addr)); | |
| 171 diag_printf(", gateway: %s\n", inet_ntoa(((struct sockaddr_in *)&route.rt_gateway)->sin_addr)); | |
| 172 if (errno != EEXIST) { | |
| 173 perror("SIOCADDRT 3"); | |
| 174 return false; | |
| 175 } | |
| 176 } | |
| 177 close(s); | |
| 178 return true; | |
| 179 } | |
| 180 #endif | |
| 181 #endif | |
| 182 | |
| 90 | 183 |
| 91 #if defined(CYGHWR_NET_DRIVER_ETH0_ADDRS_IP) \ | 184 #if defined(CYGHWR_NET_DRIVER_ETH0_ADDRS_IP) \ |
| 92 || defined(CYGHWR_NET_DRIVER_ETH1_ADDRS_IP) | 185 || defined(CYGHWR_NET_DRIVER_ETH1_ADDRS_IP) |
| 93 // | 186 // |
| 94 // Internal function which builds up a fake BOOTP database for | 187 // Internal function which builds up a fake BOOTP database for |
| 150 // Initialize network interface[s] using BOOTP/DHCP | 243 // Initialize network interface[s] using BOOTP/DHCP |
| 151 // | 244 // |
| 152 void | 245 void |
| 153 init_all_network_interfaces(void) | 246 init_all_network_interfaces(void) |
| 154 { | 247 { |
| 248 static volatile int in_init_all_network_interfaces = 0; | |
| 249 | |
| 250 cyg_scheduler_lock(); | |
| 251 while ( in_init_all_network_interfaces ) { | |
| 252 // Another thread is doing this... | |
| 253 cyg_scheduler_unlock(); | |
| 254 cyg_thread_delay( 10 ); | |
| 255 cyg_scheduler_lock(); | |
| 256 } | |
| 257 in_init_all_network_interfaces = 1; | |
| 258 cyg_scheduler_unlock(); | |
| 259 | |
| 155 #ifdef CYGHWR_NET_DRIVER_ETH0 | 260 #ifdef CYGHWR_NET_DRIVER_ETH0 |
| 156 if ( ! eth0_up ) { // Make this call idempotent | 261 if ( ! eth0_up ) { // Make this call idempotent |
| 157 #ifdef CYGHWR_NET_DRIVER_ETH0_BOOTP | 262 #ifdef CYGHWR_NET_DRIVER_ETH0_BOOTP |
| 158 // Perform a complete initialization, using BOOTP/DHCP | 263 // Perform a complete initialization, using BOOTP/DHCP |
| 159 eth0_up = true; | 264 eth0_up = true; |
| 220 eth1_up = false; | 325 eth1_up = false; |
| 221 } | 326 } |
| 222 } | 327 } |
| 223 #endif | 328 #endif |
| 224 #endif | 329 #endif |
| 330 | |
| 331 #ifdef CYGPKG_NET_NLOOP | |
| 332 #if 0 < CYGPKG_NET_NLOOP | |
| 333 { | |
| 334 static int loop_init = 0; | |
| 335 int i; | |
| 336 if ( 0 == loop_init++ ) | |
| 337 for ( i = 0; i < CYGPKG_NET_NLOOP; i++ ) | |
| 338 init_loopback_interface( i ); | |
| 339 } | |
| 340 #endif | |
| 341 #endif | |
| 342 | |
| 343 // Open the monitor to other threads. | |
| 344 in_init_all_network_interfaces = 0; | |
| 225 } | 345 } |
