network.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. unit network;
  2. {$mode objfpc}
  3. {$J+}
  4. {$INLINE ON}
  5. {$MACRO ON}
  6. {$ASSERTIONS ON}
  7. interface
  8. uses
  9. ctypes, gctypes;
  10. const
  11. INVALID_SOCKET = ( not 0 );
  12. SOCKET_ERROR = ( - 1 );
  13. SOCK_STREAM = 1;
  14. SOCK_DGRAM = 2;
  15. SOCK_RAW = 3;
  16. SO_DEBUG = $0001; (* turn on debugging info recording *)
  17. SO_ACCEPTCONN = $0002; (* socket has had listen() *)
  18. SO_REUSEADDR = $0004; (* allow local address reuse *)
  19. SO_KEEPALIVE = $0008; (* keep connections alive *)
  20. SO_DONTROUTE = $0010; (* just use interface addresses *)
  21. SO_BROADCAST = $0020; (* permit sending of broadcast msgs *)
  22. SO_USELOOPBACK = $0040; (* bypass hardware when possible *)
  23. SO_LINGER = $0080; (* linger on close if data present *)
  24. SO_OOBINLINE = $0100; (* leave received OOB data in line *)
  25. SO_REUSEPORT = $0200; (* allow local address & port reuse *)
  26. SO_DONTLINGER = ( not SO_LINGER );
  27. SO_SNDBUF = $1001; (* send buffer size *)
  28. SO_RCVBUF = $1002; (* receive buffer size *)
  29. SO_SNDLOWAT = $1003; (* send low-water mark *)
  30. SO_RCVLOWAT = $1004; (* receive low-water mark *)
  31. SO_SNDTIMEO = $1005; (* send timeout *)
  32. SO_RCVTIMEO = $1006; (* receive timeout *)
  33. SO_ERROR = $1007; (* get error status and clear *)
  34. SO_TYPE = $1008; (* get socket type *)
  35. type
  36. linger = record
  37. l_onoff : cint; (* option on/off *)
  38. l_linger : cint; (* linger time *)
  39. end;
  40. const
  41. SOL_SOCKET = $ffff; (* options for socket level *)
  42. AF_UNSPEC = 0;
  43. AF_INET = 2;
  44. PF_INET = AF_INET;
  45. PF_UNSPEC = AF_UNSPEC;
  46. IPPROTO_IP = 0;
  47. IPPROTO_TCP = 6;
  48. IPPROTO_UDP = 17;
  49. INADDR_ANY = 0;
  50. INADDR_BROADCAST = $ffffffff;
  51. MSG_DONTWAIT = $40; (* Nonblocking i/o for this operation only *)
  52. IP_TOS = 1;
  53. IP_TTL = 2;
  54. IPTOS_TOS_MASK = $1E;
  55. function IPTOS_TOS(tos: longint): longint; inline;
  56. const
  57. IPTOS_LOWDELAY = $10;
  58. IPTOS_THROUGHPUT = $08;
  59. IPTOS_RELIABILITY = $04;
  60. IPTOS_LOWCOST = $02;
  61. IPTOS_MINCOST = IPTOS_LOWCOST;
  62. IPTOS_PREC_MASK = $e0;
  63. function IPTOS_PREC(tos: longint): longint; inline;
  64. const
  65. IPTOS_PREC_NETCONTROL = $e0;
  66. IPTOS_PREC_INTERNETCONTROL = $c0;
  67. IPTOS_PREC_CRITIC_ECP = $a0;
  68. IPTOS_PREC_FLASHOVERRIDE = $80;
  69. IPTOS_PREC_FLASH = $60;
  70. IPTOS_PREC_IMMEDIATE = $40;
  71. IPTOS_PREC_PRIORITY = $20;
  72. IPTOS_PREC_ROUTINE = $00;
  73. {$if not defined(FIONREAD) or not defined(FIONBIO)}
  74. const
  75. IOCPARM_MASK = $7f;
  76. IOC_VOID = $20000000;
  77. IOC_OUT = $40000000;
  78. IOC_IN = $80000000;
  79. IOC_INOUT = (IOC_IN or IOC_OUT);
  80. function _IO(x, y: cardinal): cardinal; inline;
  81. function _IOR(x, y, t: cardinal): cardinal; inline;
  82. function _IOW(x, y, t: cardinal): cardinal; inline;
  83. {$endif}
  84. {$ifndef FIONREAD}
  85. {$define FIONREAD := _IOR(ord('f'), 127, culong);}
  86. {$endif}
  87. {$ifndef FIONBIO}
  88. {$define FIONBIO := _IOW(ord('f'), 126, culong);}
  89. {$endif}
  90. {$ifndef SIOCSHIWAT}
  91. {$define SIOCSHIWAT := _IOW(ord('s'), 0, High(culong))}
  92. {$define SIOCGHIWAT := _IOR(ord('s'), 1, High(culong))}
  93. {$define SIOCSLOWAT := _IOW(ord('s'), 2, High(culong))}
  94. {$define SIOCGLOWAT := _IOR(ord('s'), 3, High(culong))}
  95. {$define SIOCATMARK := _IOR(ord('s'), 7, High(culong))}
  96. {$endif}
  97. {$ifndef O_NONBLOCK}
  98. {$define O_NONBLOCK := &04000}
  99. {$endif}
  100. {$ifndef FD_SET}
  101. {$undef FD_SETSIZE}
  102. {$define FD_SETSIZE := 16}
  103. type
  104. {$ifndef HAVE_IN_ADDR}
  105. {$define HAVE_IN_ADDR}
  106. in_addr = record
  107. s_addr: cuint32;
  108. end;
  109. pin_addr = ^in_addr;
  110. {$endif}
  111. _fd_set = record
  112. fd_bits: array [0..((FD_SETSIZE + 7) div 8) - 1] of cuint8;
  113. end;
  114. P_fd_set = ^_fd_set;
  115. procedure FD_SET(n: longint; var p: _fd_set); inline;
  116. procedure FD_CLR(n: longint; var p: _fd_set); inline;
  117. function FD_ISSET(n: longint; p: _fd_set): boolean; inline;
  118. procedure FD_ZERO(var p: _fd_set); inline;
  119. {$endif}
  120. {$ifndef TCP_NODELAY}
  121. {$define TCP_NODELAY := $01}
  122. {$endif}
  123. {$ifndef TCP_KEEPALIVE}
  124. {$define TCP_KEEPALIVE := $02}
  125. {$endif}
  126. {$ifndef socklen_t}
  127. {$define socklen_t := cuint32}
  128. {$define psocklen_t := pcuint32}
  129. {$endif}
  130. {$ifndef htons}
  131. {$define htons(x) := (x)}
  132. {$endif}
  133. {$ifndef ntohs}
  134. {$define ntohs(x) := (x)}
  135. {$endif}
  136. {$ifndef htonl}
  137. {$define htonl(x) := (x)}
  138. {$endif}
  139. {$ifndef ntohl}
  140. {$define ntohl(x) := (x)}
  141. {$endif}
  142. {$ifndef h_addr}
  143. {$define h_addr := h_addr_list[0]}
  144. {$endif}
  145. {$ifndef IP4_ADDR}
  146. procedure IP4_ADDR(var ipaddr: in_addr; a,b,c,d: cuint32); inline;
  147. function ip4_addr1(ipaddr: in_addr): cuint32; inline;
  148. function ip4_addr2(ipaddr: in_addr): cuint32; inline;
  149. function ip4_addr3(ipaddr: in_addr): cuint32; inline;
  150. function ip4_addr4(ipaddr: in_addr): cuint32; inline;
  151. {$endif}
  152. const
  153. POLLIN = $0001;
  154. POLLPRI = $0002;
  155. POLLOUT = $0004;
  156. POLLERR = $0008;
  157. POLLHUP = $0010;
  158. POLLNVAL = $0020;
  159. type
  160. sockaddr_in = record
  161. sin_len: cuint8;
  162. sin_family: cuint8;
  163. sin_port: cuint16;
  164. sin_addr: in_addr;
  165. sin_zero: array [0..7] of cint8;
  166. end;
  167. psockaddr_in = ^sockaddr_in;
  168. sockaddr = record
  169. sa_len: cuint8;
  170. sa_family: cuint8;
  171. sa_data: array [0..13] of cint8;
  172. end;
  173. psockaddr = ^sockaddr;
  174. hostent = record
  175. h_name: pcchar;
  176. h_aliases: ppcchar;
  177. h_addrtype: cuint16;
  178. h_length: cuint16;
  179. h_addr_list: ppcchar;
  180. end;
  181. phostent = ^hostent;
  182. pollsd = record
  183. socket: cint32;
  184. events: cuint32;
  185. revents: cuint32;
  186. end;
  187. ppollsd = ^pollsd;
  188. function inet_addr(const cp: pcchar): cuint32; cdecl; external;
  189. function inet_aton(const cp: pcchar; addr: pin_addr): cint8; cdecl; external;
  190. function inet_ntoa(addr: in_addr): pcchar; cdecl; external;
  191. function if_config(local_ip, netmask, gateway: pcchar; use_dhcp: cbool): cint32; cdecl; external;
  192. function if_configex(local_ip, netmask, gateway: pin_addr; use_dhcp: cbool): cint32; cdecl; external;
  193. function net_init(): cint32; cdecl; external;
  194. {$ifdef HW_RVL}
  195. type
  196. netcallback = function(result: cint32; usrdata: pointer): cint32; cdecl;
  197. function net_init_async(cb: netcallback; usrdata: pointer): cint32; cdecl; external;
  198. function net_get_status: cint32; cdecl; external;
  199. procedure net_wc24cleanup; cdecl; external;
  200. function net_get_mac_address(mac_buf: pointer): cint32; cdecl; external;
  201. {$endif}
  202. procedure net_deinit; cdecl; external;
  203. function net_gethostip: cuint32; cdecl; external;
  204. function net_socket(domain, type_, protocol: cuint32): cint32; cdecl; external;
  205. function net_bind(s: cint32; name_: Psockaddr; namelen: socklen_t): cint32; cdecl; external;
  206. function net_listen(s: cint32; backlog: cuint32): cint32; cdecl; external;
  207. function net_accept(s: cint32; addr: Psockaddr; addrlen: Psocklen_t): cint32; cdecl; external;
  208. function net_connect(s: cint32; par1: Psockaddr; par2: socklen_t): cint32; cdecl; external;
  209. function net_write(s: cint32; data: pointer; size: cint32): cint32; cdecl; external;
  210. function net_send(s: cint32; data: pointer; size: cint32; flags: cuint32): cint32; cdecl; external;
  211. function net_sendto(s: cint32; data: pointer; len: cint32; flags: cuint32; to_: Psockaddr; tolen: socklen_t): cint32; cdecl; external;
  212. function net_recv(s: cint32; mem: pointer; len: cint32; flags: cuint32): cint32; cdecl; external;
  213. function net_recvfrom(s: cint32; mem: pointer; len: cint32; flags: cuint32; from: Psockaddr; fromlen: Psocklen_t): cint32; cdecl; external;
  214. function net_read(s: cint32; mem: pointer; len: cint32): cint32; cdecl; external;
  215. function net_close(s: cint32): cint32; cdecl; external;
  216. function net_select(maxfdp1: cint32; readset, writeset, exceptset: P_fd_set; timeout: Ptimeval): cint32; cdecl; external;
  217. function net_setsockopt(s: cint32; level, optname: cuint32; optval: pointer; optlen: socklen_t): cint32; cdecl; external;
  218. function net_ioctl(s: cint32; cmd: cuint32; argp: pointer): cint32; cdecl; external;
  219. function net_fcntl(s: cint32; cmd, flags: cuint32): cint32; cdecl; external;
  220. function net_poll(sds: Ppollsd; nsds, timeout: cint32): cint32; cdecl; external;
  221. function net_shutdown(s: cint32; how: cuint32): cint32; cdecl; external;
  222. function net_gethostbyname(addrString: pcchar): Phostent; cdecl; external;
  223. implementation
  224. function IPTOS_TOS(tos: cint): cint; inline;
  225. begin
  226. IPTOS_TOS:=tos and IPTOS_TOS_MASK;
  227. end;
  228. function IPTOS_PREC(tos: cint): cint; inline;
  229. begin
  230. IPTOS_PREC:=tos and IPTOS_PREC_MASK;
  231. end;
  232. {$if defined(FIONREAD) or defined(FIONBIO)}
  233. function _IO(x, y: cardinal): cardinal; inline;
  234. begin
  235. Result := IOC_VOID or ((x shl 8) or y);
  236. end;
  237. function _IOR(x, y, t: cardinal): cardinal; inline;
  238. begin
  239. result := IOC_OUT or ((clong(sizeof(t)) and IOCPARM_MASK) shl 16) or (x shl 8) or y;
  240. end;
  241. function _IOW(x, y, t: cardinal): cardinal; inline;
  242. begin
  243. result := IOC_IN or ((clong(sizeof(t)) and IOCPARM_MASK) shl 16) or (x shl 8) or y;
  244. end;
  245. {$endif}
  246. {$ifndef FD_SET}
  247. procedure FD_SET(n: longint; var p: _fd_set); inline;
  248. begin
  249. p.fd_bits[n div 8] := p.fd_bits[n div 8] or (1 shl (n and 7));
  250. end;
  251. procedure FD_CLR(n: longint; var p: _fd_set); inline;
  252. begin
  253. p.fd_bits[n div 8] := p.fd_bits[n div 8] and not (1 shl (n and 7));
  254. end;
  255. function FD_ISSET(n: longint; p: _fd_set): boolean; inline;
  256. begin
  257. result := (p.fd_bits[n div 8] and (1 shl (n and 7))) <> 0;
  258. end;
  259. procedure FD_ZERO(var p: _fd_set); inline;
  260. begin
  261. memset(@p, 0, sizeof(p));
  262. end;
  263. {$endif}
  264. {$ifndef IP4_ADDR}
  265. procedure IP4_ADDR(var ipaddr: in_addr; a,b,c,d: cuint32); inline;
  266. begin
  267. ipaddr.s_addr := {htonl}((cuint32(a and $ff)<<24) or (cuint32(b and $ff) shl 16) or (cuint32(c and $ff) shl 8) or cuint32(d and $ff));
  268. end;
  269. function ip4_addr1(ipaddr: in_addr): cuint32; inline;
  270. begin
  271. result := cuint32({ntohl}(ipaddr.s_addr) shr 24) and $ff;
  272. end;
  273. function ip4_addr2(ipaddr: in_addr): cuint32; inline;
  274. begin
  275. result := cuint32({ntohl}(ipaddr.s_addr) shr 16) and $ff;
  276. end;
  277. function ip4_addr3(ipaddr: in_addr): cuint32; inline;
  278. begin
  279. result := cuint32({ntohl}(ipaddr.s_addr) shr 8) and $ff;
  280. end;
  281. function ip4_addr4(ipaddr: in_addr): cuint32; inline;
  282. begin
  283. result := cuint32({ntohl}(ipaddr.s_addr)) and $ff;
  284. end;
  285. {$endif}
  286. initialization
  287. end.