]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ankh/examples/lwip/main.c
update
[l4.git] / l4 / pkg / ankh / examples / lwip / main.c
1 #include <assert.h>
2 #include <getopt.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <pthread-l4.h>
7
8 #include <l4/util/util.h>
9 #include <l4/ankh/client-c.h>
10 #include <l4/ankh/lwip-ankh.h>
11 #include <l4/ankh/session.h>
12 /* 
13  * Need to include this file before others.
14  * Sets our byteorder.
15  */
16 #include "arch/cc.h"
17
18 #include <unistd.h>
19 #include <netdb.h>
20
21 #include "netif/etharp.h"
22 #include <lwip-util.h>
23 #include "lwip/dhcp.h"
24
25 #define GETOPT_LIST_END { 0, 0, 0, 0 }
26
27 enum options
28 {
29         BUF_SIZE,
30         SHM_NAME,
31 };
32
33 /* Network interface variables */
34 ip_addr_t ipaddr, netmask, gw;
35 struct netif netif;
36 /* Set network address variables */
37 #if 0
38 IP4_ADDR(&gw, 192,168,0,1);
39 IP4_ADDR(&ipaddr, 192,168,0,2);
40 IP4_ADDR(&netmask, 255,255,255,0);
41 #endif
42
43 struct netif my_netif;
44 extern err_t ankhif_init(struct netif*);
45
46 static ankh_config_info cfg = { 1024, L4_INVALID_CAP, L4_INVALID_CAP, "" };
47
48
49 static void do_lookup(char *hostname)
50 {
51         ip_addr_t i;
52         struct hostent *h = gethostbyname(hostname);
53
54         printf("gethostbyname(%s): %s\n", hostname, h ? "SUCCESS" : "FAILED");
55         if (h) {
56                 i.addr = *(u32_t*)h->h_addr_list[0];
57                 printf("   h_name: '%s'\n", h->h_name);
58                 printf("   --> "); lwip_util_print_ip(&i); printf("\n");
59         }
60 }
61
62
63 static void dns_test(void)
64 {
65         do_lookup("os.inf.tu-dresden.de");
66         do_lookup("www.heise.de");
67         do_lookup("www.twitter.com");
68         do_lookup("www.tudos.org");
69         do_lookup("www.ifsr.de");
70         do_lookup("www.dynamo-dresden.de");
71         do_lookup("www.spiegel.de");
72         do_lookup("www.smoking-gnu.de");
73         do_lookup("www.fail.fx");
74 }
75
76
77 static void server(void)
78 {
79         int err, fd;
80         struct sockaddr_in in, clnt;
81         in.sin_family = AF_INET;
82         in.sin_port = htons(3000);
83         in.sin_addr.s_addr = my_netif.ip_addr.addr;
84
85         int sock = socket(PF_INET, SOCK_STREAM, 0);
86         printf("socket created: %d\n", sock);
87
88         err = bind(sock, (struct sockaddr*)&in, sizeof(in));
89         printf("bound to addr: %d\n", err);
90
91         err = listen(sock, 10);
92         printf("listen(): %d\n", err);
93
94         for (;;) {
95                 char buf[1024];
96                 socklen_t clnt_size = sizeof(clnt);
97                 fd = accept(sock, (struct sockaddr*)&clnt, &clnt_size);
98
99                 printf("Got connection from  ");
100                 lwip_util_print_ip((ip_addr_t*)&clnt.sin_addr); printf("\n");
101
102                 err = read(fd, buf, sizeof(buf));
103                 printf("Read from fd %d (err %d)\n", fd, err);
104                 printf("%s", buf);
105         }
106 }
107
108
109 int main(int argc, char **argv)
110 {
111         if (l4ankh_init())
112           return 1;
113
114         l4_cap_idx_t c = pthread_getl4cap(pthread_self());
115         cfg.send_thread = c;
116
117         static struct option long_opts[] = {
118                 {"bufsize", 1, 0, BUF_SIZE },
119                 {"shm", 1, 0, SHM_NAME },
120                 GETOPT_LIST_END
121         };
122
123         while (1) {
124                 int optind = 0;
125                 int opt = getopt_long(argc, argv, "b:s:", long_opts, &optind);
126                 printf("getopt: %d\n", opt);
127
128                 if (opt == -1)
129                         break;
130
131                 switch(opt) {
132                         case BUF_SIZE:
133                                 printf("buf size: %d\n", atoi(optarg));
134                                 cfg.bufsize = atoi(optarg);
135                                 break;
136                         case SHM_NAME:
137                                 printf("shm name: %s\n", optarg);
138                                 snprintf(cfg.shm_name, CFG_SHM_NAME_SIZE, "%s", optarg);
139                                 break;
140                         default:
141                                 break;
142                 }
143         }
144
145         // Start the TCP/IP thread & init stuff
146         tcpip_init(NULL, NULL);
147         struct netif *n = netif_add(&my_netif,
148                                     &ipaddr, &netmask, &gw,
149                                     &cfg, // configuration state
150                                     ankhif_init, ethernet_input);
151
152         printf("netif_add: %p (%p)\n", n, &my_netif);
153         assert(n == &my_netif);
154
155         printf("dhcp_start()\n");
156         dhcp_start(&my_netif);
157         printf("dhcp started\n");
158
159         while (!netif_is_up(&my_netif))
160                 l4_sleep(1000);
161         printf("Network interface is up.\n");
162
163         printf("IP: "); lwip_util_print_ip(&my_netif.ip_addr); printf("\n");
164         printf("GW: "); lwip_util_print_ip(&my_netif.gw); printf("\n");
165
166         dns_test();
167         server();
168
169         return 0;
170 }