packet_remote.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00038 #include <async.h>
00039 #include <errno.h>
00040 #include <ipc/packet.h>
00041 #include <sys/mman.h>
00042 
00043 #include <packet_client.h>
00044 #include <packet_remote.h>
00045 
00046 #include <net/packet.h>
00047 #include <net/packet_header.h>
00048 
00064 static int
00065 packet_return(int phone, packet_t **packet, packet_id_t packet_id, size_t size)
00066 {
00067         ipc_call_t answer;
00068         aid_t message;
00069         int rc;
00070         
00071         message = async_send_1(phone, NET_PACKET_GET, packet_id, &answer);
00072 
00073         *packet = (packet_t *) as_get_mappable_page(size);
00074         rc = async_share_in_start_0_0(phone, *packet, size);
00075         if (rc != EOK) {
00076                 munmap(*packet, size);
00077                 async_wait_for(message, NULL);
00078                 return rc;
00079         }
00080         rc = pm_add(*packet);
00081         if (rc != EOK) {
00082                 munmap(*packet, size);
00083                 async_wait_for(message, NULL);
00084                 return rc;
00085         }
00086         
00087         sysarg_t result;
00088         async_wait_for(message, &result);
00089         
00090         return result;
00091 }
00092 
00108 int packet_translate_remote(int phone, packet_t **packet, packet_id_t packet_id)
00109 {
00110         int rc;
00111         
00112         if (!packet)
00113                 return EINVAL;
00114         
00115         *packet = pm_find(packet_id);
00116         if (!*packet) {
00117                 sysarg_t size;
00118                 
00119                 rc = async_req_1_1(phone, NET_PACKET_GET_SIZE, packet_id,
00120                     &size);
00121                 if (rc != EOK)
00122                         return rc;
00123                 rc = packet_return(phone, packet, packet_id, size);
00124                 if (rc != EOK)
00125                         return rc;
00126         }
00127         if ((*packet)->next) {
00128                 packet_t *next;
00129                 
00130                 return packet_translate_remote(phone, &next, (*packet)->next);
00131         }
00132         
00133         return EOK;
00134 }
00135 
00149 packet_t *packet_get_4_remote(int phone, size_t max_content, size_t addr_len,
00150     size_t max_prefix, size_t max_suffix)
00151 {
00152         sysarg_t packet_id;
00153         sysarg_t size;
00154         int rc;
00155         
00156         rc = async_req_4_2(phone, NET_PACKET_CREATE_4, max_content, addr_len,
00157             max_prefix, max_suffix, &packet_id, &size);
00158         if (rc != EOK)
00159                 return NULL;
00160         
00161         
00162         packet_t *packet = pm_find(packet_id);
00163         if (!packet) {
00164                 rc = packet_return(phone, &packet, packet_id, size);
00165                 if (rc != EOK)
00166                         return NULL;
00167         }
00168         
00169         return packet;
00170 }
00171 
00181 packet_t *packet_get_1_remote(int phone, size_t content)
00182 {
00183         sysarg_t packet_id;
00184         sysarg_t size;
00185         int rc;
00186         
00187         rc = async_req_1_2(phone, NET_PACKET_CREATE_1, content, &packet_id,
00188             &size);
00189         if (rc != EOK)
00190                 return NULL;
00191         
00192         packet_t *packet = pm_find(packet_id);
00193         if (!packet) {
00194                 rc = packet_return(phone, &packet, packet_id, size);
00195                 if (rc != EOK)
00196                         return NULL;
00197         }
00198         
00199         return packet;
00200 }
00201 
00212 void pq_release_remote(int phone, packet_id_t packet_id)
00213 {
00214         async_msg_1(phone, NET_PACKET_RELEASE, packet_id);
00215 }
00216 

Generated on Thu Jun 2 07:45:47 2011 for HelenOS/USB by  doxygen 1.4.7