Changeset 683 for trunk/kernel/syscalls/sys_socket.c
- Timestamp:
- Jan 13, 2021, 12:36:17 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/syscalls/sys_socket.c
r670 r683 45 45 46 46 #if DEBUG_SYS_SOCKET 47 static char* socket_ cmd_type_str( uint32_t type )47 static char* socket_user_cmd_str( uint32_t type ) 48 48 { 49 49 if ( type == SOCK_CREATE ) return "CREATE"; … … 54 54 else if( type == SOCK_SEND ) return "SEND"; 55 55 else if( type == SOCK_RECV ) return "RECV"; 56 else if( type == SOCK_SENDTO ) return "SENDTO"; 57 else if( type == SOCK_RECVFROM ) return "RECVFROM"; 56 58 else return "undefined"; 57 59 } … … 79 81 80 82 #if DEBUG_SYS_SOCKET 83 char kbuf[64]; 81 84 if( DEBUG_SYS_SOCKET < (uint32_t)tm_start ) 82 printk("\n[%s] thread[%x,%x] enter /%s / a1 %x / a2 %x / a3 %x / cycle %d\n",83 __FUNCTION__, process->pid, this->trdid, socket_ cmd_type_str(cmd),85 printk("\n[%s] thread[%x,%x] enter for %s / a1 %x / a2 %x / a3 %x / cycle %d\n", 86 __FUNCTION__, process->pid, this->trdid, socket_user_cmd_str(cmd), 84 87 arg1, arg2, arg3, (uint32_t)tm_start ); 85 88 #endif … … 97 100 98 101 #if DEBUG_SYSCALLS_ERROR 99 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 100 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / domain %d =! AF_INET\n", 101 __FUNCTION__ , process->pid , this->trdid , domain ); 102 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / domain %d =! AF_INET / cycle %d\n", 103 __FUNCTION__ , process->pid , this->trdid , domain , (uint32_t)tm_start ); 102 104 #endif 103 105 this->errno = EINVAL; … … 110 112 111 113 #if DEBUG_SYSCALLS_ERROR 112 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 113 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / illegal socket type\n", 114 __FUNCTION__ , process->pid , this->trdid ); 114 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / illegal socket type / cycle %d\n", 115 __FUNCTION__ , process->pid , this->trdid , (uint32_t)tm_start); 115 116 #endif 116 117 this->errno = EINVAL; … … 126 127 127 128 #if DEBUG_SYSCALLS_ERROR 128 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 129 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / cannot create socket\n", 130 __FUNCTION__ , process->pid , this->trdid ); 129 printk("\n[ERROR] in %s : thread[%x,%x] / CREATE / cannot create socket / cycle %d\n", 130 __FUNCTION__ , process->pid , this->trdid , (uint32_t)tm_start); 131 131 #endif 132 132 this->errno = EINVAL; … … 148 148 149 149 #if DEBUG_SYSCALLS_ERROR 150 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 151 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / socket address %x unmapped\n", 152 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 150 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / socket address %x unmapped / cycle %d\n", 151 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 153 152 #endif 154 153 this->errno = EINVAL; … … 171 170 172 171 #if DEBUG_SYSCALLS_ERROR 173 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 174 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / cannot access socket[%x,%d]\n", 175 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 172 printk("\n[ERROR] in %s : thread[%x,%x] / BIND / cannot access socket[%x,%d] / cycle %d\n", 173 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 176 174 #endif 177 175 this->errno = EINVAL; … … 195 193 196 194 #if DEBUG_SYSCALLS_ERROR 197 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 198 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d]\n", 199 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 195 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d] / cycle %d\n", 196 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 200 197 #endif 201 198 this->errno = EINVAL; … … 217 214 218 215 #if DEBUG_SYSCALLS_ERROR 219 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 220 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped\n", 221 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 216 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped / cycle %d\n", 217 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 222 218 #endif 223 219 this->errno = EINVAL; … … 239 235 240 236 #if DEBUG_SYSCALLS_ERROR 241 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 242 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d]\n", 243 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 237 printk("\n[ERROR] in %s : thread[%x,%x] / LISTEN / cannot access socket[%x,%d] / cycle %d\n", 238 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 244 239 #endif 245 240 this->errno = EINVAL; … … 261 256 262 257 #if DEBUG_SYSCALLS_ERROR 263 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 264 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped\n", 265 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 258 printk("\n[ERROR] in %s : thread[%x,%x] / CONNECT / server address %x unmapped / cycle %d\n", 259 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 266 260 #endif 267 261 this->errno = EINVAL; … … 275 269 &k_sockaddr.sin_port ); 276 270 277 if( ret ) 278 { 279 280 #if DEBUG_SYSCALLS_ERROR 281 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 282 printk("\n[ERROR] in %s : thread[%x,%x] / ACCEPT / cannot access socket[%x,%d]\n", 283 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 271 if( ret < 0 ) 272 { 273 274 #if DEBUG_SYSCALLS_ERROR 275 printk("\n[ERROR] in %s : thread[%x,%x] / ACCEPT / cannot access socket[%x,%d] / cycle %d\n", 276 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 284 277 #endif 285 278 this->errno = EINVAL; … … 305 298 306 299 #if DEBUG_SYSCALLS_ERROR 307 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 308 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / buffer %x unmapped\n", 309 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 310 #endif 311 this->errno = EINVAL; 312 ret = -1; 313 break; 314 } 315 316 // check length 317 if( length == 0 ) 318 { 319 320 #if DEBUG_SYSCALLS_ERROR 321 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 322 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / buffer length is 0\n", 323 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 324 #endif 325 this->errno = EINVAL; 326 ret = -1; 327 break; 328 } 329 330 // cal relevant relevant socket function 331 ret = socket_send( fdid , u_buf , length ); 332 333 if( ret < 0 ) 334 { 335 336 #if DEBUG_SYSCALLS_ERROR 337 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 338 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / cannot access socket[%x,%d] \n", 339 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 340 #endif 341 this->errno = EINVAL; 342 } 300 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / u_buf %x unmapped / cycle %d\n", 301 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 302 #endif 303 this->errno = EINVAL; 304 ret = -1; 305 break; 306 } 307 308 // check length argument 309 if( (length == 0) || (length > (1<<CONFIG_SOCK_TX_BUF_ORDER)) ) 310 { 311 312 #if DEBUG_SYSCALLS_ERROR 313 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / bad buffer length %d / cycle %d\n", 314 __FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start ); 315 #endif 316 this->errno = EINVAL; 317 ret = -1; 318 break; 319 } 320 321 // cal relevant socket function 322 ret = socket_send( fdid, 323 u_buf, 324 length ); 325 if( ret < 0 ) 326 { 327 328 #if DEBUG_SYSCALLS_ERROR 329 printk("\n[ERROR] in %s : thread[%x,%x] / SEND / cannot access socket[%x,%d] / cycle %d\n", 330 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 331 #endif 332 this->errno = EINVAL; 333 } 334 335 #if DEBUG_SYS_SOCKET 336 if( DEBUG_SYS_SOCKET < (uint32_t)tm_start ) 337 { 338 hal_copy_from_uspace( XPTR( local_cxy , &kbuf ) , u_buf , ret ); 339 printk("\n[%s] thread[%x,%x] send %d bytes <%s>\n", 340 __FUNCTION__, process->pid, this->trdid , ret, kbuf ); 341 } 342 #endif 343 343 break; 344 344 } … … 355 355 356 356 #if DEBUG_SYSCALLS_ERROR 357 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 358 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / buffer %x unmapped\n", 359 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 360 #endif 361 this->errno = EINVAL; 362 ret = -1; 363 break; 364 } 365 366 // check length 367 if( length == 0 ) 368 { 369 370 #if DEBUG_SYSCALLS_ERROR 371 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 372 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / buffer length is 0\n", 373 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 ); 357 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / u_buf %x unmapped / cycle %d\n", 358 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 359 #endif 360 this->errno = EINVAL; 361 ret = -1; 362 break; 363 } 364 365 // check length argument 366 if( (length == 0) || (length > (1<<CONFIG_SOCK_RX_BUF_ORDER)) ) 367 { 368 369 #if DEBUG_SYSCALLS_ERROR 370 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / bad buffer length %d / cycle %d\n", 371 __FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start ); 374 372 #endif 375 373 this->errno = EINVAL; … … 379 377 380 378 // cal relevant kernel socket function 381 ret = socket_recv( fdid , u_buf , length ); 382 383 if( ret < 0 ) 384 { 385 386 #if DEBUG_SYSCALLS_ERROR 387 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 388 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / cannot access socket[%x,%d] \n", 389 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid ); 390 #endif 391 this->errno = EINVAL; 392 } 379 ret = socket_recv( fdid, 380 u_buf, 381 length ); 382 if( ret < 0 ) 383 { 384 385 #if DEBUG_SYSCALLS_ERROR 386 printk("\n[ERROR] in %s : thread[%x,%x] / RECV / cannot access socket[%x,%d] / cycle %d\n", 387 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 388 #endif 389 this->errno = EINVAL; 390 } 391 392 #if DEBUG_SYS_SOCKET 393 if( DEBUG_SYS_SOCKET < (uint32_t)tm_start ) 394 { 395 hal_copy_from_uspace( XPTR( local_cxy , &kbuf ) , u_buf , ret ); 396 printk("\n[%s] thread[%x,%x] received %d bytes <%s>\n", 397 __FUNCTION__, process->pid, this->trdid , ret, kbuf ); 398 } 399 #endif 400 break; 401 } 402 ///////////////// 403 case SOCK_SENDTO: 404 { 405 sockaddr_in_t k_remote_addr; 406 407 uint32_t fdid = (uint32_t)arg1 & 0x0000FFFF; 408 uint32_t length = (uint32_t)arg1 >> 16; 409 uint8_t * u_buf = (uint8_t *)(intptr_t)arg2; 410 sockaddr_t * u_remote_addr = (sockaddr_t *)(intptr_t)arg3; 411 412 // check u_buf mapped in user space 413 if( vmm_get_vseg( process , (intptr_t)arg2 , &vseg ) ) 414 { 415 416 #if DEBUG_SYSCALLS_ERROR 417 printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / u_buf %x unmapped / cycle %d\n", 418 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 419 #endif 420 this->errno = EINVAL; 421 ret = -1; 422 break; 423 } 424 425 // check u_remote_addr mapped in user space 426 if( vmm_get_vseg( process , (intptr_t)arg3 , &vseg ) ) 427 { 428 429 #if DEBUG_SYSCALLS_ERROR 430 printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / u_remote_addr %x unmapped / cycle %d\n", 431 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg3 , (uint32_t)tm_start ); 432 #endif 433 this->errno = EINVAL; 434 ret = -1; 435 break; 436 } 437 438 // check length argument 439 if( (length == 0) || (length > (1<<CONFIG_SOCK_TX_BUF_ORDER)) ) 440 { 441 442 #if DEBUG_SYSCALLS_ERROR 443 printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / bad length %d / cycle %d\n", 444 __FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start ); 445 #endif 446 this->errno = EINVAL; 447 ret = -1; 448 break; 449 } 450 451 // make a kernel copy of the sockaddr_t structure 452 hal_copy_from_uspace( XPTR( local_cxy , &k_remote_addr ), 453 u_remote_addr, sizeof(sockaddr_t) ); 454 455 // cal relevant socket function 456 ret = socket_sendto( fdid, 457 u_buf, 458 length, 459 k_remote_addr.sin_addr, 460 k_remote_addr.sin_port ); 461 if( ret < 0 ) 462 { 463 464 #if DEBUG_SYSCALLS_ERROR 465 printk("\n[ERROR] in %s : thread[%x,%x] / SENDTO / cannot access socket[%x,%d] / cycle %d\n", 466 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 467 #endif 468 this->errno = EINVAL; 469 } 470 471 break; 472 } 473 /////////////////// 474 case SOCK_RECVFROM: 475 { 476 sockaddr_in_t k_remote_addr; 477 478 uint32_t fdid = (uint32_t)arg1 & 0x0000FFFF; 479 uint32_t length = (uint32_t)arg1 >> 16; 480 uint8_t * u_buf = (uint8_t *)(intptr_t)arg2; 481 sockaddr_t * u_remote_addr = (sockaddr_t *)(intptr_t)arg3; 482 483 // check buffer is mapped in user space 484 if( vmm_get_vseg( process , (intptr_t)arg2 , &vseg ) ) 485 { 486 487 #if DEBUG_SYSCALLS_ERROR 488 printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / u_buf %x unmapped / cycle %d\n", 489 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg2 , (uint32_t)tm_start ); 490 #endif 491 this->errno = EINVAL; 492 ret = -1; 493 break; 494 } 495 496 // check u_remote_addr mapped in user space 497 if( vmm_get_vseg( process , (intptr_t)arg3 , &vseg ) ) 498 { 499 500 #if DEBUG_SYSCALLS_ERROR 501 printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / u_remote_addr %x unmapped / cycle %d\n", 502 __FUNCTION__ , process->pid , this->trdid , (intptr_t)arg3 , (uint32_t)tm_start ); 503 #endif 504 this->errno = EINVAL; 505 ret = -1; 506 break; 507 } 508 509 // check length argument 510 if( (length == 0) || (length > (1<<CONFIG_SOCK_RX_BUF_ORDER)) ) 511 { 512 513 #if DEBUG_SYSCALLS_ERROR 514 printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / bad length %d / cycle %d\n", 515 __FUNCTION__ , process->pid , this->trdid , length , (uint32_t)tm_start ); 516 #endif 517 this->errno = EINVAL; 518 ret = -1; 519 break; 520 } 521 522 // make a kernel copy of the sockaddr_t structure 523 hal_copy_from_uspace( XPTR( local_cxy , &k_remote_addr ), 524 u_remote_addr, sizeof(sockaddr_t) ); 525 526 // cal relevant socket function 527 ret = socket_recvfrom( fdid, 528 u_buf, 529 length, 530 k_remote_addr.sin_addr, 531 k_remote_addr.sin_port ); 532 if( ret < 0 ) 533 { 534 535 #if DEBUG_SYSCALLS_ERROR 536 printk("\n[ERROR] in %s : thread[%x,%x] / RECVFROM / cannot access socket[%x,%d] / cycle %d\n", 537 __FUNCTION__ , process->pid , this->trdid , process->pid, fdid , (uint32_t)tm_start ); 538 #endif 539 this->errno = EINVAL; 540 } 541 393 542 break; 394 543 } … … 398 547 399 548 #if DEBUG_SYSCALLS_ERROR 400 if( DEBUG_SYSCALLS_ERROR < (uint32_t)tm_start ) 401 printk("\n[ERROR] in %s : thread[%x,%x] / undefined socket operation %d\n", 402 __FUNCTION__ , process->pid , this->trdid , cmd ); 549 printk("\n[ERROR] in %s : thread[%x,%x] / undefined socket operation %d / cycle %d\n", 550 __FUNCTION__ , process->pid , this->trdid , cmd , (uint32_t)tm_start ); 403 551 #endif 404 552 this->errno = EINVAL; … … 413 561 414 562 #if DEBUG_SYS_SOCKET 415 if( DEBUG_SYS_SOCKET < tm_end ) 416 printk("\n[%s] thread[%x,%x] exit / cycle %d\n", 417 __FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end ); 563 printk("\n[%s] thread[%x,%x] exit for %s / cycle %d\n", 564 __FUNCTION__, process->pid, this->trdid, socket_user_cmd_str(cmd), (uint32_t)tm_end ); 418 565 #endif 419 566
Note: See TracChangeset
for help on using the changeset viewer.