Changes in uspace/lib/socket/packet/packet_client.c [29fa68b:caad59a] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/socket/packet/packet_client.c
r29fa68b rcaad59a 46 46 #include <packet/packet_client.h> 47 47 48 int packet_copy_data(packet_t packet, const void * data, size_t length) 49 { 50 if (!packet_is_valid(packet)) 48 int packet_copy_data(packet_t packet, const void * data, size_t length){ 49 if(! packet_is_valid(packet)){ 51 50 return EINVAL; 52 53 if (packet->data_start + length >= packet->length)51 } 52 if(packet->data_start + length >= packet->length){ 54 53 return ENOMEM; 55 54 } 56 55 memcpy((void *) packet + packet->data_start, data, length); 57 if (packet->data_start + length > packet->data_end)56 if(packet->data_start + length > packet->data_end){ 58 57 packet->data_end = packet->data_start + length; 59 58 } 60 59 return EOK; 61 60 } 62 61 63 void *packet_prefix(packet_t packet, size_t length) 64 { 65 if ((!packet_is_valid(packet)) || 66 (packet->data_start - sizeof(struct packet) - 67 2 * (packet->dest_addr - packet->src_addr) < length)) { 62 void * packet_prefix(packet_t packet, size_t length){ 63 if((! packet_is_valid(packet)) || (packet->data_start - sizeof(struct packet) - 2 * (packet->dest_addr - packet->src_addr) < length)){ 68 64 return NULL; 69 65 } 70 71 66 packet->data_start -= length; 72 67 return (void *) packet + packet->data_start; 73 68 } 74 69 75 void *packet_suffix(packet_t packet, size_t length) 76 { 77 if ((!packet_is_valid(packet)) || 78 (packet->data_end + length >= packet->length)) { 70 void * packet_suffix(packet_t packet, size_t length){ 71 if((! packet_is_valid(packet)) || (packet->data_end + length >= packet->length)){ 79 72 return NULL; 80 73 } 81 82 74 packet->data_end += length; 83 75 return (void *) packet + packet->data_end - length; 84 76 } 85 77 86 int packet_trim(packet_t packet, size_t prefix, size_t suffix) 87 { 88 if (!packet_is_valid(packet)) 78 int packet_trim(packet_t packet, size_t prefix, size_t suffix){ 79 if(! packet_is_valid(packet)){ 89 80 return EINVAL; 90 91 if (prefix + suffix > PACKET_DATA_LENGTH(packet))81 } 82 if(prefix + suffix > PACKET_DATA_LENGTH(packet)){ 92 83 return ENOMEM; 93 84 } 94 85 packet->data_start += prefix; 95 86 packet->data_end -= suffix; … … 97 88 } 98 89 99 packet_id_t packet_get_id(const packet_t packet) 100 { 90 packet_id_t packet_get_id(const packet_t packet){ 101 91 return packet_is_valid(packet) ? packet->packet_id : 0; 102 92 } 103 93 104 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest) 105 { 106 if (!packet_is_valid(packet)) 94 int packet_get_addr(const packet_t packet, uint8_t ** src, uint8_t ** dest){ 95 if(! packet_is_valid(packet)){ 107 96 return EINVAL; 108 if (!packet->addr_len) 97 } 98 if(! packet->addr_len){ 109 99 return 0; 110 if (src) 100 } 101 if(src){ 111 102 *src = (void *) packet + packet->src_addr; 112 if (dest) 103 } 104 if(dest){ 113 105 *dest = (void *) packet + packet->dest_addr; 114 106 } 115 107 return packet->addr_len; 116 108 } 117 109 118 size_t packet_get_data_length(const packet_t packet) 119 { 120 if (!packet_is_valid(packet)) 110 size_t packet_get_data_length(const packet_t packet){ 111 if(! packet_is_valid(packet)){ 121 112 return 0; 122 113 } 123 114 return PACKET_DATA_LENGTH(packet); 124 115 } 125 116 126 void *packet_get_data(const packet_t packet) 127 { 128 if (!packet_is_valid(packet)) 117 void * packet_get_data(const packet_t packet){ 118 if(! packet_is_valid(packet)){ 129 119 return NULL; 130 120 } 131 121 return (void *) packet + packet->data_start; 132 122 } 133 123 134 int 135 packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, 136 size_t addr_len) 137 { 124 int packet_set_addr(packet_t packet, const uint8_t * src, const uint8_t * dest, size_t addr_len){ 138 125 size_t padding; 139 126 size_t allocated; 140 127 141 if (!packet_is_valid(packet))128 if(! packet_is_valid(packet)){ 142 129 return EINVAL; 143 130 } 144 131 allocated = PACKET_MAX_ADDRESS_LENGTH(packet); 145 if (allocated < addr_len)132 if(allocated < addr_len){ 146 133 return ENOMEM; 147 134 } 148 135 padding = allocated - addr_len; 149 136 packet->addr_len = addr_len; 150 151 if (src) { 137 if(src){ 152 138 memcpy((void *) packet + packet->src_addr, src, addr_len); 153 if (padding)154 bzero((void *) packet + packet->src_addr + addr_len, 155 padding);156 } else{139 if(padding){ 140 bzero((void *) packet + packet->src_addr + addr_len, padding); 141 } 142 }else{ 157 143 bzero((void *) packet + packet->src_addr, allocated); 158 144 } 159 160 if (dest) { 145 if(dest){ 161 146 memcpy((void *) packet + packet->dest_addr, dest, addr_len); 162 if (padding)163 bzero((void *) packet + packet->dest_addr + addr_len, 164 padding);165 } else{147 if(padding){ 148 bzero((void *) packet + packet->dest_addr + addr_len, padding); 149 } 150 }else{ 166 151 bzero((void *) packet + packet->dest_addr, allocated); 167 152 } 168 169 153 return EOK; 170 154 } 171 155 172 packet_t packet_get_copy(int phone, packet_t packet) 173 { 156 packet_t packet_get_copy(int phone, packet_t packet){ 174 157 packet_t copy; 175 158 uint8_t * src = NULL; … … 177 160 size_t addrlen; 178 161 179 if (!packet_is_valid(packet))162 if(! packet_is_valid(packet)){ 180 163 return NULL; 181 164 } 182 165 // get a new packet 183 copy = packet_get_4_local(phone, PACKET_DATA_LENGTH(packet), 184 PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, 185 PACKET_MIN_SUFFIX(packet)); 186 if (!copy) 166 copy = packet_get_4_local(phone, PACKET_DATA_LENGTH(packet), PACKET_MAX_ADDRESS_LENGTH(packet), packet->max_prefix, PACKET_MIN_SUFFIX(packet)); 167 if(! copy){ 187 168 return NULL; 188 169 } 189 170 // get addresses 190 171 addrlen = packet_get_addr(packet, &src, &dest); 191 172 // copy data 192 if ((packet_copy_data(copy, packet_get_data(packet), 193 PACKET_DATA_LENGTH(packet)) == EOK) && 194 // copy addresses if present 195 ((addrlen <= 0) || 196 (packet_set_addr(copy, src, dest, addrlen) == EOK))) { 173 if((packet_copy_data(copy, packet_get_data(packet), PACKET_DATA_LENGTH(packet)) == EOK) 174 // copy addresses if present 175 && ((addrlen <= 0) || (packet_set_addr(copy, src, dest, addrlen) == EOK))){ 197 176 copy->order = packet->order; 198 177 copy->metric = packet->metric; 199 178 return copy; 200 } else{179 }else{ 201 180 pq_release_local(phone, copy->packet_id); 202 181 return NULL;
Note:
See TracChangeset
for help on using the changeset viewer.