Changeset a4ee3ab2 in mainline
- Timestamp:
- 2011-11-29T22:51:27Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 8fcf74f
- Parents:
- 04cd242
- Location:
- uspace
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/errno.h
r04cd242 ra4ee3ab2 98 98 #define ECONNREFUSED (-10058) 99 99 100 #define ECONNABORTED (-10059) 101 100 102 /** The requested operation was not performed. Try again later. */ 101 103 #define EAGAIN (-11002) -
uspace/srv/net/tl/tcp/sock.c
r04cd242 ra4ee3ab2 41 41 #include <ipc/socket.h> 42 42 #include <net/modules.h> 43 #include <net/socket.h> 43 44 44 45 #include "sock.h" … … 56 57 #define TCP_FREE_PORTS_END 65535 57 58 58 static int last_used_port ;59 static int last_used_port = TCP_FREE_PORTS_START - 1; 59 60 static socket_ports_t gsock; 61 62 void tcp_sock_init(void) 63 { 64 socket_ports_initialize(&gsock); 65 } 60 66 61 67 static void tcp_free_sock_data(socket_core_t *sock_core) … … 69 75 static void tcp_sock_notify_data(socket_core_t *sock_core) 70 76 { 77 log_msg(LVL_DEBUG, "tcp_sock_notify_data(%d)", sock_core->socket_id); 71 78 async_exch_t *exch = async_exchange_begin(sock_core->sess); 72 79 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id, … … 75 82 } 76 83 84 static void tcp_sock_notify_aconn(socket_core_t *lsock_core) 85 { 86 log_msg(LVL_DEBUG, "tcp_sock_notify_aconn(%d)", lsock_core->socket_id); 87 async_exch_t *exch = async_exchange_begin(lsock_core->sess); 88 async_msg_5(exch, NET_SOCKET_ACCEPTED, (sysarg_t)lsock_core->socket_id, 89 FRAGMENT_SIZE, 0, 0, 0); 90 async_exchange_end(exch); 91 } 92 77 93 static void tcp_sock_socket(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 78 94 { … … 115 131 116 132 log_msg(LVL_DEBUG, "tcp_sock_bind()"); 133 log_msg(LVL_DEBUG, " - async_data_write_accept"); 117 134 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_len); 118 135 if (rc != EOK) { … … 121 138 } 122 139 140 log_msg(LVL_DEBUG, " - call socket_bind"); 123 141 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 124 142 addr, addr_len, TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, … … 129 147 } 130 148 149 log_msg(LVL_DEBUG, " - call socket_cores_find"); 131 150 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call)); 132 151 if (sock_core != NULL) { … … 136 155 } 137 156 138 async_answer_0(callid, ENOTSUP); 157 log_msg(LVL_DEBUG, " - success"); 158 async_answer_0(callid, EOK); 139 159 140 160 /* Push one fragment notification to client's queue */ … … 166 186 167 187 socket = (tcp_sockdata_t *)sock_core->specific_data; 168 /* XXX Listen */ 188 189 /* 190 * XXX We do not do anything and defer action to accept(). 191 * This is a slight difference in semantics, but most servers 192 * would just listen() and immediately accept() in a loop. 193 * 194 * The only difference is that there is a window between 195 * listen() and accept() or two accept()s where we refuse 196 * connections. 197 */ 169 198 (void)backlog; 170 199 (void)socket; 171 200 172 async_answer_0(callid, E NOTSUP);173 174 /* Push one fragment notification to client's queue */175 tcp_sock_notify_ data(sock_core);201 async_answer_0(callid, EOK); 202 log_msg(LVL_DEBUG, "tcp_sock_listen(): notify data\n"); 203 /* Push one accept notification to client's queue */ 204 tcp_sock_notify_aconn(sock_core); 176 205 } 177 206 … … 205 234 206 235 socket = (tcp_sockdata_t *)sock_core->specific_data; 207 208 lport = 1024; /* XXX */ 236 if (sock_core->port <= 0) { 237 rc = socket_bind_free_port(&gsock, sock_core, 238 TCP_FREE_PORTS_START, TCP_FREE_PORTS_END, 239 last_used_port); 240 if (rc != EOK) { 241 async_answer_0(callid, rc); 242 return; 243 } 244 245 last_used_port = sock_core->port; 246 } 247 248 lport = sock_core->port; 209 249 fsocket.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 210 250 fsocket.port = uint16_t_be2host(addr->sin_port); … … 230 270 ipc_call_t answer; 231 271 int socket_id; 232 int nsocket_id;272 int asock_id; 233 273 socket_core_t *sock_core; 234 tcp_sockdata_t *socket; 274 socket_core_t *asock_core; 275 tcp_sockdata_t *socket; 276 tcp_sockdata_t *asocket; 277 tcp_error_t trc; 278 tcp_sock_t fsocket; 279 tcp_conn_t *conn; 280 int rc; 235 281 236 282 log_msg(LVL_DEBUG, "tcp_sock_accept()"); 237 283 238 284 socket_id = SOCKET_GET_SOCKET_ID(call); 239 nsocket_id = SOCKET_GET_NEW_SOCKET_ID(call);285 asock_id = SOCKET_GET_NEW_SOCKET_ID(call); 240 286 241 287 sock_core = socket_cores_find(&client->sockets, socket_id); … … 246 292 247 293 socket = (tcp_sockdata_t *)sock_core->specific_data; 248 /* XXX Accept */ 249 (void) socket; 250 (void) nsocket_id; 251 /* 294 295 log_msg(LVL_DEBUG, " - verify socket->conn"); 296 if (socket->conn != NULL) { 297 async_answer_0(callid, EINVAL); 298 return; 299 } 300 301 log_msg(LVL_DEBUG, " - open connection"); 302 303 fsocket.addr.ipv4 = 0x7f000001; /* XXX */ 304 fsocket.port = 1025; /* XXX */ 305 306 trc = tcp_uc_open(sock_core->port, &fsocket, ap_passive, &conn); 307 308 log_msg(LVL_DEBUG, " - decode TCP return code"); 309 310 switch (trc) { 311 case TCP_EOK: 312 rc = EOK; 313 break; 314 case TCP_ERESET: 315 rc = ECONNABORTED; 316 break; 317 default: 318 assert(false); 319 } 320 321 log_msg(LVL_DEBUG, " - check TCP return code"); 322 if (rc != EOK) { 323 async_answer_0(callid, rc); 324 return; 325 } 326 327 log_msg(LVL_DEBUG, "tcp_sock_accept(): allocate asocket\n"); 328 asocket = calloc(sizeof(tcp_sockdata_t), 1); 329 if (asocket == NULL) { 330 async_answer_0(callid, ENOMEM); 331 return; 332 } 333 334 asocket->client = client; 335 asocket->conn = conn; 336 log_msg(LVL_DEBUG, "tcp_sock_accept():create asocket\n"); 337 338 rc = socket_create(&client->sockets, client->sess, asocket, &asock_id); 339 if (rc != EOK) { 340 async_answer_0(callid, rc); 341 return; 342 } 343 log_msg(LVL_DEBUG, "tcp_sock_accept(): find acore\n"); 344 345 asock_core = socket_cores_find(&client->sockets, asock_id); 346 assert(asock_core != NULL); 347 252 348 refresh_answer(&answer, NULL); 253 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, size) 254 ... 255 async_answer_0(callid, ENOTSUP);*/ 256 257 /* TODO */ 258 answer_call(callid, ENOTSUP, &answer, 3); 349 350 SOCKET_SET_DATA_FRAGMENT_SIZE(answer, FRAGMENT_SIZE); 351 SOCKET_SET_SOCKET_ID(answer, asock_id); 352 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(struct sockaddr_in)); 353 354 answer_call(callid, asock_core->socket_id, &answer, 3); 355 356 /* Push one accept notification to client's queue */ 357 tcp_sock_notify_aconn(sock_core); 358 359 /* Push one fragment notification to client's queue */ 360 tcp_sock_notify_data(asock_core); 361 log_msg(LVL_DEBUG, "tcp_sock_listen(): notify aconn\n"); 362 259 363 } 260 364 … … 273 377 int rc; 274 378 275 log_msg(LVL_DEBUG, " ******** tcp_sock_send() *******************");379 log_msg(LVL_DEBUG, "tcp_sock_send()"); 276 380 socket_id = SOCKET_GET_SOCKET_ID(call); 277 381 fragments = SOCKET_GET_DATA_FRAGMENTS(call); … … 338 442 } 339 443 340 static void tcp_sock_recv (tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)444 static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call) 341 445 { 342 446 int socket_id; 343 447 int flags; 344 size_t length;448 size_t addr_length, length; 345 449 socket_core_t *sock_core; 346 450 tcp_sockdata_t *socket; … … 351 455 xflags_t xflags; 352 456 tcp_error_t trc; 457 struct sockaddr_in addr; 458 tcp_sock_t *rsock; 353 459 int rc; 354 460 355 log_msg(LVL_DEBUG, "tcp_sock_recv ()");461 log_msg(LVL_DEBUG, "tcp_sock_recv[from]()"); 356 462 357 463 socket_id = SOCKET_GET_SOCKET_ID(call); … … 365 471 366 472 socket = (tcp_sockdata_t *)sock_core->specific_data; 473 if (socket->conn == NULL) { 474 async_answer_0(callid, ENOTCONN); 475 return; 476 } 367 477 368 478 (void)flags; … … 370 480 trc = tcp_uc_receive(socket->conn, buffer, FRAGMENT_SIZE, &data_len, 371 481 &xflags); 482 log_msg(LVL_DEBUG, "**** tcp_uc_receive done"); 372 483 373 484 switch (trc) { … … 383 494 } 384 495 496 log_msg(LVL_DEBUG, "**** tcp_uc_receive -> %d", rc); 385 497 if (rc != EOK) { 386 498 async_answer_0(callid, rc); … … 388 500 } 389 501 502 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 503 /* Fill addr */ 504 rsock = &socket->conn->ident.foreign; 505 addr.sin_family = AF_INET; 506 addr.sin_addr.s_addr = host2uint32_t_be(rsock->addr.ipv4); 507 addr.sin_port = host2uint16_t_be(rsock->port); 508 509 log_msg(LVL_DEBUG, "addr read receive"); 510 if (!async_data_read_receive(&rcallid, &addr_length)) { 511 async_answer_0(callid, EINVAL); 512 return; 513 } 514 515 if (addr_length > sizeof(addr)) 516 addr_length = sizeof(addr); 517 518 log_msg(LVL_DEBUG, "addr read finalize"); 519 rc = async_data_read_finalize(rcallid, &addr, addr_length); 520 if (rc != EOK) { 521 async_answer_0(callid, EINVAL); 522 return; 523 } 524 } 525 526 log_msg(LVL_DEBUG, "data read receive"); 390 527 if (!async_data_read_receive(&rcallid, &length)) { 391 528 async_answer_0(callid, EINVAL); … … 393 530 } 394 531 395 if (length < data_len) { 396 async_data_read_finalize(rcallid, buffer, length); 397 if (rc == EOK) 398 rc = EOVERFLOW; 399 } else { 532 if (length > data_len) 400 533 length = data_len; 401 } 402 534 535 log_msg(LVL_DEBUG, "data read finalize"); 403 536 rc = async_data_read_finalize(rcallid, buffer, length); 404 length = 0; 537 538 if (length < data_len && rc == EOK) 539 rc = EOVERFLOW; 405 540 406 541 SOCKET_SET_READ_DATA_LENGTH(answer, length); … … 409 544 /* Push one fragment notification to client's queue */ 410 545 tcp_sock_notify_data(sock_core); 411 }412 413 static void tcp_sock_recvfrom(tcp_client_t *client, ipc_callid_t callid, ipc_call_t call)414 {415 log_msg(LVL_DEBUG, "tcp_sock_recvfrom()");416 async_answer_0(callid, ENOTSUP);417 546 } 418 547 … … 502 631 break; 503 632 case NET_SOCKET_RECV: 504 tcp_sock_recv(&client, callid, call);505 break;506 633 case NET_SOCKET_RECVFROM: 507 634 tcp_sock_recvfrom(&client, callid, call); -
uspace/srv/net/tl/tcp/sock.h
r04cd242 ra4ee3ab2 38 38 #include <async.h> 39 39 40 extern void tcp_sock_init(void); 40 41 extern int tcp_sock_connection(async_sess_t *, ipc_callid_t, ipc_call_t); 41 42 -
uspace/srv/net/tl/tcp/tcp.c
r04cd242 ra4ee3ab2 377 377 log_msg(LVL_DEBUG, "tl_initialize()"); 378 378 379 tcp_sock_init(); 380 379 381 ip_sess = ip_bind_service(SERVICE_IP, IPPROTO_TCP, SERVICE_TCP, 380 382 tcp_receiver); -
uspace/srv/net/tl/tcp/test.c
r04cd242 ra4ee3ab2 118 118 async_usleep(1000*1000); 119 119 120 rc = thread_create(test_srv, NULL, "test_srv", &srv_tid); 121 if (rc != EOK) { 122 printf("Failed to create server thread.\n"); 123 return; 120 if (0) { 121 rc = thread_create(test_srv, NULL, "test_srv", &srv_tid); 122 if (rc != EOK) { 123 printf("Failed to create server thread.\n"); 124 return; 125 } 124 126 } 125 127 -
uspace/srv/net/tl/tcp/ucall.c
r04cd242 ra4ee3ab2 57 57 * XXX We should be able to call active open on an existing listening 58 58 * connection. 59 * XXX We should be able to get connection structure immediately, before 60 * establishment. 59 61 */ 60 62 tcp_error_t tcp_uc_open(uint16_t lport, tcp_sock_t *fsock, acpass_t acpass, … … 77 79 /* Synchronize (initiate) connection */ 78 80 tcp_conn_sync(nconn); 79 80 /* Wait for connection to be established or reset */ 81 log_msg(LVL_DEBUG, "tcp_uc_open: Wait for connection.");82 fibril_mutex_lock(&nconn->cstate_lock);83 while (nconn->cstate == st_syn_sent ||84 nconn->cstate == st_syn_received) {85 fibril_condvar_wait(&nconn->cstate_cv, &nconn->cstate_lock);86 }87 if (nconn->cstate != st_established) {88 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset.");89 assert(nconn->cstate == st_closed); 90 fibril_mutex_unlock(&nconn->cstate_lock);91 return TCP_ERESET;92 }81 } 82 83 /* Wait for connection to be established or reset */ 84 log_msg(LVL_DEBUG, "tcp_uc_open: Wait for connection."); 85 fibril_mutex_lock(&nconn->cstate_lock); 86 while (nconn->cstate == st_listen || 87 nconn->cstate == st_syn_sent || 88 nconn->cstate == st_syn_received) { 89 fibril_condvar_wait(&nconn->cstate_cv, &nconn->cstate_lock); 90 } 91 92 if (nconn->cstate != st_established) { 93 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was reset."); 94 assert(nconn->cstate == st_closed); 93 95 fibril_mutex_unlock(&nconn->cstate_lock); 94 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established."); 95 } 96 return TCP_ERESET; 97 } 98 99 fibril_mutex_unlock(&nconn->cstate_lock); 100 log_msg(LVL_DEBUG, "tcp_uc_open: Connection was established."); 96 101 97 102 *conn = nconn;
Note:
See TracChangeset
for help on using the changeset viewer.