Changes in uspace/srv/net/udp/sock.c [6b0b508:db81577] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/udp/sock.c
r6b0b508 rdb81577 88 88 static void udp_sock_notify_data(socket_core_t *sock_core) 89 89 { 90 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id);90 log_msg(LVL_DEBUG, "udp_sock_notify_data(%d)", sock_core->socket_id); 91 91 async_exch_t *exch = async_exchange_begin(sock_core->sess); 92 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t) 92 async_msg_5(exch, NET_SOCKET_RECEIVED, (sysarg_t)sock_core->socket_id, 93 93 UDP_FRAGMENT_SIZE, 0, 0, 1); 94 94 async_exchange_end(exch); … … 103 103 ipc_call_t answer; 104 104 105 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_socket()");105 log_msg(LVL_DEBUG, "udp_sock_socket()"); 106 106 sock = calloc(1, sizeof(udp_sockdata_t)); 107 107 if (sock == NULL) { … … 167 167 udp_error_t urc; 168 168 169 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_bind()");170 log_msg(L OG_DEFAULT, LVL_DEBUG, " - async_data_write_accept");169 log_msg(LVL_DEBUG, "udp_sock_bind()"); 170 log_msg(LVL_DEBUG, " - async_data_write_accept"); 171 171 172 172 addr = NULL; … … 177 177 goto out; 178 178 } 179 180 if (addr_size != sizeof(struct sockaddr_in)) { 181 async_answer_0(callid, EINVAL); 182 goto out; 183 } 184 185 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind"); 179 180 log_msg(LVL_DEBUG, " - call socket_bind"); 186 181 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 187 182 addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, … … 191 186 goto out; 192 187 } 193 194 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find"); 188 189 if (addr_size != sizeof(struct sockaddr_in)) { 190 async_answer_0(callid, EINVAL); 191 goto out; 192 } 193 194 log_msg(LVL_DEBUG, " - call socket_cores_find"); 195 195 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call)); 196 196 if (sock_core == NULL) { … … 222 222 } 223 223 224 log_msg(L OG_DEFAULT, LVL_DEBUG, " - success");224 log_msg(LVL_DEBUG, " - success"); 225 225 async_answer_0(callid, rc); 226 226 out: … … 231 231 static void udp_sock_listen(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 232 232 { 233 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_listen()");233 log_msg(LVL_DEBUG, "udp_sock_listen()"); 234 234 async_answer_0(callid, ENOTSUP); 235 235 } … … 237 237 static void udp_sock_connect(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 238 238 { 239 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_connect()");239 log_msg(LVL_DEBUG, "udp_sock_connect()"); 240 240 async_answer_0(callid, ENOTSUP); 241 241 } … … 243 243 static void udp_sock_accept(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 244 244 { 245 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_accept()");245 log_msg(LVL_DEBUG, "udp_sock_accept()"); 246 246 async_answer_0(callid, ENOTSUP); 247 247 } … … 249 249 static void udp_sock_sendto(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 250 250 { 251 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()"); 252 253 struct sockaddr_in *addr = NULL; 254 udp_sock_t fsock; 255 udp_sock_t *fsock_ptr; 256 251 int socket_id; 252 int fragments; 253 int index; 254 struct sockaddr_in *addr; 255 size_t addr_size; 256 socket_core_t *sock_core; 257 udp_sockdata_t *socket; 258 udp_sock_t fsock, *fsockp; 259 ipc_call_t answer; 260 ipc_callid_t wcallid; 261 size_t length; 262 uint8_t buffer[UDP_FRAGMENT_SIZE]; 263 udp_error_t urc; 264 int rc; 265 266 log_msg(LVL_DEBUG, "udp_sock_send()"); 267 268 addr = NULL; 269 257 270 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) { 258 size_t addr_size; 259 int rc = async_data_write_accept((void **) &addr, false, 271 rc = async_data_write_accept((void **) &addr, false, 260 272 0, 0, 0, &addr_size); 261 273 if (rc != EOK) { … … 263 275 goto out; 264 276 } 265 277 266 278 if (addr_size != sizeof(struct sockaddr_in)) { 267 279 async_answer_0(callid, EINVAL); 268 280 goto out; 269 281 } 270 282 271 283 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 272 284 fsock.port = uint16_t_be2host(addr->sin_port); 273 fsock_ptr = &fsock; 274 } else 275 fsock_ptr = NULL; 276 277 int socket_id = SOCKET_GET_SOCKET_ID(call); 278 285 fsockp = &fsock; 286 } else { 287 fsockp = NULL; 288 } 289 290 socket_id = SOCKET_GET_SOCKET_ID(call); 291 fragments = SOCKET_GET_DATA_FRAGMENTS(call); 279 292 SOCKET_GET_FLAGS(call); 280 281 socket_core_t *sock_core = 282 socket_cores_find(&client->sockets, socket_id); 293 294 sock_core = socket_cores_find(&client->sockets, socket_id); 283 295 if (sock_core == NULL) { 284 296 async_answer_0(callid, ENOTSOCK); 285 297 goto out; 286 298 } 287 288 udp_sockdata_t *socket = 289 (udp_sockdata_t *) sock_core->specific_data; 290 291 if (sock_core->port <= 0) { 299 300 if (sock_core->port == 0) { 292 301 /* Implicitly bind socket to port */ 293 int rc = socket_bind_free_port(&gsock, sock_core, 294 UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, last_used_port); 302 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 303 addr, addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 304 last_used_port); 295 305 if (rc != EOK) { 296 306 async_answer_0(callid, rc); 297 307 goto out; 298 308 } 299 300 assert(sock_core->port > 0); 301 302 udp_error_t urc = udp_uc_set_local_port(socket->assoc, 303 sock_core->port); 304 305 if (urc != UDP_EOK) { 306 // TODO: better error handling 307 async_answer_0(callid, EINTR); 308 goto out; 309 } 310 311 last_used_port = sock_core->port; 312 } 313 309 } 310 311 socket = (udp_sockdata_t *)sock_core->specific_data; 314 312 fibril_mutex_lock(&socket->lock); 315 313 316 314 if (socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) { 317 315 /* Determine local IP address */ 318 316 inet_addr_t loc_addr, rem_addr; 319 320 rem_addr.ipv4 = fsock _ptr? fsock.addr.ipv4 :317 318 rem_addr.ipv4 = fsockp ? fsock.addr.ipv4 : 321 319 socket->assoc->ident.foreign.addr.ipv4; 322 323 intrc = inet_get_srcaddr(&rem_addr, 0, &loc_addr);320 321 rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); 324 322 if (rc != EOK) { 325 323 fibril_mutex_unlock(&socket->lock); 326 324 async_answer_0(callid, rc); 327 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_sendto: Failed to "325 log_msg(LVL_DEBUG, "udp_sock_sendto: Failed to " 328 326 "determine local address."); 329 327 return; 330 328 } 331 329 332 330 socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4; 333 log_msg(L OG_DEFAULT, LVL_DEBUG, "Local IP address is %x",331 log_msg(LVL_DEBUG, "Local IP address is %x", 334 332 socket->assoc->ident.local.addr.ipv4); 335 333 } 336 334 335 337 336 assert(socket->assoc != NULL); 338 339 int fragments = SOCKET_GET_DATA_FRAGMENTS(call); 340 for (int index = 0; index < fragments; index++) { 341 ipc_callid_t wcallid; 342 size_t length; 343 337 338 for (index = 0; index < fragments; index++) { 344 339 if (!async_data_write_receive(&wcallid, &length)) { 345 340 fibril_mutex_unlock(&socket->lock); … … 347 342 goto out; 348 343 } 349 344 350 345 if (length > UDP_FRAGMENT_SIZE) 351 346 length = UDP_FRAGMENT_SIZE; 352 353 uint8_t buffer[UDP_FRAGMENT_SIZE]; 354 int rc = async_data_write_finalize(wcallid, buffer, length); 347 348 rc = async_data_write_finalize(wcallid, buffer, length); 355 349 if (rc != EOK) { 356 350 fibril_mutex_unlock(&socket->lock); … … 358 352 goto out; 359 353 } 360 361 udp_error_t urc = 362 udp_uc_send(socket->assoc, fsock_ptr, buffer, length, 0); 363 354 355 urc = udp_uc_send(socket->assoc, fsockp, buffer, length, 0); 356 364 357 switch (urc) { 365 358 case UDP_EOK: 366 359 rc = EOK; 367 360 break; 368 case UDP_ENORES:369 rc = ENO MEM;370 break; 371 case UDP_EUNSPEC:372 rc = E INVAL;373 break; 374 case UDP_ENOROUTE:375 rc = E IO;376 break; 361 /* case TCP_ENOTEXIST: 362 rc = ENOTCONN; 363 break; 364 case TCP_ECLOSING: 365 rc = ENOTCONN; 366 break; 367 case TCP_ERESET: 368 rc = ECONNABORTED; 369 break;*/ 377 370 default: 378 371 assert(false); 379 372 } 380 373 381 374 if (rc != EOK) { 382 375 fibril_mutex_unlock(&socket->lock); … … 385 378 } 386 379 } 387 388 ipc_call_t answer;389 380 390 381 IPC_SET_ARG1(answer, 0); … … 414 405 int rc; 415 406 416 log_msg(L OG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client);407 log_msg(LVL_DEBUG, "%p: udp_sock_recv[from]()", client); 417 408 418 409 socket_id = SOCKET_GET_SOCKET_ID(call); … … 436 427 (void)flags; 437 428 438 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock");429 log_msg(LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock"); 439 430 fibril_mutex_lock(&socket->recv_buffer_lock); 440 431 while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) { 441 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv");432 log_msg(LVL_DEBUG, "udp_sock_recvfrom(): wait for cv"); 442 433 fibril_condvar_wait(&socket->recv_buffer_cv, 443 434 &socket->recv_buffer_lock); 444 435 } 445 436 446 log_msg(L OG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer");437 log_msg(LVL_DEBUG, "Got data in sock recv_buffer"); 447 438 448 439 rsock = socket->recv_fsock; … … 450 441 urc = socket->recv_error; 451 442 452 log_msg(L OG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len);443 log_msg(LVL_DEBUG, "**** recv data_len=%zu", data_len); 453 444 454 445 switch (urc) { … … 467 458 } 468 459 469 log_msg(L OG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc);460 log_msg(LVL_DEBUG, "**** udp_uc_receive -> %d", rc); 470 461 if (rc != EOK) { 471 462 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 481 472 addr.sin_port = host2uint16_t_be(rsock.port); 482 473 483 log_msg(L OG_DEFAULT, LVL_DEBUG, "addr read receive");474 log_msg(LVL_DEBUG, "addr read receive"); 484 475 if (!async_data_read_receive(&rcallid, &addr_length)) { 485 476 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 492 483 addr_length = sizeof(addr); 493 484 494 log_msg(L OG_DEFAULT, LVL_DEBUG, "addr read finalize");485 log_msg(LVL_DEBUG, "addr read finalize"); 495 486 rc = async_data_read_finalize(rcallid, &addr, addr_length); 496 487 if (rc != EOK) { … … 502 493 } 503 494 504 log_msg(L OG_DEFAULT, LVL_DEBUG, "data read receive");495 log_msg(LVL_DEBUG, "data read receive"); 505 496 if (!async_data_read_receive(&rcallid, &length)) { 506 497 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 513 504 length = data_len; 514 505 515 log_msg(L OG_DEFAULT, LVL_DEBUG, "data read finalize");506 log_msg(LVL_DEBUG, "data read finalize"); 516 507 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 517 508 … … 519 510 rc = EOVERFLOW; 520 511 521 log_msg(L OG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length);512 log_msg(LVL_DEBUG, "read_data_length <- %zu", length); 522 513 IPC_SET_ARG2(answer, 0); 523 514 SOCKET_SET_READ_DATA_LENGTH(answer, length); … … 535 526 static void udp_sock_close(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 536 527 { 537 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_sock_close()"); 538 int socket_id = SOCKET_GET_SOCKET_ID(call); 539 540 socket_core_t *sock_core = 541 socket_cores_find(&client->sockets, socket_id); 528 int socket_id; 529 socket_core_t *sock_core; 530 udp_sockdata_t *socket; 531 int rc; 532 533 log_msg(LVL_DEBUG, "tcp_sock_close()"); 534 socket_id = SOCKET_GET_SOCKET_ID(call); 535 536 sock_core = socket_cores_find(&client->sockets, socket_id); 542 537 if (sock_core == NULL) { 543 538 async_answer_0(callid, ENOTSOCK); 544 539 return; 545 540 } 546 547 udp_sockdata_t *socket = 548 (udp_sockdata_t *) sock_core->specific_data; 541 542 socket = (udp_sockdata_t *)sock_core->specific_data; 549 543 fibril_mutex_lock(&socket->lock); 550 551 intrc = socket_destroy(NULL, socket_id, &client->sockets, &gsock,544 545 rc = socket_destroy(NULL, socket_id, &client->sockets, &gsock, 552 546 udp_free_sock_data); 553 547 if (rc != EOK) { … … 556 550 return; 557 551 } 558 552 559 553 fibril_mutex_unlock(&socket->lock); 560 554 async_answer_0(callid, EOK); … … 563 557 static void udp_sock_getsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 564 558 { 565 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_getsockopt()");559 log_msg(LVL_DEBUG, "udp_sock_getsockopt()"); 566 560 async_answer_0(callid, ENOTSUP); 567 561 } … … 569 563 static void udp_sock_setsockopt(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 570 564 { 571 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_setsockopt()");565 log_msg(LVL_DEBUG, "udp_sock_setsockopt()"); 572 566 async_answer_0(callid, ENOTSUP); 573 567 } … … 580 574 size_t rcvd; 581 575 582 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_recv_fibril()");576 log_msg(LVL_DEBUG, "udp_sock_recv_fibril()"); 583 577 584 578 while (true) { 585 log_msg(L OG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()");579 log_msg(LVL_DEBUG, "[] wait for rcv buffer empty()"); 586 580 fibril_mutex_lock(&sock->recv_buffer_lock); 587 581 while (sock->recv_buffer_used != 0) { … … 589 583 &sock->recv_buffer_lock); 590 584 } 591 592 log_msg(L OG_DEFAULT, LVL_DEBUG, "[] call udp_uc_receive()");585 586 log_msg(LVL_DEBUG, "[] call udp_uc_receive()"); 593 587 urc = udp_uc_receive(sock->assoc, sock->recv_buffer, 594 588 UDP_FRAGMENT_SIZE, &rcvd, &xflags, &sock->recv_fsock); 595 589 sock->recv_error = urc; 596 590 597 591 udp_sock_notify_data(sock->sock_core); 598 592 599 593 if (urc != UDP_EOK) { 600 594 fibril_condvar_broadcast(&sock->recv_buffer_cv); … … 602 596 break; 603 597 } 604 605 log_msg(L OG_DEFAULT, LVL_DEBUG, "[] got data - broadcast recv_buffer_cv");606 598 599 log_msg(LVL_DEBUG, "[] got data - broadcast recv_buffer_cv"); 600 607 601 sock->recv_buffer_used = rcvd; 608 602 fibril_mutex_unlock(&sock->recv_buffer_lock); … … 628 622 629 623 while (true) { 630 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_connection: wait");624 log_msg(LVL_DEBUG, "udp_sock_connection: wait"); 631 625 callid = async_get_call(&call); 632 626 if (!IPC_GET_IMETHOD(call)) 633 627 break; 634 628 635 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_connection: METHOD=%d",629 log_msg(LVL_DEBUG, "udp_sock_connection: METHOD=%d", 636 630 (int)IPC_GET_IMETHOD(call)); 637 631 … … 676 670 677 671 /* Clean up */ 678 log_msg(L OG_DEFAULT, LVL_DEBUG, "udp_sock_connection: Clean up");672 log_msg(LVL_DEBUG, "udp_sock_connection: Clean up"); 679 673 async_hangup(client.sess); 680 674 socket_cores_release(NULL, &client.sockets, &gsock, udp_free_sock_data);
Note:
See TracChangeset
for help on using the changeset viewer.