

Public Member Functions | |
| IPNetwork () | |
| int | ttl () const |
| int | noloopback_broken () |
| int | setmttl (Socket, int) |
| int | setmloop (Socket, int) |
| int | command (int argc, const char *const *argv) |
| Socket | rchannel () |
| Socket | schannel () |
| int | send (u_char *buf, int len) |
| int | recv (u_char *buf, int len, sockaddr &from, double &) |
| in_addr & | laddr () |
| in_addr & | dstaddr () |
| int | add_membership (Socket, in_addr &grp) |
| int | drop_membership (Socket, in_addr &grp) |
| virtual int | recv (netpkt_handler callback, void *clientdata) |
| int | mode () |
Static Public Member Functions | |
| int | bindsock (Socket, in_addr &, u_int16_t, sockaddr_in &) |
| int | connectsock (Socket, in_addr &, u_int16_t, sockaddr_in &) |
| int | rbufsize (Socket, int) |
| int | sbufsize (Socket, int) |
| int | nonblock (int fd) |
| int | parsemode (const char *) |
| char * | modename (int) |
Protected Member Functions | |
| void | reset (int reconfigure) |
| virtual int | open (int mode) |
| virtual void | reconfigure () |
| int | close () |
Protected Attributes | |
| in_addr | destaddr_ |
| in_addr | localaddr_ |
| int | mttl_ |
| Socket | rsock_ |
| Socket | ssock_ |
| int | noloopback_broken_ |
| int | loop_ |
| time_t | last_reset_ |
| int | mode_ |
|
|
Definition at line 178 of file net-ip.cc. References destaddr_, localaddr_, and NIDEBUG.
00178 : 00179 mttl_(0), 00180 rsock_(-1), 00181 ssock_(-1), 00182 noloopback_broken_(0), 00183 loop_(1) 00184 { 00185 localaddr_.s_addr = 0L; 00186 destaddr_.s_addr = 0L; 00187 NIDEBUG("IPNetwork: ctor\n"); 00188 } |
|
||||||||||||
|
Definition at line 769 of file net-ip.cc. References NIDEBUG3. Referenced by UDPIPNetwork::add_membership(), and command().
00770 {
00771
00772 #if defined(IP_ADD_MEMBERSHIP)
00773 if (IN_CLASSD(ntohl(addr.s_addr))) {
00774 #ifdef notdef
00775 /*
00776 * Try to bind the multicast address as the socket
00777 * dest address. On many systems this won't work
00778 * so fall back to a destination of INADDR_ANY if
00779 * the first bind fails.
00780 */
00781 sockaddr_in sin;
00782 memset(&sin, 0, sizeof(sin));
00783 sin.sin_family = AF_INET;
00784 sin.sin_addr = addr;
00785 if (::bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
00786 sin.sin_addr.s_addr = INADDR_ANY;
00787 if (::bind(fd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
00788 fprintf(stderr,
00789 "IPNetwork(%s): add_membership: unable to bind to addr %s: %s\n",
00790 name(), inet_ntoa(sin.sin_addr),
00791 strerror(errno));
00792 return (-1);
00793 }
00794 }
00795 #endif
00796 /*
00797 * XXX This is bogus multicast setup that really
00798 * shouldn't have to be done (group membership should be
00799 * implicit in the IP class D address, route should contain
00800 * ttl & no loopback flag, etc.). Steve Deering has promised
00801 * to fix this for the 4.4bsd release. We're all waiting
00802 * with bated breath.
00803 */
00804 struct ip_mreq mr;
00805
00806 mr.imr_multiaddr = addr;
00807 mr.imr_interface.s_addr = INADDR_ANY;
00808 if (::setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
00809 (char *)&mr, sizeof(mr)) < 0) {
00810 fprintf(stderr, "IPNetwork(%s): add_membership: unable to add membership for addr %s: %s\n",
00811 name(), inet_ntoa(addr), strerror(errno));
00812 return (-1);
00813 }
00814 NIDEBUG3("IPNetwork(%s): add_membership for grp %s done\n",
00815 name(), inet_ntoa(addr));
00816 return (0);
00817 }
00818 #else
00819 fprintf(stderr, "IPNetwork(%s): add_membership: host does not support IP multicast\n",
00820 name());
00821 #endif
00822 NIDEBUG3("IPNetwork(%s): add_membership for grp %s failed\n",
00823 name(), inet_ntoa(addr));
00824 return (-1);
00825 }
|
|
||||||||||||||||||||
|
Definition at line 861 of file net-ip.cc. Referenced by UDPIPNetwork::add_membership(), and UDPIPNetwork::bind().
00862 {
00863 memset((char *)&sin, 0, sizeof(sin));
00864 sin.sin_family = AF_INET;
00865 sin.sin_port = port;
00866 sin.sin_addr = addr;
00867 return(::bind(s, (struct sockaddr *)&sin, sizeof(sin)));
00868 }
|
|
|
Definition at line 751 of file net-ip.cc. References rsock_, and ssock_. Referenced by command().
|
|
||||||||||||
|
Reimplemented from Network. Reimplemented in UDPIPNetwork. Definition at line 594 of file net-ip.cc. References add_membership(), close(), Network::command(), destaddr_, drop_membership(), localaddr_, LookupHostAddr(), Network::mode(), mttl_, open(), Network::parsemode(), rchannel(), schannel(), and setmloop(). Referenced by UDPIPNetwork::command().
00595 {
00596 Tcl& tcl = Tcl::instance();
00597 if (argc == 2) {
00598 if (strcmp(argv[1], "close") == 0) {
00599 close();
00600 return (TCL_OK);
00601 }
00602 char* cp = tcl.result();
00603 if (strcmp(argv[1], "destaddr") == 0) {
00604 strcpy(cp, inet_ntoa(destaddr_));
00605 return (TCL_OK);
00606 }
00607 if (strcmp(argv[1], "localaddr") == 0) {
00608 strcpy(cp, inet_ntoa(localaddr_));
00609 return (TCL_OK);
00610 }
00611 if (strcmp(argv[1], "mttl") == 0) {
00612 tcl.resultf("%d", mttl_);
00613 return (TCL_OK);
00614 }
00615 /* for backward compatability */
00616 if (strcmp(argv[1], "ismulticast") == 0) {
00617 tcl.result(IN_CLASSD(ntohl(destaddr_.s_addr)) ?
00618 "1" : "0");
00619 return (TCL_OK);
00620 }
00621 if (strcmp(argv[1], "addr") == 0) {
00622 strcpy(cp, inet_ntoa(destaddr_));
00623 return (TCL_OK);
00624 }
00625 if (strcmp(argv[1], "ttl") == 0) {
00626 tcl.resultf("%d", mttl_);
00627 return (TCL_OK);
00628 }
00629 if (strcmp(argv[1], "interface") == 0) {
00630 strcpy(cp, inet_ntoa(localaddr_));
00631 return (TCL_OK);
00632 }
00633 } else if (argc == 3) {
00634
00635 if (strcmp(argv[1], "open") == 0) {
00636 int mode = parsemode(argv[2]);
00637 if (open(mode) < 0)
00638 return (TCL_ERROR);
00639 return (TCL_OK);
00640 }
00641 if (strcmp(argv[1], "add-membership") == 0) {
00642 in_addr addr;
00643 addr.s_addr = LookupHostAddr(argv[2]);
00644 if (add_membership(rchannel(), addr) < 0)
00645 tcl.result("0");
00646 else
00647 tcl.result("1");
00648 return (TCL_OK);
00649 }
00650 if (strcmp(argv[1], "drop-membership") == 0) {
00651 in_addr addr;
00652 addr.s_addr = LookupHostAddr(argv[2]);
00653 if (drop_membership(rchannel(), addr) < 0)
00654 tcl.result("0");
00655 else
00656 tcl.result("1");
00657 return (TCL_OK);
00658 }
00659 if (strcmp(argv[1], "loopback") == 0) {
00660 int val = atoi(argv[2]);
00661 if (strcmp(argv[2], "true") == 0)
00662 val = 1;
00663 else if (strcmp(argv[2], "false") == 0)
00664 val = 0;
00665 if (setmloop(schannel(), val) < 0)
00666 tcl.result("0");
00667 else
00668 tcl.result("1");
00669 return (TCL_OK);
00670 }
00671 }
00672 return (Network::command(argc, argv));
00673 }
|
Here is the call graph for this function:

|
||||||||||||||||||||
|
Definition at line 871 of file net-ip.cc. Referenced by UDPIPNetwork::bind(), UDPIPNetwork::connect(), and open().
00872 {
00873 memset((char *)&sin, 0, sizeof(sin));
00874 sin.sin_family = AF_INET;
00875 sin.sin_port = port;
00876 sin.sin_addr = addr;
00877 return(::connect(s, (struct sockaddr *)&sin, sizeof(sin)));
00878 }
|
|
||||||||||||
|
Definition at line 832 of file net-ip.cc. References NIDEBUG3. Referenced by command().
00833 {
00834
00835 #if defined(IP_DROP_MEMBERSHIP)
00836 if (IN_CLASSD(ntohl(addr.s_addr))) {
00837 struct ip_mreq mr;
00838
00839 mr.imr_multiaddr = addr;
00840 mr.imr_interface.s_addr = INADDR_ANY;
00841 if (::setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP,
00842 (char *)&mr, sizeof(mr)) < 0) {
00843 fprintf(stderr, "IPNetwork(%s): drop_membership: unable to drop membership for addr %s: %s\n",
00844 name(), inet_ntoa(addr), strerror(errno));
00845 return (-1);
00846 }
00847 NIDEBUG3("IPNetwork(%s): drop_membership for grp %s done\n",
00848 name(), inet_ntoa(addr));
00849 return (0);
00850 }
00851 #else
00852 fprintf(stderr, "IPNetwork(%s): drop_membership: host does not support IP multicast\n",
00853 name());
00854 #endif
00855 NIDEBUG3("IPNetwork(%s): drop_membership for grp %s failed\n",
00856 name(), inet_ntoa(addr));
00857 return (-1);
00858 }
|
|
|
Definition at line 114 of file net-ip.cc. References destaddr_.
00114 { return (destaddr_); }
|
|
|
Definition at line 113 of file net-ip.cc. References localaddr_.
00113 { return (localaddr_); }
|
|
|
Definition at line 71 of file net.h. References Network::mode_. Referenced by PcapFileNetwork::command(), PcapLiveNetwork::command(), command(), TapAgent::linknet(), TCPTapAgent::recvpkt(), TapAgent::recvpkt(), IPTapAgent::recvpkt(), TCPTapAgent::sendpkt(), TapAgent::sendpkt(), and IPTapAgent::sendpkt().
00071 { return mode_; }
|
|
|
Definition at line 154 of file net.cc. Referenced by Network::command().
00155 {
00156 switch (mode) {
00157 case O_RDONLY:
00158 return ("readonly");
00159 case O_WRONLY:
00160 return ("writeonly");
00161 case O_RDWR:
00162 return ("readwrite");
00163 }
00164 return ("unknown");
00165 }
|
|
|
Definition at line 101 of file net-ip.cc. References noloopback_broken_.
00101 { // no loopback filter?
00102 return (noloopback_broken_);
00103 }
|
|
|
Definition at line 111 of file net.cc. Referenced by UDPIPNetwork::open().
00112 {
00113 #ifdef WIN32
00114 u_long flag = 1;
00115 if (ioctlsocket(fd, FIONBIO, &flag) == -1) {
00116 fprintf(stderr,
00117 "Network::nonblock(): ioctlsocket: FIONBIO: %lu\n",
00118 GetLastError());
00119 return -1;
00120 }
00121 #else
00122 int flags;
00123 if ((flags = fcntl(fd, F_GETFL, 0)) < 0) {
00124 perror("Network::nonblock(): fcntl");
00125 return (-1);
00126 }
00127 #if defined(hpux) || defined(__hpux)
00128 flags |= O_NONBLOCK;
00129 #else
00130 flags |= O_NONBLOCK|O_NDELAY;
00131 #endif
00132 if (fcntl(fd, F_SETFL, flags) == -1) {
00133 perror("Network::nonblock(): fcntl: F_SETFL");
00134 return -1;
00135 }
00136 #endif
00137 return 0;
00138 }
|
|
|
Reimplemented in UDPIPNetwork. Definition at line 702 of file net-ip.cc. References connectsock(), Network::mode_, NIDEBUG5, rsock_, Socket, and ssock_. Referenced by command(), and reset().
00703 {
00704 // obtain a raw socket we can use to send ip datagrams
00705 Socket fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
00706 if (fd < 0) {
00707 perror("socket(RAW)");
00708 if (::getuid() != 0 && ::geteuid() != 0) {
00709 fprintf(stderr,
00710 "IPNetwork(%s): open: use of the Network/IP object requires super-user privs\n",
00711 name());
00712 }
00713
00714 return (-1);
00715 }
00716
00717 // turn on HDRINCL option (we will be writing IP header)
00718 // in FreeBSD 2.2.5 (and possibly others), the IP id field
00719 // is set by the kernel routine rip_output()
00720 // only if it is non-zero, so we should be ok.
00721 int one = 1;
00722 if (::setsockopt(fd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) {
00723 fprintf(stderr,
00724 "IPNetwork(%s): open: unable to turn on IP_HDRINCL: %s\n",
00725 name(), strerror(errno));
00726 return (-1);
00727 }
00728 #ifndef __linux__
00729 // sort of curious, but do a connect() even though we have
00730 // HDRINCL on. Otherwise, we get ENOTCONN when doing a send()
00731 sockaddr_in sin;
00732 in_addr ia = { INADDR_ANY };
00733 if (connectsock(fd, ia, 0, sin) < 0) {
00734 fprintf(stderr,
00735 "IPNetwork(%s): open: unable to connect : %s\n",
00736 name(), strerror(errno));
00737 }
00738 #endif
00739 rsock_ = ssock_ = fd;
00740 mode_ = mode;
00741 NIDEBUG5("IPNetwork(%s): opened with mode %d, rsock_:%d, ssock_:%d\n",
00742 name(), mode_, rsock_, ssock_);
00743 return 0;
00744 }
|
Here is the call graph for this function:

|
|
Definition at line 141 of file net.cc. Referenced by PcapFileNetwork::command(), PcapLiveNetwork::command(), and command().
00142 {
00143 if (strcmp(mname, "readonly") == 0) {
00144 return (O_RDONLY);
00145 } else if (strcmp(mname, "readwrite") == 0) {
00146 return (O_RDWR);
00147 } else if (strcmp(mname, "writeonly") == 0) {
00148 return (O_WRONLY);
00149 }
00150 return (::atoi(mname));
00151 }
|
|
||||||||||||
|
Definition at line 886 of file net-ip.cc. Referenced by UDPIPNetwork::open().
00887 {
00888 return(::setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&cnt, sizeof(cnt)));
00889 }
|
|
|
Implements Network. Definition at line 107 of file net-ip.cc. References rsock_, and Socket. Referenced by command().
00107 { return(rsock_); } // virtual in Network
|
|
|
Reimplemented in UDPIPNetwork. Definition at line 950 of file net-ip.cc. Referenced by reset().
00951 {
00952 }
|
|
||||||||||||
|
Reimplemented in PcapNetwork. Definition at line 62 of file net.h.
00062 {
00063 Tcl::instance().evalf("%s info class", name());
00064 fprintf( stderr, "Callback Interface to receiving packets"
00065 " unsupported in class %s\n",
00066 Tcl::instance().result() );
00067 return 0;
00068 } // callback called for every packet
|
|
||||||||||||||||||||
|
Implements Network. Reimplemented in UDPIPNetwork. Definition at line 547 of file net-ip.cc. References abort(), Scheduler::clock(), Scheduler::instance(), len, Network::mode_, rsock_, socklen_t, and ts.
00548 {
00549 if (mode_ == O_WRONLY) {
00550 fprintf(stderr,
00551 "IPNetwork(%s) recv while in writeonly mode!\n",
00552 name());
00553 abort();
00554 }
00555 int fromlen = sizeof(sa);
00556 int cc = ::recvfrom(rsock_, (char*)buf, len, 0, &sa, (socklen_t*)&fromlen);
00557 if (cc < 0) {
00558 if (errno != EWOULDBLOCK)
00559 perror("recvfrom");
00560 return (-1);
00561 }
00562 ts = Scheduler::instance().clock();
00563 return (cc);
00564 }
|
Here is the call graph for this function:

|
|
Definition at line 921 of file net-ip.cc. References last_reset_, Network::mode_, NIDEBUG2, open(), reconfigure(), rsock_, and ssock_. Referenced by UDPIPNetwork::send().
00922 {
00923 time_t t = time(0);
00924 int d = int(t - last_reset_);
00925 NIDEBUG2("IPNetwork(%s): reset\n", name());
00926 if (d > 3) { // Steve: why?
00927 last_reset_ = t;
00928 if (ssock_ >= 0)
00929 (void)::close(ssock_);
00930 if (rsock_ >= 0)
00931 (void)::close(rsock_);
00932 if (open(mode_) < 0) {
00933 fprintf(stderr,
00934 "IPNetwork(%s): couldn't reset\n",
00935 name());
00936 mode_ = -1;
00937 return;
00938 }
00939 if (restart)
00940 (void) reconfigure();
00941 }
00942 }
|
Here is the call graph for this function:

|
||||||||||||
|
Definition at line 880 of file net-ip.cc. Referenced by UDPIPNetwork::open().
00881 {
00882 return(::setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&cnt, sizeof(cnt)));
00883 }
|
|
|
Implements Network. Definition at line 108 of file net-ip.cc. References Socket, and ssock_. Referenced by command(), and UDPIPNetwork::send().
00108 { return(ssock_); } // virtual in Network
|
|
||||||||||||
|
Implements Network. Reimplemented in UDPIPNetwork. Definition at line 573 of file net-ip.cc.
00574 {
00575 struct ip *ip = (struct ip*) buf;
00576 #ifdef __linux__
00577 // For raw sockets on linux the send does not work,
00578 // all packets show up only on the loopback device and are not routed
00579 // to the correct host. Using sendto on a closed socket solves this problem
00580 ip->ip_len = (ip->ip_len);
00581 ip->ip_off = (ip->ip_off);
00582 sockaddr_in sin;
00583 memset((char *)&sin, 0, sizeof(sin));
00584 sin.sin_family = AF_INET;
00585 sin.sin_addr = ip->ip_dst;
00586 return (::sendto(ssock_, (char*)buf, len, 0,(sockaddr *) &sin,sizeof(sin)));
00587 #else
00588 ip->ip_len = ntohs(ip->ip_len);
00589 ip->ip_off = ntohs(ip->ip_off);
00590 return (::send(ssock_, (char*)buf, len, 0));
00591 #endif
00592 }
|
|
||||||||||||
|
Definition at line 892 of file net-ip.cc. References loop_, and noloopback_broken_. Referenced by command().
00893 {
00894
00895 #ifdef IP_MULTICAST_LOOP
00896 u_char c = loop;
00897
00898 if (::setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &c, sizeof(c)) < 0) {
00899 /*
00900 * If we cannot turn off loopback (Like on the
00901 * Microsoft TCP/IP stack), then declare this
00902 * option broken so that our packets can be
00903 * filtered on the recv path.
00904 */
00905 if (c != loop) {
00906 noloopback_broken_ = 1;
00907 loop_ = c;
00908 }
00909 return (-1);
00910 }
00911 noloopback_broken_ = 0;
00912 #else
00913 fprintf(stderr, "IPNetwork(%s): msetloop: host does not support IP multicast\n",
00914 name());
00915 #endif
00916 loop_ = c;
00917 return (0);
00918 }
|
|
||||||||||||
|
Definition at line 675 of file net-ip.cc.
00676 {
00677 /* set the multicast TTL */
00678
00679 #ifdef WIN32
00680 u_int t = ttl;
00681 #else
00682 u_char t = ttl;
00683 #endif
00684
00685 t = (ttl > 255) ? 255 : (ttl < 0) ? 0 : ttl;
00686 if (::setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL,
00687 (char*)&t, sizeof(t)) < 0) {
00688 fprintf(stderr,
00689 "IPNetwork(%s): couldn't set multicast ttl to %d\n",
00690 name(), t);
00691 return (-1);
00692 }
00693 return (0);
00694 }
|
|
|
Definition at line 100 of file net-ip.cc. References mttl_.
00100 { return (mttl_); } // current mcast ttl
|
|
|
Definition at line 127 of file net-ip.cc. Referenced by command(), UDPIPNetwork::connect(), dstaddr(), and IPNetwork(). |
|
|
Definition at line 141 of file net-ip.cc. Referenced by UDPIPNetwork::connect(), and reset(). |
|
|
Definition at line 128 of file net-ip.cc. Referenced by UDPIPNetwork::bind(), command(), IPNetwork(), laddr(), and UDPIPNetwork::recv(). |
|
|
Definition at line 133 of file net-ip.cc. Referenced by UDPIPNetwork::recv(), and setmloop(). |
|
|
Definition at line 76 of file net.h. Referenced by UDPIPNetwork::bind(), Network::command(), UDPIPNetwork::connect(), Network::mode(), Network::Network(), PcapFileNetwork::open(), PcapLiveNetwork::open(), open(), UDPIPNetwork::open(), recv(), and reset(). |
|
|
|
|
|
Definition at line 132 of file net-ip.cc. Referenced by noloopback_broken(), UDPIPNetwork::recv(), and setmloop(). |
|
|
Definition at line 130 of file net-ip.cc. Referenced by UDPIPNetwork::bind(), close(), open(), UDPIPNetwork::open(), rchannel(), recv(), UDPIPNetwork::recv(), and reset(). |
|
|
Definition at line 131 of file net-ip.cc. Referenced by close(), UDPIPNetwork::connect(), open(), UDPIPNetwork::open(), reset(), schannel(), and send(). |
1.3.3