Subversion Repositories HelenOS

Rev

Rev 4163 | Rev 4243 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4163 Rev 4192
Line 46... Line 46...
46
 
46
 
47
#include "../../include/sockaddr.h"
47
#include "../../include/sockaddr.h"
48
#include "../../include/socket.h"
48
#include "../../include/socket.h"
49
#include "../../netif/device.h"
49
#include "../../netif/device.h"
50
#include "../../structures/measured_strings.h"
50
#include "../../structures/measured_strings.h"
-
 
51
#include "../../structures/module_map.h"
51
#include "../../structures/packet/packet_client.h"
52
#include "../../structures/packet/packet_client.h"
52
 
53
 
53
#include "ip.h"
54
#include "ip.h"
54
#include "ip_messages.h"
55
#include "ip_messages.h"
55
#include "ip_module.h"
56
#include "ip_module.h"
56
 
57
 
57
#define DEFAULT_IPV     4
58
#define DEFAULT_IPV     4
58
 
59
 
-
 
60
#define ARP_NAME                "arp"
-
 
61
#define ARP_FILENAME            "/srv/arp"
-
 
62
 
59
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
63
#define IPC_GET_DEVICE( call )      ( device_id_t ) IPC_GET_ARG1( * call )
60
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG1( * call )
64
#define IPC_GET_PACKET( call )      ( packet_id_t ) IPC_GET_ARG1( * call )
61
#define IPC_GET_PROTO( call )       ( int ) IPC_GET_ARG1( * call )
65
#define IPC_GET_PROTO( call )       ( int ) IPC_GET_ARG1( * call )
62
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
66
#define IPC_GET_SERVICE( call )     ( services_t ) IPC_GET_ARG2( * call )
63
#define IPC_GET_STATE( call )       ( device_state_t ) IPC_GET_ARG2( * call )
67
#define IPC_GET_STATE( call )       ( device_state_t ) IPC_GET_ARG2( * call )
Line 74... Line 78...
74
void    ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall );
78
void    ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall );
75
 
79
 
76
/** Initializes the module.
80
/** Initializes the module.
77
 */
81
 */
78
int ip_initialize( void ){
82
int ip_initialize( void ){
-
 
83
    ERROR_DECLARE;
-
 
84
 
79
    ip_netifs_initialize( & ip_globals.netifs );
85
    ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
80
    ip_protos_initialize( & ip_globals.protos );
86
    ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
-
 
87
    ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
-
 
88
    ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, 0 ));
81
    return EOK;
89
    return EOK;
82
}
90
}
83
 
91
 
84
int ip_echo_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t * answer1, ipcarg_t * answer2, ipcarg_t * answer3, ipcarg_t * answer4, ipcarg_t * answer5 ){
92
int ip_echo_message( ipcarg_t arg1, ipcarg_t arg2, ipcarg_t arg3, ipcarg_t arg4, ipcarg_t arg5, ipcarg_t * answer1, ipcarg_t * answer2, ipcarg_t * answer3, ipcarg_t * answer4, ipcarg_t * answer5 ){
85
    if( answer1 ) * answer1 = arg1;
93
    if( answer1 ) * answer1 = arg1;
Line 91... Line 99...
91
}
99
}
92
 
100
 
93
int ip_device_message( device_id_t device_id, services_t service ){
101
int ip_device_message( device_id_t device_id, services_t service ){
94
    ERROR_DECLARE;
102
    ERROR_DECLARE;
95
 
103
 
96
    ip_netif_ref        ip_netif;
104
    ip_netif_ref    ip_netif;
97
    aid_t           message;
105
    aid_t           message;
98
    ipc_call_t      answer;
106
    ipc_call_t      answer;
99
    measured_string_t   configuration[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
107
    measured_string_t   configuration[ 9 ] = {{ "IPV", 3 }, { "IP_CONFIG", 9 }, { "IP_ADDR", 7 }, { "NETMASK", 7 }, { "GATEWAY", 7 }, { "BROADCAST", 9 }, { "DNS1", 4 }, { "DNS2", 4 }, { "ARP", 3 }};
100
    int         count = 9;
108
    int         count = 9;
101
    measured_string_ref settings;
109
    measured_string_ref settings;
102
    char *          data;
110
    char *          data;
-
 
111
    ipcarg_t        result;
-
 
112
    int             index;
103
 
113
 
104
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
114
    ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
105
    if( ! ip_netif ) return ENOMEM;
115
    if( ! ip_netif ) return ENOMEM;
106
    ip_netif->device_id = device_id;
116
    ip_netif->device_id = device_id;
107
    // get configuration
117
    // get configuration
Line 109... Line 119...
109
    message = async_send_2( ip_globals.networking_phone, NET_NET_GET_DEVICE_CONF, ip_netif->device_id, count, & answer );
119
    message = async_send_2( ip_globals.networking_phone, NET_NET_GET_DEVICE_CONF, ip_netif->device_id, count, & answer );
110
    // send names and get settings
120
    // send names and get settings
111
    if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
121
    if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
112
    || ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
122
    || ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
113
        async_wait_for( message, NULL );
123
        async_wait_for( message, NULL );
-
 
124
        free( ip_netif );
-
 
125
        return ERROR_CODE;
-
 
126
    }
-
 
127
    async_wait_for( message, & result );
-
 
128
    if( ERROR_OCCURRED( result )){
-
 
129
        if( settings ){
-
 
130
            free( settings );
-
 
131
            free( data );
-
 
132
        };
-
 
133
        free( ip_netif );
114
        return ERROR_CODE;
134
        return ERROR_CODE;
115
    }
135
    }
116
    if( settings ){
136
    if( settings ){
117
        if( settings[ 0 ].value ){
137
        if( settings[ 0 ].value ){
118
            ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
138
            ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
119
        }else{
139
        }else{
120
            ip_netif->ipv = DEFAULT_IPV;
140
            ip_netif->ipv = DEFAULT_IPV;
121
        }
141
        }
122
        ip_netif->dhcp = ! strcmp( settings[ 1 ].value, "DHCP" );
142
        ip_netif->dhcp = ! strncmp( settings[ 1 ].value, "dhcp", 4 );
123
        if( ip_netif->dhcp ){
143
        if( ip_netif->dhcp ){
124
            // TODO dhcp
144
            // TODO dhcp
-
 
145
            free( settings );
-
 
146
            free( data );
125
            free( ip_netif );
147
            free( ip_netif );
126
            return ENOTSUP;
148
            return ENOTSUP;
127
        }else if( ip_netif->ipv == 4 ){
149
        }else if( ip_netif->ipv == 4 ){
128
            if( ERROR_OCCURRED( inet_pton( AF_INET, settings[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
150
            if( ERROR_OCCURRED( inet_pton( AF_INET, settings[ 2 ].value, ( uint8_t * ) & ip_netif->address ))
129
            || ERROR_OCCURRED( inet_pton( AF_INET, settings[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
151
            || ERROR_OCCURRED( inet_pton( AF_INET, settings[ 3 ].value, ( uint8_t * ) & ip_netif->netmask ))
130
            || ( inet_pton( AF_INET, settings[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
152
            || ( inet_pton( AF_INET, settings[ 4 ].value, ( uint8_t * ) & ip_netif->gateway ) == EINVAL )
131
            || ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
153
            || ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
132
            || ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
154
            || ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
133
            || ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
155
            || ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
-
 
156
                free( settings );
-
 
157
                free( data );
134
                free( ip_netif );
158
                free( ip_netif );
135
                return EINVAL;
159
                return EINVAL;
136
            }
160
            }
137
        }else{
161
        }else{
138
            // TODO ipv6
162
            // TODO ipv6
-
 
163
            free( settings );
-
 
164
            free( data );
139
            free( ip_netif );
165
            free( ip_netif );
140
            return ENOTSUP;
166
            return ENOTSUP;
141
        }
167
        }
-
 
168
        if( settings[ 8 ].value ){
-
 
169
            ip_netif->arp = get_running_module( & ip_globals.modules, settings[ 8 ].value );
-
 
170
            if( ! ip_netif->arp ){
-
 
171
                printf( "\nFailed to start the arp %s", settings[ 8 ].value );
-
 
172
                free( settings );
-
 
173
                free( data );
142
        // TODO ARP module
174
                free( ip_netif );
-
 
175
                return EINVAL;
-
 
176
            }
-
 
177
        }else{
-
 
178
            ip_netif->arp = NULL;
-
 
179
        }
-
 
180
        free( settings );
-
 
181
        free( data );
143
    }
182
    }
144
    // TODO arp module
-
 
145
    // TODO register
-
 
146
    free( settings );
-
 
147
    free( data );
-
 
148
    // end request
-
 
149
    // TODO mapping
183
    // TODO mapping
-
 
184
    ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
-
 
185
    if( ip_netif->phone < 0 ){
-
 
186
        printf( "\nFailed to contact the nil service %d", service );
-
 
187
        free( ip_netif );
-
 
188
        return ip_netif->phone;
-
 
189
    }
-
 
190
    if( ip_netif->arp ){
-
 
191
        message = async_send_3( ip_netif->arp->phone, NET_ARP_DEVICE, ip_netif->device_id, SERVICE_IP, service, & answer );
-
 
192
        configuration[ 0 ].value = ( char * ) & ip_netif->address;
-
 
193
        configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
-
 
194
        if( ERROR_OCCURRED( measured_strings_send( ip_netif->arp->phone, & configuration[ 0 ], 1 ))){
-
 
195
            free( ip_netif );
-
 
196
            return ERROR_CODE;
-
 
197
        }
150
    async_wait_for( message, NULL );
198
        async_wait_for( message, & result );
-
 
199
        if( ERROR_OCCURRED( result )){
-
 
200
            free( ip_netif );
-
 
201
            return ERROR_CODE;
-
 
202
        }
-
 
203
    }
-
 
204
    index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
-
 
205
    if( index < 0 ){
-
 
206
        free( ip_netif );
-
 
207
        return index;
-
 
208
    }
-
 
209
    if( ip_netif->arp ) ++ ip_netif->arp->usage;
151
    // print the settings
210
    // print the settings
152
    printf( "\nNew device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
211
    printf( "\nNew device registered:\n\tid\t= %d\n\tphone\t= %d\n\tIPV\t= %d", ip_netif->device_id, ip_netif->phone, ip_netif->ipv );
153
    printf( "\n\tconfiguration\t= %s", ip_netif->dhcp ? "dhcp" : "static" );
212
    printf( "\n\tconfiguration\t= %s", ip_netif->dhcp ? "dhcp" : "static" );
154
    // TODO ipv6
213
    // TODO ipv6 addresses
155
    data = malloc( INET_ADDRSTRLEN );
214
    data = malloc( INET_ADDRSTRLEN );
156
    if( data ){
215
    if( data ){
157
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
216
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
158
        printf( "\n\taddress\t= %s", data );
217
        printf( "\n\taddress\t= %s", data );
159
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
218
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->netmask, data, INET_ADDRSTRLEN );
Line 166... Line 225...
166
        printf( "\n\tdns1\t= %s", data );
225
        printf( "\n\tdns1\t= %s", data );
167
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
226
        inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->dns2, data, INET_ADDRSTRLEN );
168
        printf( "\n\tdns2\t= %s", data );
227
        printf( "\n\tdns2\t= %s", data );
169
        free( data );
228
        free( data );
170
    }
229
    }
171
    // TODO mapping
-
 
172
    ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
-
 
173
    if( ERROR_OCCURRED( ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ))){
-
 
174
        free( ip_netif );
-
 
175
        return ERROR_CODE;
-
 
176
    }
-
 
177
    return EOK;
230
    return EOK;
178
}
231
}
179
 
232
 
180
void ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall ){
233
void ip_driver_receiver( ipc_callid_t iid, ipc_call_t * icall ){
181
    ERROR_DECLARE;
234
    ERROR_DECLARE;
Line 227... Line 280...
227
    printf( "\nip - device %d changed state to %d\n", device_id, state );
280
    printf( "\nip - device %d changed state to %d\n", device_id, state );
228
    return EOK;
281
    return EOK;
229
}
282
}
230
 
283
 
231
int ip_register_message( int protocol, int phone ){
284
int ip_register_message( int protocol, int phone ){
232
    ERROR_DECLARE;
-
 
233
 
-
 
234
    ip_proto_ref    proto;
285
    ip_proto_ref    proto;
-
 
286
    int             index;
235
 
287
 
236
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
288
    proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
237
    if( ! proto ) return ENOMEM;
289
    if( ! proto ) return ENOMEM;
238
    proto->protocol = protocol;
290
    proto->protocol = protocol;
239
    proto->phone = phone;
291
    proto->phone = phone;
240
    if( ERROR_OCCURRED( ip_protos_add( & ip_globals.protos, proto->protocol, proto ))){
292
    index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
-
 
293
    if( index < 0 ){
241
        free( proto );
294
        free( proto );
242
        return ERROR_CODE;
295
        return index;
243
    }
296
    }
244
    printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d", proto->protocol, proto->phone );
297
    printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d", proto->protocol, proto->phone );
245
    return EOK;
298
    return EOK;
246
}
299
}
247
 
300
 
248
int ip_send_message( device_id_t device_id, packet_t packet ){
301
int ip_send_message( device_id_t device_id, packet_t packet ){
249
    // TODO send packet
302
    // TODO send packet
250
    printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
303
    printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
251
    packet_release( ip_globals.networking_phone, packet_get_id( packet ));
304
    pq_release( ip_globals.networking_phone, packet_get_id( packet ));
252
    return EOK;
305
    return EOK;
253
}
306
}
254
 
307
 
255
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
308
int ip_message( ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count ){
256
    ERROR_DECLARE;
309
    ERROR_DECLARE;
Line 267... Line 320...
267
        case NET_IL_DEVICE:
320
        case NET_IL_DEVICE:
268
            return ip_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
321
            return ip_device_message( IPC_GET_DEVICE( call ), IPC_GET_SERVICE( call ));
269
        case IPC_M_CONNECT_TO_ME:
322
        case IPC_M_CONNECT_TO_ME:
270
            return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
323
            return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
271
        case NET_IP_SEND:
324
        case NET_IP_SEND:
272
            if( ERROR_OCCURRED( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )))){
325
            ERROR_PROPAGATE( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
273
                printf( "\nIP send E %d", ERROR_CODE );
-
 
274
                return ERROR_CODE;
-
 
275
            }
-
 
276
            return ip_send_message( IPC_GET_DEVICE( call ), packet );
326
            return ip_send_message( IPC_GET_DEVICE( call ), packet );
277
    }
327
    }
278
    return ENOTSUP;
328
    return ENOTSUP;
279
}
329
}
280
 
330