Changes in uspace/lib/net/tl/tl_common.c [e526f08:14f1db0] in mainline
- File:
-
- 1 edited
-
uspace/lib/net/tl/tl_common.c (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/net/tl/tl_common.c
re526f08 r14f1db0 36 36 */ 37 37 38 #include <net/socket_codes.h>39 #include <net/in.h>40 #include <net/in6.h>41 #include <net/inet.h>42 38 #include <async.h> 43 39 #include <ipc/services.h> 44 #include <errno.h> 45 #include <err.h> 46 47 #include <net/packet.h> 48 #include <packet_client.h> 40 41 #include <net_err.h> 42 #include <packet/packet.h> 43 #include <packet/packet_client.h> 49 44 #include <packet_remote.h> 50 #include <net /device.h>45 #include <net_device.h> 51 46 #include <icmp_interface.h> 47 #include <in.h> 48 #include <in6.h> 49 #include <inet.h> 50 #include <ip_local.h> 52 51 #include <ip_remote.h> 52 #include <socket_codes.h> 53 #include <socket_errno.h> 53 54 #include <ip_interface.h> 54 55 #include <tl_interface.h> … … 57 58 DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t); 58 59 59 int 60 tl_get_address_port(const struct sockaddr *addr, int addrlen, uint16_t *port) 61 { 62 const struct sockaddr_in *address_in; 63 const struct sockaddr_in6 *address_in6; 64 65 if ((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))) 66 return EINVAL; 67 68 switch (addr->sa_family) { 69 case AF_INET: 70 if (addrlen != sizeof(struct sockaddr_in)) 71 return EINVAL; 72 73 address_in = (struct sockaddr_in *) addr; 74 *port = ntohs(address_in->sin_port); 75 break; 76 case AF_INET6: 77 if (addrlen != sizeof(struct sockaddr_in6)) 78 return EINVAL; 79 80 address_in6 = (struct sockaddr_in6 *) addr; 81 *port = ntohs(address_in6->sin6_port); 82 break; 83 default: 84 return EAFNOSUPPORT; 85 } 86 60 int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){ 61 const struct sockaddr_in * address_in; 62 const struct sockaddr_in6 * address_in6; 63 64 if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){ 65 return EINVAL; 66 } 67 switch(addr->sa_family){ 68 case AF_INET: 69 if(addrlen != sizeof(struct sockaddr_in)){ 70 return EINVAL; 71 } 72 address_in = (struct sockaddr_in *) addr; 73 *port = ntohs(address_in->sin_port); 74 break; 75 case AF_INET6: 76 if(addrlen != sizeof(struct sockaddr_in6)){ 77 return EINVAL; 78 } 79 address_in6 = (struct sockaddr_in6 *) addr; 80 *port = ntohs(address_in6->sin6_port); 81 break; 82 default: 83 return EAFNOSUPPORT; 84 } 87 85 return EOK; 88 86 } … … 114 112 return EBADMEM; 115 113 116 *packet_dimension = packet_dimensions_find(packet_dimensions, 117 device_id); 114 *packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 118 115 if (!*packet_dimension) { 119 116 /* Ask for and remember them if not found */ … … 139 136 } 140 137 141 int 142 tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, 143 device_id_t device_id, size_t content) 144 { 138 int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){ 145 139 packet_dimension_ref packet_dimension; 146 140 147 141 packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 148 if (!packet_dimension)142 if(! packet_dimension){ 149 143 return ENOENT; 144 } 150 145 packet_dimension->content = content; 151 152 if (device_id != DEVICE_INVALID_ID) { 153 packet_dimension = packet_dimensions_find(packet_dimensions, 154 DEVICE_INVALID_ID); 155 156 if (packet_dimension) { 157 if (packet_dimension->content >= content) 146 if(device_id != DEVICE_INVALID_ID){ 147 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID); 148 if(packet_dimension){ 149 if(packet_dimension->content >= content){ 158 150 packet_dimension->content = content; 159 else 160 packet_dimensions_exclude(packet_dimensions, 161 DEVICE_INVALID_ID); 162 151 }else{ 152 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID); 153 } 163 154 } 164 155 } 165 166 156 return EOK; 167 157 } 168 158 169 int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port) 170 { 171 struct sockaddr_in *address_in; 172 struct sockaddr_in6 *address_in6; 159 int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){ 160 struct sockaddr_in * address_in; 161 struct sockaddr_in6 * address_in6; 173 162 size_t length; 174 163 175 if (addrlen < 0)176 return EINVAL; 177 164 if(addrlen < 0){ 165 return EINVAL; 166 } 178 167 length = (size_t) addrlen; 179 if (length < sizeof(struct sockaddr)) 180 return EINVAL; 181 182 switch (addr->sa_family) { 183 case AF_INET: 184 if (length != sizeof(struct sockaddr_in)) 185 return EINVAL; 186 address_in = (struct sockaddr_in *) addr; 187 address_in->sin_port = htons(port); 188 return EOK; 189 case AF_INET6: 190 if (length != sizeof(struct sockaddr_in6)) 191 return EINVAL; 192 address_in6 = (struct sockaddr_in6 *) addr; 193 address_in6->sin6_port = htons(port); 194 return EOK; 195 default: 196 return EAFNOSUPPORT; 197 } 198 } 199 200 int 201 tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, 202 services_t error) 203 { 168 if(length < sizeof(struct sockaddr)){ 169 return EINVAL; 170 } 171 switch(addr->sa_family){ 172 case AF_INET: 173 if(length != sizeof(struct sockaddr_in)){ 174 return EINVAL; 175 } 176 address_in = (struct sockaddr_in *) addr; 177 address_in->sin_port = htons(port); 178 return EOK; 179 case AF_INET6: 180 if(length != sizeof(struct sockaddr_in6)){ 181 return EINVAL; 182 } 183 address_in6 = (struct sockaddr_in6 *) addr; 184 address_in6->sin6_port = htons(port); 185 return EOK; 186 default: 187 return EAFNOSUPPORT; 188 } 189 } 190 191 int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){ 204 192 packet_t next; 205 uint8_t * src;193 uint8_t * src; 206 194 int length; 207 195 … … 212 200 213 201 length = packet_get_addr(packet, &src, NULL); 214 if ((length > 0) && (!error) && (icmp_phone >= 0) && 215 // set both addresses to the source one (avoids the source address 216 // deletion before setting the destination one) 217 (packet_set_addr(packet, src, src, (size_t) length) == EOK)) { 202 if((length > 0) 203 && (! error) 204 && (icmp_phone >= 0) 205 // set both addresses to the source one (avoids the source address deletion before setting the destination one) 206 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){ 218 207 return EOK; 219 } else208 }else{ 220 209 pq_release_remote(packet_phone, packet_get_id(packet)); 221 210 } 222 211 return ENOENT; 223 212 } 224 213 225 int 226 tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, 227 const packet_dimension_ref dimension, const struct sockaddr *addr, 228 socklen_t addrlen) 229 { 214 int tl_socket_read_packet_data(int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen){ 230 215 ERROR_DECLARE; 231 216 … … 234 219 void * data; 235 220 236 if (!dimension)237 return EINVAL; 238 221 if(! dimension){ 222 return EINVAL; 223 } 239 224 // get the data length 240 if (!async_data_write_receive(&callid, &length))241 return EINVAL; 242 225 if(! async_data_write_receive(&callid, &length)){ 226 return EINVAL; 227 } 243 228 // get a new packet 244 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, 245 prefix + dimension->prefix, dimension->suffix); 246 if (!packet) 229 *packet = packet_get_4_remote(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix); 230 if(! packet){ 247 231 return ENOMEM; 248 232 } 249 233 // allocate space in the packet 250 234 data = packet_suffix(*packet, length); 251 if (!data){235 if(! data){ 252 236 pq_release_remote(packet_phone, packet_get_id(*packet)); 253 237 return ENOMEM; 254 238 } 255 256 239 // read the data into the packet 257 if (ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) || 258 // set the packet destination address 259 ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, 260 addrlen))) { 240 if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) 241 // set the packet destination address 242 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){ 261 243 pq_release_remote(packet_phone, packet_get_id(*packet)); 262 244 return ERROR_CODE; 263 245 } 264 265 246 return (int) length; 266 247 }
Note:
See TracChangeset
for help on using the changeset viewer.
