Global replace of Nicira Networks.
[sliver-openvswitch.git] / lib / packets.c
index eaf3e43..84ca590 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
 #include <stdlib.h>
 #include "byte-order.h"
 #include "csum.h"
+#include "flow.h"
 #include "dynamic-string.h"
 #include "ofpbuf.h"
 
@@ -478,3 +479,57 @@ packet_set_udp_port(struct ofpbuf *packet, ovs_be16 src, ovs_be16 dst)
         uh->udp_dst = dst;
     }
 }
+
+/* If 'packet' is a TCP packet, returns the TCP flags.  Otherwise, returns 0.
+ *
+ * 'flow' must be the flow corresponding to 'packet' and 'packet''s header
+ * pointers must be properly initialized (e.g. with flow_extract()). */
+uint8_t
+packet_get_tcp_flags(const struct ofpbuf *packet, const struct flow *flow)
+{
+    if ((flow->dl_type == htons(ETH_TYPE_IP) ||
+         flow->dl_type == htons(ETH_TYPE_IPV6)) &&
+        flow->nw_proto == IPPROTO_TCP && packet->l7) {
+        const struct tcp_header *tcp = packet->l4;
+        return TCP_FLAGS(tcp->tcp_ctl);
+    } else {
+        return 0;
+    }
+}
+
+/* Appends a string representation of the TCP flags value 'tcp_flags'
+ * (e.g. obtained via packet_get_tcp_flags() or TCP_FLAGS) to 's', in the
+ * format used by tcpdump. */
+void
+packet_format_tcp_flags(struct ds *s, uint8_t tcp_flags)
+{
+    if (!tcp_flags) {
+        ds_put_cstr(s, "none");
+        return;
+    }
+
+    if (tcp_flags & TCP_SYN) {
+        ds_put_char(s, 'S');
+    }
+    if (tcp_flags & TCP_FIN) {
+        ds_put_char(s, 'F');
+    }
+    if (tcp_flags & TCP_PSH) {
+        ds_put_char(s, 'P');
+    }
+    if (tcp_flags & TCP_RST) {
+        ds_put_char(s, 'R');
+    }
+    if (tcp_flags & TCP_URG) {
+        ds_put_char(s, 'U');
+    }
+    if (tcp_flags & TCP_ACK) {
+        ds_put_char(s, '.');
+    }
+    if (tcp_flags & 0x40) {
+        ds_put_cstr(s, "[40]");
+    }
+    if (tcp_flags & 0x80) {
+        ds_put_cstr(s, "[80]");
+    }
+}