Rev 4350 | Rev 4506 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3466 | mejdrech | 1 | /* |
3912 | mejdrech | 2 | * Copyright (c) 2009 Lukas Mejdrech |
3466 | mejdrech | 3 | * All rights reserved. |
4 | * |
||
5 | * Redistribution and use in source and binary forms, with or without |
||
6 | * modification, are permitted provided that the following conditions |
||
7 | * are met: |
||
8 | * |
||
9 | * - Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * - Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * - The name of the author may not be used to endorse or promote products |
||
15 | * derived from this software without specific prior written permission. |
||
16 | * |
||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
27 | */ |
||
28 | |||
3912 | mejdrech | 29 | /** @addtogroup ip |
30 | * @{ |
||
3466 | mejdrech | 31 | */ |
32 | |||
33 | /** @file |
||
34 | */ |
||
3666 | mejdrech | 35 | |
3466 | mejdrech | 36 | #include <async.h> |
37 | #include <errno.h> |
||
38 | #include <stdio.h> |
||
4327 | mejdrech | 39 | #include <string.h> |
3846 | mejdrech | 40 | |
3466 | mejdrech | 41 | #include <ipc/ipc.h> |
42 | #include <ipc/services.h> |
||
43 | |||
3886 | mejdrech | 44 | #include "../../err.h" |
45 | #include "../../messages.h" |
||
46 | #include "../../modules.h" |
||
3466 | mejdrech | 47 | |
4307 | mejdrech | 48 | #include "../../include/net_interface.h" |
3886 | mejdrech | 49 | #include "../../include/sockaddr.h" |
50 | #include "../../include/socket.h" |
||
4243 | mejdrech | 51 | #include "../../include/device.h" |
4307 | mejdrech | 52 | #include "../../include/arp_interface.h" |
53 | #include "../../include/nil_interface.h" |
||
54 | #include "../../include/il_interface.h" |
||
55 | #include "../../include/ip_interface.h" |
||
3886 | mejdrech | 56 | #include "../../structures/measured_strings.h" |
4192 | mejdrech | 57 | #include "../../structures/module_map.h" |
3901 | mejdrech | 58 | #include "../../structures/packet/packet_client.h" |
3846 | mejdrech | 59 | |
4307 | mejdrech | 60 | #include "../../nil/nil_messages.h" |
61 | |||
62 | #include "../il_messages.h" |
||
63 | |||
3466 | mejdrech | 64 | #include "ip.h" |
3846 | mejdrech | 65 | #include "ip_module.h" |
3466 | mejdrech | 66 | |
3846 | mejdrech | 67 | #define DEFAULT_IPV 4 |
3685 | mejdrech | 68 | |
4192 | mejdrech | 69 | #define ARP_NAME "arp" |
70 | #define ARP_FILENAME "/srv/arp" |
||
71 | |||
3666 | mejdrech | 72 | ip_globals_t ip_globals; |
3466 | mejdrech | 73 | |
3666 | mejdrech | 74 | DEVICE_MAP_IMPLEMENT( ip_netifs, ip_netif_t ) |
75 | |||
3846 | mejdrech | 76 | INT_MAP_IMPLEMENT( ip_protos, ip_proto_t ) |
3685 | mejdrech | 77 | |
4307 | mejdrech | 78 | int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ); |
79 | int ip_register( int il_phone, int protocol, int phone ); |
||
3846 | mejdrech | 80 | |
3466 | mejdrech | 81 | /** Initializes the module. |
82 | */ |
||
4351 | mejdrech | 83 | int ip_initialize( async_client_conn_t client_connection ){ |
4192 | mejdrech | 84 | ERROR_DECLARE; |
85 | |||
86 | ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs )); |
||
87 | ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos )); |
||
4351 | mejdrech | 88 | ip_globals.client_connection = client_connection; |
4192 | mejdrech | 89 | ERROR_PROPAGATE( modules_initialize( & ip_globals.modules )); |
4307 | mejdrech | 90 | ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, arp_task_get_id(), arp_connect_module )); |
3466 | mejdrech | 91 | return EOK; |
92 | } |
||
93 | |||
4350 | mejdrech | 94 | int ip_device_req( int il_phone, device_id_t device_id, services_t netif ){ |
3666 | mejdrech | 95 | ERROR_DECLARE; |
96 | |||
4307 | mejdrech | 97 | ip_netif_ref ip_netif; |
98 | measured_string_t names[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }}; |
||
99 | measured_string_ref configuration; |
||
100 | int count = 9; |
||
101 | char * data; |
||
102 | int index; |
||
3666 | mejdrech | 103 | |
4307 | mejdrech | 104 | configuration = & names[ 0 ]; |
3846 | mejdrech | 105 | ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t )); |
106 | if( ! ip_netif ) return ENOMEM; |
||
107 | ip_netif->device_id = device_id; |
||
108 | // get configuration |
||
4307 | mejdrech | 109 | ERROR_PROPAGATE( net_get_device_conf_req( ip_globals.net_phone, ip_netif->device_id, & configuration, count, & data )); |
110 | if( configuration ){ |
||
111 | if( configuration[ 0 ].value ){ |
||
112 | ip_netif->ipv = strtol( configuration[ 0 ].value, NULL, 0 ); |
||
3846 | mejdrech | 113 | }else{ |
114 | ip_netif->ipv = DEFAULT_IPV; |
||
115 | } |
||
4332 | mejdrech | 116 | ip_netif->dhcp = ! str_lcmp( configuration[ 1 ].value, "dhcp", configuration[ 1 ].length ); |
3846 | mejdrech | 117 | if( ip_netif->dhcp ){ |
118 | // TODO dhcp |
||
4307 | mejdrech | 119 | net_free_settings( configuration, data ); |
3846 | mejdrech | 120 | free( ip_netif ); |
121 | return ENOTSUP; |
||
122 | }else if( ip_netif->ipv == 4 ){ |
||
4307 | mejdrech | 123 | if( ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 2 ].value, ( uint8_t * ) & ip_netif->address )) |
124 | || ERROR_OCCURRED( inet_pton( AF_INET, configuration[ 3 ].value, ( uint8_t * ) & ip_netif->netmask )) |
||
125 | || ( inet_pton( AF_INET, configuration[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL ) |
||
126 | || ( inet_pton( AF_INET, configuration[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL ) |
||
127 | || ( inet_pton( AF_INET, configuration[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL ) |
||
128 | || ( inet_pton( AF_INET, configuration[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){ |
||
129 | net_free_settings( configuration, data ); |
||
3846 | mejdrech | 130 | free( ip_netif ); |
131 | return EINVAL; |
||
132 | } |
||
133 | }else{ |
||
134 | // TODO ipv6 |
||
4307 | mejdrech | 135 | net_free_settings( configuration, data ); |
3846 | mejdrech | 136 | free( ip_netif ); |
137 | return ENOTSUP; |
||
138 | } |
||
4307 | mejdrech | 139 | if( configuration[ 8 ].value ){ |
140 | ip_netif->arp = get_running_module( & ip_globals.modules, configuration[ 8 ].value ); |
||
4192 | mejdrech | 141 | if( ! ip_netif->arp ){ |
4307 | mejdrech | 142 | printf( "Failed to start the arp %s\n", configuration[ 8 ].value ); |
143 | net_free_settings( configuration, data ); |
||
4192 | mejdrech | 144 | free( ip_netif ); |
145 | return EINVAL; |
||
146 | } |
||
147 | }else{ |
||
148 | ip_netif->arp = NULL; |
||
149 | } |
||
4307 | mejdrech | 150 | net_free_settings( configuration, data ); |
3846 | mejdrech | 151 | } |
4351 | mejdrech | 152 | ip_netif->phone = bind_service( netif, ip_netif->device_id, SERVICE_IP, 0, ip_globals.client_connection ); |
4192 | mejdrech | 153 | if( ip_netif->phone < 0 ){ |
4350 | mejdrech | 154 | printf( "Failed to contact the nil service %d\n", netif ); |
4192 | mejdrech | 155 | free( ip_netif ); |
156 | return ip_netif->phone; |
||
157 | } |
||
158 | if( ip_netif->arp ){ |
||
159 | configuration[ 0 ].value = ( char * ) & ip_netif->address; |
||
160 | configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 ); |
||
4350 | mejdrech | 161 | if( ERROR_OCCURRED( arp_device_req( ip_netif->arp->phone, ip_netif->device_id, SERVICE_IP, netif, & configuration[ 0 ] ))){ |
4192 | mejdrech | 162 | free( ip_netif ); |
163 | return ERROR_CODE; |
||
164 | } |
||
165 | } |
||
166 | index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ); |
||
167 | if( index < 0 ){ |
||
168 | free( ip_netif ); |
||
169 | return index; |
||
170 | } |
||
171 | if( ip_netif->arp ) ++ ip_netif->arp->usage; |
||
3846 | mejdrech | 172 | // print the settings |
4307 | mejdrech | 173 | printf( "New device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d\n", ip_netif->device_id, ip_netif->phone, ip_netif->ipv ); |
174 | printf( "\tconfiguration\t= %s\n", ip_netif->dhcp ? "dhcp" : "static" ); |
||
4192 | mejdrech | 175 | // TODO ipv6 addresses |
3846 | mejdrech | 176 | data = malloc( INET_ADDRSTRLEN ); |
177 | if( data ){ |
||
178 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN ); |
||
4307 | mejdrech | 179 | printf( "\taddress\t= %s\n", data ); |
3846 | mejdrech | 180 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN ); |
4307 | mejdrech | 181 | printf( "\tnetmask\t= %s\n", data ); |
3846 | mejdrech | 182 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->gateway, data, INET_ADDRSTRLEN ); |
4307 | mejdrech | 183 | printf( "\tgateway\t= %s\n", data ); |
3846 | mejdrech | 184 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->broadcast, data, INET_ADDRSTRLEN ); |
4307 | mejdrech | 185 | printf( "\tbroadcast\t= %s\n", data ); |
3846 | mejdrech | 186 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns1, data, INET_ADDRSTRLEN ); |
4307 | mejdrech | 187 | printf( "\tdns1\t= %s\n", data ); |
3846 | mejdrech | 188 | inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN ); |
4307 | mejdrech | 189 | printf( "\tdns2\t= %s\n", data ); |
3846 | mejdrech | 190 | free( data ); |
191 | } |
||
192 | return EOK; |
||
193 | } |
||
194 | |||
4307 | mejdrech | 195 | int ip_device_state_msg( int il_phone, device_id_t device_id, device_state_t state ){ |
4332 | mejdrech | 196 | // ERROR_DECLARE; |
4243 | mejdrech | 197 | |
198 | ip_netif_ref netif; |
||
199 | |||
4332 | mejdrech | 200 | /* measured_string_t address; |
4243 | mejdrech | 201 | measured_string_ref translation; |
202 | char * data; |
||
4332 | mejdrech | 203 | */ |
4243 | mejdrech | 204 | netif = ip_netifs_find( & ip_globals.netifs, device_id ); |
205 | if( ! netif ) return ENOENT; |
||
3846 | mejdrech | 206 | // TODO state |
4307 | mejdrech | 207 | printf( "ip - device %d changed state to %d\n\n", device_id, state ); |
4332 | mejdrech | 208 | /* if( netif->arp ){ |
4243 | mejdrech | 209 | address.value = ( char * ) & netif->gateway; |
210 | address.length = CONVERT_SIZE( in_addr_t, char, 1 ); |
||
4327 | mejdrech | 211 | if( ERROR_OCCURRED( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data ))){ |
212 | sleep( 2 ); |
||
213 | ERROR_PROPAGATE( arp_translate_req( netif->arp->phone, netif->device_id, SERVICE_IP, & address, & translation, & data )); |
||
214 | } |
||
4307 | mejdrech | 215 | printf( "\tgateway translated to\t= %X:%X:%X:%X:%X:%X\n", data[ 0 ], data[ 1 ], data[ 2 ], data[ 3 ], data[ 4 ], data[ 5 ] ); |
4243 | mejdrech | 216 | free( translation ); |
217 | free( data ); |
||
218 | } |
||
4332 | mejdrech | 219 | */ return EOK; |
3846 | mejdrech | 220 | } |
221 | |||
4307 | mejdrech | 222 | int ip_bind_service( services_t service, services_t me, async_client_conn_t receiver ){ |
223 | //TODO receive function |
||
224 | return EOK; |
||
225 | } |
||
226 | |||
227 | int ip_connect_module( services_t service ){ |
||
228 | return EOK; |
||
229 | } |
||
230 | |||
231 | int ip_register( int il_phone, int protocol, int phone ){ |
||
3846 | mejdrech | 232 | ip_proto_ref proto; |
4192 | mejdrech | 233 | int index; |
3846 | mejdrech | 234 | |
235 | proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t )); |
||
236 | if( ! proto ) return ENOMEM; |
||
237 | proto->protocol = protocol; |
||
238 | proto->phone = phone; |
||
4192 | mejdrech | 239 | index = ip_protos_add( & ip_globals.protos, proto->protocol, proto ); |
240 | if( index < 0 ){ |
||
3846 | mejdrech | 241 | free( proto ); |
4192 | mejdrech | 242 | return index; |
3846 | mejdrech | 243 | } |
4307 | mejdrech | 244 | printf( "New protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d\n", proto->protocol, proto->phone ); |
3846 | mejdrech | 245 | return EOK; |
246 | } |
||
247 | |||
4307 | mejdrech | 248 | int ip_send_msg( int il_phone, device_id_t device_id, packet_t packet, services_t target ){ |
3846 | mejdrech | 249 | // TODO send packet |
4307 | mejdrech | 250 | printf( "Packet to send via %d: %s\n", device_id, packet_get_data( packet )); |
251 | pq_release( ip_globals.net_phone, packet_get_id( packet )); |
||
3846 | mejdrech | 252 | return EOK; |
253 | } |
||
254 | |||
255 | int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){ |
||
256 | ERROR_DECLARE; |
||
257 | |||
258 | packet_t packet; |
||
259 | |||
260 | * answer_count = 0; |
||
261 | switch( IPC_GET_METHOD( * call )){ |
||
3466 | mejdrech | 262 | case IPC_M_PHONE_HUNGUP: |
263 | return EOK; |
||
3666 | mejdrech | 264 | case NET_IL_DEVICE: |
4307 | mejdrech | 265 | return ip_device_req( 0, IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call )); |
3846 | mejdrech | 266 | case IPC_M_CONNECT_TO_ME: |
4307 | mejdrech | 267 | return ip_register( 0, IL_GET_PROTO( call ), IPC_GET_PHONE( call )); |
268 | case NET_IL_SEND: |
||
269 | ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
270 | return ip_send_msg( 0, IPC_GET_DEVICE( call ), packet, 0 ); |
||
4351 | mejdrech | 271 | case NET_IL_DEVICE_STATE: |
272 | case NET_NIL_DEVICE_STATE: |
||
273 | return ip_device_state_msg( 0, IPC_GET_DEVICE( call ), IPC_GET_STATE( call )); |
||
274 | // TODO packet received |
||
275 | case NET_IL_RECEIVED: |
||
276 | case NET_NIL_RECEIVED: |
||
277 | ERROR_PROPAGATE( packet_translate( ip_globals.net_phone, & packet, IPC_GET_PACKET( call ))); |
||
278 | //return il_receive_msg( 0, IPC_GET_DEVICE( call ), packet ); |
||
3466 | mejdrech | 279 | } |
280 | return ENOTSUP; |
||
281 | } |
||
282 | |||
283 | /** @} |
||
284 | */ |