Changeset eb522e8 in mainline for uspace/srv/net/tl/tcp/tcp.c
- Timestamp:
- 2011-06-01T08:43:42Z (15 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8d6c1f1
- Parents:
- 9e2e715 (diff), e51a514 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - File:
-
- 1 edited
-
uspace/srv/net/tl/tcp/tcp.c (modified) (84 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/tl/tcp/tcp.c
r9e2e715 reb522e8 36 36 */ 37 37 38 #include "tcp.h"39 #include "tcp_header.h"40 #include "tcp_module.h"41 42 38 #include <assert.h> 43 39 #include <async.h> … … 48 44 #include <errno.h> 49 45 50 #include <ipc/ipc.h>51 46 #include <ipc/services.h> 52 47 #include <ipc/net.h> … … 68 63 #include <ip_interface.h> 69 64 #include <icmp_client.h> 70 #include <icmp_ interface.h>65 #include <icmp_remote.h> 71 66 #include <net_interface.h> 72 67 #include <socket_core.h> 73 68 #include <tl_common.h> 74 #include <tl_local.h> 75 #include <tl_interface.h> 69 #include <tl_remote.h> 70 #include <tl_skel.h> 71 72 #include "tcp.h" 73 #include "tcp_header.h" 76 74 77 75 /** TCP module name. */ 78 #define NAME "TCP protocol"76 #define NAME "tcp" 79 77 80 78 /** The TCP window default value. */ … … 127 125 typedef struct tcp_timeout tcp_timeout_t; 128 126 129 /** Type definition of the TCP timeout pointer.130 * @see tcp_timeout131 */132 typedef tcp_timeout_t *tcp_timeout_ref;133 134 127 /** TCP reply timeout data. 135 128 * Used as a timeouting fibril argument. … … 144 137 145 138 /** Local sockets. */ 146 socket_cores_ reflocal_sockets;139 socket_cores_t *local_sockets; 147 140 148 141 /** Socket identifier. */ … … 159 152 160 153 /** Port map key. */ 161 char*key;154 uint8_t *key; 162 155 163 156 /** Port map key length. */ … … 165 158 }; 166 159 167 static int tcp_release_and_return(packet_t , int);168 static void tcp_prepare_operation_header(socket_core_ ref, tcp_socket_data_ref,169 tcp_header_ ref, int synchronize, int);170 static int tcp_prepare_timeout(int (*)(void *), socket_core_ ref,171 tcp_socket_data_ ref, size_t, tcp_socket_state_t, suseconds_t, int);172 static void tcp_free_socket_data(socket_core_ ref);160 static int tcp_release_and_return(packet_t *, int); 161 static void tcp_prepare_operation_header(socket_core_t *, tcp_socket_data_t *, 162 tcp_header_t *, int synchronize, int); 163 static int tcp_prepare_timeout(int (*)(void *), socket_core_t *, 164 tcp_socket_data_t *, size_t, tcp_socket_state_t, suseconds_t, int); 165 static void tcp_free_socket_data(socket_core_t *); 173 166 174 167 static int tcp_timeout(void *); … … 176 169 static int tcp_release_after_timeout(void *); 177 170 178 static int tcp_process_packet(device_id_t, packet_t , services_t);179 static int tcp_connect_core(socket_core_ ref, socket_cores_ref,171 static int tcp_process_packet(device_id_t, packet_t *, services_t); 172 static int tcp_connect_core(socket_core_t *, socket_cores_t *, 180 173 struct sockaddr *, socklen_t); 181 static int tcp_queue_prepare_packet(socket_core_ ref, tcp_socket_data_ref,182 packet_t , size_t);183 static int tcp_queue_packet(socket_core_ ref, tcp_socket_data_ref, packet_t,174 static int tcp_queue_prepare_packet(socket_core_t *, tcp_socket_data_t *, 175 packet_t *, size_t); 176 static int tcp_queue_packet(socket_core_t *, tcp_socket_data_t *, packet_t *, 184 177 size_t); 185 static packet_t tcp_get_packets_to_send(socket_core_ref, tcp_socket_data_ref);186 static void tcp_send_packets(device_id_t, packet_t );187 188 static void tcp_process_acknowledgement(socket_core_ ref, tcp_socket_data_ref,189 tcp_header_ ref);190 static packet_t tcp_send_prepare_packet(socket_core_ref, tcp_socket_data_ref,191 packet_t , size_t, size_t);192 static packet_t tcp_prepare_copy(socket_core_ref, tcp_socket_data_ref, packet_t,193 size_t, size_t);194 /* static */ void tcp_retransmit_packet(socket_core_ ref, tcp_socket_data_ref,178 static packet_t *tcp_get_packets_to_send(socket_core_t *, tcp_socket_data_t *); 179 static void tcp_send_packets(device_id_t, packet_t *); 180 181 static void tcp_process_acknowledgement(socket_core_t *, tcp_socket_data_t *, 182 tcp_header_t *); 183 static packet_t *tcp_send_prepare_packet(socket_core_t *, tcp_socket_data_t *, 184 packet_t *, size_t, size_t); 185 static packet_t *tcp_prepare_copy(socket_core_t *, tcp_socket_data_t *, 186 packet_t *, size_t, size_t); 187 /* static */ void tcp_retransmit_packet(socket_core_t *, tcp_socket_data_t *, 195 188 size_t); 196 static int tcp_create_notification_packet(packet_t *, socket_core_ref, 197 tcp_socket_data_ref, int, int); 198 static void tcp_refresh_socket_data(tcp_socket_data_ref); 199 200 static void tcp_initialize_socket_data(tcp_socket_data_ref); 201 202 static int tcp_process_listen(socket_core_ref, tcp_socket_data_ref, 203 tcp_header_ref, packet_t, struct sockaddr *, struct sockaddr *, size_t); 204 static int tcp_process_syn_sent(socket_core_ref, tcp_socket_data_ref, 205 tcp_header_ref, packet_t); 206 static int tcp_process_syn_received(socket_core_ref, tcp_socket_data_ref, 207 tcp_header_ref, packet_t); 208 static int tcp_process_established(socket_core_ref, tcp_socket_data_ref, 209 tcp_header_ref, packet_t, int, size_t); 210 static int tcp_queue_received_packet(socket_core_ref, tcp_socket_data_ref, 211 packet_t, int, size_t); 212 213 static int tcp_received_msg(device_id_t, packet_t, services_t, services_t); 189 static int tcp_create_notification_packet(packet_t **, socket_core_t *, 190 tcp_socket_data_t *, int, int); 191 static void tcp_refresh_socket_data(tcp_socket_data_t *); 192 193 static void tcp_initialize_socket_data(tcp_socket_data_t *); 194 195 static int tcp_process_listen(socket_core_t *, tcp_socket_data_t *, 196 tcp_header_t *, packet_t *, struct sockaddr *, struct sockaddr *, size_t); 197 static int tcp_process_syn_sent(socket_core_t *, tcp_socket_data_t *, 198 tcp_header_t *, packet_t *); 199 static int tcp_process_syn_received(socket_core_t *, tcp_socket_data_t *, 200 tcp_header_t *, packet_t *); 201 static int tcp_process_established(socket_core_t *, tcp_socket_data_t *, 202 tcp_header_t *, packet_t *, int, size_t); 203 static int tcp_queue_received_packet(socket_core_t *, tcp_socket_data_t *, 204 packet_t *, int, size_t); 205 static void tcp_queue_received_end_of_data(socket_core_t *socket); 206 207 static int tcp_received_msg(device_id_t, packet_t *, services_t, services_t); 214 208 static int tcp_process_client_messages(ipc_callid_t, ipc_call_t); 215 209 216 static int tcp_listen_message(socket_cores_ ref, int, int);217 static int tcp_connect_message(socket_cores_ ref, int, struct sockaddr *,210 static int tcp_listen_message(socket_cores_t *, int, int); 211 static int tcp_connect_message(socket_cores_t *, int, struct sockaddr *, 218 212 socklen_t); 219 static int tcp_recvfrom_message(socket_cores_ ref, int, int, size_t *);220 static int tcp_send_message(socket_cores_ ref, int, int, size_t *, int);221 static int tcp_accept_message(socket_cores_ ref, int, int, size_t *, size_t *);222 static int tcp_close_message(socket_cores_ ref, int);213 static int tcp_recvfrom_message(socket_cores_t *, int, int, size_t *); 214 static int tcp_send_message(socket_cores_t *, int, int, size_t *, int); 215 static int tcp_accept_message(socket_cores_t *, int, int, size_t *, size_t *); 216 static int tcp_close_message(socket_cores_t *, int); 223 217 224 218 /** TCP global data. */ 225 219 tcp_globals_t tcp_globals; 226 220 227 /** Initializes the TCP module. 228 * 229 * @param[in] client_connection The client connection processing function. The 230 * module skeleton propagates its own one. 231 * @returns EOK on success. 232 * @returns ENOMEM if there is not enough memory left. 233 */ 234 int tcp_initialize(async_client_conn_t client_connection) 235 { 236 int rc; 237 238 assert(client_connection); 239 240 fibril_rwlock_initialize(&tcp_globals.lock); 241 fibril_rwlock_write_lock(&tcp_globals.lock); 242 243 tcp_globals.icmp_phone = icmp_connect_module(SERVICE_ICMP, 244 ICMP_CONNECT_TIMEOUT); 245 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 246 SERVICE_TCP, client_connection); 247 if (tcp_globals.ip_phone < 0) { 248 fibril_rwlock_write_unlock(&tcp_globals.lock); 249 return tcp_globals.ip_phone; 250 } 251 252 rc = socket_ports_initialize(&tcp_globals.sockets); 253 if (rc != EOK) 254 goto out; 255 256 rc = packet_dimensions_initialize(&tcp_globals.dimensions); 257 if (rc != EOK) { 258 socket_ports_destroy(&tcp_globals.sockets); 259 goto out; 260 } 261 262 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 263 264 out: 265 fibril_rwlock_write_unlock(&tcp_globals.lock); 266 return rc; 267 } 268 269 int tcp_received_msg(device_id_t device_id, packet_t packet, 221 int tcp_received_msg(device_id_t device_id, packet_t *packet, 270 222 services_t receiver, services_t error) 271 223 { … … 285 237 } 286 238 287 int tcp_process_packet(device_id_t device_id, packet_t packet, services_t error)239 int tcp_process_packet(device_id_t device_id, packet_t *packet, services_t error) 288 240 { 289 241 size_t length; 290 242 size_t offset; 291 243 int result; 292 tcp_header_ refheader;293 socket_core_ refsocket;294 tcp_socket_data_ refsocket_data;295 packet_t next_packet;244 tcp_header_t *header; 245 socket_core_t *socket; 246 tcp_socket_data_t *socket_data; 247 packet_t *next_packet; 296 248 size_t total_length; 297 249 uint32_t checksum; … … 343 295 344 296 /* Get tcp header */ 345 header = (tcp_header_ ref) packet_get_data(packet);297 header = (tcp_header_t *) packet_get_data(packet); 346 298 if (!header) 347 299 return tcp_release_and_return(packet, NO_DATA); 348 300 349 // printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), 350 // ntohs(header->destination_port)); 351 301 #if 0 302 printf("header len %d, port %d \n", TCP_HEADER_LENGTH(header), 303 ntohs(header->destination_port)); 304 #endif 352 305 result = packet_get_addr(packet, (uint8_t **) &src, (uint8_t **) &dest); 353 306 if (result <= 0) … … 362 315 /* Find the destination socket */ 363 316 socket = socket_port_find(&tcp_globals.sockets, 364 ntohs(header->destination_port), ( const char*) src, addrlen);317 ntohs(header->destination_port), (uint8_t *) src, addrlen); 365 318 if (!socket) { 366 319 /* Find the listening destination socket */ 367 320 socket = socket_port_find(&tcp_globals.sockets, 368 ntohs(header->destination_port), SOCKET_MAP_KEY_LISTENING,369 0);321 ntohs(header->destination_port), 322 (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 370 323 } 371 324 … … 380 333 381 334 printf("socket id %d\n", socket->socket_id); 382 socket_data = (tcp_socket_data_ ref) socket->specific_data;335 socket_data = (tcp_socket_data_t *) socket->specific_data; 383 336 assert(socket_data); 384 337 … … 458 411 459 412 has_error_service: 460 fibril_rwlock_ read_unlock(&tcp_globals.lock);413 fibril_rwlock_write_unlock(&tcp_globals.lock); 461 414 462 415 /* TODO error reporting/handling */ … … 497 450 } 498 451 499 int tcp_process_established(socket_core_ ref socket, tcp_socket_data_ref500 socket_data, tcp_header_ ref header, packet_tpacket, int fragments,452 int tcp_process_established(socket_core_t *socket, tcp_socket_data_t * 453 socket_data, tcp_header_t *header, packet_t *packet, int fragments, 501 454 size_t total_length) 502 455 { 503 packet_t next_packet;504 packet_t tmp_packet;456 packet_t *next_packet; 457 packet_t *tmp_packet; 505 458 uint32_t old_incoming; 506 459 size_t order; … … 509 462 size_t offset; 510 463 uint32_t new_sequence_number; 464 bool forced_ack; 511 465 int rc; 512 466 … … 517 471 assert(packet); 518 472 473 forced_ack = false; 474 519 475 new_sequence_number = ntohl(header->sequence_number); 520 476 old_incoming = socket_data->next_incoming; 521 477 522 if (header->finalize) 523 socket_data->fin_incoming = new_sequence_number; 478 if (header->finalize) { 479 socket_data->fin_incoming = new_sequence_number + 480 total_length - TCP_HEADER_LENGTH(header); 481 } 524 482 525 483 /* Trim begining if containing expected data */ … … 765 723 /* Release duplicite or restricted */ 766 724 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 767 } 768 769 /* Change state according to the acknowledging incoming fin */ 770 if (IS_IN_INTERVAL_OVERFLOW(old_incoming, socket_data->fin_incoming, 771 socket_data->next_incoming)) { 725 forced_ack = true; 726 } 727 728 /* If next in sequence is an incoming FIN */ 729 if (socket_data->next_incoming == socket_data->fin_incoming) { 730 /* Advance sequence number */ 731 socket_data->next_incoming += 1; 732 733 /* Handle FIN */ 772 734 switch (socket_data->state) { 773 735 case TCP_SOCKET_FIN_WAIT_1: … … 776 738 socket_data->state = TCP_SOCKET_CLOSING; 777 739 break; 778 /*case TCP_ESTABLISHED:*/ 740 case TCP_SOCKET_ESTABLISHED: 741 /* Queue end-of-data marker on the socket. */ 742 tcp_queue_received_end_of_data(socket); 743 socket_data->state = TCP_SOCKET_CLOSE_WAIT; 744 break; 779 745 default: 780 746 socket_data->state = TCP_SOCKET_CLOSE_WAIT; … … 784 750 785 751 packet = tcp_get_packets_to_send(socket, socket_data); 786 if (!packet ) {752 if (!packet && (socket_data->next_incoming != old_incoming || forced_ack)) { 787 753 /* Create the notification packet */ 788 754 rc = tcp_create_notification_packet(&packet, socket, … … 805 771 } 806 772 807 int tcp_queue_received_packet(socket_core_ refsocket,808 tcp_socket_data_ ref socket_data, packet_tpacket, int fragments,773 int tcp_queue_received_packet(socket_core_t *socket, 774 tcp_socket_data_t *socket_data, packet_t *packet, int fragments, 809 775 size_t total_length) 810 776 { 811 packet_dimension_ refpacket_dimension;777 packet_dimension_t *packet_dimension; 812 778 int rc; 813 779 … … 834 800 /* Notify the destination socket */ 835 801 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 836 ( ipcarg_t) socket->socket_id,802 (sysarg_t) socket->socket_id, 837 803 ((packet_dimension->content < socket_data->data_fragment_size) ? 838 804 packet_dimension->content : socket_data->data_fragment_size), 0, 0, 839 ( ipcarg_t) fragments);805 (sysarg_t) fragments); 840 806 841 807 return EOK; 842 808 } 843 809 844 int tcp_process_syn_sent(socket_core_ref socket, tcp_socket_data_ref 845 socket_data, tcp_header_ref header, packet_t packet) 846 { 847 packet_t next_packet; 810 /** Queue end-of-data marker on the socket. 811 * 812 * Next element in the sequence space is FIN. Queue end-of-data marker 813 * on the socket. 814 * 815 * @param socket Socket 816 */ 817 static void tcp_queue_received_end_of_data(socket_core_t *socket) 818 { 819 assert(socket != NULL); 820 821 /* Notify the destination socket */ 822 async_msg_5(socket->phone, NET_SOCKET_RECEIVED, 823 (sysarg_t) socket->socket_id, 824 0, 0, 0, 825 (sysarg_t) 0 /* 0 fragments == no more data */); 826 } 827 828 int tcp_process_syn_sent(socket_core_t *socket, tcp_socket_data_t * 829 socket_data, tcp_header_t *header, packet_t *packet) 830 { 831 packet_t *next_packet; 848 832 int rc; 849 833 … … 900 884 } 901 885 902 int tcp_process_listen(socket_core_ reflistening_socket,903 tcp_socket_data_ ref listening_socket_data, tcp_header_refheader,904 packet_t packet, struct sockaddr *src, struct sockaddr *dest,886 int tcp_process_listen(socket_core_t *listening_socket, 887 tcp_socket_data_t *listening_socket_data, tcp_header_t *header, 888 packet_t *packet, struct sockaddr *src, struct sockaddr *dest, 905 889 size_t addrlen) 906 890 { 907 packet_t next_packet;908 socket_core_ refsocket;909 tcp_socket_data_ refsocket_data;891 packet_t *next_packet; 892 socket_core_t *socket; 893 tcp_socket_data_t *socket_data; 910 894 int socket_id; 911 895 int listening_socket_id = listening_socket->socket_id; … … 922 906 return tcp_release_and_return(packet, EINVAL); 923 907 924 socket_data = (tcp_socket_data_ ref) malloc(sizeof(*socket_data));908 socket_data = (tcp_socket_data_t *) malloc(sizeof(*socket_data)); 925 909 if (!socket_data) 926 910 return tcp_release_and_return(packet, ENOMEM); … … 971 955 /* Find the destination socket */ 972 956 listening_socket = socket_port_find(&tcp_globals.sockets, 973 listening_port, SOCKET_MAP_KEY_LISTENING, 0);957 listening_port, (uint8_t *) SOCKET_MAP_KEY_LISTENING, 0); 974 958 if (!listening_socket || 975 959 (listening_socket->socket_id != listening_socket_id)) { … … 979 963 } 980 964 listening_socket_data = 981 (tcp_socket_data_ ref) listening_socket->specific_data;965 (tcp_socket_data_t *) listening_socket->specific_data; 982 966 assert(listening_socket_data); 983 967 … … 991 975 return ENOTSOCK; 992 976 } 993 socket_data = (tcp_socket_data_ ref) socket->specific_data;977 socket_data = (tcp_socket_data_t *) socket->specific_data; 994 978 assert(socket_data); 995 979 996 980 rc = socket_port_add(&tcp_globals.sockets, listening_port, socket, 997 ( const char*) socket_data->addr, socket_data->addrlen);981 (uint8_t *) socket_data->addr, socket_data->addrlen); 998 982 assert(socket == socket_port_find(&tcp_globals.sockets, listening_port, 999 ( const char*) socket_data->addr, socket_data->addrlen));983 (uint8_t *) socket_data->addr, socket_data->addrlen)); 1000 984 1001 985 // rc = socket_bind_free_port(&tcp_globals.sockets, socket, … … 1060 1044 } 1061 1045 1062 int tcp_process_syn_received(socket_core_ refsocket,1063 tcp_socket_data_ ref socket_data, tcp_header_ref header, packet_tpacket)1064 { 1065 socket_core_ reflistening_socket;1066 tcp_socket_data_ reflistening_socket_data;1046 int tcp_process_syn_received(socket_core_t *socket, 1047 tcp_socket_data_t *socket_data, tcp_header_t *header, packet_t *packet) 1048 { 1049 socket_core_t *listening_socket; 1050 tcp_socket_data_t *listening_socket_data; 1067 1051 int rc; 1068 1052 … … 1079 1063 tcp_process_acknowledgement(socket, socket_data, header); 1080 1064 1081 socket_data->next_incoming = ntohl(header->sequence_number); // + 1;1065 socket_data->next_incoming = ntohl(header->sequence_number); /* + 1; */ 1082 1066 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); 1083 1067 socket_data->state = TCP_SOCKET_ESTABLISHED; … … 1086 1070 if (listening_socket) { 1087 1071 listening_socket_data = 1088 (tcp_socket_data_ ref) listening_socket->specific_data;1072 (tcp_socket_data_t *) listening_socket->specific_data; 1089 1073 assert(listening_socket_data); 1090 1074 … … 1095 1079 /* Notify the destination socket */ 1096 1080 async_msg_5(socket->phone, NET_SOCKET_ACCEPTED, 1097 ( ipcarg_t) listening_socket->socket_id,1081 (sysarg_t) listening_socket->socket_id, 1098 1082 socket_data->data_fragment_size, TCP_HEADER_SIZE, 1099 0, ( ipcarg_t) socket->socket_id);1083 0, (sysarg_t) socket->socket_id); 1100 1084 1101 1085 fibril_rwlock_write_unlock(socket_data->local_lock); … … 1127 1111 } 1128 1112 1129 void tcp_process_acknowledgement(socket_core_ refsocket,1130 tcp_socket_data_ ref socket_data, tcp_header_refheader)1113 void tcp_process_acknowledgement(socket_core_t *socket, 1114 tcp_socket_data_t *socket_data, tcp_header_t *header) 1131 1115 { 1132 1116 size_t number; 1133 1117 size_t length; 1134 packet_t packet;1135 packet_t next;1136 packet_t acknowledged = NULL;1118 packet_t *packet; 1119 packet_t *next; 1120 packet_t *acknowledged = NULL; 1137 1121 uint32_t old; 1138 1122 … … 1220 1204 } 1221 1205 1206 /** Per-connection initialization 1207 * 1208 */ 1209 void tl_connection(void) 1210 { 1211 } 1212 1222 1213 /** Processes the TCP message. 1223 1214 * … … 1227 1218 * @param[out] answer_count The last parameter for the actual answer in the 1228 1219 * answer parameter. 1229 * @return sEOK on success.1230 * @return sENOTSUP if the message is not known.1220 * @return EOK on success. 1221 * @return ENOTSUP if the message is not known. 1231 1222 * 1232 1223 * @see tcp_interface.h 1233 1224 * @see IS_NET_TCP_MESSAGE() 1234 1225 */ 1235 int 1236 tcp_message_standalone(ipc_callid_t callid, ipc_call_t *call, 1237 ipc_call_t *answer, int *answer_count) 1238 { 1239 packet_t packet; 1240 int rc; 1241 1226 int tl_message(ipc_callid_t callid, ipc_call_t *call, 1227 ipc_call_t *answer, size_t *answer_count) 1228 { 1242 1229 assert(call); 1243 1230 assert(answer); … … 1245 1232 1246 1233 *answer_count = 0; 1247 switch (IPC_GET_METHOD(*call)) { 1248 case NET_TL_RECEIVED: 1249 // fibril_rwlock_read_lock(&tcp_globals.lock); 1250 rc = packet_translate_remote(tcp_globals.net_phone, &packet, 1251 IPC_GET_PACKET(call)); 1252 if (rc != EOK) { 1253 // fibril_rwlock_read_unlock(&tcp_globals.lock); 1254 return rc; 1255 } 1256 rc = tcp_received_msg(IPC_GET_DEVICE(call), packet, SERVICE_TCP, 1257 IPC_GET_ERROR(call)); 1258 // fibril_rwlock_read_unlock(&tcp_globals.lock); 1259 return rc; 1234 switch (IPC_GET_IMETHOD(*call)) { 1260 1235 case IPC_M_CONNECT_TO_ME: 1261 1236 return tcp_process_client_messages(callid, *call); … … 1265 1240 } 1266 1241 1267 void tcp_refresh_socket_data(tcp_socket_data_ refsocket_data)1242 void tcp_refresh_socket_data(tcp_socket_data_t *socket_data) 1268 1243 { 1269 1244 assert(socket_data); … … 1281 1256 } 1282 1257 1283 void tcp_initialize_socket_data(tcp_socket_data_ refsocket_data)1258 void tcp_initialize_socket_data(tcp_socket_data_t *socket_data) 1284 1259 { 1285 1260 assert(socket_data); … … 1296 1271 bool keep_on_going = true; 1297 1272 socket_cores_t local_sockets; 1298 int app_phone = IPC_GET_PHONE( &call);1273 int app_phone = IPC_GET_PHONE(call); 1299 1274 struct sockaddr *addr; 1300 1275 int socket_id; … … 1303 1278 fibril_rwlock_t lock; 1304 1279 ipc_call_t answer; 1305 int answer_count;1306 tcp_socket_data_ refsocket_data;1307 socket_core_ refsocket;1308 packet_dimension_ refpacket_dimension;1280 size_t answer_count; 1281 tcp_socket_data_t *socket_data; 1282 socket_core_t *socket; 1283 packet_dimension_t *packet_dimension; 1309 1284 1310 1285 /* … … 1328 1303 1329 1304 /* Process the call */ 1330 switch (IPC_GET_ METHOD(call)) {1305 switch (IPC_GET_IMETHOD(call)) { 1331 1306 case IPC_M_PHONE_HUNGUP: 1332 1307 keep_on_going = false; … … 1336 1311 case NET_SOCKET: 1337 1312 socket_data = 1338 (tcp_socket_data_ ref) malloc(sizeof(*socket_data));1313 (tcp_socket_data_t *) malloc(sizeof(*socket_data)); 1339 1314 if (!socket_data) { 1340 1315 res = ENOMEM; … … 1370 1345 1371 1346 case NET_SOCKET_BIND: 1372 res = data_receive((void **) &addr, &addrlen); 1347 res = async_data_write_accept((void **) &addr, false, 1348 0, 0, 0, &addrlen); 1373 1349 if (res != EOK) 1374 1350 break; … … 1383 1359 SOCKET_GET_SOCKET_ID(call)); 1384 1360 if (socket) { 1385 socket_data = (tcp_socket_data_ ref)1361 socket_data = (tcp_socket_data_t *) 1386 1362 socket->specific_data; 1387 1363 assert(socket_data); … … 1407 1383 1408 1384 case NET_SOCKET_CONNECT: 1409 res = data_receive((void **) &addr, &addrlen); 1385 res = async_data_write_accept((void **) &addr, false, 1386 0, 0, 0, &addrlen); 1410 1387 if (res != EOK) 1411 1388 break; … … 1458 1435 1459 1436 case NET_SOCKET_SENDTO: 1460 res = data_receive((void **) &addr, &addrlen); 1437 res = async_data_write_accept((void **) &addr, false, 1438 0, 0, 0, &addrlen); 1461 1439 if (res != EOK) 1462 1440 break; … … 1528 1506 1529 1507 /* Release the application phone */ 1530 ipc_hangup(app_phone);1508 async_hangup(app_phone); 1531 1509 1532 1510 printf("release\n"); … … 1540 1518 int tcp_timeout(void *data) 1541 1519 { 1542 tcp_timeout_ reftimeout = data;1520 tcp_timeout_t *timeout = data; 1543 1521 int keep_write_lock = false; 1544 socket_core_ refsocket;1545 tcp_socket_data_ refsocket_data;1522 socket_core_t *socket; 1523 tcp_socket_data_t *socket_data; 1546 1524 1547 1525 assert(timeout); … … 1561 1539 goto out; 1562 1540 1563 socket_data = (tcp_socket_data_ ref) socket->specific_data;1541 socket_data = (tcp_socket_data_t *) socket->specific_data; 1564 1542 assert(socket_data); 1565 1543 if (socket_data->local_sockets != timeout->local_sockets) … … 1617 1595 int tcp_release_after_timeout(void *data) 1618 1596 { 1619 tcp_timeout_ reftimeout = data;1620 socket_core_ refsocket;1621 tcp_socket_data_ refsocket_data;1597 tcp_timeout_t *timeout = data; 1598 socket_core_t *socket; 1599 tcp_socket_data_t *socket_data; 1622 1600 fibril_rwlock_t *local_lock; 1623 1601 … … 1635 1613 1636 1614 if (socket && (socket->socket_id == timeout->socket_id)) { 1637 socket_data = (tcp_socket_data_ ref) socket->specific_data;1615 socket_data = (tcp_socket_data_t *) socket->specific_data; 1638 1616 assert(socket_data); 1639 1617 if (socket_data->local_sockets == timeout->local_sockets) { … … 1656 1634 } 1657 1635 1658 void tcp_retransmit_packet(socket_core_ ref socket, tcp_socket_data_ref1636 void tcp_retransmit_packet(socket_core_t *socket, tcp_socket_data_t * 1659 1637 socket_data, size_t sequence_number) 1660 1638 { 1661 packet_t packet;1662 packet_t copy;1639 packet_t *packet; 1640 packet_t *copy; 1663 1641 size_t data_length; 1664 1642 … … 1683 1661 } 1684 1662 1685 int tcp_listen_message(socket_cores_ reflocal_sockets, int socket_id,1663 int tcp_listen_message(socket_cores_t *local_sockets, int socket_id, 1686 1664 int backlog) 1687 1665 { 1688 socket_core_ refsocket;1689 tcp_socket_data_ refsocket_data;1666 socket_core_t *socket; 1667 tcp_socket_data_t *socket_data; 1690 1668 1691 1669 assert(local_sockets); … … 1700 1678 1701 1679 /* Get the socket specific data */ 1702 socket_data = (tcp_socket_data_ ref) socket->specific_data;1680 socket_data = (tcp_socket_data_t *) socket->specific_data; 1703 1681 assert(socket_data); 1704 1682 … … 1709 1687 } 1710 1688 1711 int tcp_connect_message(socket_cores_ reflocal_sockets, int socket_id,1689 int tcp_connect_message(socket_cores_t *local_sockets, int socket_id, 1712 1690 struct sockaddr *addr, socklen_t addrlen) 1713 1691 { 1714 socket_core_ refsocket;1692 socket_core_t *socket; 1715 1693 int rc; 1716 1694 … … 1730 1708 if (socket->port > 0) { 1731 1709 socket_ports_exclude(&tcp_globals.sockets, 1732 socket->port );1710 socket->port, free); 1733 1711 socket->port = 0; 1734 1712 } … … 1737 1715 } 1738 1716 1739 int tcp_connect_core(socket_core_ ref socket, socket_cores_reflocal_sockets,1717 int tcp_connect_core(socket_core_t *socket, socket_cores_t *local_sockets, 1740 1718 struct sockaddr *addr, socklen_t addrlen) 1741 1719 { 1742 tcp_socket_data_ refsocket_data;1743 packet_t packet;1720 tcp_socket_data_t *socket_data; 1721 packet_t *packet; 1744 1722 int rc; 1745 1723 … … 1749 1727 1750 1728 /* Get the socket specific data */ 1751 socket_data = (tcp_socket_data_ ref) socket->specific_data;1729 socket_data = (tcp_socket_data_t *) socket->specific_data; 1752 1730 assert(socket_data); 1753 1731 assert(socket->specific_data == socket_data); … … 1805 1783 fibril_rwlock_write_unlock(socket_data->local_lock); 1806 1784 1785 socket_data->state = TCP_SOCKET_SYN_SENT; 1786 1807 1787 /* Send the packet */ 1808 1788 printf("connecting %d\n", packet_get_id(packet)); … … 1828 1808 } 1829 1809 1830 int tcp_queue_prepare_packet(socket_core_ refsocket,1831 tcp_socket_data_ ref socket_data, packet_tpacket, size_t data_length)1832 { 1833 tcp_header_ refheader;1810 int tcp_queue_prepare_packet(socket_core_t *socket, 1811 tcp_socket_data_t *socket_data, packet_t *packet, size_t data_length) 1812 { 1813 tcp_header_t *header; 1834 1814 int rc; 1835 1815 … … 1839 1819 1840 1820 /* Get TCP header */ 1841 header = (tcp_header_ ref) packet_get_data(packet);1821 header = (tcp_header_t *) packet_get_data(packet); 1842 1822 if (!header) 1843 1823 return NO_DATA; … … 1859 1839 } 1860 1840 1861 int tcp_queue_packet(socket_core_ ref socket, tcp_socket_data_refsocket_data,1862 packet_t packet, size_t data_length)1841 int tcp_queue_packet(socket_core_t *socket, tcp_socket_data_t *socket_data, 1842 packet_t *packet, size_t data_length) 1863 1843 { 1864 1844 int rc; … … 1881 1861 } 1882 1862 1883 packet_t tcp_get_packets_to_send(socket_core_ref socket, tcp_socket_data_ref1863 packet_t *tcp_get_packets_to_send(socket_core_t *socket, tcp_socket_data_t * 1884 1864 socket_data) 1885 1865 { 1886 packet_t packet;1887 packet_t copy;1888 packet_t sending = NULL;1889 packet_t previous = NULL;1866 packet_t *packet; 1867 packet_t *copy; 1868 packet_t *sending = NULL; 1869 packet_t *previous = NULL; 1890 1870 size_t data_length; 1891 1871 int rc; … … 1941 1921 } 1942 1922 1943 packet_t tcp_send_prepare_packet(socket_core_ref socket, tcp_socket_data_ref1944 socket_data, packet_t packet, size_t data_length, size_t sequence_number)1945 { 1946 tcp_header_ refheader;1923 packet_t *tcp_send_prepare_packet(socket_core_t *socket, tcp_socket_data_t * 1924 socket_data, packet_t *packet, size_t data_length, size_t sequence_number) 1925 { 1926 tcp_header_t *header; 1947 1927 uint32_t checksum; 1948 1928 int rc; … … 1961 1941 1962 1942 /* Get the header */ 1963 header = (tcp_header_ ref) packet_get_data(packet);1943 header = (tcp_header_t *) packet_get_data(packet); 1964 1944 if (!header) { 1965 1945 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); … … 2002 1982 } 2003 1983 2004 packet_t tcp_prepare_copy(socket_core_ref socket, tcp_socket_data_ref2005 socket_data, packet_t packet, size_t data_length, size_t sequence_number)2006 { 2007 packet_t copy;1984 packet_t *tcp_prepare_copy(socket_core_t *socket, tcp_socket_data_t * 1985 socket_data, packet_t *packet, size_t data_length, size_t sequence_number) 1986 { 1987 packet_t *copy; 2008 1988 2009 1989 assert(socket); … … 2020 2000 } 2021 2001 2022 void tcp_send_packets(device_id_t device_id, packet_t packet)2023 { 2024 packet_t next;2002 void tcp_send_packets(device_id_t device_id, packet_t *packet) 2003 { 2004 packet_t *next; 2025 2005 2026 2006 while (packet) { … … 2032 2012 } 2033 2013 2034 void tcp_prepare_operation_header(socket_core_ refsocket,2035 tcp_socket_data_ ref socket_data, tcp_header_refheader, int synchronize,2014 void tcp_prepare_operation_header(socket_core_t *socket, 2015 tcp_socket_data_t *socket_data, tcp_header_t *header, int synchronize, 2036 2016 int finalize) 2037 2017 { … … 2050 2030 2051 2031 int tcp_prepare_timeout(int (*timeout_function)(void *tcp_timeout_t), 2052 socket_core_ ref socket, tcp_socket_data_refsocket_data,2032 socket_core_t *socket, tcp_socket_data_t *socket_data, 2053 2033 size_t sequence_number, tcp_socket_state_t state, suseconds_t timeout, 2054 2034 int globals_read_only) 2055 2035 { 2056 tcp_timeout_ refoperation_timeout;2036 tcp_timeout_t *operation_timeout; 2057 2037 fid_t fibril; 2058 2038 … … 2077 2057 2078 2058 /* Copy the key */ 2079 operation_timeout->key = (( char*) operation_timeout) +2059 operation_timeout->key = ((uint8_t *) operation_timeout) + 2080 2060 sizeof(*operation_timeout); 2081 2061 operation_timeout->key_length = socket->key_length; … … 2087 2067 if (!fibril) { 2088 2068 free(operation_timeout); 2089 return EPARTY; /* FIXME: use another EC */ 2090 } 2069 return ENOMEM; 2070 } 2071 2091 2072 // fibril_mutex_lock(&socket_data->operation.mutex); 2092 2073 /* Start the timeout fibril */ … … 2096 2077 } 2097 2078 2098 int tcp_recvfrom_message(socket_cores_ reflocal_sockets, int socket_id,2079 int tcp_recvfrom_message(socket_cores_t *local_sockets, int socket_id, 2099 2080 int flags, size_t *addrlen) 2100 2081 { 2101 socket_core_ refsocket;2102 tcp_socket_data_ refsocket_data;2082 socket_core_t *socket; 2083 tcp_socket_data_t *socket_data; 2103 2084 int packet_id; 2104 packet_t packet;2085 packet_t *packet; 2105 2086 size_t length; 2106 2087 int rc; … … 2117 2098 return NO_DATA; 2118 2099 2119 socket_data = (tcp_socket_data_ ref) socket->specific_data;2100 socket_data = (tcp_socket_data_t *) socket->specific_data; 2120 2101 2121 2102 /* Check state */ … … 2154 2135 } 2155 2136 2156 int tcp_send_message(socket_cores_ reflocal_sockets, int socket_id,2137 int tcp_send_message(socket_cores_t *local_sockets, int socket_id, 2157 2138 int fragments, size_t *data_fragment_size, int flags) 2158 2139 { 2159 socket_core_ refsocket;2160 tcp_socket_data_ refsocket_data;2161 packet_dimension_ refpacket_dimension;2162 packet_t packet;2140 socket_core_t *socket; 2141 tcp_socket_data_t *socket_data; 2142 packet_dimension_t *packet_dimension; 2143 packet_t *packet; 2163 2144 size_t total_length; 2164 tcp_header_ refheader;2145 tcp_header_t *header; 2165 2146 int index; 2166 2147 int result; … … 2179 2160 return NO_DATA; 2180 2161 2181 socket_data = (tcp_socket_data_ ref) socket->specific_data;2162 socket_data = (tcp_socket_data_t *) socket->specific_data; 2182 2163 2183 2164 /* Check state */ … … 2211 2192 2212 2193 tcp_prepare_operation_header(socket, socket_data, header, 0, 0); 2213 rc = tcp_queue_packet(socket, socket_data, packet, 0);2194 rc = tcp_queue_packet(socket, socket_data, packet, total_length); 2214 2195 if (rc != EOK) 2215 2196 return rc; … … 2230 2211 2231 2212 int 2232 tcp_close_message(socket_cores_ reflocal_sockets, int socket_id)2233 { 2234 socket_core_ refsocket;2235 tcp_socket_data_ refsocket_data;2236 packet_t packet;2213 tcp_close_message(socket_cores_t *local_sockets, int socket_id) 2214 { 2215 socket_core_t *socket; 2216 tcp_socket_data_t *socket_data; 2217 packet_t *packet; 2237 2218 int rc; 2238 2219 … … 2243 2224 2244 2225 /* Get the socket specific data */ 2245 socket_data = (tcp_socket_data_ ref) socket->specific_data;2226 socket_data = (tcp_socket_data_t *) socket->specific_data; 2246 2227 assert(socket_data); 2247 2228 … … 2298 2279 } 2299 2280 2300 int tcp_create_notification_packet(packet_t * packet, socket_core_refsocket,2301 tcp_socket_data_ refsocket_data, int synchronize, int finalize)2302 { 2303 packet_dimension_ refpacket_dimension;2304 tcp_header_ refheader;2281 int tcp_create_notification_packet(packet_t **packet, socket_core_t *socket, 2282 tcp_socket_data_t *socket_data, int synchronize, int finalize) 2283 { 2284 packet_dimension_t *packet_dimension; 2285 tcp_header_t *header; 2305 2286 int rc; 2306 2287 … … 2332 2313 } 2333 2314 2334 int tcp_accept_message(socket_cores_ reflocal_sockets, int socket_id,2315 int tcp_accept_message(socket_cores_t *local_sockets, int socket_id, 2335 2316 int new_socket_id, size_t *data_fragment_size, size_t *addrlen) 2336 2317 { 2337 socket_core_ refaccepted;2338 socket_core_ refsocket;2339 tcp_socket_data_ refsocket_data;2340 packet_dimension_ refpacket_dimension;2318 socket_core_t *accepted; 2319 socket_core_t *socket; 2320 tcp_socket_data_t *socket_data; 2321 packet_dimension_t *packet_dimension; 2341 2322 int rc; 2342 2323 … … 2351 2332 2352 2333 /* Get the socket specific data */ 2353 socket_data = (tcp_socket_data_ ref) socket->specific_data;2334 socket_data = (tcp_socket_data_t *) socket->specific_data; 2354 2335 assert(socket_data); 2355 2336 … … 2369 2350 2370 2351 /* Get the socket specific data */ 2371 socket_data = (tcp_socket_data_ ref) accepted->specific_data;2352 socket_data = (tcp_socket_data_t *) accepted->specific_data; 2372 2353 assert(socket_data); 2373 2354 /* TODO can it be in another state? */ … … 2405 2386 } 2406 2387 2407 void tcp_free_socket_data(socket_core_ refsocket)2408 { 2409 tcp_socket_data_ refsocket_data;2388 void tcp_free_socket_data(socket_core_t *socket) 2389 { 2390 tcp_socket_data_t *socket_data; 2410 2391 2411 2392 assert(socket); … … 2414 2395 2415 2396 /* Get the socket specific data */ 2416 socket_data = (tcp_socket_data_ ref) socket->specific_data;2397 socket_data = (tcp_socket_data_t *) socket->specific_data; 2417 2398 assert(socket_data); 2418 2399 … … 2447 2428 * @return The result parameter. 2448 2429 */ 2449 int tcp_release_and_return(packet_t packet, int result)2430 int tcp_release_and_return(packet_t *packet, int result) 2450 2431 { 2451 2432 pq_release_remote(tcp_globals.net_phone, packet_get_id(packet)); … … 2453 2434 } 2454 2435 2455 /** Default thread for new connections.2436 /** Process IPC messages from the IP module 2456 2437 * 2457 * @param[in] iid The initial message identifier.2458 * @param[in ] icall The initial message call structure.2438 * @param[in] iid Message identifier. 2439 * @param[in,out] icall Message parameters. 2459 2440 * 2460 2441 */ 2461 static void tl_client_connection(ipc_callid_t iid, ipc_call_t * icall) 2462 { 2463 /* 2464 * Accept the connection 2465 * - Answer the first IPC_M_CONNECT_ME_TO call. 2466 */ 2467 ipc_answer_0(iid, EOK); 2468 2442 static void tcp_receiver(ipc_callid_t iid, ipc_call_t *icall) 2443 { 2444 packet_t *packet; 2445 int rc; 2446 2469 2447 while (true) { 2470 ipc_call_t answer; 2471 int answer_count; 2472 2473 /* Clear the answer structure */ 2474 refresh_answer(&answer, &answer_count); 2475 2476 /* Fetch the next message */ 2477 ipc_call_t call; 2478 ipc_callid_t callid = async_get_call(&call); 2479 2480 /* Process the message */ 2481 int res = tl_module_message_standalone(callid, &call, &answer, 2482 &answer_count); 2483 2484 /* 2485 * End if told to either by the message or the processing 2486 * result. 2487 */ 2488 if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || 2489 (res == EHANGUP)) 2490 return; 2491 2492 /* 2493 * Answer the message 2494 */ 2495 answer_call(callid, res, &answer, answer_count); 2496 } 2497 } 2498 2499 /** Starts the module. 2448 switch (IPC_GET_IMETHOD(*icall)) { 2449 case NET_TL_RECEIVED: 2450 rc = packet_translate_remote(tcp_globals.net_phone, &packet, 2451 IPC_GET_PACKET(*icall)); 2452 if (rc == EOK) 2453 rc = tcp_received_msg(IPC_GET_DEVICE(*icall), packet, 2454 SERVICE_TCP, IPC_GET_ERROR(*icall)); 2455 2456 async_answer_0(iid, (sysarg_t) rc); 2457 break; 2458 default: 2459 async_answer_0(iid, (sysarg_t) ENOTSUP); 2460 } 2461 2462 iid = async_get_call(icall); 2463 } 2464 } 2465 2466 /** Initialize the TCP module. 2500 2467 * 2501 * @returns EOK on success. 2502 * @returns Other error codes as defined for each specific module 2503 * start function. 2468 * @param[in] net_phone Network module phone. 2469 * 2470 * @return EOK on success. 2471 * @return ENOMEM if there is not enough memory left. 2472 * 2504 2473 */ 2505 int 2506 main(int argc, char *argv[]) 2507 { 2508 int rc; 2509 2510 rc = tl_module_start_standalone(tl_client_connection); 2474 int tl_initialize(int net_phone) 2475 { 2476 fibril_rwlock_initialize(&tcp_globals.lock); 2477 fibril_rwlock_write_lock(&tcp_globals.lock); 2478 2479 tcp_globals.net_phone = net_phone; 2480 2481 tcp_globals.icmp_phone = icmp_connect_module(ICMP_CONNECT_TIMEOUT); 2482 tcp_globals.ip_phone = ip_bind_service(SERVICE_IP, IPPROTO_TCP, 2483 SERVICE_TCP, tcp_receiver); 2484 if (tcp_globals.ip_phone < 0) { 2485 fibril_rwlock_write_unlock(&tcp_globals.lock); 2486 return tcp_globals.ip_phone; 2487 } 2488 2489 int rc = socket_ports_initialize(&tcp_globals.sockets); 2490 if (rc != EOK) 2491 goto out; 2492 2493 rc = packet_dimensions_initialize(&tcp_globals.dimensions); 2494 if (rc != EOK) { 2495 socket_ports_destroy(&tcp_globals.sockets, free); 2496 goto out; 2497 } 2498 2499 tcp_globals.last_used_port = TCP_FREE_PORTS_START - 1; 2500 2501 out: 2502 fibril_rwlock_write_unlock(&tcp_globals.lock); 2511 2503 return rc; 2504 } 2505 2506 int main(int argc, char *argv[]) 2507 { 2508 return tl_module_start(SERVICE_TCP); 2512 2509 } 2513 2510
Note:
See TracChangeset
for help on using the changeset viewer.
