OLD | NEW |
1 #define _LARGEFILE64_SOURCE 1 | 1 #define _LARGEFILE64_SOURCE 1 |
2 #include "process-manager.h" | 2 #include "process-manager.h" |
3 #include "process.h" | 3 #include "process.h" |
4 #include "utils.h" | 4 #include "utils.h" |
5 #include "unix-fd.h" | 5 #include "unix-fd.h" |
6 #include "unix-file-fd.h" | 6 #include "unix-file-fd.h" |
7 #include "unix-socket-fd.h" | 7 #include "unix-socket-fd.h" |
8 #include "unix-datagram-socket-fd.h" | 8 #include "unix-datagram-socket-fd.h" |
9 #include "unix-stream-socket-fd.h" | 9 #include "unix-stream-socket-fd.h" |
10 #include "waiter.h" | 10 #include "waiter.h" |
11 #include "simu-fcntl.h" | 11 #include "simu-fcntl.h" |
12 #include "simu-unistd.h" | 12 #include "simu-unistd.h" |
13 #include "simu-poll.h" | 13 #include "simu-poll.h" |
14 #include "simu-stdio.h" | 14 #include "simu-stdio.h" |
15 #include "sys/simu-socket.h" | 15 #include "sys/simu-socket.h" |
16 #include "sys/simu-ioctl.h" | 16 #include "sys/simu-ioctl.h" |
17 #include "sys/simu-stat.h" | 17 #include "sys/simu-stat.h" |
18 #include "sys/simu-mman.h" | 18 #include "sys/simu-mman.h" |
19 #include "sys/simu-select.h" | 19 #include "sys/simu-select.h" |
20 #include "ns3/log.h" | 20 #include "ns3/log.h" |
21 #include "ns3/socket-factory.h" | 21 #include "ns3/socket-factory.h" |
22 #include "ns3/socket.h" | 22 #include "ns3/socket.h" |
23 #include "ns3/uinteger.h" | 23 #include "ns3/uinteger.h" |
24 #include "ns3/event-id.h" | 24 #include "ns3/event-id.h" |
25 #include "ns3/simulator.h" | 25 #include "ns3/simulator.h" |
26 #include "ns3/nstime.h" | 26 #include "ns3/nstime.h" |
27 #include <fcntl.h> | 27 #include <fcntl.h> |
28 #include <errno.h> | 28 #include <errno.h> |
| 29 #include <linux/netlink.h> |
| 30 #include <linux/rtnetlink.h> |
29 #include <sys/mman.h> | 31 #include <sys/mman.h> |
30 #include <sys/types.h> | 32 #include <sys/types.h> |
| 33 #include "ns3/node.h" |
31 | 34 |
32 NS_LOG_COMPONENT_DEFINE ("SimuFd"); | 35 NS_LOG_COMPONENT_DEFINE ("SimuFd"); |
33 | 36 |
34 | 37 |
35 #ifdef _K_SS_MAXSIZE | 38 #ifdef _K_SS_MAXSIZE |
36 #define SOCK_MAX_ADDRESS_SIZE _K_SS_MAXSIZE | 39 #define SOCK_MAX_ADDRESS_SIZE _K_SS_MAXSIZE |
37 #else | 40 #else |
38 #define SOCK_MAX_ADDRESS_SIZE 128 | 41 #define SOCK_MAX_ADDRESS_SIZE 128 |
39 #endif | 42 #endif |
40 | 43 |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 if (index == -1) | 400 if (index == -1) |
398 { | 401 { |
399 current->err = EBADF; | 402 current->err = EBADF; |
400 return -1; | 403 return -1; |
401 } | 404 } |
402 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; | 405 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; |
403 int retval = unixFd->Write (buf, count); | 406 int retval = unixFd->Write (buf, count); |
404 return retval; | 407 return retval; |
405 } | 408 } |
406 | 409 |
| 410 ssize_t simu_writev (int fd, const struct iovec *iov, int iovcnt) |
| 411 { |
| 412 Thread *current = Current (); |
| 413 NS_LOG_FUNCTION (current << UtilsGetNodeId () << fd << iov << iovcnt); |
| 414 NS_ASSERT (current != 0); |
| 415 int index = UtilsSearchOpenFd (fd); |
| 416 if (index == -1) |
| 417 { |
| 418 current->err = EBADF; |
| 419 return -1; |
| 420 } |
| 421 |
| 422 size_t count = 0; |
| 423 for (int i = 0; i < iovcnt; ++i) |
| 424 count += iov[i].iov_len; |
| 425 |
| 426 if (count == 0)· |
| 427 { |
| 428 current->err = EINVAL; |
| 429 return -1; |
| 430 } |
| 431 |
| 432 char buf[count], *bufp = buf; |
| 433 for (int i = 0; i < iovcnt; ++i) |
| 434 { |
| 435 memcpy (bufp, iov[i].iov_base, iov[i].iov_len); |
| 436 bufp += iov[i].iov_len; |
| 437 } |
| 438 |
| 439 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; |
| 440 int retval = unixFd->Write (buf, count); |
| 441 |
| 442 return retval; |
| 443 } |
| 444 |
407 ssize_t simu_read (int fd, void *buf, size_t count) | 445 ssize_t simu_read (int fd, void *buf, size_t count) |
408 { | 446 { |
409 Thread *current = Current (); | 447 Thread *current = Current (); |
410 NS_LOG_FUNCTION (current << UtilsGetNodeId () << fd << buf << count); | 448 NS_LOG_FUNCTION (current << UtilsGetNodeId () << fd << buf << count); |
411 NS_ASSERT (current != 0); | 449 NS_ASSERT (current != 0); |
412 int index = UtilsSearchOpenFd (fd); | 450 int index = UtilsSearchOpenFd (fd); |
413 if (index == -1) | 451 if (index == -1) |
414 { | 452 { |
415 current->err = EBADF; | 453 current->err = EBADF; |
416 return -1; | 454 return -1; |
417 } | 455 } |
418 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; | 456 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; |
419 int retval = unixFd->Read (buf, count); | 457 int retval = unixFd->Read (buf, count); |
420 return retval; | 458 return retval; |
421 } | 459 } |
422 int simu_socket (int domain, int type, int protocol) | 460 int simu_socket (int domain, int type, int protocol) |
423 { | 461 { |
424 Thread *current = Current (); | 462 Thread *current = Current (); |
425 ProcessManager *manager = current->process->manager; | 463 ProcessManager *manager = current->process->manager; |
426 NS_LOG_FUNCTION (current << UtilsGetNodeId () << domain << type << protocol); | 464 NS_LOG_FUNCTION (current << UtilsGetNodeId () << domain << type << protocol); |
427 NS_ASSERT (current != 0); | 465 NS_ASSERT (current != 0); |
428 NS_ASSERT (manager != 0); | 466 NS_ASSERT (manager != 0); |
429 NS_ASSERT (domain == PF_INET); | 467 NS_ASSERT (domain == PF_INET || domain == PF_NETLINK || domain == PF_INET6); |
430 | 468 |
431 int fd = UtilsAllocateFd (); | 469 int fd = UtilsAllocateFd (); |
432 if (fd == -1) | 470 if (fd == -1) |
433 { | 471 { |
434 current->err = EMFILE; | 472 current->err = EMFILE; |
435 return -1; | 473 return -1; |
436 } | 474 } |
437 | 475 |
438 Ptr<Socket> sock; | 476 Ptr<Socket> sock; |
439 Ptr<UnixSocketFd> socket; | 477 Ptr<UnixSocketFd> socket; |
440 | 478 |
441 switch (type) { | 479 if (domain == PF_INET) |
442 case SOCK_DGRAM: { | 480 { |
443 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); | 481 switch (type) { |
444 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); | 482 case SOCK_DGRAM: { |
445 sock = factory->CreateSocket (); | 483 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
446 socket = Create<UnixDatagramSocketFd> (sock); | 484 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
447 } break; | 485 sock = factory->CreateSocket (); |
448 case SOCK_RAW: { | 486 socket = Create<UnixDatagramSocketFd> (sock); |
449 TypeId tid = TypeId::LookupByName ("ns3::Ipv4RawSocketFactory"); | 487 } break; |
450 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); | 488 case SOCK_RAW: { |
451 sock = factory->CreateSocket (); | 489 TypeId tid = TypeId::LookupByName ("ns3::Ipv4RawSocketFactory"); |
452 sock->SetAttribute ("Protocol", UintegerValue (protocol)); | 490 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
453 socket = Create<UnixDatagramSocketFd> (sock); | 491 sock = factory->CreateSocket (); |
454 } break; | 492 sock->SetAttribute ("Protocol", UintegerValue (protocol)); |
455 case SOCK_STREAM: { | 493 socket = Create<UnixDatagramSocketFd> (sock); |
456 TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory"); | 494 } break; |
457 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); | 495 case SOCK_STREAM: { |
458 sock = factory->CreateSocket (); | 496 TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory"); |
459 socket = Create<UnixStreamSocketFd> (sock); | 497 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
460 } break; | 498 sock = factory->CreateSocket (); |
| 499 socket = Create<UnixStreamSocketFd> (sock); |
| 500 } break; |
| 501 } |
| 502 } |
| 503 else if (domain == PF_INET6) |
| 504 { |
| 505 switch (type) { |
| 506 case SOCK_DGRAM: { |
| 507 TypeId tid = TypeId::LookupByName ("ns3::Udp6SocketFactory"); |
| 508 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
| 509 sock = factory->CreateSocket (); |
| 510 socket = Create<UnixDatagramSocketFd> (sock); |
| 511 } break; |
| 512 case SOCK_RAW: { |
| 513 TypeId tid = TypeId::LookupByName ("ns3::Icmpv6SocketFactory"); |
| 514 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
| 515 sock = factory->CreateSocket (); |
| 516 sock->Bind(); |
| 517 // sock->SetAttribute ("Protocol", UintegerValue (protocol)); |
| 518 socket = Create<UnixDatagramSocketFd> (sock); |
| 519 } break; |
| 520 case SOCK_STREAM: { |
| 521 TypeId tid = TypeId::LookupByName ("ns3::Tcp6SocketFactory"); |
| 522 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
| 523 sock = factory->CreateSocket (); |
| 524 socket = Create<UnixStreamSocketFd> (sock); |
| 525 } break; |
| 526 } |
| 527 } |
| 528 else if (domain == PF_NETLINK) |
| 529 { |
| 530 if (type != SOCK_DGRAM && type != SOCK_RAW) |
| 531 { |
| 532 current->err = ESOCKTNOSUPPORT; |
| 533 return -1; |
| 534 } |
| 535 /*for now we only support NETLINK_ROUTE*/ |
| 536 if (protocol != NETLINK_ROUTE) |
| 537 { |
| 538 current->err = EPROTONOSUPPORT; |
| 539 return -1; |
| 540 } |
| 541 ······ |
| 542 TypeId tid= TypeId::LookupByName("ns3::NetlinkSocketFactory"); |
| 543 Ptr<SocketFactory> factory = manager->GetObject<SocketFactory> (tid); |
| 544 sock = factory->CreateSocket (); |
| 545 socket = Create<UnixDatagramSocketFd> (sock); |
461 } | 546 } |
462 | 547 |
463 current->process->openFiles.push_back (std::make_pair (fd, socket)); | 548 current->process->openFiles.push_back (std::make_pair (fd, socket)); |
464 return fd; | 549 return fd; |
465 } | 550 } |
466 int simu_bind (int fd, const struct sockaddr *my_addr, socklen_t addrlen) | 551 int simu_bind (int fd, const struct sockaddr *my_addr, socklen_t addrlen) |
467 { | 552 { |
468 Thread *current = Current (); | 553 Thread *current = Current (); |
469 NS_LOG_FUNCTION (current << UtilsGetNodeId () << fd << my_addr << addrlen); | 554 NS_LOG_FUNCTION (current << UtilsGetNodeId () << fd << my_addr << addrlen); |
470 NS_ASSERT (current != 0); | 555 NS_ASSERT (current != 0); |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 int index = UtilsSearchOpenFd (fildes); | 853 int index = UtilsSearchOpenFd (fildes); |
769 if (index == -1) | 854 if (index == -1) |
770 { | 855 { |
771 current->err = EBADF; | 856 current->err = EBADF; |
772 return -1; | 857 return -1; |
773 } | 858 } |
774 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; | 859 Ptr<UnixFd> unixFd = current->process->openFiles[index].second; |
775 off_t retval = unixFd->Lseek (offset, whence); | 860 off_t retval = unixFd->Lseek (offset, whence); |
776 return retval;···· | 861 return retval;···· |
777 } | 862 } |
| 863 int simu_fcntl(int fd, int cmd) |
| 864 { |
| 865 NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << fd << cmd); |
| 866 NS_ASSERT (Current () != 0); |
| 867 Thread *current = Current (); |
778 | 868 |
779 | 869 int index = UtilsSearchOpenFd (fd); |
| 870 if (index == -1) |
| 871 { |
| 872 current->err = EBADF; |
| 873 return -1; |
| 874 } |
| 875 // Currently NOP for this sys_call |
| 876 return 0; |
| 877 } |
OLD | NEW |