-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwip_unix.c
executable file
·109 lines (85 loc) · 2.44 KB
/
lwip_unix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "timer.h"
#include <signal.h>
#include "mintapif.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/timers.h"
#include "lwip/dhcp.h"
/* (manual) host IP configuration */
static ip_addr_t ipaddr, netmask, gw;
/* nonstatic debug cmd option, exported in lwipopts.h */
unsigned char debug_flags;
static struct netif netif;
static sigset_t mask, oldmask, empty;
static void printhost(void)
{
char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
strncpy(ip_str, ipaddr_ntoa(&ipaddr), sizeof(ip_str));
strncpy(nm_str, ipaddr_ntoa(&netmask), sizeof(nm_str));
strncpy(gw_str, ipaddr_ntoa(&gw), sizeof(gw_str));
printf("Host at %s mask %s gateway %s\n", ip_str, nm_str, gw_str);
}
static int dhcp;
void lwip_initlwip(int usedhcp)
{
/* startup defaults (may be overridden by one or more opts) */
IP4_ADDR(&gw, 192,168,0,1);
IP4_ADDR(&ipaddr, 192,168,0,2);
IP4_ADDR(&netmask, 255,255,255,0);
/* use debug flags defined by debug.h */
debug_flags = LWIP_DBG_OFF;
#ifdef PERF
perf_init("/tmp/minimal.perf");
#endif /* PERF */
lwip_init();
printf("TCP/IP initialized.\n");
netif_add(&netif, &ipaddr, &netmask, &gw, NULL, mintapif_init, ethernet_input);
netif_set_default(&netif);
timer_init();
timer_set_interval(TIMER_EVT_ETHARPTMR, ARP_TMR_INTERVAL / 10);
if(usedhcp){
dhcp = 1;
dhcp_start(&netif);
printf("DHCP started\n");
} else {
dhcp = 0;
netif_set_up(&netif);
printhost();
}
printf("Applications started.\n");
}
void lwip_handler()
{
/* poll for input packet and ensure
select() or read() arn't interrupted */
sigemptyset(&mask);
sigaddset(&mask, SIGALRM);
sigprocmask(SIG_BLOCK, &mask, &oldmask);
/* start of critical section,
poll netif, pass packet to lwIP */
if (mintapif_select(&netif) > 0)
{
/* work, immediatly end critical section
hoping lwIP ended quickly ... */
sigprocmask(SIG_SETMASK, &oldmask, NULL);
}
else
{
/* no work, wait a little (10 msec) for SIGALRM */
sigemptyset(&empty);
sigsuspend(&empty);
/* ... end critical section */
sigprocmask(SIG_SETMASK, &oldmask, NULL);
}
sys_check_timeouts();
if((dhcp == 1) && (netif.dhcp->state == DHCP_BOUND)){
dhcp = 2;
ipaddr = netif.ip_addr;
netmask = netif.netmask;
gw = netif.gw;
printhost();
}
}