// Example: here's a quickie implementation of the messages in the
// socks5 protocol.

struct socks5_client_version {
   u8 version IN [5];
   u8 n_methods;
   u8 methods[n_methods];
}

struct socks5_server_method {
   u8 version IN [5];
   u8 method;
}

const CMD_CONNECT = 1;
const CMD_BIND = 2;
const CMD_UDP_ASSOCIATE = 3;
// This is a tor extension
const CMD_RESOLVE = 0xF0;
const CMD_RESOLVE_PTR = 0xF1;

const ATYPE_IPV4 = 1;
const ATYPE_IPV6 = 4;
const ATYPE_DOMAINNAME = 3;

struct domainname {
   u8 len;
   char name[len];
}

struct socks5_client_request {
   u8 version IN [5];
   u8 command IN [CMD_CONNECT, CMD_BIND, CMD_UDP_ASSOCIATE, CMD_RESOLVE, CMD_RESOLVE_PTR];
   u8 reserved IN [0];
   u8 atype;
   union dest_addr[atype] {
     ATYPE_IPV4: u32 ipv4;
     ATYPE_IPV6: u8 ipv6[16];
     ATYPE_DOMAINNAME: struct domainname domainname;
     default: fail;
   };
   u16 dest_port;
}

struct socks5_server_reply {
   u8 version IN [5];
   u8 reply;
   u8 reserved IN [0];
   u8 atype;
   union bind_addr[atype] {
     ATYPE_IPV4: u32 ipv4;
     ATYPE_IPV6: u8 ipv6[16];
     ATYPE_DOMAINNAME: struct domainname domainname;
     default: fail;
   };
   u16 bind_port;
}

struct socks5_client_userpass_auth {
   u8 version IN [1];
   u8 username_len;
   char username[username_len];
   u8 passwd_len;
   char passwd[passwd_len];
}

struct socks5_server_userpass_auth {
   u8 version IN [1];
   u8 status;
}

// Oh why not.  Here's socks4 and socks4a.

struct socks4_client_request {
   u8 version IN [4];
   u8 command IN [CMD_CONNECT,CMD_BIND,CMD_RESOLVE,CMD_RESOLVE_PTR];
   u16 port;
   u32 addr;
   nulterm username;
   union socks4a_addr[addr] {
      1..255:
               nulterm hostname;
      default:
               ;
   };
}

struct socks4_server_reply {
   u8 version IN [4];
   u8 status;
   u16 port;
   u32 addr;
}

