Esempio n. 1
0
def fix_parsed (m):
  """
  Translate parsed packet data to dicts and stuff
  """
  if m is None:
    return {"type":"raw","data":[]}
  if isinstance(m, basestring):
    return {"type":"raw","data":[ord(b) for b in m]}
  assert isinstance(m, packet_base)
  if not m.parsed:
    u = fix_parsed(m.raw)
    u['unparsed_type'] = m.__class__.__name__
    return u
  r = {}
  for k,v in fields_of(m, primitives_only = False).iteritems():
    if is_scalar(v):
      r[k] = v
    elif isinstance(v, (IPAddr, EthAddr)):
      r[k] = str(v)
  if hasattr(m, "payload"):
    r['payload'] = fix_parsed(m.payload)
  if 'raw' in r:
    #r['raw'] = [ord(b) for b in m['raw']]
    del r['raw']
  if 'next' in r: del r['next']
  r['type'] = m.__class__.__name__
  return r
Esempio n. 2
0
def fix_parsed(m):
    """
  Translate parsed packet data to dicts and stuff
  """
    if m is None:
        return {"type": "raw", "data": []}
    if isinstance(m, basestring):
        return {"type": "raw", "data": [ord(b) for b in m]}
    assert isinstance(m, packet_base)
    if not m.parsed:
        u = fix_parsed(m.raw)
        u['unparsed_type'] = m.__class__.__name__
        return u
    r = {}
    for k, v in fields_of(m, primitives_only=False).iteritems():
        if is_scalar(v):
            r[k] = v
        elif isinstance(v, (IPAddr, EthAddr)):
            r[k] = str(v)
    if hasattr(m, "payload"):
        r['payload'] = fix_parsed(m.payload)
    if 'raw' in r:
        #r['raw'] = [ord(b) for b in m['raw']]
        del r['raw']
    if 'next' in r: del r['next']
    r['type'] = m.__class__.__name__
    return r
Esempio n. 3
0
def action_to_dict (a):
  d = {}
  d['type'] = of.ofp_action_type_map.get(a.type, a.type)
  for k,v in fields_of(a).iteritems():
    if k in ['type','length']: continue
    if k == "port":
      v = of.ofp_port_map.get(v,v)
    d[k] = v
  return d
Esempio n. 4
0
def action_to_dict(a):
    d = {}
    d['type'] = of.ofp_action_type_map.get(a.type, a.type)
    for k, v in fields_of(a).iteritems():
        if k in ['type', 'length']: continue
        if k == "port":
            v = of.ofp_port_map.get(v, v)
        d[k] = v
    return d
Esempio n. 5
0
def flow_stats_to_list (flowstats):
  """
  Takes a list of flow stats
  """
  stats = []
  for stat in flowstats:
    s = {}
    stats.append(s)
    for k,v in fields_of(stat).iteritems():
      if k == 'length': continue
      if k.startswith('pad'): continue
      if k == 'match': v = match_to_dict(v)
      elif k == 'actions':
        v = [action_to_dict(a) for a in v]
      s[k] = v
  return stats
Esempio n. 6
0
def flow_stats_to_list(flowstats):
    """
  Takes a list of flow stats
  """
    stats = []
    for stat in flowstats:
        s = {}
        stats.append(s)
        for k, v in fields_of(stat).iteritems():
            if k == 'length': continue
            if k.startswith('pad'): continue
            if k == 'match': v = match_to_dict(v)
            elif k == 'actions':
                v = [action_to_dict(a) for a in v]
            s[k] = v
    return stats