Changeset 98abd40 in mainline for uspace/srv/net/udp/sock.c
- Timestamp:
- 2013-07-06T21:57:22Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c8bb1633, cdc8a391
- Parents:
- b8e72fd1 (diff), 507c6f3 (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
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/udp/sock.c
rb8e72fd1 r98abd40 159 159 static void udp_sock_bind(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 160 160 { 161 int rc;162 struct sockaddr_in *addr;163 size_t addr_size;164 socket_core_t *sock_core;165 udp_sockdata_t *socket;166 udp_sock_t fsock;167 udp_error_t urc;168 169 161 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_bind()"); 170 162 log_msg(LOG_DEFAULT, LVL_DEBUG, " - async_data_write_accept"); 171 172 addr= NULL;173 174 rc = async_data_write_accept((void **) &addr, false, 0, 0, 0, &addr_size);163 164 struct sockaddr_in6 *addr6 = NULL; 165 size_t addr_len; 166 int rc = async_data_write_accept((void **) &addr6, false, 0, 0, 0, &addr_len); 175 167 if (rc != EOK) { 176 168 async_answer_0(callid, rc); 177 goto out; 178 } 179 180 if (addr_size != sizeof(struct sockaddr_in)) { 169 return; 170 } 171 172 if ((addr_len != sizeof(struct sockaddr_in)) && 173 (addr_len != sizeof(struct sockaddr_in6))) { 181 174 async_answer_0(callid, EINVAL); 182 175 goto out; 183 176 } 184 177 178 struct sockaddr_in *addr = (struct sockaddr_in *) addr6; 179 185 180 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_bind"); 181 186 182 rc = socket_bind(&client->sockets, &gsock, SOCKET_GET_SOCKET_ID(call), 187 addr , addr_size, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END,183 addr6, addr_len, UDP_FREE_PORTS_START, UDP_FREE_PORTS_END, 188 184 last_used_port); 189 185 if (rc != EOK) { … … 193 189 194 190 log_msg(LOG_DEFAULT, LVL_DEBUG, " - call socket_cores_find"); 195 sock_core = socket_cores_find(&client->sockets, SOCKET_GET_SOCKET_ID(call)); 191 192 socket_core_t *sock_core = socket_cores_find(&client->sockets, 193 SOCKET_GET_SOCKET_ID(call)); 196 194 if (sock_core == NULL) { 197 195 async_answer_0(callid, ENOENT); 198 196 goto out; 199 197 } 200 201 socket = (udp_sockdata_t *)sock_core->specific_data; 202 203 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 204 fsock.port = sock_core->port; 205 urc = udp_uc_set_local(socket->assoc, &fsock); 206 198 199 udp_sockdata_t *socket = 200 (udp_sockdata_t *) sock_core->specific_data; 201 202 udp_sock_t fsocket; 203 204 fsocket.port = sock_core->port; 205 206 switch (addr->sin_family) { 207 case AF_INET: 208 inet_sockaddr_in_addr(addr, &fsocket.addr); 209 break; 210 case AF_INET6: 211 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 212 break; 213 default: 214 async_answer_0(callid, EINVAL); 215 goto out; 216 } 217 218 udp_error_t urc = udp_uc_set_local(socket->assoc, &fsocket); 219 207 220 switch (urc) { 208 221 case UDP_EOK: … … 221 234 assert(false); 222 235 } 223 236 224 237 log_msg(LOG_DEFAULT, LVL_DEBUG, " - success"); 225 238 async_answer_0(callid, rc); 239 226 240 out: 227 if (addr != NULL)228 free(addr );241 if (addr6 != NULL) 242 free(addr6); 229 243 } 230 244 … … 251 265 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_send()"); 252 266 253 struct sockaddr_in *addr = NULL; 254 udp_sock_t fsock; 255 udp_sock_t *fsock_ptr; 267 struct sockaddr_in6 *addr6 = NULL; 268 struct sockaddr_in *addr; 269 udp_sock_t fsocket; 270 udp_sock_t *fsocket_ptr; 256 271 257 272 if (IPC_GET_IMETHOD(call) == NET_SOCKET_SENDTO) { 258 size_t addr_ size;259 int rc = async_data_write_accept((void **) &addr , false,260 0, 0, 0, &addr_ size);273 size_t addr_len; 274 int rc = async_data_write_accept((void **) &addr6, false, 275 0, 0, 0, &addr_len); 261 276 if (rc != EOK) { 262 277 async_answer_0(callid, rc); 263 goto out; 264 } 265 266 if (addr_size != sizeof(struct sockaddr_in)) { 278 return; 279 } 280 281 if ((addr_len != sizeof(struct sockaddr_in)) && 282 (addr_len != sizeof(struct sockaddr_in6))) { 267 283 async_answer_0(callid, EINVAL); 268 284 goto out; 269 285 } 270 286 271 fsock.addr.ipv4 = uint32_t_be2host(addr->sin_addr.s_addr); 272 fsock.port = uint16_t_be2host(addr->sin_port); 273 fsock_ptr = &fsock; 287 addr = (struct sockaddr_in *) addr6; 288 289 switch (addr->sin_family) { 290 case AF_INET: 291 inet_sockaddr_in_addr(addr, &fsocket.addr); 292 break; 293 case AF_INET6: 294 inet_sockaddr_in6_addr(addr6, &fsocket.addr); 295 break; 296 default: 297 async_answer_0(callid, EINVAL); 298 goto out; 299 } 300 301 fsocket.port = uint16_t_be2host(addr->sin_port); 302 fsocket_ptr = &fsocket; 274 303 } else 275 fsock _ptr = NULL;304 fsocket_ptr = NULL; 276 305 277 306 int socket_id = SOCKET_GET_SOCKET_ID(call); … … 314 343 fibril_mutex_lock(&socket->lock); 315 344 316 if ( socket->assoc->ident.local.addr.ipv4 == UDP_IPV4_ANY) {345 if (inet_addr_is_any(&socket->assoc->ident.local.addr)) { 317 346 /* Determine local IP address */ 318 inet_addr_t loc_addr, rem_addr; 319 320 rem_addr.ipv4 = fsock_ptr ? fsock.addr.ipv4 : 321 socket->assoc->ident.foreign.addr.ipv4; 347 inet_addr_t loc_addr; 348 inet_addr_t rem_addr; 349 350 rem_addr = fsocket_ptr ? fsocket.addr : 351 socket->assoc->ident.foreign.addr; 322 352 323 353 int rc = inet_get_srcaddr(&rem_addr, 0, &loc_addr); … … 330 360 } 331 361 332 socket->assoc->ident.local.addr.ipv4 = loc_addr.ipv4; 333 log_msg(LOG_DEFAULT, LVL_DEBUG, "Local IP address is %x", 334 socket->assoc->ident.local.addr.ipv4); 362 socket->assoc->ident.local.addr = loc_addr; 335 363 } 336 364 … … 360 388 361 389 udp_error_t urc = 362 udp_uc_send(socket->assoc, fsock _ptr, buffer, length, 0);390 udp_uc_send(socket->assoc, fsocket_ptr, buffer, length, 0); 363 391 364 392 switch (urc) { … … 395 423 396 424 out: 397 if (addr != NULL)398 free(addr );425 if (addr6 != NULL) 426 free(addr6); 399 427 } 400 428 401 429 static void udp_sock_recvfrom(udp_client_t *client, ipc_callid_t callid, ipc_call_t call) 402 430 { 403 int socket_id;404 int flags;405 size_t addr_length, length;406 socket_core_t *sock_core;407 udp_sockdata_t *socket;408 ipc_call_t answer;409 ipc_callid_t rcallid;410 size_t data_len;411 udp_error_t urc;412 udp_sock_t rsock;413 struct sockaddr_in addr;414 int rc;415 416 431 log_msg(LOG_DEFAULT, LVL_DEBUG, "%p: udp_sock_recv[from]()", client); 417 418 socket_id = SOCKET_GET_SOCKET_ID(call);419 flags = SOCKET_GET_FLAGS(call);420 421 sock_core =socket_cores_find(&client->sockets, socket_id);432 433 int socket_id = SOCKET_GET_SOCKET_ID(call); 434 435 socket_core_t *sock_core = 436 socket_cores_find(&client->sockets, socket_id); 422 437 if (sock_core == NULL) { 423 438 async_answer_0(callid, ENOTSOCK); 424 439 return; 425 440 } 426 427 socket = (udp_sockdata_t *)sock_core->specific_data; 441 442 udp_sockdata_t *socket = 443 (udp_sockdata_t *) sock_core->specific_data; 444 428 445 fibril_mutex_lock(&socket->lock); 429 446 430 447 if (socket->assoc == NULL) { 431 448 fibril_mutex_unlock(&socket->lock); … … 433 450 return; 434 451 } 435 436 (void)flags; 437 452 438 453 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): lock recv_buffer lock"); 454 439 455 fibril_mutex_lock(&socket->recv_buffer_lock); 440 while (socket->recv_buffer_used == 0 && socket->recv_error == UDP_EOK) { 456 457 while ((socket->recv_buffer_used == 0) && 458 (socket->recv_error == UDP_EOK)) { 441 459 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_sock_recvfrom(): wait for cv"); 442 460 fibril_condvar_wait(&socket->recv_buffer_cv, 443 461 &socket->recv_buffer_lock); 444 462 } 445 463 446 464 log_msg(LOG_DEFAULT, LVL_DEBUG, "Got data in sock recv_buffer"); 447 448 rsock = socket->recv_fsock; 449 data_len = socket->recv_buffer_used; 450 urc = socket->recv_error; 451 465 466 size_t data_len = socket->recv_buffer_used; 467 udp_error_t urc = socket->recv_error; 468 452 469 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** recv data_len=%zu", data_len); 453 470 471 int rc; 472 454 473 switch (urc) { 455 474 case UDP_EOK: … … 466 485 assert(false); 467 486 } 468 487 469 488 log_msg(LOG_DEFAULT, LVL_DEBUG, "**** udp_uc_receive -> %d", rc); 489 470 490 if (rc != EOK) { 471 491 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 474 494 return; 475 495 } 476 496 497 ipc_callid_t rcallid; 498 size_t addr_size = 0; 499 477 500 if (IPC_GET_IMETHOD(call) == NET_SOCKET_RECVFROM) { 478 /* Fill addr */ 479 addr.sin_family = AF_INET; 480 addr.sin_addr.s_addr = host2uint32_t_be(rsock.addr.ipv4); 481 addr.sin_port = host2uint16_t_be(rsock.port); 482 483 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 484 if (!async_data_read_receive(&rcallid, &addr_length)) { 501 /* Fill address */ 502 udp_sock_t *rsock = &socket->recv_fsock; 503 struct sockaddr_in addr; 504 struct sockaddr_in6 addr6; 505 size_t addr_length; 506 507 uint16_t addr_af = inet_addr_sockaddr_in(&rsock->addr, &addr, 508 &addr6); 509 510 switch (addr_af) { 511 case AF_INET: 512 addr.sin_port = host2uint16_t_be(rsock->port); 513 514 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read receive"); 515 if (!async_data_read_receive(&rcallid, &addr_length)) { 516 fibril_mutex_unlock(&socket->recv_buffer_lock); 517 fibril_mutex_unlock(&socket->lock); 518 async_answer_0(callid, EINVAL); 519 return; 520 } 521 522 if (addr_length > sizeof(addr)) 523 addr_length = sizeof(addr); 524 525 addr_size = sizeof(addr); 526 527 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 528 rc = async_data_read_finalize(rcallid, &addr, addr_length); 529 if (rc != EOK) { 530 fibril_mutex_unlock(&socket->recv_buffer_lock); 531 fibril_mutex_unlock(&socket->lock); 532 async_answer_0(callid, EINVAL); 533 return; 534 } 535 536 break; 537 case AF_INET6: 538 addr6.sin6_port = host2uint16_t_be(rsock->port); 539 540 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read receive"); 541 if (!async_data_read_receive(&rcallid, &addr_length)) { 542 fibril_mutex_unlock(&socket->recv_buffer_lock); 543 fibril_mutex_unlock(&socket->lock); 544 async_answer_0(callid, EINVAL); 545 return; 546 } 547 548 if (addr_length > sizeof(addr6)) 549 addr_length = sizeof(addr6); 550 551 addr_size = sizeof(addr6); 552 553 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr6 read finalize"); 554 rc = async_data_read_finalize(rcallid, &addr6, addr_length); 555 if (rc != EOK) { 556 fibril_mutex_unlock(&socket->recv_buffer_lock); 557 fibril_mutex_unlock(&socket->lock); 558 async_answer_0(callid, EINVAL); 559 return; 560 } 561 562 break; 563 default: 485 564 fibril_mutex_unlock(&socket->recv_buffer_lock); 486 565 fibril_mutex_unlock(&socket->lock); … … 488 567 return; 489 568 } 490 491 if (addr_length > sizeof(addr)) 492 addr_length = sizeof(addr); 493 494 log_msg(LOG_DEFAULT, LVL_DEBUG, "addr read finalize"); 495 rc = async_data_read_finalize(rcallid, &addr, addr_length); 496 if (rc != EOK) { 497 fibril_mutex_unlock(&socket->recv_buffer_lock); 498 fibril_mutex_unlock(&socket->lock); 499 async_answer_0(callid, EINVAL); 500 return; 501 } 502 } 503 569 } 570 504 571 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read receive"); 572 573 size_t length; 505 574 if (!async_data_read_receive(&rcallid, &length)) { 506 575 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 509 578 return; 510 579 } 511 580 512 581 if (length > data_len) 513 582 length = data_len; 514 583 515 584 log_msg(LOG_DEFAULT, LVL_DEBUG, "data read finalize"); 585 516 586 rc = async_data_read_finalize(rcallid, socket->recv_buffer, length); 517 518 if ( length < data_len && rc == EOK)587 588 if ((length < data_len) && (rc == EOK)) 519 589 rc = EOVERFLOW; 520 590 521 591 log_msg(LOG_DEFAULT, LVL_DEBUG, "read_data_length <- %zu", length); 592 593 ipc_call_t answer; 594 522 595 IPC_SET_ARG2(answer, 0); 523 596 SOCKET_SET_READ_DATA_LENGTH(answer, length); 524 SOCKET_SET_ADDRESS_LENGTH(answer, sizeof(addr));597 SOCKET_SET_ADDRESS_LENGTH(answer, addr_size); 525 598 async_answer_3(callid, EOK, IPC_GET_ARG1(answer), 526 599 IPC_GET_ARG2(answer), IPC_GET_ARG3(answer)); 527 600 528 601 socket->recv_buffer_used = 0; 529 602 530 603 fibril_condvar_broadcast(&socket->recv_buffer_cv); 531 604 fibril_mutex_unlock(&socket->recv_buffer_lock); … … 602 675 while (true) { 603 676 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] wait for rcv buffer empty()"); 604 while ( sock->recv_buffer_used != 0 && sock->sock_core != NULL) {677 while ((sock->recv_buffer_used != 0) && (sock->sock_core != NULL)) { 605 678 fibril_condvar_wait(&sock->recv_buffer_cv, 606 679 &sock->recv_buffer_lock); … … 623 696 log_msg(LOG_DEFAULT, LVL_DEBUG, "[] urc != UDP_EOK, break"); 624 697 fibril_condvar_broadcast(&sock->recv_buffer_cv); 698 fibril_mutex_unlock(&sock->recv_buffer_lock); 625 699 break; 626 700 }
Note:
See TracChangeset
for help on using the changeset viewer.