Changeset 5516

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

Added UDP send support

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • seattle/branches/nacl_repy/seattlelib/lind_net_calls.py

    r5513 r5516  
    261261      # BUG: I need to avoid leaking sockets, so I should close the previous... 
    262262      raise UnimplementedError("I should close the previous UDP listener when re-binding") 
     263    if localip == '0.0.0.0': 
     264      localip = getmyip() 
    263265    udpsockobj = listenformessage(localip,localport) 
    264266    filedescriptortable[fd]['socketobjectid'] = _insert_into_socketobjecttable(udpsockobj) 
     
    381383  # end is connected... 
    382384  if remoteip == '' and remoteport == 0: 
    383     return send_syscall(fd, message, flags) 
     385    print "warning: sending back to send." 
     386    return send_syscall(fd,message) 
    384387 
    385388  if filedescriptortable[fd]['state'] == CONNECTED or filedescriptortable[fd]['state'] == LISTEN: 
     
    397400    if 'localip' not in filedescriptortable[fd]: 
    398401      localip = getmyip() 
    399       localport = _get_available_tcp_port() 
     402      localport = int(_get_available_tcp_port()) 
    400403    else: 
    401404      localip = filedescriptortable[fd]['localip'] 
    402       localport = filedescriptortable[fd]['localport'] 
     405      localport = int(filedescriptortable[fd]['localport']) 
    403406 
    404407    try: 
    405408      # BUG: The timeout it configurable, right? 
    406409      bytessent = sendmessage(remoteip, remoteport, message, localip, localport) 
    407  
    408410    except AddressBindingError, e: 
    409411      raise SyscallError('connect_syscall','ENETUNREACH','Network was unreachable because of inability to access local port / IP') 
     
    448450 
    449451  # includes NOTCONNECTED and LISTEN 
    450   if filedescriptortable[fd]['state'] != CONNECTED: 
     452  if  filedescriptortable[fd]['protocol'] == IPPROTO_TCP and filedescriptortable[fd]['state'] != CONNECTED: 
    451453    raise SyscallError("send_syscall","ENOTCONN","The descriptor is not connected.") 
    452454 
    453455 
    454   if filedescriptortable[fd]['protocol'] != IPPROTO_TCP: 
     456  if filedescriptortable[fd]['protocol'] != IPPROTO_TCP and filedescriptortable[fd]['protocol'] != IPPROTO_UDP: 
    455457    raise SyscallError("send_syscall","EOPNOTSUPP","send not supported on this protocol.") 
    456458     
     
    477479      # return the characters sent! 
    478480      return bytessent 
    479  
     481  elif filedescriptortable[fd]['protocol'] == IPPROTO_UDP: 
     482 
     483    remoteip = filedescriptortable[fd]['remoteip'] 
     484    remoteport = filedescriptortable[fd]['remoteport'] 
     485 
     486    bytessent = sendto_syscall(fd, message, remoteip, remoteport, flags) 
     487    return bytessent 
    480488  else: 
    481489    raise UnimplementedError("Unknown protocol in send()") 
     
    521529    # includes NOTCONNECTED and LISTEN 
    522530    if filedescriptortable[fd]['state'] != CONNECTED: 
    523       raise SyscallError("recvfrom_syscall","ENOTCONN","The descriptor is not connected.") 
     531      raise SyscallError("recvfrom_syscall","ENOTCONN","The descriptor is not connected."+str(filedescriptortable[fd]['state'])) 
    524532    # I'm ready to recv, get the socket object... 
    525533    sockobj = socketobjecttable[filedescriptortable[fd]['socketobjectid']] 
     
    573581  # If UDP, recieve a message and return... 
    574582  elif filedescriptortable[fd]['protocol'] == IPPROTO_UDP: 
    575  
    576583    # BUG / HELP!!!: Calling this with UDP and without binding does something I 
    577584    # don't really understand...   It seems to block but I don't know what is  
     
    12161223 
    12171224 
    1218 # int socketpair(int domain, int type, int protocol, int socket_vector[2]); 
    1219 # ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags); 
    1220 # ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags); 
    1221  
    1222  
    1223 def inet_ntoa(ipaddress): 
    1224   """ 
    1225   Convert an IP address in integer form to a dot-and-number string 
    1226  
    1227   This is  
    1228    
    1229   """ 
    1230   a,b,c,d = struct_unpack("<B<B<B<B",ipaddress) 
    1231   return str(a) + "." + str(b) + "." +str(c) + "." +str(d)  
    1232  
    1233  
    1234 def inet_aton(ipaddress): 
    1235   """ 
    1236   Convert an IP address in string format to its integer octet format 
    1237    
    1238   """ 
    1239   return struct_unpack("<I",struct_pack("<B<B<B<B",*map(int, ipaddress.split("."))))[0] 
     1225def poll_syscall(fds, timeout): 
     1226  """  
     1227    http://linux.die.net/man/2/poll 
     1228 
     1229    returns a list of io status indicators for each of the 
     1230    file handles in fds 
     1231 
     1232  """ 
     1233 
     1234  return_code = 0 
     1235 
     1236  reply = [] 
     1237 
     1238  for structpoll in fds: 
     1239    fd = structpoll['fd'] 
     1240    events = structpoll['events'] 
     1241    read = events & POLLIN > 0  
     1242    write = events & POLLOUT > 0  
     1243    err = events & POLLERR > 0 
     1244    reads = [] 
     1245    writes = [] 
     1246    errors = [] 
     1247    if read: 
     1248      reads.append(fd) 
     1249    if write: 
     1250      writes.append(fd) 
     1251    if err: 
     1252      errors.append(fd) 
     1253    #print reads, writes, errors 
     1254 
     1255    newfd = select_syscall(fd, reads, writes, errors, 0) 
     1256    #print newfd 
     1257     
     1258  return return_code 
     1259 
     1260