Changes in uspace/srv/net/tl/tl_common.c [91478aa:aadf01e] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tl_common.c
r91478aa raadf01e 55 55 #include "tl_common.h" 56 56 57 DEVICE_MAP_IMPLEMENT( packet_dimensions, packet_dimension_t ); 58 59 int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){ 60 const struct sockaddr_in * address_in; 61 const struct sockaddr_in6 * address_in6; 62 63 if(( addrlen <= 0 ) || (( size_t ) addrlen < sizeof( struct sockaddr ))) return EINVAL; 64 switch( addr->sa_family ){ 57 DEVICE_MAP_IMPLEMENT(packet_dimensions, packet_dimension_t); 58 59 int tl_get_address_port(const struct sockaddr * addr, int addrlen, uint16_t * port){ 60 const struct sockaddr_in * address_in; 61 const struct sockaddr_in6 * address_in6; 62 63 if((addrlen <= 0) || ((size_t) addrlen < sizeof(struct sockaddr))){ 64 return EINVAL; 65 } 66 switch(addr->sa_family){ 65 67 case AF_INET: 66 if( addrlen != sizeof( struct sockaddr_in )) return EINVAL; 67 address_in = ( struct sockaddr_in * ) addr; 68 * port = ntohs( address_in->sin_port ); 68 if(addrlen != sizeof(struct sockaddr_in)){ 69 return EINVAL; 70 } 71 address_in = (struct sockaddr_in *) addr; 72 *port = ntohs(address_in->sin_port); 69 73 break; 70 74 case AF_INET6: 71 if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL; 72 address_in6 = ( struct sockaddr_in6 * ) addr; 73 * port = ntohs( address_in6->sin6_port ); 75 if(addrlen != sizeof(struct sockaddr_in6)){ 76 return EINVAL; 77 } 78 address_in6 = (struct sockaddr_in6 *) addr; 79 *port = ntohs(address_in6->sin6_port); 74 80 break; 75 81 default: … … 79 85 } 80 86 81 int tl_get_ip_packet_dimension( int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension){87 int tl_get_ip_packet_dimension(int ip_phone, packet_dimensions_ref packet_dimensions, device_id_t device_id, packet_dimension_ref * packet_dimension){ 82 88 ERROR_DECLARE; 83 89 84 if( ! packet_dimension ) return EBADMEM; 85 86 * packet_dimension = packet_dimensions_find( packet_dimensions, device_id ); 87 if( ! * packet_dimension ){ 90 if(! packet_dimension){ 91 return EBADMEM; 92 } 93 94 *packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 95 if(! * packet_dimension){ 88 96 // ask for and remember them if not found 89 * packet_dimension = malloc( sizeof( ** packet_dimension )); 90 if( ! * packet_dimension ) return ENOMEM; 91 if( ERROR_OCCURRED( ip_packet_size_req( ip_phone, device_id, * packet_dimension ))){ 92 free( * packet_dimension ); 97 *packet_dimension = malloc(sizeof(** packet_dimension)); 98 if(! * packet_dimension){ 99 return ENOMEM; 100 } 101 if(ERROR_OCCURRED(ip_packet_size_req(ip_phone, device_id, * packet_dimension))){ 102 free(*packet_dimension); 93 103 return ERROR_CODE; 94 104 } 95 ERROR_CODE = packet_dimensions_add( packet_dimensions, device_id, * packet_dimension);96 if( ERROR_CODE < 0){97 free( * packet_dimension);105 ERROR_CODE = packet_dimensions_add(packet_dimensions, device_id, * packet_dimension); 106 if(ERROR_CODE < 0){ 107 free(*packet_dimension); 98 108 return ERROR_CODE; 99 109 } … … 102 112 } 103 113 104 int tl_update_ip_packet_dimension( packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content ){ 105 packet_dimension_ref packet_dimension; 106 107 packet_dimension = packet_dimensions_find( packet_dimensions, device_id ); 108 if( ! packet_dimension ) return ENOENT; 114 int tl_update_ip_packet_dimension(packet_dimensions_ref packet_dimensions, device_id_t device_id, size_t content){ 115 packet_dimension_ref packet_dimension; 116 117 packet_dimension = packet_dimensions_find(packet_dimensions, device_id); 118 if(! packet_dimension){ 119 return ENOENT; 120 } 109 121 packet_dimension->content = content; 110 if( device_id != DEVICE_INVALID_ID){111 packet_dimension = packet_dimensions_find( packet_dimensions, DEVICE_INVALID_ID);112 if( packet_dimension){113 if( packet_dimension->content >= content){122 if(device_id != DEVICE_INVALID_ID){ 123 packet_dimension = packet_dimensions_find(packet_dimensions, DEVICE_INVALID_ID); 124 if(packet_dimension){ 125 if(packet_dimension->content >= content){ 114 126 packet_dimension->content = content; 115 127 }else{ 116 packet_dimensions_exclude( packet_dimensions, DEVICE_INVALID_ID);128 packet_dimensions_exclude(packet_dimensions, DEVICE_INVALID_ID); 117 129 } 118 130 } … … 121 133 } 122 134 123 int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){ 124 struct sockaddr_in * address_in; 125 struct sockaddr_in6 * address_in6; 126 size_t length; 127 128 if( addrlen < 0 ) return EINVAL; 129 length = ( size_t ) addrlen; 130 if( length < sizeof( struct sockaddr )) return EINVAL; 131 switch( addr->sa_family ){ 135 int tl_set_address_port(struct sockaddr * addr, int addrlen, uint16_t port){ 136 struct sockaddr_in * address_in; 137 struct sockaddr_in6 * address_in6; 138 size_t length; 139 140 if(addrlen < 0){ 141 return EINVAL; 142 } 143 length = (size_t) addrlen; 144 if(length < sizeof(struct sockaddr)){ 145 return EINVAL; 146 } 147 switch(addr->sa_family){ 132 148 case AF_INET: 133 if( length != sizeof( struct sockaddr_in )) return EINVAL; 134 address_in = ( struct sockaddr_in * ) addr; 135 address_in->sin_port = htons( port ); 149 if(length != sizeof(struct sockaddr_in)){ 150 return EINVAL; 151 } 152 address_in = (struct sockaddr_in *) addr; 153 address_in->sin_port = htons(port); 136 154 return EOK; 137 155 case AF_INET6: 138 if( length != sizeof( struct sockaddr_in6 )) return EINVAL; 139 address_in6 = ( struct sockaddr_in6 * ) addr; 140 address_in6->sin6_port = htons( port ); 156 if(length != sizeof(struct sockaddr_in6)){ 157 return EINVAL; 158 } 159 address_in6 = (struct sockaddr_in6 *) addr; 160 address_in6->sin6_port = htons(port); 141 161 return EOK; 142 162 default: … … 145 165 } 146 166 147 int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error){148 packet_t 149 uint8_t * 150 int 167 int tl_prepare_icmp_packet(int packet_phone, int icmp_phone, packet_t packet, services_t error){ 168 packet_t next; 169 uint8_t * src; 170 int length; 151 171 152 172 // detach the first packet and release the others 153 next = pq_detach( packet);154 if( next){155 pq_release( packet_phone, packet_get_id( next));156 } 157 length = packet_get_addr( packet, & src, NULL);158 if(( length > 0)159 && ( ! error)160 && ( icmp_phone >= 0)173 next = pq_detach(packet); 174 if(next){ 175 pq_release(packet_phone, packet_get_id(next)); 176 } 177 length = packet_get_addr(packet, &src, NULL); 178 if((length > 0) 179 && (! error) 180 && (icmp_phone >= 0) 161 181 // set both addresses to the source one (avoids the source address deletion before setting the destination one) 162 && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK)){182 && (packet_set_addr(packet, src, src, (size_t) length) == EOK)){ 163 183 return EOK; 164 184 }else{ 165 pq_release( packet_phone, packet_get_id( packet));185 pq_release(packet_phone, packet_get_id(packet)); 166 186 } 167 187 return ENOENT; 168 188 } 169 189 170 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){190 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){ 171 191 ERROR_DECLARE; 172 192 173 ipc_callid_t callid; 174 size_t length; 175 void * data; 176 177 if( ! dimension ) return EINVAL; 193 ipc_callid_t callid; 194 size_t length; 195 void * data; 196 197 if(! dimension){ 198 return EINVAL; 199 } 178 200 // get the data length 179 if( ! async_data_write_receive( & callid, & length )) return EINVAL; 201 if(! async_data_write_receive(&callid, &length)){ 202 return EINVAL; 203 } 180 204 // get a new packet 181 * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix ); 182 if( ! packet ) return ENOMEM; 205 *packet = packet_get_4(packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix); 206 if(! packet){ 207 return ENOMEM; 208 } 183 209 // allocate space in the packet 184 data = packet_suffix( * packet, length);185 if( ! data){186 pq_release( packet_phone, packet_get_id( * packet));210 data = packet_suffix(*packet, length); 211 if(! data){ 212 pq_release(packet_phone, packet_get_id(*packet)); 187 213 return ENOMEM; 188 214 } 189 215 // read the data into the packet 190 if( ERROR_OCCURRED( async_data_write_finalize( callid, data, length))216 if(ERROR_OCCURRED(async_data_write_finalize(callid, data, length)) 191 217 // set the packet destination address 192 || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen))){193 pq_release( packet_phone, packet_get_id( * packet));218 || ERROR_OCCURRED(packet_set_addr(*packet, NULL, (uint8_t *) addr, addrlen))){ 219 pq_release(packet_phone, packet_get_id(*packet)); 194 220 return ERROR_CODE; 195 221 } 196 return ( int) length;222 return (int) length; 197 223 } 198 224
Note:
See TracChangeset
for help on using the changeset viewer.