Changeset 5518

Show
Ignore:
Timestamp:
06/16/12 23:09:00 (7 years ago)
Author:
cmatthew
Message:

refactored some of the parser code to its own file.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/branches/nacl_repy/seattlelib/lind_parsers.repy

    r5510 r5518  
    77 
    88 
     9def inet_ntoa(ipaddress): 
     10  """ 
     11  Convert an IP address in integer form to a presentation string 
     12 
     13  This is  
     14   
     15  """ 
     16  a,b,c,d = struct_unpack("<B<B<B<B",ipaddress) 
     17  return str(a) + "." + str(b) + "." +str(c) + "." +str(d)  
     18 
     19 
     20def inet_aton(ipaddress): 
     21  """ 
     22  Convert an IP address in presentation format to its integer octet format 
     23   
     24  """ 
     25  return struct_unpack("<I",struct_pack("<B<B<B<B",*map(int, ipaddress.split("."))))[0] 
     26 
     27 
     28struct_sockaddr_format = '<h>H<I<Q'  # family, port, address, padding 
     29 
    930def parse_sockaddr_struct(sock): 
    1031    """parse a struct sockaddr, and pull out family, port, ip and padding""" 
    11     format = '<h>H<I<Q'  # family, port, address, padding 
    12     assert struct_calcsize(format) == 16 
    13     return struct_unpack(format, sock) 
     32    fmt = struct_sockaddr_format 
     33    assert struct_calcsize(fmt) == 16 
     34    return struct_unpack(fmt, sock) 
    1435 
    1536 
    1637def pack_struct_sockaddr(family, ip, port): 
    17     pass 
     38    packed_ip = inet_aton(ip) 
     39    print type(port) 
     40    return struct_pack(struct_sockaddr_format, family, port, packed_ip, 0)  
    1841 
     42 
     43# struct pollfd { 
     44#     int   fd;         /* file descriptor */ 
     45#     short events;     /* requested events */ 
     46#     short revents;    /* returned events */ 
     47#     }; 
     48 
     49single_struct_pollfd_fmt = "<I<h<h" 
     50 
     51def parse_struct_pollfds(pollfds_str, nfds): 
     52    struct_siz = struct_calcsize(single_struct_pollfd_fmt) 
     53    mynfds = len(pollfds_str) / struct_siz 
     54    assert mynfds == nfds, "Struct pollfd size missmatch." 
     55 
     56    def split(input, size): 
     57      return [input[start:start+size] for start in range(0, len(input), size)] 
     58 
     59    structs = split(pollfds_str, struct_siz) 
     60     
     61    result = map(lambda x: struct_unpack(single_struct_pollfd_fmt, x), structs) 
     62    result = map(lambda x: {'fd':x[0], 'events':x[1], 'revents':x[2]}, result) 
     63    print result 
     64    return result 
    1965 
    2066def pack_stat_struct(struct_tuple):