PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/socket.fs

https://github.com/darrylhein/gforth_webserver
F# | 403 lines | 353 code | 50 blank | 0 comment | 1 complexity | 8a928ef24900c8769797000028e4a0b1 MD5 | raw file
Possible License(s): GPL-3.0
  1. \ socket interface
  2. \ Copyright (C) 1998,2000,2003,2005,2006,2007,2008,2009,2010,2011,2012 Free Software Foundation, Inc.
  3. \ This file is part of Gforth.
  4. \ Gforth is free software; you can redistribute it and/or
  5. \ modify it under the terms of the GNU General Public License
  6. \ as published by the Free Software Foundation, either version 3
  7. \ of the License, or (at your option) any later version.
  8. \ This program is distributed in the hope that it will be useful,
  9. \ but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. \ GNU General Public License for more details.
  12. \ You should have received a copy of the GNU General Public License
  13. \ along with this program. If not, see http://www.gnu.org/licenses/.
  14. c-library socket
  15. \c #include <netdb.h>
  16. \c #include <unistd.h>
  17. c-function gethostname gethostname a n -- n ( c-addr u -- ior )
  18. \c #include <errno.h>
  19. \c #define get_errno() errno
  20. c-function errno get_errno -- n ( -- value )
  21. \c #include <sys/types.h>
  22. \c #include <sys/socket.h>
  23. c-function socket socket n n n -- n ( class type proto -- fd )
  24. c-function closesocket close n -- n ( fd -- ior )
  25. c-function connect connect n a n -- n ( fd sock size -- err )
  26. c-function send send n a n n -- n ( socket buffer count flags -- size )
  27. c-function recv recv n a n n -- n ( socket buffer count flags -- size )
  28. c-function recvfrom recvfrom n a n n a a -- n ( socket buffer count flags srcaddr addrlen -- size )
  29. c-function sendto sendto n a n n a n -- n ( socket buffer count flags srcaddr addrlen -- size )
  30. c-function listen() listen n n -- n ( socket backlog -- err )
  31. c-function bind bind n a n -- n ( socket sockaddr socklen --- err )
  32. c-function accept() accept n a a -- n ( socket sockaddr addrlen -- fd )
  33. \c #include <stdio.h>
  34. c-function fdopen fdopen n a -- a ( fd fileattr -- file )
  35. \c #include <fcntl.h>
  36. c-function fcntl fcntl n n n -- n ( fd n1 n2 -- ior )
  37. \c #include <arpa/inet.h>
  38. c-function htonl htonl n -- n ( x -- x' )
  39. c-function htons htons n -- n ( x -- x' )
  40. c-function ntohl ntohl n -- n ( x -- x' )
  41. \c #define fileno1(file) fileno((FILE*)(file))
  42. c-function fileno fileno1 a -- n ( file* -- fd )
  43. \c #include <poll.h>
  44. c-function poll poll a n n -- n ( fds nfds timeout -- r )
  45. e? os-type s" linux-gnu" str= [IF]
  46. c-function ppoll ppoll a n a a -- n ( fds nfds timeout_ts sigmask -- r )
  47. \c #include <sys/epoll.h>
  48. c-function epoll_create epoll_create n -- n ( n -- epfd )
  49. c-function epoll_ctl epoll_ctl n n n a -- n ( epfd op fd event -- r )
  50. c-function epoll_wait epoll_wait n a n n -- n ( epfd events maxevs timeout -- r )
  51. s" uname -r" r/o open-pipe throw
  52. dup >r slurp-fid r> close-pipe throw drop
  53. 2dup s" 3." string-prefix? >r drop free throw r> [IF]
  54. c-function recvmmsg recvmmsg n a n n a -- n ( sockfd hdr vlen flag timeout -- r )
  55. c-function sendmmsg sendmmsg n a n n -- n ( sockfd hdr vlen flag -- r )
  56. [THEN]
  57. [THEN]
  58. \c #include <netdb.h>
  59. c-function getaddrinfo getaddrinfo a a a a -- n ( node service hints res -- r )
  60. c-function freeaddrinfo freeaddrinfo a -- void ( res -- )
  61. c-function gai_strerror gai_strerror n -- a ( errcode -- addr )
  62. c-function setsockopt setsockopt n n n a n -- n ( sockfd level optname optval optlen -- r )
  63. end-c-library
  64. e? os-type s" darwin" string-prefix? [IF] : darwin ; [THEN]
  65. 4 4 2Constant int%
  66. 2 2 2Constant short%
  67. int% 2Constant size_t%
  68. struct
  69. cell% field h_name
  70. cell% field h_aliases
  71. int% field h_addrtype
  72. int% field h_length
  73. cell% field h_addr_list
  74. end-struct hostent
  75. struct
  76. short% field family
  77. short% field port
  78. int% field sin_addr
  79. int% 2* field padding
  80. end-struct sockaddr_in4
  81. struct
  82. short% field sin6_family
  83. short% field sin6_port
  84. int% field sin6_flowinfo
  85. int% 4 * field sin6_addr
  86. int% field sin6_scope_id
  87. end-struct sockaddr_in6
  88. sockaddr_in4 %alignment sockaddr_in6 %alignment max
  89. sockaddr_in4 %size sockaddr_in6 %size max 2Constant sockaddr_in
  90. struct
  91. int% field fd
  92. short% field events
  93. short% field revents
  94. end-struct pollfd
  95. struct
  96. int% field ai_flags
  97. int% field ai_family
  98. int% field ai_socktype
  99. int% field ai_protocol
  100. size_t% field ai_addrlen
  101. [IFDEF] darwin
  102. cell% field ai_canonname
  103. cell% field ai_addr
  104. [ELSE]
  105. cell% field ai_addr
  106. cell% field ai_canonname
  107. [THEN]
  108. cell% field ai_next
  109. end-struct addrinfo
  110. e? os-type s" linux" string-prefix? [IF]
  111. struct
  112. cell% field iov_base
  113. cell% field iov_len
  114. end-struct iovec
  115. struct
  116. cell% field msg_name
  117. cell% field msg_namelen
  118. cell% field msg_iov \ iovec structures
  119. cell% field msg_iovlen
  120. cell% field msg_control
  121. cell% field msg_controllen
  122. cell% field msg_flags
  123. cell% field msg_len
  124. end-struct mmsghdr
  125. [THEN]
  126. ' family alias family+port \ 0.6.2 32-bit field; used by itools
  127. Create sockaddr-tmp
  128. sockaddr-tmp sockaddr_in %size dup allot erase
  129. Create hints
  130. hints addrinfo %size dup allot erase
  131. Variable addrres
  132. Variable sockopt-on
  133. : c-string ( addr u -- addr' )
  134. tuck pad swap move pad + 0 swap c! pad ;
  135. 0 Constant PF_UNSPEC
  136. 2 Constant PF_INET
  137. [IFDEF] darwin
  138. 30 Constant PF_INET6
  139. $0210 Constant AF_INET
  140. $1E1C Constant AF_INET6
  141. 27 Constant IPV6_V6ONLY
  142. 35 Constant EWOULDBLOCK
  143. $40 Constant MSG_WAITALL
  144. $006 Constant O_NONBLOCK|O_RDWR
  145. $1006 Constant SO_RCVTIMEO
  146. 4 Constant SO_REUSEADDR
  147. $FFFF Constant SOL_SOCKET
  148. [ELSE]
  149. 10 Constant PF_INET6
  150. 2 Constant AF_INET
  151. 10 Constant AF_INET6
  152. 26 Constant IPV6_V6ONLY
  153. 11 Constant EWOULDBLOCK
  154. $100 Constant MSG_WAITALL
  155. $10000 Constant MSG_WAITFORONE
  156. $802 Constant O_NONBLOCK|O_RDWR
  157. 20 Constant SO_RCVTIMEO
  158. 2 Constant SO_REUSEADDR
  159. 1 Constant SOL_SOCKET
  160. [THEN]
  161. 1 Constant SOCK_STREAM
  162. 2 Constant SOCK_DGRAM
  163. 0 Constant IPPROTO_IP
  164. 41 Constant IPPROTO_IPV6
  165. 10 Constant IP_MTU_DISCOVER
  166. 23 Constant IPV6_MTU_DISCOVER
  167. 67 Constant IP_DONTFRAG
  168. 2 Constant IP_PMTUDISC_DO
  169. 4 Constant F_SETFL
  170. $001 Constant POLLIN
  171. $002 Constant POLLPRI
  172. $004 Constant POLLOUT
  173. 2variable socket-timeout-d 2000. socket-timeout-d 2!
  174. : new-socket ( -- socket )
  175. PF_INET SOCK_STREAM 0 socket
  176. dup 0<= abort" no free socket" ;
  177. : new-socket6 ( -- socket )
  178. PF_INET6 SOCK_STREAM 0 socket
  179. dup 0<= abort" no free socket"
  180. dup IPPROTO_IPV6 IPV6_V6ONLY sockopt-on dup on 4 setsockopt drop ;
  181. : new-udp-socket ( -- socket )
  182. PF_INET SOCK_DGRAM 0 socket
  183. dup 0<= abort" no free socket"
  184. [IFDEF] darwin
  185. \ dup IPPROTO_IP IP_DONTFRAG sockopt-on 1 over l! 4
  186. \ setsockopt drop
  187. [ELSE]
  188. dup IPPROTO_IP IP_MTU_DISCOVER sockopt-on IP_PMTUDISC_DO over l! 4
  189. setsockopt drop
  190. [THEN] ;
  191. : new-udp-socket6 ( -- socket )
  192. PF_INET6 SOCK_DGRAM 0 socket
  193. dup 0<= abort" no free socket"
  194. [IFDEF] darwin
  195. \ dup IPPROTO_IP IP_DONTFRAG sockopt-on 1 over l! 4
  196. \ setsockopt drop
  197. [ELSE]
  198. dup IPPROTO_IPV6 IPV6_MTU_DISCOVER sockopt-on IP_PMTUDISC_DO over l! 4
  199. setsockopt drop
  200. [THEN]
  201. dup IPPROTO_IPV6 IPV6_V6ONLY sockopt-on dup on 4 setsockopt drop ;
  202. : new-udp-socket46 ( -- socket )
  203. PF_INET6 SOCK_DGRAM 0 socket
  204. dup 0<= abort" no free socket"
  205. [IFDEF] darwin
  206. \ dup IPPROTO_IP IP_DONTFRAG sockopt-on 1 over l! 4
  207. \ setsockopt drop
  208. [ELSE]
  209. dup IPPROTO_IPV6 IPV6_MTU_DISCOVER sockopt-on IP_PMTUDISC_DO over l! 4
  210. setsockopt drop
  211. [THEN]
  212. ;
  213. \ getaddrinfo based open-socket
  214. : >hints ( socktype -- )
  215. hints addrinfo %size erase
  216. PF_UNSPEC hints ai_family l!
  217. hints ai_socktype l! ;
  218. : get-info ( addr u port -- info )
  219. base @ >r decimal 0 <<# 0 hold #s #> r> base ! drop
  220. >r c-string r> hints addrres getaddrinfo #>>
  221. ?dup IF
  222. gai_strerror cstring>sstring type
  223. true abort" getaddrinfo failed" THEN
  224. addrres @ ;
  225. : get-socket ( info -- socket ) dup >r >r
  226. BEGIN r@ WHILE
  227. r@ ai_family l@ r@ ai_socktype l@ r@ ai_protocol l@ socket
  228. dup 0>= IF
  229. dup r@ ai_addr @ r@ ai_addrlen l@ connect
  230. IF
  231. closesocket drop
  232. ELSE
  233. s" w+" c-string fdopen
  234. rdrop r> freeaddrinfo EXIT
  235. THEN
  236. ELSE drop THEN
  237. r> ai_next @ >r REPEAT
  238. rdrop r> freeaddrinfo true abort" can't connect" ;
  239. : open-socket ( addr u port -- fid )
  240. SOCK_STREAM >hints get-info get-socket ;
  241. : open-udp-socket ( addr u port -- fid )
  242. SOCK_DGRAM >hints get-info get-socket ;
  243. : create-server ( port# -- lsocket )
  244. sockaddr-tmp sockaddr_in %size erase
  245. AF_INET sockaddr-tmp family w!
  246. htons sockaddr-tmp port w!
  247. new-socket
  248. dup 0< abort" no free socket" >r
  249. r@ SOL_SOCKET SO_REUSEADDR sockopt-on 1 over ! 4 setsockopt drop
  250. r@ sockaddr-tmp sockaddr_in4 %size bind 0= IF r> exit ENDIF
  251. r> drop true abort" bind :: failed" ;
  252. : create-server6 ( port# -- lsocket )
  253. sockaddr-tmp sockaddr_in %size erase
  254. AF_INET6 sockaddr-tmp family w!
  255. htons sockaddr-tmp port w!
  256. new-socket6
  257. dup 0< abort" no free socket" >r
  258. r@ sockaddr-tmp sockaddr_in6 %size bind 0= IF r> exit ENDIF
  259. r> drop true abort" bind :: failed" ;
  260. : create-udp-server ( port# -- lsocket )
  261. sockaddr-tmp sockaddr_in %size erase
  262. AF_INET sockaddr-tmp family w!
  263. htons sockaddr-tmp port w!
  264. new-udp-socket
  265. dup 0< abort" no free socket" >r
  266. r@ sockaddr-tmp sockaddr_in4 %size bind 0= IF r> exit ENDIF
  267. r> drop true abort" bind :: failed" ;
  268. : create-udp-server6 ( port# -- lsocket )
  269. sockaddr-tmp sockaddr_in6 %size erase
  270. AF_INET6 sockaddr-tmp family w!
  271. htons sockaddr-tmp port w!
  272. new-udp-socket6
  273. dup 0< abort" no free socket" >r
  274. r@ sockaddr-tmp sockaddr_in6 %size bind 0= IF r> exit ENDIF
  275. r> drop true abort" bind :: failed" ;
  276. : create-udp-server46 ( port# -- lsocket )
  277. sockaddr-tmp sockaddr_in6 %size erase
  278. AF_INET6 sockaddr-tmp family w!
  279. htons sockaddr-tmp port w!
  280. new-udp-socket46
  281. dup 0< abort" no free socket" >r
  282. r@ sockaddr-tmp sockaddr_in6 %size bind 0= IF r> exit ENDIF
  283. r> drop true abort" bind :: failed" ;
  284. \ from itools.frt
  285. ' open-socket Alias open-service
  286. : $put ( c-addr1 u1 c-addr2 -- ) swap cmove ;
  287. : $+ ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 )
  288. { c-addr1 u1 c-addr2 u2 }
  289. u1 u2 + allocate throw
  290. c-addr1 u1 2 pick $put
  291. c-addr2 u2 2 pick u1 + $put
  292. u1 u2 + ;
  293. Create hostname$ 0 c, 255 chars allot
  294. Create alen 16 ,
  295. Create crlf 2 c, 13 c, 10 c,
  296. : listen ( lsocket /queue -- )
  297. listen() 0< abort" listen :: failed" ;
  298. \ This call blocks the server until a client appears. The client uses socket to
  299. \ converse with the server.
  300. : accept-socket ( lsocket -- socket )
  301. 16 alen !
  302. sockaddr-tmp alen accept()
  303. dup 0< IF errno cr ." accept() :: error #" .
  304. abort" accept :: failed"
  305. ENDIF s" w+" c-string fdopen ;
  306. : +cr ( c-addr1 u1 -- c-addr2 u2 ) crlf count $+ ;
  307. : blocking-mode ( socket flag -- ) >r fileno
  308. f_setfl r> IF 0
  309. ELSE o_nonblock|o_rdwr
  310. THEN
  311. fcntl 0< abort" blocking-mode failed" ;
  312. : hostname ( -- c-addr u )
  313. hostname$ c@ 0= IF
  314. hostname$ 1+ 255 gethostname drop
  315. hostname$ 1+ 255 0 scan nip 255 swap - hostname$ c!
  316. THEN
  317. hostname$ count ;
  318. : set-socket-timeout ( u -- ) 200 + s>d socket-timeout-d 2! ;
  319. : get-socket-timeout ( -- u ) socket-timeout-d 2@ drop 200 - ;
  320. : write-socket ( c-addr size socket -- ) fileno -rot 0 send 0< throw ;
  321. : close-socket ( socket -- ) fileno closesocket drop ;
  322. : (rs) ( socket c-addr maxlen -- c-addr size )
  323. 2 pick >r r@ false blocking-mode rot fileno -rot
  324. over >r msg_waitall recv
  325. dup 0< IF 0 max
  326. errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
  327. THEN
  328. r> swap
  329. r> true blocking-mode ;
  330. : read-socket ( socket c-addr maxlen -- c-addr u )
  331. utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
  332. BEGIN
  333. socket c-addr maxlen (rs) dup 0=
  334. utime tmax d< and
  335. WHILE
  336. 2drop
  337. REPEAT ;
  338. : (rs-from) ( socket c-addr maxlen -- c-addr size )
  339. 2 pick >r r@ false blocking-mode rot fileno -rot
  340. over >r msg_waitall sockaddr-tmp alen recvfrom
  341. dup 0< IF 0 max
  342. errno dup 0<> swap ewouldblock <> and abort" (rs) :: socket read error"
  343. THEN
  344. r> swap
  345. r> true blocking-mode ;
  346. : read-socket-from ( socket c-addr maxlen -- c-addr u )
  347. utime socket-timeout-d 2@ d+ { socket c-addr maxlen d: tmax -- c-addr size }
  348. BEGIN
  349. socket c-addr maxlen (rs-from) dup 0=
  350. utime tmax d< and
  351. WHILE
  352. 2drop
  353. REPEAT ;