Debian Codegraph @

lwip-contrib/ports/unix/netif/slirpif.c

raw symbols SCIP
1 /* This is part of LWIPv6
2 *
3 * LWIPV6_SLIRP interface
4 * Copyright 2010 Renzo Davoli University of Bologna - Italy
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * (some code from tapif interface by Adam Dunkels <[email protected]>, BSD)
21 */
22
23
24 #include "lwip/opt.h"
25 #ifdef LWSLIRP
26
27 #include <fcntl.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <sys/types.h>
33 #include <sys/uio.h>
34 #include <sys/socket.h>
35
36 #include "lwip/debug.h"
37
38 #include "lwip/def.h"
39 #include "lwip/ip.h"
40 #include "lwip/mem.h"
41 #include "lwip/pbuf.h"
42 #include "lwip/sys.h"
43
44 #include "netif/etharp.h"
45
46 #ifdef IPv6_AUTO_CONFIGURATION
47 #include "lwip/ip_autoconf.h"
48 #endif
49
50 #if LWIP_NL
51 #include "lwip/arphdr.h"
52 #endif
53
54 #if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
55 #include "netif/tcpdump.h"
56 #endif /* LWIP_DEBUG && LWIP_TCPDUMP */
57
58 #include <sys/ioctl.h>
59 #include <sys/un.h>
60 #include <stdint.h>
61 #include <libgen.h>
62 #include <sys/poll.h>
63
64 /*-----------------------------------------------------------------------------------*/
65
66 #ifndef SLIRPIF_DEBUG
67 #define SLIRPIF_DEBUG DBG_OFF
68 #endif
69
70 #define IFNAME0 's'
71 #define IFNAME1 'l'
72
73 /*-----------------------------------------------------------------------------------*/
74
75 static const struct eth_addr ethbroadcast = { {0xff, 0xff, 0xff, 0xff, 0xff, 0xff} };
76
77 struct slirpif {
78 /* Add whatever per-interface state that is needed here. */
79 char *path; /* for future msocket extension */
80 };
81
82 /* Forward declarations. */
83 static err_t slirpif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);
84
85 /* ctl: create slirp sockets */
86 static err_t slirpif_ctl(struct netif *netif, int request, void *arg)
87 {
88 struct slirpif *slirpif = netif->state;
89
90 if (slirpif) {
91 switch (request) {
92 case NETIFCTL_CLEANUP:
93 if (slirpif->path)
94 mem_free(slirpif->path);
95 mem_free(slirpif);
96 break;
97 case NETIFCTL_SLIRPSOCK_DGRAM:
98 return socket(AF_INET6, SOCK_DGRAM, 0);
99 /* return msocket(slirpif->path, AF_INET6, SOCK_DGARM, 0); */
100 case NETIFCTL_SLIRPSOCK_STREAM:
101 return socket(AF_INET6, SOCK_STREAM, 0);
102 /* return msocket(slirpif->path, AF_INET6, SOCK_STREAM, 0); */
103 }
104 return ERR_OK;
105 }
106 }
107
108 static err_t slirpif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
109 {
110 struct pbuf *r, *q;
111 char *ptr;
112 LWIP_DEBUGF(SLIRPIF_DEBUG, ("%s: start\n",__func__));
113
114 /*printf("slirpif_output %x:%x:%x:%x\n",
115 ipaddr->addr[0],
116 ipaddr->addr[1],
117 ipaddr->addr[2],
118 ipaddr->addr[3]); */
119
120 if (! (netif->flags & NETIF_FLAG_UP)) {
121 return ERR_OK;
122 }
123
124 r = pbuf_alloc(PBUF_RAW, p->tot_len, PBUF_RAM);
125 if (r != NULL) {
126 ptr = r->payload;
127
128 for(q = p; q != NULL; q = q->next) {
129 memcpy(ptr, q->payload, q->len);
130 ptr += q->len;
131 }
132 netif->input( r, netif );
133
134 return ERR_OK;
135 }
136
137 return ERR_MEM;
138 }
139
140 /*-----------------------------------------------------------------------------------*/
141 /*
142 * slirpif_init():
143 *
144 * Should be called at the beginning of the program to set up the
145 * network interface. It calls the function low_level_init() to do the
146 * actual setup of the hardware.
147 *
148 */
149 /*-----------------------------------------------------------------------------------*/
150 err_t slirpif_init(struct netif * netif)
151 {
152 struct slirpif *slirpif;
153
154 slirpif = mem_malloc(sizeof(struct slirpif));
155 memset(slirpif, 0, sizeof(struct slirpif));
156 if (!slirpif)
157 return ERR_MEM;
158
159 netif->state = slirpif;
160 netif->name[0] = IFNAME0;
161 netif->name[1] = IFNAME1;
162 netif->link_type = NETIF_SLIRPIF;
163 netif->num=netif_next_num(netif,NETIF_SLIRPIF);
164 netif->output = slirpif_output;
165 netif->netifctl = slirpif_ctl;
166 netif->mtu = 65535;
167 /* hardware address length */
168 netif->hwaddr_len = 6;
169 netif->flags |= NETIF_FLAG_BROADCAST;
170 #if LWIP_NL
171 netif->type = ARPHRD_ETHER;
172 #endif
173
174 return ERR_OK;
175 }
176 #endif
177