This app shows how to handle netflow packets.

Signed-off-by: OHMURA Kei <[email protected]>
---
 ryu/app/netflow_dumper.py |   73 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 ryu/app/netflow_dumper.py

diff --git a/ryu/app/netflow_dumper.py b/ryu/app/netflow_dumper.py
new file mode 100644
index 0000000..eb6fcfc
--- /dev/null
+++ b/ryu/app/netflow_dumper.py
@@ -0,0 +1,73 @@
+# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+# implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+import gevent
+import logging
+import struct
+
+from gevent import socket
+from ryu.base import app_manager
+from ryu.lib.packet import netflowv5
+
+LOG = logging.getLogger('ryu.app.netflow_dumper')
+
+DEFAULT_HOST = '127.0.0.1'  # TODO:XXX
+DEFAULT_PORT = 9996  # TODO:XXX
+
+NETFLOW_V5 = 0x05
+
+
+class NetFlowDumper(app_manager.RyuApp):
+    def __init__(self, *args, **kwargs):
+        super(NetFlowDumper, self).__init__(*args, **kwargs)
+        self.sock = None
+        self.thread = None
+        self._start_recv()
+
+    def close(self):
+        # closing..
+        pass
+
+    def ipaddr_to_str(self, ip):
+        return socket.inet_ntoa(struct.pack('!I', ip))
+
+    def ipaddr_to_int(self, s):
+        return struct.unpack('!I', socket.inet_aton(s))[0]
+
+    def _recv_loop(self):
+        while True:
+            (data, addrport) = self.sock.recvfrom(8192)
+
+            (v,) = struct.unpack("!H", data[:2])
+            if v == NETFLOW_V5:
+                netflow = netflowv5.NetFlowV5.parser(data)
+                flows = netflow.flows
+                LOG.info('NetFlow V%d containing %d flows',
+                         netflow.version, netflow.count)
+
+                for i in range(netflow.count):
+                    LOG.info('Flow%d: %s -> %s ', i,
+                             self.ipaddr_to_str(flows[i].srcaddr),
+                             self.ipaddr_to_str(flows[i].dstaddr))
+                    LOG.info('%d packets and %d bytes in the flow',
+                             flows[i].dpkts, flows[i].doctets)
+            else:
+                LOG.info('NetFlow V%d is not suppported yet', v)
+
+    def _start_recv(self):
+        self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+        self.sock.bind((DEFAULT_HOST, DEFAULT_PORT))
+        self.thread = gevent.spawn_later(0, self._recv_loop)
-- 
1.7.9.5


------------------------------------------------------------------------------
Master Visual Studio, SharePoint, SQL, ASP.NET, C# 2012, HTML5, CSS,
MVC, Windows 8 Apps, JavaScript and much more. Keep your skills current
with LearnDevNow - 3,200 step-by-step video tutorials by Microsoft
MVPs and experts. ON SALE this month only -- learn more at:
http://p.sf.net/sfu/learnnow-d2d
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to