Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > 2b12d0199a2bbc4a02ef3834d926ac4a > files > 162

systemtap-client-1.8-6.el5.x86_64.rpm

#!/usr/bin/stap

global drop_count

probe begin {

  // Make sure the protocol is either UDP or TCP
  // and the number of packets is positive.
  if ((@1 != "TCP" && @1 != "UDP" && @1 != "ALL") || ($2 <0))
  {
    printf("Please enter \"TCP\", \"UDP\" or \"ALL\" on the command line, followed by the number of packets to drop.\n")
    exit()
  }
  else
    printf("Dropping packets! Ctrl-C to exit.\n")
}

probe netfilter.ipv4.local_in {

  // If the protocol matches that specified (or ALL),
  // make sure we have not exceeded the number
  // provided, then drop the packet.
  if(convert_protocol(protocol) == @1 || @1 == "ALL") {
    if(@count(drop_count[@1]) >= $2 && $2 != 0)
      exit()
    else {
      $verdict = nf_drop
      drop_count[@1]<<<length
    }
  }
}

probe end {

  foreach (proto in drop_count)
    printf("%d %s packets dropped, %d total bytes\n",
        @count(drop_count[proto]), proto, @sum(drop_count[proto]))
}

// Convert numerical protocol to string equivalent
function convert_protocol (proto_n){

  proto_s = "Other"
  if(proto_n == 6)
    proto_s = "TCP"
  else if (proto_n == 17)
    proto_s = "UDP"

  return proto_s
}