- 
                Notifications
    
You must be signed in to change notification settings  - Fork 14
 
Network support for WASI preview 2 #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            dsseng
  wants to merge
  9
  commits into
  tinygo-org:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
dsseng:wasip2
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +556
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      37358cf
              
                Add _AF_INET6 for WASI sockets
              
              
                dsseng 5b385fa
              
                Initial WASIp2 network driver
              
              
                dsseng 293efab
              
                net: wasip2: implement GetHostByName
              
              
                dsseng 3b08fdf
              
                net: wasip2: refactor, support TCP connect
              
              
                dsseng 4c562f1
              
                net: wasip2: UDP client
              
              
                dsseng 6966225
              
                logging fixup
              
              
                dsseng 61b4272
              
                fix large TCP sends
              
              
                dsseng c175c49
              
                review comments
              
              
                dsseng 532e0bc
              
                Bind to 0.0.0.0 if no address specified (:80)
              
              
                dsseng File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| //go:build wasip2 | ||
| 
     | 
||
| // L3/L4 network/transport layer implementation using WASI preview 2 sockets | ||
| 
     | 
||
| package net | ||
| 
     | 
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/netip" | ||
| "time" | ||
| 
     | 
||
| instancenetwork "internal/wasi/sockets/v0.2.0/instance-network" | ||
| ipnamelookup "internal/wasi/sockets/v0.2.0/ip-name-lookup" | ||
| "internal/wasi/sockets/v0.2.0/network" | ||
| ) | ||
| 
     | 
||
| var errInvalidSocketFD = errors.New("wasip2: invalid socket fd") | ||
| 
     | 
||
| func tinygoToWasiAddr(ip netip.AddrPort) network.IPSocketAddress { | ||
| if ip.Addr().Is4() { | ||
| return network.IPSocketAddressIPv4(network.IPv4SocketAddress{ | ||
| Port: ip.Port(), | ||
| Address: ip.Addr().As4(), | ||
| }) | ||
| } | ||
| 
     | 
||
| if ip.Addr().Is6() { | ||
| as16 := ip.Addr().As16() | ||
| var as8uint16 [8]uint16 | ||
| for i := 0; i < 8; i++ { | ||
| as8uint16[i] = uint16(as16[i*2])<<8 | uint16(as16[i*2+1]) | ||
| } | ||
| return network.IPSocketAddressIPv6(network.IPv6SocketAddress{ | ||
| Port: ip.Port(), | ||
| Address: as8uint16, | ||
| }) | ||
| } | ||
| 
     | 
||
| return network.IPSocketAddressIPv4(network.IPv4SocketAddress{ | ||
| Port: ip.Port(), | ||
| }) | ||
| } | ||
| 
     | 
||
| func wasiAddrToTinygo(addr network.IPSocketAddress) netip.AddrPort { | ||
| if addr4 := addr.IPv4(); addr4 != nil { | ||
| return netip.AddrPortFrom(netip.AddrFrom4(addr4.Address), addr4.Port) | ||
| } | ||
| 
     | 
||
| if addr6 := addr.IPv6(); addr6 != nil { | ||
| var as16 [16]byte | ||
| for i := 0; i < 8; i++ { | ||
| as16[i*2] = byte(addr6.Address[i] >> 8) | ||
| as16[i*2+1] = byte(addr6.Address[i] & 0xFF) | ||
| } | ||
| return netip.AddrPortFrom(netip.AddrFrom16(as16), addr6.Port) | ||
| } | ||
| 
     | 
||
| return netip.AddrPort{} | ||
| } | ||
| 
     | 
||
| type wasip2Socket interface { | ||
| Recv(buf []byte, flags int, deadline time.Time) (int, error) | ||
| Send(buf []byte, flags int, deadline time.Time) (int, error) | ||
| Close() error | ||
| Listen(backlog int) error | ||
| Bind(globalNetwork instancenetwork.Network, ip network.IPSocketAddress) error | ||
| Connect(globalNetwork instancenetwork.Network, host string, ip network.IPSocketAddress) error | ||
| Accept() (wasip2Socket, *network.IPSocketAddress, error) | ||
| } | ||
| 
     | 
||
| // wasip2Netdev is a netdev that uses WASI preview 2 sockets | ||
| type wasip2Netdev struct { | ||
| fds map[int]wasip2Socket | ||
| nextFd int | ||
| net instancenetwork.Network | ||
| } | ||
| 
     | 
||
| func init() { | ||
| useNetdev(&wasip2Netdev{ | ||
| fds: make(map[int]wasip2Socket), | ||
| nextFd: 0, | ||
| net: instancenetwork.InstanceNetwork(), | ||
| }) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) GetHostByName(name string) (netip.Addr, error) { | ||
| res := ipnamelookup.ResolveAddresses(n.net, name) | ||
| 
     | 
||
| if res.IsErr() { | ||
| return netip.Addr{}, fmt.Errorf("failed to resolve address: %s", res.Err().String()) | ||
| } | ||
| 
     | 
||
| stream := res.OK() | ||
| pollable := stream.Subscribe() | ||
| 
     | 
||
| for { | ||
| pollable.Block() | ||
| res := stream.ResolveNextAddress() | ||
| 
     | 
||
| if res.IsErr() { | ||
| return netip.Addr{}, fmt.Errorf("failed to get resolved address: %s", res.Err().String()) | ||
| } | ||
| 
     | 
||
| if res.OK().None() { | ||
| return netip.Addr{}, errors.New("no addresses found") | ||
| } | ||
| 
     | 
||
| // TODO: handle IPv6 | ||
| if addr4 := res.OK().Some().IPv4(); addr4 != nil { | ||
| return netip.AddrFrom4(*addr4), nil | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Addr() (netip.Addr, error) { | ||
| return netip.Addr{}, errors.New("wasip2 TODO Addr") | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) getNextFD() int { | ||
| n.nextFd++ | ||
| return n.nextFd - 1 | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Socket(domain int, stype int, protocol int) (sockfd int, _ error) { | ||
| af := network.IPAddressFamilyIPv4 | ||
| if domain == _AF_INET6 { | ||
| af = network.IPAddressFamilyIPv6 | ||
| } | ||
| 
     | 
||
| var sock wasip2Socket | ||
| var err error | ||
| 
     | 
||
| switch stype { | ||
| case _SOCK_STREAM: | ||
| sock, err = createTCPSocket(af) | ||
| if err != nil { | ||
| return -1, err | ||
| } | ||
| case _SOCK_DGRAM: | ||
| sock, err = createUDPSocket(af) | ||
| if err != nil { | ||
| return -1, err | ||
| } | ||
| default: | ||
| return -1, fmt.Errorf("wasip2: unsupported socket type %d", stype) | ||
| } | ||
| 
     | 
||
| fd := n.getNextFD() | ||
| n.fds[fd] = sock | ||
| 
     | 
||
| return fd, nil | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Bind(sockfd int, ip netip.AddrPort) error { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| return sock.Bind(n.net, tinygoToWasiAddr(ip)) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Connect(sockfd int, host string, ip netip.AddrPort) error { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| return sock.Connect(n.net, host, tinygoToWasiAddr(ip)) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Listen(sockfd int, backlog int) error { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| return sock.Listen(backlog) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Accept(sockfd int) (int, netip.AddrPort, error) { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return -1, netip.AddrPort{}, errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| newSock, raddr, err := sock.Accept() | ||
| if err != nil { | ||
| return -1, netip.AddrPort{}, fmt.Errorf("failed to accept connection: %s", err.Error()) | ||
| } | ||
| 
     | 
||
| fd := n.getNextFD() | ||
| n.fds[fd] = newSock | ||
| 
     | 
||
| return fd, wasiAddrToTinygo(*raddr), nil | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Send(sockfd int, buf []byte, flags int, deadline time.Time) (int, error) { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return -1, errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| return sock.Send(buf, flags, deadline) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Recv(sockfd int, buf []byte, flags int, deadline time.Time) (int, error) { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return -1, errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| return sock.Recv(buf, flags, deadline) | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) Close(sockfd int) error { | ||
| sock, ok := n.fds[sockfd] | ||
| if !ok { | ||
| return errInvalidSocketFD | ||
| } | ||
| 
     | 
||
| delete(n.fds, sockfd) | ||
| 
     | 
||
| return sock.Close() | ||
| } | ||
| 
     | 
||
| func (n *wasip2Netdev) SetSockOpt(sockfd int, level int, opt int, value interface{}) error { | ||
| return errors.New("wasip2 TODO set socket option") | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't end up really used, nor did I test IPv6. Should we drop it for now?