Starkkz

socket_wrapper.lua

Jul 27th, 2014
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.44 KB | None | 0 0
  1. local ffi = require("ffi")
  2. local fd_lib
  3. local print = print
  4. local tostring = tostring
  5. local tonumber = tonumber
  6. local strsub = string.sub
  7.  
  8. local function isnil(object)
  9.     if object then
  10.         return strsub(tostring(object), -4) == "NULL"
  11.     end
  12.     return true
  13. end
  14.  
  15. module("socket_wrapper")
  16.  
  17. INVALID_SOCKET = -1
  18. SOL_SOCKET = 0xFFFF
  19. INADDR_ANY = 0
  20. INADDR_NONE = 0XFFFFFFFF
  21.  
  22. AF_INET = 2
  23. SOCK_STREAM = 1
  24. SOCK_DGRAM = 2
  25. SOCKET_ERROR = -1
  26.  
  27. SO_DEBUG = 1
  28. SO_ACCEPTCONN = 2
  29. SO_REUSEADDR = 4
  30. SO_KEEPALIVE = 8
  31. SO_DONTREROUTE = 0X10
  32. SO_BROADCAST = 0X20
  33. SO_USELOOPBACK = 0X40
  34. SO_LINGER = 0X80
  35. SO_OOBINLINE = 0X100
  36.  
  37. SO_SNDBUF = 0X1001
  38. SO_RCVBUF = 0X1002
  39. SO_SNDLOWAT = 0X1003
  40. SO_RCVLOWAT = 0X1004
  41. SO_SNDTIMEO = 0X1005
  42. SO_RCVTIMEO = 0X1006
  43. SO_ERROR = 0X1007
  44. SO_TYPE = 0X1008
  45.  
  46. SO_SYNCHRONOUS_ALERT = 0X10
  47. SO_SYNCHRONOUS_NONALERT = 0X20
  48.  
  49. TCP_NODELAY = 0X0001
  50. TCP_BSDURGENT = 0X7000
  51.  
  52. IPPROTO_UDP = 17
  53. IPPROTO_TCP = 6
  54.  
  55. SD_SEND = 1
  56. SD_RECEIVE = 0
  57. SD_BOTH = 2
  58.  
  59. if ffi.os == "Windows" then
  60.     FIONREAD = 0x4004667F
  61.  
  62.     SO_OPENTYPE = 0X7008
  63.     SO_MAXDG = 0X7009
  64.     SO_MAXPATHDG = 0X700A
  65.     SO_UPDATE_ACCEPT_CONTENXT = 0X700B
  66.     SO_CONNECT_TIME = 0X700C
  67.  
  68.     sock = ffi.load("ws2_32")
  69.     ffi.cdef [[
  70.         struct sockaddr {
  71.             unsigned short sa_family;
  72.             char sa_data[14];
  73.         };
  74.         struct in_addr {
  75.             uint32_t s_addr;
  76.         };
  77.         struct sockaddr_in {
  78.             short   sin_family;
  79.             unsigned short sin_port;
  80.             struct  in_addr sin_addr;
  81.             char    sin_zero[8];
  82.         };
  83.         typedef unsigned short WORD;
  84.         typedef struct WSAData {
  85.             WORD wVersion;
  86.             WORD wHighVersion;
  87.             char szDescription[257];
  88.             char szSystemStatus[129];
  89.             unsigned short iMaxSockets;
  90.             unsigned short iMaxUdpDg;
  91.             char *lpVendorInfo;
  92.         } WSADATA, *LPWSADATA;
  93.         typedef struct hostent {
  94.             char *h_name;
  95.             char **h_aliases;
  96.             short h_addrtype;
  97.             short h_length;
  98.             char **h_addr_list;
  99.         };
  100.         typedef struct timeval {
  101.             long tv_sec;
  102.             long tv_usec;
  103.         } timeval;
  104.         typedef uint16_t u_short;
  105.         typedef uint32_t u_int;
  106.         typedef unsigned long u_long;
  107.         typedef uintptr_t SOCKET;
  108.         typedef struct fd_set {u_int fd_count;SOCKET  fd_array[64];} fd_set;
  109.         u_long htonl(u_long hostlong);
  110.         u_short htons(u_short hostshort);
  111.         u_short ntohs(u_short netshort);
  112.         u_long ntohl(u_long netlong);
  113.         unsigned long inet_addr(const char *cp);
  114.         char *inet_ntoa(struct in_addr in);
  115.         SOCKET socket(int af, int type, int protocol);
  116.         SOCKET accept(SOCKET s,struct sockaddr *addr,int *addrlen);
  117.         int bind(SOCKET s, const struct sockaddr *name, int namelen);
  118.         int closesocket(SOCKET s);
  119.         int connect(SOCKET s, const struct sockaddr *name, int namelen);
  120.         int getsockname(SOCKET s, struct sockaddr *addr, int *namelen);
  121.         int ioctlsocket(SOCKET s, long cmd, u_long *argp);
  122.         int listen(SOCKET s, int backlog);
  123.         int recv(SOCKET s, char *buf, int len, int flags);
  124.         int recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
  125.         int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);
  126.         int send(SOCKET s, const char *buf, int len, int flags);
  127.         int sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen);
  128.         int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen);
  129.         int shutdown(SOCKET s, int how);
  130.         struct hostent *gethostbyname(const char *name);
  131.         struct hostent *gethostbyaddr(const char *addr, int len, int type);
  132.  
  133.         int __WSAFDIsSet(SOCKET fd, fd_set * set);
  134.         int WSAStartup(WORD wVersionRequested, LPWSADATA lpWSAData);
  135.         int WSACleanup(void);
  136.         int WSAGetLastError(void);
  137.  
  138.         int atexit(void (__cdecl * func)( void));
  139.     ]]
  140.  
  141.     WSA_INVALID_HANDLE = 6
  142.     WSA_NOT_ENOUGH_MEMORY = 8
  143.     WSA_INVALID_PARAMETER = 87
  144.     WSA_OPERATION_ABORTED = 995
  145.     WSA_IO_INCOMPLETE = 996
  146.     WSA_IO_PENDING = 997
  147.     WSAEINTR = 10004
  148.     WSAEBADF = 10009
  149.     WSAEACCES = 10013
  150.     WSAEFAULT = 10014
  151.     WSAEINVAL = 10022
  152.     WSAEMFILE = 10024
  153.     WSAEWOULDBLOCK = 10035
  154.     WSAEINPROGRESS = 10036
  155.     WSAEALREADY = 10037
  156.     WSAENOTSOCK = 10038
  157.     WSAEDESTADDRREQ = 10039
  158.     WSAEMSGSIZE = 10040
  159.     WSAEPROTOTYPE = 10041
  160.     WSAENOPROTOOPT = 10042
  161.     WSAEPROTONOSUPPORT = 10043
  162.     WSAESOCKTNOSUPPORT = 10044
  163.     WSAEOPNOTSUPP = 10045
  164.     WSAEPFNOSUPPORT = 10046
  165.     WSAEAFNOSUPPORT = 10047
  166.     WSAEADDRINUSE = 10048
  167.     WSAEADDRNOTAVAIL = 10049
  168.     WSAENETDOWN = 10050
  169.     WSAENETUNREACH = 10051
  170.     WSAENETRESET = 10052
  171.  
  172.     getError = sock.WSAGetLastError
  173.  
  174.     local ws = ffi.new("WSADATA")
  175.     sock.WSAStartup(0x101, ws)
  176.     ffi.C.atexit(sock.WSACleanup)
  177.  
  178.     fd_lib = {
  179.         FD_CLR = function (fd, set)
  180.             for i = 0, set.fd_count do
  181.                 if set.fd_array[i] == fd then
  182.                     while i < set.fd_count-1 do
  183.                         set.fd_array[i] = set.fd_array[i + 1]
  184.                         i = i + 1
  185.                     end
  186.                     set.fd_count = set.fd_count - 1
  187.                     break
  188.                 end
  189.             end
  190.         end,
  191.         FD_SET = function (fd, set)
  192.             local Index = 0
  193.             for i = 0, set.fd_count do
  194.                 if set.fd_array[i] == fd then
  195.                     Index = i
  196.                     break
  197.                 end
  198.             end
  199.  
  200.             if Index == set.fd_count then
  201.                 if set.fd_count < 64 then
  202.                     set.fd_array[Index] = fd
  203.                     set.fd_count = set.fd_count + 1
  204.                 end
  205.             end
  206.         end,
  207.         FD_ZERO = function (set)
  208.             set.fd_count = 0
  209.         end,
  210.         FD_ISSET = sock.__WSAFDIsSet,
  211.     }
  212.  
  213.     function ioctl_(s, cmd, argp)
  214.         return sock.ioctlsocket(s, cmd, argp)
  215.     end
  216.  
  217.     function inet_addr_(cp)
  218.         return sock.inet_addr(cp)
  219.     end
  220.  
  221.     function inet_ntoa_(_in)
  222.         return sock.inet_ntoa(_in)
  223.     end
  224. else
  225.     sock = ffi.load("sys/socket.h")
  226.     netdb = ffi.load("netdb.h")
  227.     ffi.cdef [[
  228.         struct sockaddr {
  229.             unsigned short sa_family;
  230.             char sa_data[14];
  231.         };
  232.         struct in_addr {
  233.             uint32_t s_addr;
  234.         };
  235.         struct sockaddr_in {
  236.             short   sin_family;
  237.             u_short sin_port;
  238.             struct  in_addr sin_addr;
  239.             char    sin_zero[8];
  240.         };
  241.         typedef struct hostent {
  242.             char *h_name;
  243.             char **h_aliases;
  244.             short h_addrtype;
  245.             short h_length;
  246.             char **h_addr_list;
  247.         };
  248.         typedef struct timeval {
  249.             long int tv_sec;
  250.             long int tv_usec;
  251.         };
  252.         typedef uint16_t u_short;
  253.         typedef uint32_t u_int;
  254.         typedef unsigned long u_long;
  255.         typedef uintptr_t SOCKET;
  256.         typedef struct fd_set {u_int fd_count;SOCKET  fd_array[64];} fd_set;
  257.         u_long htonl(u_long hostlong);
  258.         u_short htons(u_short hostshort);
  259.         u_short ntohs(u_short netshort);
  260.         u_long ntohl(u_long netlong);
  261.         unsigned long inet_addr(const char *cp);
  262.         char *inet_ntoa(struct in_addr in);
  263.         SOCKET socket(int af, int type, int protocol);
  264.         SOCKET accept(SOCKET s,struct sockaddr *addr,int *addrlen);
  265.         int bind(SOCKET s, const struct sockaddr *name, int namelen);
  266.         int close(SOCKET s);
  267.         int connect(SOCKET s, const struct sockaddr *name, int namelen);
  268.         int getsockname(SOCKET s, struct sockaddr *addr, int *namelen);
  269.         int ioctl(SOCKET s, long cmd, u_long *argp);
  270.         int listen(SOCKET s, int backlog);
  271.         int recv(SOCKET s, char *buf, int len, int flags);
  272.         int recvfrom(SOCKET s, char *buf, int len, int flags, struct sockaddr *from, int *fromlen);
  273.         int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);
  274.         int send(SOCKET s, const char *buf, int len, int flags);
  275.         int sendto(SOCKET s, const char *buf, int len, int flags, const struct sockaddr *to, int tolen);
  276.         int setsockopt(SOCKET s, int level, int optname, const char *optval, int optlen);
  277.         int shutdown(SOCKET s, int how);
  278.         struct hostent *gethostbyname(const char *name);
  279.         struct hostent *gethostbyaddr(const char *addr, int len, int type);
  280.     ]]
  281.  
  282.     fd_lib = ffi.load("sys/select.h")
  283.     ffi.cdef [[
  284.         void FD_CLR(int fd, fd_set *set);
  285.         void FD_SET(int fd, fd_set *set);
  286.         void FD_ZERO(fd_set *set);
  287.         int FD_ISSET(int fd, fd_set *set);
  288.     ]]
  289.  
  290.     function ioctl_(s, cmd, argp)
  291.         return sock.ioctl(s, cmd, argp)
  292.     end
  293.  
  294.     function inet_addr_(cp)
  295.         return sock.inet_addr(cp)
  296.     end
  297.  
  298.     function inet_ntoa_(_in)
  299.         return sock.inet_ntoa(_in)
  300.     end
  301.  
  302.     if ffi.os == "MacOS" then
  303.         FIONREAD = 0x4004667F
  304.     elseif ffi.os == "Linux" then
  305.         FIONREAD = 0x0000541B
  306.     end
  307. end
  308.  
  309. function htons_(n)
  310.     return sock.htons(n)
  311. end
  312.  
  313. function ntohs_(n)
  314.     return sock.ntohs(n)
  315. end
  316.  
  317. function htonl_(n)
  318.     return sock.htonl(n)
  319. end
  320.  
  321. function ntohl_(n)
  322.     return sock.ntohl(n)
  323. end
  324.  
  325. function socket_(addr_type, comm_type, protocol)
  326.     return sock.socket(addr_type, comm_type, protocol)
  327. end
  328.  
  329. if ffi.os == "Windows" then
  330.     function closesocket_(s)
  331.         return sock.closesocket(s)
  332.     end
  333. else
  334.     function closesocket_(s)
  335.         return sock.close(s)
  336.     end
  337. end
  338.  
  339. function bind_(socket, addr_type, port)
  340.     local sa = ffi.new("struct sockaddr_in")
  341.     if addr_type ~= AF_INET then
  342.         return -1
  343.     end
  344.  
  345.     ffi.fill(sa, 0, ffi.sizeof(sa))
  346.     sa.sin_family = addr_type
  347.     sa.sin_addr.s_addr = htonl_(INADDR_ANY)
  348.     sa.sin_port = htons_(port)
  349.  
  350.     local _sa = ffi.cast("struct sockaddr *", sa)
  351.     return sock.bind(socket, _sa, ffi.sizeof(sa))
  352. end
  353.  
  354. function gethostbyaddr_(addr, addr_len, addr_type)
  355.     local e = sock.gethostbyaddr(addr, addr_len, addr_type)
  356.     if not isnil(e) then
  357.         return e.h_name
  358.     end
  359. end
  360.  
  361. function gethostbyname_(name)
  362.     local e = sock.gethostbyname(name)
  363.     if not isnil(e) then
  364.         return e.h_addr_list, e.h_addrtype, e.h_length
  365.     end
  366. end
  367.  
  368. function connect_(socket, addr, addr_type, addr_len, port)
  369.     local sa = ffi.new("struct sockaddr_in")
  370.     if addr_type == AF_INET then
  371.         ffi.fill(sa, 0, ffi.sizeof(sa))
  372.         sa.sin_family = addr_type
  373.         sa.sin_port = htons_(port)
  374.         ffi.copy(sa.sin_addr, addr, addr_len)
  375.  
  376.         return sock.connect(socket, sa, ffi.sizeof(sa))
  377.     end
  378. end
  379.  
  380. function listen_(socket, backlog)
  381.     return sock.listen(socket, backlog)
  382. end
  383.  
  384. function accept_(socket, addr, addr_len)
  385.     return sock.accept(socket, addr, addr_len)
  386. end
  387.  
  388. function select_(n_read, r_socks, n_write, w_socks, n_except, e_socks, millis)
  389.     local r_set = ffi.new("fd_set")
  390.     local w_set = ffi.new("fd_set")
  391.     local e_set = ffi.new("fd_set")
  392.  
  393.     r_socks = r_socks or {}
  394.     w_socks = w_socks or {}
  395.     e_socks = e_socks or {}
  396.  
  397.     local n = -1
  398.  
  399.     fd_lib.FD_ZERO(r_set)
  400.     for i = 0, n_read do
  401.         if r_socks[i] then
  402.             fd_lib.FD_SET(r_socks[i], r_set)
  403.             if r_socks[i] > n then
  404.                 n = r_socks[i]
  405.             end
  406.         end
  407.     end
  408.  
  409.     fd_lib.FD_ZERO(w_set)
  410.     for i = 0, n_write do
  411.         if w_socks[i] then
  412.             fd_lib.FD_SET(w_socks[i], w_set)
  413.             if w_socks[i] > n then
  414.                 n = w_socks[i]
  415.             end
  416.         end
  417.     end
  418.  
  419.     fd_lib.FD_ZERO(e_set)
  420.     for i = 0, n_except do
  421.         if e_socks[i] then
  422.             fd_lib.FD_SET(e_socks[i], e_set)
  423.             if e_socks[i] > n then
  424.                 n = e_socks[i]
  425.             end
  426.         end
  427.     end
  428.  
  429.     local tvp
  430.     if millis < 0 then
  431.         tvp = ffi.new("struct timeval[0]")
  432.     else
  433.         tv = ffi.new("struct timeval")
  434.         tv.tv_sec = millis / 1000
  435.         tv.tv_usec = (millis % 1000) / 1000
  436.         tvp = ffi.new("struct timeval[1]")
  437.         tvp[0] = tv
  438.     end
  439.  
  440. --int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout);
  441.  
  442.     local r = sock.select(n + 1, r_set, w_set, e_set, tvp)
  443.     if r < 0 then
  444.         return r
  445.     end
  446.  
  447.     for i = 0, n_read do
  448.         if r_socks[i] and not fd_lib.FD_ISSET(r_socks[i], r_set) then
  449.             r_socks[i] = nil
  450.         end
  451.     end
  452.     for i = 0, n_write do
  453.         if w_socks[i] and not fd_lib.FD_ISSET(w_socks[i], w_set) then
  454.             w_rocks[i] = nil
  455.         end
  456.     end
  457.     for i = 0, n_except do
  458.         if e_socks[i] and not fd_lib.FD_ISSET(e_socks[i], e_set) then
  459.             e_socks[i] = nil
  460.         end
  461.     end
  462.     return r
  463. end
  464.  
  465. function send_(socket, buf, size, flags)
  466.     return sock.send(socket, buf, size, flags)
  467. end
  468.  
  469. function sendto_(socket, buf, size, flags, dest_ip, dest_port)
  470.     local sa = ffi.new("struct sockaddr_in")
  471.     ffi.fill(sa, 0, ffi.sizeof(sa))
  472.     sa.sin_family = AF_INET
  473.     sa.sin_addr.s_addr = htonl_(dest_ip)
  474.     sa.sin_port = htons_(dest_port)
  475.     return sock.sendto(socket, buf, size, flags, sa, ffi.sizeof(sa))
  476. end
  477.  
  478. function recv_(socket, buf, size, flags)
  479.     return sock.recv(socket, buf, size, flags)
  480. end
  481.  
  482. function recvfrom_(socket, buf, size, flags, ip, port)
  483.     local sa = ffi.new("struct sockaddr_in")
  484.     ffi.fill(sa, 0, ffi.sizeof(sa))
  485.  
  486.     local sasize = ffi.sizeof(sa)
  487.     local count = sock.recvfrom(socket, buf, size, flags, sa, sasize)
  488.     ip = ntohl_(sa.sin_addr.s_addr)
  489.     port = ntohs_(sa.sin_port)
  490.     return count
  491. end
  492.  
  493. function setsockopt_(socket, level, optname, optval, count)
  494.     return sock.setsockopt(socket, level, optname, optval, count)
  495. end
  496.  
  497. function getsockopt_(socket, level, optname, optval, count)
  498.     return sock.getsockopt(socket, level, optname, optval, count)
  499. end
  500.  
  501. function shutdown_(socket, how)
  502.     return sock.shutdown(socket, how)
  503. end
  504.  
  505. function getsockname_(socket, addr, len)
  506.     return sock.getsockname(socket, addr, len)
  507. end
  508.  
  509. function getpeername_(socket, addr, len)
  510.     return sock.getpeername(socket, addr, len)
  511. end
Advertisement
Add Comment
Please, Sign In to add comment