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