Subversion Repositories HelenOS

Compare Revisions

Ignore whitespace Rev 4191 → Rev 4192

/branches/network/uspace/srv/net/il/arp/arp.c
37,9 → 37,9
 
#include <async.h>
#include <malloc.h>
#include <mem.h>
#include <rwlock.h>
#include <stdio.h>
#include <string.h>
 
#include <ipc/ipc.h>
#include <ipc/services.h>
192,6 → 192,7
ipc_call_t answer;
ipcarg_t result;
arp_proto_ref proto;
int index;
 
rwlock_write_lock( & arp_globals.lock );
// an existing device?
213,14 → 214,17
rwlock_write_unlock( & arp_globals.lock );
return ERROR_CODE;
}
if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){
index = arp_protos_add( & device->protos, proto->service, proto );
if( index < 0 ){
rwlock_write_unlock( & arp_globals.lock );
free( proto );
return ERROR_CODE;
return result;
}
}
printf( "\nCache of the existing device %d cleaned", device->device_id );
}else{
index = protocol_map( service, protocol );
if( ! index ) return ENOENT;
// create a new device
device = ( arp_device_ref ) malloc( sizeof( arp_device_t ));
if( ! device ){
227,6 → 231,7
rwlock_write_unlock( & arp_globals.lock );
return ENOMEM;
}
device->hardware = index;
device->device_id = device_id;
if( ERROR_OCCURRED( arp_protos_initialize( & device->protos ))
|| ERROR_OCCURRED( arp_proto_create( & proto, protocol, address ))){
234,15 → 239,22
free( device );
return ERROR_CODE;
}
if( ERROR_OCCURRED( arp_protos_add( & device->protos, proto->service, proto ))){
index = arp_protos_add( & device->protos, proto->service, proto );
if( index < 0 ){
rwlock_write_unlock( & arp_globals.lock );
arp_protos_destroy( & device->protos );
free( device );
return ERROR_CODE;
return index;
}
device->service = service;
// bind the new one
device->phone = bind_service( device->service, device->device_id, SERVICE_ARP, 0, arp_receiver );
if( device->phone < 0 ){
rwlock_write_unlock( & arp_globals.lock );
arp_protos_destroy( & device->protos );
free( device );
return EREFUSED;
}
// get packet dimensions
if( ERROR_OCCURRED( async_req_1_4( device->phone, NET_NIL_PACKET_SPACE, device_id, & device->addr_len, & device->prefix, & device->content, & device->suffix ))){
rwlock_write_unlock( & arp_globals.lock );
292,7 → 304,7
free( device );
return ERROR_CODE;
}
printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= %x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
printf( "\nNew device registered:\n\tid\t= %d\n\ttype\t= 0x%x\n\tservice\t= %d", device->device_id, device->hardware, device->service );
}
rwlock_write_unlock( & arp_globals.lock );
return EOK;
345,7 → 357,7
length += device->addr->length;
memcpy((( uint8_t * ) header ) + length, proto->addr->value, proto->addr->length );
length += proto->addr->length;
memset((( uint8_t * ) header ) + length, 0, device->addr->length );
bzero((( uint8_t * ) header ) + length, device->addr->length );
length += device->addr->length;
memcpy((( uint8_t * ) header ) + length, target->value, target->length );
packet_set_addr( packet, ( uint8_t * ) device->addr->value, ( uint8_t * ) device->broadcast_addr->value, CONVERT_SIZE( char, uint8_t, device->addr->length ));
428,7 → 440,7
rwlock_read_unlock( & arp_globals.lock );
}else{
rwlock_read_unlock( & arp_globals.lock );
packet_release( arp_globals.networking_phone, packet_get_id( packet ));
pq_release( arp_globals.networking_phone, packet_get_id( packet ));
}
}
return EOK;
453,13 → 465,13
int count;
arp_proto_ref proto;
 
count = arp_protos_count( & device->protos );
while( count > 0 ){
for( count = arp_protos_count( & device->protos ) - 1; count >= 0; -- count ){
proto = arp_protos_get_index( & device->protos, count );
if( proto->addr ) free( proto->addr );
if( proto->addr_data ) free( proto->addr_data );
arp_addr_destroy( & proto->addresses );
-- count;
if( proto ){
if( proto->addr ) free( proto->addr );
if( proto->addr_data ) free( proto->addr_data );
arp_addr_destroy( & proto->addresses );
}
}
arp_protos_clear( & device->protos );
}
469,8 → 481,7
arp_device_ref device;
 
rwlock_write_lock( & arp_globals.lock );
count = arp_cache_count( & arp_globals.cache );
while( count > 0 ){
for( count = arp_cache_count( & arp_globals.cache ) - 1; count >= 0; -- count ){
device = arp_cache_get_index( & arp_globals.cache, count );
if( device ){
clear_device( device );
/branches/network/uspace/srv/net/il/ip/ip.c
48,6 → 48,7
#include "../../include/socket.h"
#include "../../netif/device.h"
#include "../../structures/measured_strings.h"
#include "../../structures/module_map.h"
#include "../../structures/packet/packet_client.h"
 
#include "ip.h"
56,6 → 57,9
 
#define DEFAULT_IPV 4
 
#define ARP_NAME "arp"
#define ARP_FILENAME "/srv/arp"
 
#define IPC_GET_DEVICE( call ) ( device_id_t ) IPC_GET_ARG1( * call )
#define IPC_GET_PACKET( call ) ( packet_id_t ) IPC_GET_ARG1( * call )
#define IPC_GET_PROTO( call ) ( int ) IPC_GET_ARG1( * call )
76,8 → 80,12
/** Initializes the module.
*/
int ip_initialize( void ){
ip_netifs_initialize( & ip_globals.netifs );
ip_protos_initialize( & ip_globals.protos );
ERROR_DECLARE;
 
ERROR_PROPAGATE( ip_netifs_initialize( & ip_globals.netifs ));
ERROR_PROPAGATE( ip_protos_initialize( & ip_globals.protos ));
ERROR_PROPAGATE( modules_initialize( & ip_globals.modules ));
ERROR_PROPAGATE( add_module( NULL, & ip_globals.modules, ARP_NAME, ARP_FILENAME, SERVICE_ARP, 0 ));
return EOK;
}
 
93,7 → 101,7
int ip_device_message( device_id_t device_id, services_t service ){
ERROR_DECLARE;
 
ip_netif_ref ip_netif;
ip_netif_ref ip_netif;
aid_t message;
ipc_call_t answer;
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,6 → 108,8
int count = 9;
measured_string_ref settings;
char * data;
ipcarg_t result;
int index;
 
ip_netif = ( ip_netif_ref ) malloc( sizeof( ip_netif_t ));
if( ! ip_netif ) return ENOMEM;
111,8 → 121,18
if( ERROR_OCCURRED( measured_strings_send( ip_globals.networking_phone, configuration, count ))
|| ERROR_OCCURRED( measured_strings_return( ip_globals.networking_phone, & settings, & data, count ))){
async_wait_for( message, NULL );
free( ip_netif );
return ERROR_CODE;
}
async_wait_for( message, & result );
if( ERROR_OCCURRED( result )){
if( settings ){
free( settings );
free( data );
};
free( ip_netif );
return ERROR_CODE;
}
if( settings ){
if( settings[ 0 ].value ){
ip_netif->ipv = strtol( settings[ 0 ].value, NULL, 0 );
119,9 → 139,11
}else{
ip_netif->ipv = DEFAULT_IPV;
}
ip_netif->dhcp = ! strcmp( settings[ 1 ].value, "DHCP" );
ip_netif->dhcp = ! strncmp( settings[ 1 ].value, "dhcp", 4 );
if( ip_netif->dhcp ){
// TODO dhcp
free( settings );
free( data );
free( ip_netif );
return ENOTSUP;
}else if( ip_netif->ipv == 4 ){
131,27 → 153,64
|| ( inet_pton( AF_INET, settings[ 5 ].value, ( uint8_t * ) & ip_netif->broadcast ) == EINVAL )
|| ( inet_pton( AF_INET, settings[ 6 ].value, ( uint8_t * ) & ip_netif->dns1 ) == EINVAL )
|| ( inet_pton( AF_INET, settings[ 7 ].value, ( uint8_t * ) & ip_netif->dns2 ) == EINVAL )){
free( settings );
free( data );
free( ip_netif );
return EINVAL;
}
}else{
// TODO ipv6
free( settings );
free( data );
free( ip_netif );
return ENOTSUP;
}
// TODO ARP module
if( settings[ 8 ].value ){
ip_netif->arp = get_running_module( & ip_globals.modules, settings[ 8 ].value );
if( ! ip_netif->arp ){
printf( "\nFailed to start the arp %s", settings[ 8 ].value );
free( settings );
free( data );
free( ip_netif );
return EINVAL;
}
}else{
ip_netif->arp = NULL;
}
free( settings );
free( data );
}
// TODO arp module
// TODO register
free( settings );
free( data );
// end request
// TODO mapping
async_wait_for( message, NULL );
ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
if( ip_netif->phone < 0 ){
printf( "\nFailed to contact the nil service %d", service );
free( ip_netif );
return ip_netif->phone;
}
if( ip_netif->arp ){
message = async_send_3( ip_netif->arp->phone, NET_ARP_DEVICE, ip_netif->device_id, SERVICE_IP, service, & answer );
configuration[ 0 ].value = ( char * ) & ip_netif->address;
configuration[ 0 ].length = CONVERT_SIZE( in_addr_t, char, 1 );
if( ERROR_OCCURRED( measured_strings_send( ip_netif->arp->phone, & configuration[ 0 ], 1 ))){
free( ip_netif );
return ERROR_CODE;
}
async_wait_for( message, & result );
if( ERROR_OCCURRED( result )){
free( ip_netif );
return ERROR_CODE;
}
}
index = ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif );
if( index < 0 ){
free( ip_netif );
return index;
}
if( ip_netif->arp ) ++ ip_netif->arp->usage;
// print the settings
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 );
printf( "\n\tconfiguration\t= %s", ip_netif->dhcp ? "dhcp" : "static" );
// TODO ipv6
// TODO ipv6 addresses
data = malloc( INET_ADDRSTRLEN );
if( data ){
inet_ntop( AF_INET, ( uint8_t * ) & ip_netif->address, data, INET_ADDRSTRLEN );
168,12 → 227,6
printf( "\n\tdns2\t= %s", data );
free( data );
}
// TODO mapping
ip_netif->phone = bind_service( service, ip_netif->device_id, SERVICE_IP, 0, ip_driver_receiver );
if( ERROR_OCCURRED( ip_netifs_add( & ip_globals.netifs, ip_netif->device_id, ip_netif ))){
free( ip_netif );
return ERROR_CODE;
}
return EOK;
}
 
229,17 → 282,17
}
 
int ip_register_message( int protocol, int phone ){
ERROR_DECLARE;
 
ip_proto_ref proto;
int index;
 
proto = ( ip_proto_ref ) malloc( sizeof( ip_protos_t ));
if( ! proto ) return ENOMEM;
proto->protocol = protocol;
proto->phone = phone;
if( ERROR_OCCURRED( ip_protos_add( & ip_globals.protos, proto->protocol, proto ))){
index = ip_protos_add( & ip_globals.protos, proto->protocol, proto );
if( index < 0 ){
free( proto );
return ERROR_CODE;
return index;
}
printf( "\nNew protocol registered:\n\tprotocol\t= %d\n\tphone\t= %d", proto->protocol, proto->phone );
return EOK;
248,7 → 301,7
int ip_send_message( device_id_t device_id, packet_t packet ){
// TODO send packet
printf( "Packet to send via %d: %s", device_id, packet_get_data( packet ));
packet_release( ip_globals.networking_phone, packet_get_id( packet ));
pq_release( ip_globals.networking_phone, packet_get_id( packet ));
return EOK;
}
 
269,10 → 322,7
case IPC_M_CONNECT_TO_ME:
return ip_register_message( IPC_GET_PROTO( call ), IPC_GET_PHONE( call ));
case NET_IP_SEND:
if( ERROR_OCCURRED( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )))){
printf( "\nIP send E %d", ERROR_CODE );
return ERROR_CODE;
}
ERROR_PROPAGATE( packet_translate( ip_globals.networking_phone, & packet, IPC_GET_PACKET( call )));
return ip_send_message( IPC_GET_DEVICE( call ), packet );
}
return ENOTSUP;
/branches/network/uspace/srv/net/il/ip/Makefile
37,7 → 37,9
$(NAME).c \
$(NET_BASE)module.c \
$(NET_BASE)modules.c \
$(STRUCTURES)char_map.c \
$(STRUCTURES)measured_strings.c \
$(STRUCTURES)module_map.c \
$(STRUCTURES)packet/packet.c \
$(STRUCTURES)packet/packet_client.c \
$(NET_BASE)sockaddr.c
/branches/network/uspace/srv/net/il/ip/ip.h
42,6 → 42,8
#include "../../include/sockaddr.h"
#include "../../netif/device.h"
 
#include "../../structures/module_map.h"
 
typedef struct ip_netif ip_netif_t;
typedef ip_netif_t * ip_netif_ref;
 
57,6 → 59,7
struct ip_netif{
device_id_t device_id;
int phone;
module_ref arp;
int ipv;
int dhcp;
in_addr_t address;
77,6 → 80,7
int networking_phone;
ip_netifs_t netifs;
ip_protos_t protos;
modules_t modules;
};
 
#endif