/branches/network/uspace/srv/net/structures/packet/packet_queue.h |
---|
0,0 → 1,61 |
/* |
* Copyright (c) 2008 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup net |
* @{ |
*/ |
/** @file |
*/ |
#ifndef __NET_PACKET_QUEUE__ |
#define __NET_PACKET_QUEUE__ |
#include "packet.h" |
typedef struct pq_item pq_item_t; |
typedef pq_item * pq_item_ref; |
typedef pq_item_ref pq_head_t; |
typedef pq_head * pq_head_ref; |
struct pq_item{ |
int order; |
size_t metric; |
packet_t packet; |
pq_item_ref previous; |
pq_item_ref next; |
}; |
pq_head_t pq_create( packet_t packet, int order, size_t metric ); |
pq_item_ref pq_add( pq_head_ref queue, packet_t packet, int order, size_t metric ); |
void pq_destroy( pq_head_t queue ); |
#endif |
/** @} |
*/ |
/branches/network/uspace/srv/net/structures/packet/packet.c |
---|
0,0 → 1,158 |
/* |
* Copyright (c) 2008 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup net |
* @{ |
*/ |
/** @file |
*/ |
#include <errno.h> |
#include <unistd.h> |
#include <string.h> |
#include <ipc/ipc.h> |
#include <sys/mman.h> |
#include "../../err.h" |
#include "packet.h" |
#define PACKET_MAGIC_VALUE 0x11227788 |
struct packet{ |
size_t length; |
size_t max_prefix; |
size_t max_content; |
size_t data_start; |
size_t data_end; |
int magic_value; |
}; |
int packet_is_valid( packet_t packet ); |
packet_t packet_create( size_t max_prefix, size_t max_content, size_t max_sufix ){ |
size_t length; |
packet_t packet; |
length = max_prefix + max_content + max_sufix; |
packet = ( packet_t ) mmap( NULL, sizeof( struct packet ) + length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
if( packet == MAP_FAILED ) return NULL; |
packet->length = length; |
packet->max_prefix = max_prefix; |
packet->max_content = max_content; |
packet->data_start = sizeof( struct packet ) + packet->max_prefix; |
packet->data_end = packet->data_start; |
packet->magic_value = PACKET_MAGIC_VALUE; |
return packet; |
} |
int packet_is_valid( packet_t packet ){ |
return packet && ( packet->magic_value == PACKET_MAGIC_VALUE ); |
} |
packet_t packet_copy( packet_t packet ){ |
packet_t new; |
if( ! packet_is_valid( packet )) return NULL; |
new = ( packet_t ) mmap( NULL, packet->length, PROTO_READ | PROTO_WRITE, MAP_SHARED | MAP_ANONYMOUS, 0, 0 ); |
if( new == MAP_FAILED ) return NULL; |
memcpy( new, packet, packet->length ); |
return new; |
} |
int packet_copy_data( packet_t packet, void * data, size_t length ){ |
if( ! packet_is_valid( packet )) return EINVAL; |
if( packet->data_start + length >= (( size_t ) packet ) + packet->length ) return ENOMEM; |
memcpy( packet + packet->data_start, data, length ); |
if( packet->data_start + length > packet->data_end ){ |
packet->data_end = packet->data_start + length; |
} |
return EOK; |
} |
void * packet_prepend( packet_t packet, size_t length ){ |
if(( ! packet_is_valid( packet )) || ( packet->data_start - sizeof( struct packet ) < length )) return NULL; |
packet->data_start -= length; |
return packet + packet->data_start; |
} |
void * packet_append( packet_t packet, size_t length ){ |
if(( ! packet_is_valid( packet )) || ( packet->data_end + length >= (( size_t ) packet ) + packet->length )) return NULL; |
packet->data_end += length; |
return packet + packet->data_end - length; |
} |
int packet_trim( packet_t packet, size_t prefix, size_t sufix ){ |
if(( ! packet_is_valid( packet )) || ( prefix + sufix > packet->data_end - packet->data_start )) return EINVAL; |
packet->data_start += prefix; |
packet->data_end -= sufix; |
return EOK; |
} |
int packet_send( packet_t packet, int phone ){ |
if( ! packet_is_valid( packet )) return EINVAL; |
return ipc_share_out_start( phone, packet, PROTO_READ | PROTO_WRITE ); |
} |
int packet_receive( packet_ref packet ){ |
ERROR_DECLARE; |
ipc_callid_t callid; |
size_t size; |
int flags; |
if( ! packet ) return EINVAL; |
if( ! ipc_share_out_receive( & callid, & size, & flags )) return EINVAL; |
* packet = ( packet_t ) as_get_mappable_page( size ); |
if( !( * packet )) return ENOMEM; |
if( ERROR_OCCURED( ipc_share_out_finalize( callid, * packet ))){ |
munmap( * packet, size ); |
return ERROR_CODE; |
} |
return EOK; |
} |
int packet_destroy( packet_t packet ){ |
if( ! packet_is_valid( packet )) return EINVAL; |
return munmap( packet, sizeof( struct packet ) + packet->length ); |
} |
size_t packet_get_data_length( packet_t packet ){ |
if( ! packet_is_valid( packet )) return 0; |
return packet->data_end - packet->data_start; |
} |
void * packet_get_data( packet_t packet ){ |
if( ! packet_is_valid( packet )) return NULL; |
return packet + packet->data_start; |
} |
/** @} |
*/ |
/branches/network/uspace/srv/net/structures/packet/packet_queue.c |
---|
0,0 → 1,110 |
/* |
* Copyright (c) 2008 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup net |
* @{ |
*/ |
/** @file |
*/ |
#include <malloc.h> |
#include "../../configuration.h" |
#include "packet.h" |
#include "packet_queue.h" |
#define PQ_MAGIC_VALUE 0x23465968; |
static inline int pq_is_valid( pq_item_ref item ); |
pq_item_ref pq_add( pq_head * queue, packet_t packet, int order, size_t metric ){ |
pq_item_ref item; |
pq_item_ref tmp; |
tmp = pq_create( packet, order, metric ); |
if( ! tmp ) return NULL; |
item = queue; |
while( pq_is_valid( item )){ |
if( item->order < order ){ |
if( item->next && pq_is_valid( item->next )){ |
item = item->next; |
}else{ |
item->next = tmp; |
tmp->previous = item; |
return tmp; |
} |
}else{ |
tmp->previous = item->previous; |
tmp->next = item; |
item->previous = tmp; |
if( tmp->previous ){ |
tmp->previous->next = tmp; |
}else{ |
* queue = tmp; |
} |
return tmp; |
} |
} |
} |
pq_head pq_create( packet_t packet, int order, size_t metric ){ |
pq_item_ref item; |
item = ( pq_item_ref ) malloc( sizeof( pq_item_t )); |
if( ! item ) return NULL; |
item->order = order; |
item->metric = metric; |
item->packet = packet; |
item->previous = NULL; |
item->next = NULL; |
item->magic_value = PQ_MAGIC_VALUE; |
return item; |
} |
void pq_destroy( pq_head queue ){ |
pq_item_ref actual; |
pq_item_ref next; |
actual = queue; |
while( pq_is_valid( actual )){ |
next = actual->next; |
actual->magic_value = 0; |
free( actual ); |
actual = next; |
} |
} |
static inline int pq_is_valid( pq_item_ref item ){ |
return item && ( item->magic_value == PQ_MAGIC_VALUE ); |
} |
/** @} |
*/ |
/branches/network/uspace/srv/net/structures/packet/packet.h |
---|
0,0 → 1,62 |
/* |
* Copyright (c) 2008 Lukas Mejdrech |
* All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions |
* are met: |
* |
* - Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* - Redistributions in binary form must reproduce the above copyright |
* notice, this list of conditions and the following disclaimer in the |
* documentation and/or other materials provided with the distribution. |
* - The name of the author may not be used to endorse or promote products |
* derived from this software without specific prior written permission. |
* |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
*/ |
/** @addtogroup net |
* @{ |
*/ |
/** @file |
*/ |
#ifndef __NET_PACKET_H__ |
#define __NET_PACKET_H__ |
#define PACKET_PREPEND( packet, type ) ( type * ) packet_prepend(( packet ), sizeof( type )) |
#define PACKET_APPEND( packet, type ) ( type * ) packet_append(( packet ), sizeof( type )) |
#define PACKET_TRIM( packet, prefix, sufix ) packet_trim(( packet ), sizeof( prefix ), sizeof( sufix )) |
typedef struct packet * packet_t; |
typedef packet_t * packet_ref; |
packet_t packet_create( size_t max_prefix, size_t max_content, size_t max_sufix ); |
void * packet_prepend( packet_t packet, size_t length ); |
void * packet_append( packet_t packet, size_t length ); |
packet_t packet_copy( packet_t packet ); |
int packet_copy_data( packet_t packet, void * data, size_t length ); |
// TODO protocol identification? |
int packet_send( packet_t packet, int phone ); |
int packet_receive( packet_ref packet ); |
int packet_trim( packet_t packet, size_t prefix, size_t sufix ); |
int packet_destroy( packet_t packet ); |
size_t packet_get_data_length( packet_t packet ); |
void * packet_get_data( packet_t packet ); |
#endif |
/** @} |
*/ |
/branches/network/uspace/srv/net/structures/packet/. |
---|
Property changes: |
Added: svn:mergeinfo |