kibi-swipl-linux-x64-gnu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (550) hide show
  1. package/README.md +29 -0
  2. package/bin/swipl +0 -0
  3. package/bin/swipl-win +0 -0
  4. package/build-manifest.json +70 -0
  5. package/lib/cmake/swipl/SWIPLConfig.cmake +21 -0
  6. package/lib/cmake/swipl/SWIPLConfigVersion.cmake +65 -0
  7. package/lib/cmake/swipl/SWIPLTargets-release.cmake +28 -0
  8. package/lib/cmake/swipl/SWIPLTargets.cmake +109 -0
  9. package/lib/swipl/ABI +1 -0
  10. package/lib/swipl/LICENSE +41 -0
  11. package/lib/swipl/README.md +116 -0
  12. package/lib/swipl/app/README.md +25 -0
  13. package/lib/swipl/app/app.pl +87 -0
  14. package/lib/swipl/app/pack.pl +350 -0
  15. package/lib/swipl/app/qlf.pl +521 -0
  16. package/lib/swipl/app/splfr.pl +177 -0
  17. package/lib/swipl/app/sys.pl +310 -0
  18. package/lib/swipl/bin/swipl.home +1 -0
  19. package/lib/swipl/boot/apply.pl +73 -0
  20. package/lib/swipl/boot/attvar.pl +344 -0
  21. package/lib/swipl/boot/autoload.pl +1226 -0
  22. package/lib/swipl/boot/bags.pl +283 -0
  23. package/lib/swipl/boot/dcg.pl +388 -0
  24. package/lib/swipl/boot/dicts.pl +193 -0
  25. package/lib/swipl/boot/dwim.pl +485 -0
  26. package/lib/swipl/boot/engines.pl +152 -0
  27. package/lib/swipl/boot/expand.pl +1628 -0
  28. package/lib/swipl/boot/gc.pl +65 -0
  29. package/lib/swipl/boot/history.pl +304 -0
  30. package/lib/swipl/boot/init.pl +4682 -0
  31. package/lib/swipl/boot/iri.pl +64 -0
  32. package/lib/swipl/boot/license.pl +390 -0
  33. package/lib/swipl/boot/load.pl +81 -0
  34. package/lib/swipl/boot/messages.pl +2521 -0
  35. package/lib/swipl/boot/packs.pl +306 -0
  36. package/lib/swipl/boot/predopts.pl +154 -0
  37. package/lib/swipl/boot/qlf.pl +141 -0
  38. package/lib/swipl/boot/rc.pl +263 -0
  39. package/lib/swipl/boot/syspred.pl +1634 -0
  40. package/lib/swipl/boot/tabling.pl +2321 -0
  41. package/lib/swipl/boot/threads.pl +42 -0
  42. package/lib/swipl/boot/toplevel.pl +2248 -0
  43. package/lib/swipl/boot/topvars.pl +261 -0
  44. package/lib/swipl/boot.prc +0 -0
  45. package/lib/swipl/cmake/swipl.cmake +131 -0
  46. package/lib/swipl/customize/README.md +23 -0
  47. package/lib/swipl/customize/edit +101 -0
  48. package/lib/swipl/customize/init.pl +164 -0
  49. package/lib/swipl/demo/README.md +33 -0
  50. package/lib/swipl/demo/likes.pl +44 -0
  51. package/lib/swipl/doc/packages/examples/chr/bool.chr +281 -0
  52. package/lib/swipl/doc/packages/examples/chr/chrdif.chr +87 -0
  53. package/lib/swipl/doc/packages/examples/chr/chrfreeze.chr +6 -0
  54. package/lib/swipl/doc/packages/examples/chr/family.chr +116 -0
  55. package/lib/swipl/doc/packages/examples/chr/fib.chr +24 -0
  56. package/lib/swipl/doc/packages/examples/chr/fibonacci.chr +31 -0
  57. package/lib/swipl/doc/packages/examples/chr/gcd.chr +28 -0
  58. package/lib/swipl/doc/packages/examples/chr/leq.chr +34 -0
  59. package/lib/swipl/doc/packages/examples/chr/listdom.chr +138 -0
  60. package/lib/swipl/doc/packages/examples/chr/primes.chr +29 -0
  61. package/lib/swipl/doc/packages/examples/http/README.md +98 -0
  62. package/lib/swipl/doc/packages/examples/http/calc.pl +92 -0
  63. package/lib/swipl/doc/packages/examples/http/demo_body.pl +232 -0
  64. package/lib/swipl/doc/packages/examples/http/demo_client.pl +165 -0
  65. package/lib/swipl/doc/packages/examples/http/demo_daemon.pl +38 -0
  66. package/lib/swipl/doc/packages/examples/http/demo_files.pl +50 -0
  67. package/lib/swipl/doc/packages/examples/http/demo_hello.pl +15 -0
  68. package/lib/swipl/doc/packages/examples/http/demo_login.pl +280 -0
  69. package/lib/swipl/doc/packages/examples/http/demo_openid.pl +177 -0
  70. package/lib/swipl/doc/packages/examples/http/demo_pwp.pl +20 -0
  71. package/lib/swipl/doc/packages/examples/http/demo_rest.pl +56 -0
  72. package/lib/swipl/doc/packages/examples/http/demo_threads.pl +52 -0
  73. package/lib/swipl/doc/packages/examples/http/linux-init-script +126 -0
  74. package/lib/swipl/doc/packages/examples/http/pwp/context.pwp +23 -0
  75. package/lib/swipl/doc/packages/examples/http/pwp/index.pwp +25 -0
  76. package/lib/swipl/doc/packages/examples/http/pwp/pwp1.pwp +12 -0
  77. package/lib/swipl/doc/packages/examples/http/pwp/pwp2.pwp +10 -0
  78. package/lib/swipl/doc/packages/examples/http/pwp/pwp3.pwp +25 -0
  79. package/lib/swipl/doc/packages/examples/http/pwp/pwp4.pwp +23 -0
  80. package/lib/swipl/doc/packages/examples/http/pwp/pwp5.pwp +11 -0
  81. package/lib/swipl/doc/packages/examples/http/pwp/pwp6.pwp +3 -0
  82. package/lib/swipl/doc/packages/examples/http/pwp/pwp7.pwp +3 -0
  83. package/lib/swipl/doc/packages/examples/http/pwp/pwp8.pwp +3 -0
  84. package/lib/swipl/doc/packages/examples/http/pwp/pwpdb.pl +12 -0
  85. package/lib/swipl/doc/packages/examples/http/systemd-script.service +18 -0
  86. package/lib/swipl/doc/packages/examples/http/upstart-script.conf +20 -0
  87. package/lib/swipl/doc/packages/examples/plunit/read.pl +17 -0
  88. package/lib/swipl/doc/packages/examples/plunit/simple.pl +33 -0
  89. package/lib/swipl/doc/packages/examples/ssl/client.pl +83 -0
  90. package/lib/swipl/doc/packages/examples/ssl/etc/README +52 -0
  91. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.p12 +0 -0
  92. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-cert.pem +85 -0
  93. package/lib/swipl/doc/packages/examples/ssl/etc/client/client-key.pem +30 -0
  94. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/cacert.pem +82 -0
  95. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/careq.pem +18 -0
  96. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/crlnumber +1 -0
  97. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt +3 -0
  98. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/index.txt.attr +1 -0
  99. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7C.pem +85 -0
  100. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/newcerts/9DC08797A0F63F7D.pem +85 -0
  101. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/private/cakey.pem +30 -0
  102. package/lib/swipl/doc/packages/examples/ssl/etc/demoCA/serial +1 -0
  103. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-cert.pem +85 -0
  104. package/lib/swipl/doc/packages/examples/ssl/etc/server/server-key.pem +30 -0
  105. package/lib/swipl/doc/packages/examples/ssl/https.pl +267 -0
  106. package/lib/swipl/doc/packages/examples/ssl/server.pl +106 -0
  107. package/lib/swipl/include/SWI-Prolog.h +1544 -0
  108. package/lib/swipl/include/SWI-Stream.h +537 -0
  109. package/lib/swipl/include/Yap/YapInterface.h +283 -0
  110. package/lib/swipl/include/sicstus/sicstus.h +452 -0
  111. package/lib/swipl/lib/x86_64-linux/cgi.so +0 -0
  112. package/lib/swipl/lib/x86_64-linux/crypt.so +0 -0
  113. package/lib/swipl/lib/x86_64-linux/crypto4pl.so +0 -0
  114. package/lib/swipl/lib/x86_64-linux/double_metaphone.so +0 -0
  115. package/lib/swipl/lib/x86_64-linux/files.so +0 -0
  116. package/lib/swipl/lib/x86_64-linux/hashstream.so +0 -0
  117. package/lib/swipl/lib/x86_64-linux/http_stream.so +0 -0
  118. package/lib/swipl/lib/x86_64-linux/isub.so +0 -0
  119. package/lib/swipl/lib/x86_64-linux/json.so +0 -0
  120. package/lib/swipl/lib/x86_64-linux/libswipl.so.10 +0 -0
  121. package/lib/swipl/lib/x86_64-linux/mallocinfo.so +0 -0
  122. package/lib/swipl/lib/x86_64-linux/md54pl.so +0 -0
  123. package/lib/swipl/lib/x86_64-linux/memfile.so +0 -0
  124. package/lib/swipl/lib/x86_64-linux/ntriples.so +0 -0
  125. package/lib/swipl/lib/x86_64-linux/pcre4pl.so +0 -0
  126. package/lib/swipl/lib/x86_64-linux/porter_stem.so +0 -0
  127. package/lib/swipl/lib/x86_64-linux/process.so +0 -0
  128. package/lib/swipl/lib/x86_64-linux/prolog_stream.so +0 -0
  129. package/lib/swipl/lib/x86_64-linux/rdf_db.so +0 -0
  130. package/lib/swipl/lib/x86_64-linux/readutil.so +0 -0
  131. package/lib/swipl/lib/x86_64-linux/rlimit.so +0 -0
  132. package/lib/swipl/lib/x86_64-linux/sched.so +0 -0
  133. package/lib/swipl/lib/x86_64-linux/sgml2pl.so +0 -0
  134. package/lib/swipl/lib/x86_64-linux/sha4pl.so +0 -0
  135. package/lib/swipl/lib/x86_64-linux/snowball.so +0 -0
  136. package/lib/swipl/lib/x86_64-linux/socket.so +0 -0
  137. package/lib/swipl/lib/x86_64-linux/ssl4pl.so +0 -0
  138. package/lib/swipl/lib/x86_64-linux/streaminfo.so +0 -0
  139. package/lib/swipl/lib/x86_64-linux/syslog.so +0 -0
  140. package/lib/swipl/lib/x86_64-linux/time.so +0 -0
  141. package/lib/swipl/lib/x86_64-linux/turtle.so +0 -0
  142. package/lib/swipl/lib/x86_64-linux/uid.so +0 -0
  143. package/lib/swipl/lib/x86_64-linux/unix.so +0 -0
  144. package/lib/swipl/lib/x86_64-linux/uri.so +0 -0
  145. package/lib/swipl/lib/x86_64-linux/websocket.so +0 -0
  146. package/lib/swipl/lib/x86_64-linux/zlib4pl.so +0 -0
  147. package/lib/swipl/library/INDEX.pl +1020 -0
  148. package/lib/swipl/library/aggregate.pl +755 -0
  149. package/lib/swipl/library/ansi_term.pl +653 -0
  150. package/lib/swipl/library/apply.pl +426 -0
  151. package/lib/swipl/library/apply_macros.pl +497 -0
  152. package/lib/swipl/library/arithmetic.pl +320 -0
  153. package/lib/swipl/library/assoc.pl +522 -0
  154. package/lib/swipl/library/atom.pl +169 -0
  155. package/lib/swipl/library/backcomp.pl +688 -0
  156. package/lib/swipl/library/base32.pl +291 -0
  157. package/lib/swipl/library/base64.pl +441 -0
  158. package/lib/swipl/library/broadcast.pl +161 -0
  159. package/lib/swipl/library/build/cmake.pl +137 -0
  160. package/lib/swipl/library/build/conan.pl +232 -0
  161. package/lib/swipl/library/build/make.pl +168 -0
  162. package/lib/swipl/library/build/tools.pl +825 -0
  163. package/lib/swipl/library/charsio.pl +195 -0
  164. package/lib/swipl/library/check.pl +1097 -0
  165. package/lib/swipl/library/check_installation.pl +912 -0
  166. package/lib/swipl/library/clp/INDEX.pl +157 -0
  167. package/lib/swipl/library/clp/bounds.pl +2036 -0
  168. package/lib/swipl/library/clp/clp_distinct.pl +263 -0
  169. package/lib/swipl/library/clp/clp_events.pl +92 -0
  170. package/lib/swipl/library/clp/clpb.pl +1896 -0
  171. package/lib/swipl/library/clp/clpfd.pl +7906 -0
  172. package/lib/swipl/library/clp/simplex.pl +1580 -0
  173. package/lib/swipl/library/codesio.pl +182 -0
  174. package/lib/swipl/library/coinduction.pl +183 -0
  175. package/lib/swipl/library/console_input.pl +289 -0
  176. package/lib/swipl/library/csv.pl +611 -0
  177. package/lib/swipl/library/ctypes.pl +161 -0
  178. package/lib/swipl/library/date.pl +370 -0
  179. package/lib/swipl/library/dcg/INDEX.pl +35 -0
  180. package/lib/swipl/library/dcg/basics.pl +514 -0
  181. package/lib/swipl/library/dcg/high_order.pl +227 -0
  182. package/lib/swipl/library/debug.pl +499 -0
  183. package/lib/swipl/library/dialect/bim.pl +186 -0
  184. package/lib/swipl/library/dialect/commons.pl +77 -0
  185. package/lib/swipl/library/dialect/eclipse/test_util_iso.pl +355 -0
  186. package/lib/swipl/library/dialect/hprolog/format.pl +44 -0
  187. package/lib/swipl/library/dialect/hprolog.pl +314 -0
  188. package/lib/swipl/library/dialect/ifprolog.pl +1317 -0
  189. package/lib/swipl/library/dialect/iso/iso_predicates.pl +277 -0
  190. package/lib/swipl/library/dialect/sicstus/README.TXT +1 -0
  191. package/lib/swipl/library/dialect/sicstus/arrays.pl +121 -0
  192. package/lib/swipl/library/dialect/sicstus/block.pl +278 -0
  193. package/lib/swipl/library/dialect/sicstus/lists.pl +167 -0
  194. package/lib/swipl/library/dialect/sicstus/ordsets.pl +52 -0
  195. package/lib/swipl/library/dialect/sicstus/sockets.pl +174 -0
  196. package/lib/swipl/library/dialect/sicstus/system.pl +262 -0
  197. package/lib/swipl/library/dialect/sicstus/terms.pl +57 -0
  198. package/lib/swipl/library/dialect/sicstus/timeout.pl +92 -0
  199. package/lib/swipl/library/dialect/sicstus.pl +582 -0
  200. package/lib/swipl/library/dialect/sicstus4/aggregate.pl +60 -0
  201. package/lib/swipl/library/dialect/sicstus4/between.pl +62 -0
  202. package/lib/swipl/library/dialect/sicstus4/clpfd.pl +75 -0
  203. package/lib/swipl/library/dialect/sicstus4/file_systems.pl +413 -0
  204. package/lib/swipl/library/dialect/sicstus4/lists.pl +274 -0
  205. package/lib/swipl/library/dialect/sicstus4/ordsets.pl +54 -0
  206. package/lib/swipl/library/dialect/sicstus4/samsort.pl +86 -0
  207. package/lib/swipl/library/dialect/sicstus4/sets.pl +124 -0
  208. package/lib/swipl/library/dialect/sicstus4/sockets.pl +164 -0
  209. package/lib/swipl/library/dialect/sicstus4/system.pl +80 -0
  210. package/lib/swipl/library/dialect/sicstus4/terms.pl +89 -0
  211. package/lib/swipl/library/dialect/sicstus4/timeout.pl +43 -0
  212. package/lib/swipl/library/dialect/sicstus4/types.pl +103 -0
  213. package/lib/swipl/library/dialect/sicstus4.pl +200 -0
  214. package/lib/swipl/library/dialect/swi/syspred_options.pl +250 -0
  215. package/lib/swipl/library/dialect/xsb/README.md +20 -0
  216. package/lib/swipl/library/dialect/xsb/basics.pl +318 -0
  217. package/lib/swipl/library/dialect/xsb/consult.pl +87 -0
  218. package/lib/swipl/library/dialect/xsb/curr_sym.pl +57 -0
  219. package/lib/swipl/library/dialect/xsb/error_handler.pl +111 -0
  220. package/lib/swipl/library/dialect/xsb/gensym.pl +53 -0
  221. package/lib/swipl/library/dialect/xsb/gpp.pl +138 -0
  222. package/lib/swipl/library/dialect/xsb/intern.pl +48 -0
  223. package/lib/swipl/library/dialect/xsb/lists.pl +47 -0
  224. package/lib/swipl/library/dialect/xsb/machine.pl +295 -0
  225. package/lib/swipl/library/dialect/xsb/ordsets.pl +53 -0
  226. package/lib/swipl/library/dialect/xsb/setof.pl +54 -0
  227. package/lib/swipl/library/dialect/xsb/source.pl +308 -0
  228. package/lib/swipl/library/dialect/xsb/standard.pl +169 -0
  229. package/lib/swipl/library/dialect/xsb/storage.pl +70 -0
  230. package/lib/swipl/library/dialect/xsb/string.pl +76 -0
  231. package/lib/swipl/library/dialect/xsb/thread.pl +62 -0
  232. package/lib/swipl/library/dialect/xsb/timed_call.pl +158 -0
  233. package/lib/swipl/library/dialect/xsb.pl +688 -0
  234. package/lib/swipl/library/dialect/yap/README.TXT +8 -0
  235. package/lib/swipl/library/dialect/yap.pl +283 -0
  236. package/lib/swipl/library/dialect.pl +123 -0
  237. package/lib/swipl/library/dicts.pl +399 -0
  238. package/lib/swipl/library/dif.pl +426 -0
  239. package/lib/swipl/library/edinburgh.pl +138 -0
  240. package/lib/swipl/library/edit.pl +783 -0
  241. package/lib/swipl/library/error.pl +495 -0
  242. package/lib/swipl/library/exceptions.pl +231 -0
  243. package/lib/swipl/library/explain.pl +543 -0
  244. package/lib/swipl/library/ext/RDF/INDEX.pl +25 -0
  245. package/lib/swipl/library/ext/RDF/rdf.pl +450 -0
  246. package/lib/swipl/library/ext/RDF/rdf_parser.pl +649 -0
  247. package/lib/swipl/library/ext/RDF/rdf_triple.pl +448 -0
  248. package/lib/swipl/library/ext/RDF/rdf_write.pl +701 -0
  249. package/lib/swipl/library/ext/RDF/rewrite_term.pl +177 -0
  250. package/lib/swipl/library/ext/chr/chr/a_star.pl +80 -0
  251. package/lib/swipl/library/ext/chr/chr/binomialheap.pl +142 -0
  252. package/lib/swipl/library/ext/chr/chr/builtins.pl +629 -0
  253. package/lib/swipl/library/ext/chr/chr/chr_compiler_errors.pl +179 -0
  254. package/lib/swipl/library/ext/chr/chr/chr_compiler_options.pl +382 -0
  255. package/lib/swipl/library/ext/chr/chr/chr_compiler_utility.pl +338 -0
  256. package/lib/swipl/library/ext/chr/chr/chr_debug.pl +59 -0
  257. package/lib/swipl/library/ext/chr/chr/chr_hashtable_store.pl +425 -0
  258. package/lib/swipl/library/ext/chr/chr/chr_integertable_store.pl +139 -0
  259. package/lib/swipl/library/ext/chr/chr/chr_messages.pl +176 -0
  260. package/lib/swipl/library/ext/chr/chr/chr_op.pl +53 -0
  261. package/lib/swipl/library/ext/chr/chr/chr_runtime.pl +1016 -0
  262. package/lib/swipl/library/ext/chr/chr/chr_translate.pl +25424 -0
  263. package/lib/swipl/library/ext/chr/chr/clean_code.pl +252 -0
  264. package/lib/swipl/library/ext/chr/chr/find.pl +78 -0
  265. package/lib/swipl/library/ext/chr/chr/guard_entailment.pl +19432 -0
  266. package/lib/swipl/library/ext/chr/chr/listmap.pl +108 -0
  267. package/lib/swipl/library/ext/chr/chr/pairlist.pl +78 -0
  268. package/lib/swipl/library/ext/chr/chr.pl +686 -0
  269. package/lib/swipl/library/ext/clib/INDEX.pl +158 -0
  270. package/lib/swipl/library/ext/clib/cgi.pl +86 -0
  271. package/lib/swipl/library/ext/clib/crypt.pl +44 -0
  272. package/lib/swipl/library/ext/clib/filesex.pl +644 -0
  273. package/lib/swipl/library/ext/clib/hash_stream.pl +119 -0
  274. package/lib/swipl/library/ext/clib/mallocinfo.pl +138 -0
  275. package/lib/swipl/library/ext/clib/md5.pl +68 -0
  276. package/lib/swipl/library/ext/clib/memfile.pl +61 -0
  277. package/lib/swipl/library/ext/clib/process.pl +573 -0
  278. package/lib/swipl/library/ext/clib/prolog_server.pl +157 -0
  279. package/lib/swipl/library/ext/clib/prolog_stream.pl +80 -0
  280. package/lib/swipl/library/ext/clib/rlimit.pl +54 -0
  281. package/lib/swipl/library/ext/clib/sched.pl +85 -0
  282. package/lib/swipl/library/ext/clib/sha.pl +162 -0
  283. package/lib/swipl/library/ext/clib/socket.pl +1102 -0
  284. package/lib/swipl/library/ext/clib/streaminfo.pl +103 -0
  285. package/lib/swipl/library/ext/clib/streampool.pl +136 -0
  286. package/lib/swipl/library/ext/clib/syslog.pl +180 -0
  287. package/lib/swipl/library/ext/clib/time.pl +194 -0
  288. package/lib/swipl/library/ext/clib/udp_broadcast.pl +975 -0
  289. package/lib/swipl/library/ext/clib/uid.pl +220 -0
  290. package/lib/swipl/library/ext/clib/unix.pl +317 -0
  291. package/lib/swipl/library/ext/clib/uri.pl +530 -0
  292. package/lib/swipl/library/ext/clib/uuid.pl +207 -0
  293. package/lib/swipl/library/ext/http/http/INDEX.pl +266 -0
  294. package/lib/swipl/library/ext/http/http/README.md +34 -0
  295. package/lib/swipl/library/ext/http/http/ax.pl +189 -0
  296. package/lib/swipl/library/ext/http/http/dcg_basics.pl +49 -0
  297. package/lib/swipl/library/ext/http/http/graphql.pl +1367 -0
  298. package/lib/swipl/library/ext/http/http/html_decl.pl +416 -0
  299. package/lib/swipl/library/ext/http/http/html_head.pl +654 -0
  300. package/lib/swipl/library/ext/http/http/html_quasiquotations.pl +120 -0
  301. package/lib/swipl/library/ext/http/http/html_write.pl +1425 -0
  302. package/lib/swipl/library/ext/http/http/htmx.pl +139 -0
  303. package/lib/swipl/library/ext/http/http/http_authenticate.pl +364 -0
  304. package/lib/swipl/library/ext/http/http/http_client.pl +412 -0
  305. package/lib/swipl/library/ext/http/http/http_cookie.pl +245 -0
  306. package/lib/swipl/library/ext/http/http/http_cors.pl +176 -0
  307. package/lib/swipl/library/ext/http/http/http_digest.pl +820 -0
  308. package/lib/swipl/library/ext/http/http/http_dirindex.pl +296 -0
  309. package/lib/swipl/library/ext/http/http/http_dispatch.pl +1552 -0
  310. package/lib/swipl/library/ext/http/http/http_dyn_workers.pl +214 -0
  311. package/lib/swipl/library/ext/http/http/http_error.pl +126 -0
  312. package/lib/swipl/library/ext/http/http/http_exception.pl +225 -0
  313. package/lib/swipl/library/ext/http/http/http_files.pl +163 -0
  314. package/lib/swipl/library/ext/http/http/http_header.pl +2820 -0
  315. package/lib/swipl/library/ext/http/http/http_hook.pl +106 -0
  316. package/lib/swipl/library/ext/http/http/http_host.pl +204 -0
  317. package/lib/swipl/library/ext/http/http/http_load.pl +113 -0
  318. package/lib/swipl/library/ext/http/http/http_log.pl +738 -0
  319. package/lib/swipl/library/ext/http/http/http_multipart_plugin.pl +232 -0
  320. package/lib/swipl/library/ext/http/http/http_open.pl +1821 -0
  321. package/lib/swipl/library/ext/http/http/http_openid.pl +1561 -0
  322. package/lib/swipl/library/ext/http/http/http_parameters.pl +504 -0
  323. package/lib/swipl/library/ext/http/http/http_path.pl +380 -0
  324. package/lib/swipl/library/ext/http/http/http_proxy.pl +80 -0
  325. package/lib/swipl/library/ext/http/http/http_pwp.pl +351 -0
  326. package/lib/swipl/library/ext/http/http/http_redis_plugin.pl +332 -0
  327. package/lib/swipl/library/ext/http/http/http_server.pl +105 -0
  328. package/lib/swipl/library/ext/http/http/http_server_files.pl +126 -0
  329. package/lib/swipl/library/ext/http/http/http_server_health.pl +186 -0
  330. package/lib/swipl/library/ext/http/http/http_session.pl +1040 -0
  331. package/lib/swipl/library/ext/http/http/http_sgml_plugin.pl +137 -0
  332. package/lib/swipl/library/ext/http/http/http_stream.pl +376 -0
  333. package/lib/swipl/library/ext/http/http/http_unix_daemon.pl +989 -0
  334. package/lib/swipl/library/ext/http/http/http_wrapper.pl +559 -0
  335. package/lib/swipl/library/ext/http/http/hub.pl +600 -0
  336. package/lib/swipl/library/ext/http/http/js_write.pl +461 -0
  337. package/lib/swipl/library/ext/http/http/mimepack.pl +224 -0
  338. package/lib/swipl/library/ext/http/http/mimetype.pl +276 -0
  339. package/lib/swipl/library/ext/http/http/term_html.pl +797 -0
  340. package/lib/swipl/library/ext/http/http/thread_httpd.pl +1181 -0
  341. package/lib/swipl/library/ext/http/http/web/css/dirindex.css +24 -0
  342. package/lib/swipl/library/ext/http/http/web/css/openid.css +57 -0
  343. package/lib/swipl/library/ext/http/http/web/icons/back.png +0 -0
  344. package/lib/swipl/library/ext/http/http/web/icons/c.png +0 -0
  345. package/lib/swipl/library/ext/http/http/web/icons/compressed.png +0 -0
  346. package/lib/swipl/library/ext/http/http/web/icons/folder.png +0 -0
  347. package/lib/swipl/library/ext/http/http/web/icons/generic.png +0 -0
  348. package/lib/swipl/library/ext/http/http/web/icons/layout.png +0 -0
  349. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-square.png +0 -0
  350. package/lib/swipl/library/ext/http/http/web/icons/openid-logo-tiny.png +0 -0
  351. package/lib/swipl/library/ext/http/http/websocket.pl +729 -0
  352. package/lib/swipl/library/ext/http/http/yadis.pl +205 -0
  353. package/lib/swipl/library/ext/json/INDEX.pl +36 -0
  354. package/lib/swipl/library/ext/json/http/INDEX.pl +16 -0
  355. package/lib/swipl/library/ext/json/http/http_json.pl +490 -0
  356. package/lib/swipl/library/ext/json/http/js_grammar.pl +53 -0
  357. package/lib/swipl/library/ext/json/http/json.pl +48 -0
  358. package/lib/swipl/library/ext/json/http/json_convert.pl +48 -0
  359. package/lib/swipl/library/ext/json/json.pl +1215 -0
  360. package/lib/swipl/library/ext/json/json_convert.pl +681 -0
  361. package/lib/swipl/library/ext/json/json_grammar.pl +349 -0
  362. package/lib/swipl/library/ext/json/json_rpc_client.pl +423 -0
  363. package/lib/swipl/library/ext/json/json_rpc_common.pl +104 -0
  364. package/lib/swipl/library/ext/json/json_rpc_server.pl +489 -0
  365. package/lib/swipl/library/ext/json/json_schema.pl +2111 -0
  366. package/lib/swipl/library/ext/nlp/INDEX.pl +15 -0
  367. package/lib/swipl/library/ext/nlp/double_metaphone.pl +86 -0
  368. package/lib/swipl/library/ext/nlp/isub.pl +162 -0
  369. package/lib/swipl/library/ext/nlp/porter_stem.pl +50 -0
  370. package/lib/swipl/library/ext/nlp/snowball.pl +97 -0
  371. package/lib/swipl/library/ext/pcre/INDEX.pl +17 -0
  372. package/lib/swipl/library/ext/pcre/pcre.pl +974 -0
  373. package/lib/swipl/library/ext/plunit/INDEX.pl +20 -0
  374. package/lib/swipl/library/ext/plunit/plunit.pl +2631 -0
  375. package/lib/swipl/library/ext/plunit/test_wizard.pl +249 -0
  376. package/lib/swipl/library/ext/semweb/semweb/INDEX.pl +288 -0
  377. package/lib/swipl/library/ext/semweb/semweb/rdf11.pl +2106 -0
  378. package/lib/swipl/library/ext/semweb/semweb/rdf11_containers.pl +299 -0
  379. package/lib/swipl/library/ext/semweb/semweb/rdf_cache.pl +278 -0
  380. package/lib/swipl/library/ext/semweb/semweb/rdf_compare.pl +132 -0
  381. package/lib/swipl/library/ext/semweb/semweb/rdf_db.pl +3468 -0
  382. package/lib/swipl/library/ext/semweb/semweb/rdf_edit.pl +1145 -0
  383. package/lib/swipl/library/ext/semweb/semweb/rdf_http_plugin.pl +274 -0
  384. package/lib/swipl/library/ext/semweb/semweb/rdf_library.pl +1021 -0
  385. package/lib/swipl/library/ext/semweb/semweb/rdf_litindex.pl +1035 -0
  386. package/lib/swipl/library/ext/semweb/semweb/rdf_ntriples.pl +422 -0
  387. package/lib/swipl/library/ext/semweb/semweb/rdf_persistency.pl +1576 -0
  388. package/lib/swipl/library/ext/semweb/semweb/rdf_portray.pl +182 -0
  389. package/lib/swipl/library/ext/semweb/semweb/rdf_prefixes.pl +681 -0
  390. package/lib/swipl/library/ext/semweb/semweb/rdf_sandbox.pl +153 -0
  391. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle.pl +54 -0
  392. package/lib/swipl/library/ext/semweb/semweb/rdf_turtle_write.pl +1587 -0
  393. package/lib/swipl/library/ext/semweb/semweb/rdf_zlib_plugin.pl +52 -0
  394. package/lib/swipl/library/ext/semweb/semweb/rdfa.pl +1510 -0
  395. package/lib/swipl/library/ext/semweb/semweb/rdfs.pl +475 -0
  396. package/lib/swipl/library/ext/semweb/semweb/sparql_client.pl +643 -0
  397. package/lib/swipl/library/ext/semweb/semweb/turtle.pl +455 -0
  398. package/lib/swipl/library/ext/sgml/DTD/HTML4.dcl +88 -0
  399. package/lib/swipl/library/ext/sgml/DTD/HTML4.dtd +1092 -0
  400. package/lib/swipl/library/ext/sgml/DTD/HTML4.soc +6 -0
  401. package/lib/swipl/library/ext/sgml/DTD/HTML5.dtd +906 -0
  402. package/lib/swipl/library/ext/sgml/DTD/HTMLlat1.ent +195 -0
  403. package/lib/swipl/library/ext/sgml/DTD/HTMLspec.ent +77 -0
  404. package/lib/swipl/library/ext/sgml/DTD/HTMLsym.ent +241 -0
  405. package/lib/swipl/library/ext/sgml/INDEX.pl +64 -0
  406. package/lib/swipl/library/ext/sgml/c14n2.pl +203 -0
  407. package/lib/swipl/library/ext/sgml/iso_639.pl +631 -0
  408. package/lib/swipl/library/ext/sgml/pwp.pl +705 -0
  409. package/lib/swipl/library/ext/sgml/sgml.pl +815 -0
  410. package/lib/swipl/library/ext/sgml/sgml_write.pl +1003 -0
  411. package/lib/swipl/library/ext/sgml/xpath.pl +620 -0
  412. package/lib/swipl/library/ext/sgml/xsdp_types.pl +232 -0
  413. package/lib/swipl/library/ext/ssl/INDEX.pl +60 -0
  414. package/lib/swipl/library/ext/ssl/crypto.pl +1015 -0
  415. package/lib/swipl/library/ext/ssl/http/INDEX.pl +5 -0
  416. package/lib/swipl/library/ext/ssl/http/http_ssl_plugin.pl +296 -0
  417. package/lib/swipl/library/ext/ssl/saml.pl +609 -0
  418. package/lib/swipl/library/ext/ssl/ssl.pl +647 -0
  419. package/lib/swipl/library/ext/ssl/xmldsig.pl +391 -0
  420. package/lib/swipl/library/ext/ssl/xmlenc.pl +354 -0
  421. package/lib/swipl/library/ext/zlib/INDEX.pl +8 -0
  422. package/lib/swipl/library/ext/zlib/zlib.pl +112 -0
  423. package/lib/swipl/library/fastrw.pl +105 -0
  424. package/lib/swipl/library/files.pl +80 -0
  425. package/lib/swipl/library/gensym.pl +111 -0
  426. package/lib/swipl/library/git.pl +1027 -0
  427. package/lib/swipl/library/hashtable.pl +365 -0
  428. package/lib/swipl/library/heaps.pl +252 -0
  429. package/lib/swipl/library/hotfix.pl +239 -0
  430. package/lib/swipl/library/http/jquery.pl +103 -0
  431. package/lib/swipl/library/http/web/js/jquery-3.6.0.min.js +2 -0
  432. package/lib/swipl/library/increval.pl +217 -0
  433. package/lib/swipl/library/intercept.pl +263 -0
  434. package/lib/swipl/library/iostream.pl +237 -0
  435. package/lib/swipl/library/iri_scheme/file.pl +70 -0
  436. package/lib/swipl/library/lazy_lists.pl +481 -0
  437. package/lib/swipl/library/listing.pl +1362 -0
  438. package/lib/swipl/library/lists.pl +860 -0
  439. package/lib/swipl/library/lynx/INDEX.pl +14 -0
  440. package/lib/swipl/library/lynx/format.pl +380 -0
  441. package/lib/swipl/library/lynx/html_style.pl +129 -0
  442. package/lib/swipl/library/lynx/html_text.pl +865 -0
  443. package/lib/swipl/library/lynx/pldoc_style.pl +87 -0
  444. package/lib/swipl/library/macros.pl +451 -0
  445. package/lib/swipl/library/main.pl +1200 -0
  446. package/lib/swipl/library/make.pl +197 -0
  447. package/lib/swipl/library/modules.pl +129 -0
  448. package/lib/swipl/library/nb_rbtrees.pl +267 -0
  449. package/lib/swipl/library/nb_set.pl +203 -0
  450. package/lib/swipl/library/obfuscate.pl +115 -0
  451. package/lib/swipl/library/occurs.pl +241 -0
  452. package/lib/swipl/library/operators.pl +182 -0
  453. package/lib/swipl/library/option.pl +388 -0
  454. package/lib/swipl/library/optparse.pl +966 -0
  455. package/lib/swipl/library/ordsets.pl +518 -0
  456. package/lib/swipl/library/oset.pl +160 -0
  457. package/lib/swipl/library/pairs.pl +187 -0
  458. package/lib/swipl/library/persistency.pl +713 -0
  459. package/lib/swipl/library/pio.pl +47 -0
  460. package/lib/swipl/library/portray_text.pl +277 -0
  461. package/lib/swipl/library/pprint.pl +940 -0
  462. package/lib/swipl/library/predicate_options.pl +975 -0
  463. package/lib/swipl/library/prolog_autoload.pl +233 -0
  464. package/lib/swipl/library/prolog_breakpoints.pl +396 -0
  465. package/lib/swipl/library/prolog_clause.pl +1104 -0
  466. package/lib/swipl/library/prolog_code.pl +416 -0
  467. package/lib/swipl/library/prolog_codewalk.pl +1248 -0
  468. package/lib/swipl/library/prolog_colour.pl +3301 -0
  469. package/lib/swipl/library/prolog_colour.qlf +0 -0
  470. package/lib/swipl/library/prolog_config.pl +154 -0
  471. package/lib/swipl/library/prolog_coverage.pl +1333 -0
  472. package/lib/swipl/library/prolog_debug.pl +404 -0
  473. package/lib/swipl/library/prolog_deps.pl +675 -0
  474. package/lib/swipl/library/prolog_evaluable.pl +234 -0
  475. package/lib/swipl/library/prolog_format.pl +219 -0
  476. package/lib/swipl/library/prolog_history.pl +161 -0
  477. package/lib/swipl/library/prolog_jiti.pl +333 -0
  478. package/lib/swipl/library/prolog_locale.pl +61 -0
  479. package/lib/swipl/library/prolog_metainference.pl +307 -0
  480. package/lib/swipl/library/prolog_pack.pl +3906 -0
  481. package/lib/swipl/library/prolog_profile.pl +346 -0
  482. package/lib/swipl/library/prolog_qlfmake.pl +533 -0
  483. package/lib/swipl/library/prolog_source.pl +1188 -0
  484. package/lib/swipl/library/prolog_stack.pl +794 -0
  485. package/lib/swipl/library/prolog_trace.pl +365 -0
  486. package/lib/swipl/library/prolog_versions.pl +347 -0
  487. package/lib/swipl/library/prolog_wrap.pl +164 -0
  488. package/lib/swipl/library/prolog_xref.pl +3090 -0
  489. package/lib/swipl/library/prolog_xref.qlf +0 -0
  490. package/lib/swipl/library/pure_input.pl +266 -0
  491. package/lib/swipl/library/qpforeign.pl +695 -0
  492. package/lib/swipl/library/qsave.pl +1516 -0
  493. package/lib/swipl/library/quasi_quotations.pl +300 -0
  494. package/lib/swipl/library/quintus.pl +437 -0
  495. package/lib/swipl/library/random.pl +459 -0
  496. package/lib/swipl/library/rbtrees.pl +1153 -0
  497. package/lib/swipl/library/readln.pl +256 -0
  498. package/lib/swipl/library/readutil.pl +304 -0
  499. package/lib/swipl/library/record.pl +544 -0
  500. package/lib/swipl/library/rwlocks.pl +172 -0
  501. package/lib/swipl/library/sandbox.pl +1384 -0
  502. package/lib/swipl/library/semweb/dc.rdfs +198 -0
  503. package/lib/swipl/library/semweb/eor.rdfs +95 -0
  504. package/lib/swipl/library/semweb/owl.owl +287 -0
  505. package/lib/swipl/library/semweb/rdf_library.ttl +90 -0
  506. package/lib/swipl/library/semweb/rdfs.rdfs +251 -0
  507. package/lib/swipl/library/settings.pl +755 -0
  508. package/lib/swipl/library/shell.pl +462 -0
  509. package/lib/swipl/library/shlib.pl +670 -0
  510. package/lib/swipl/library/solution_sequences.pl +439 -0
  511. package/lib/swipl/library/sort.pl +119 -0
  512. package/lib/swipl/library/statistics.pl +541 -0
  513. package/lib/swipl/library/streams.pl +118 -0
  514. package/lib/swipl/library/strings.pl +469 -0
  515. package/lib/swipl/library/system.pl +95 -0
  516. package/lib/swipl/library/tables.pl +381 -0
  517. package/lib/swipl/library/tableutil.pl +771 -0
  518. package/lib/swipl/library/tabling.pl +42 -0
  519. package/lib/swipl/library/terms.pl +457 -0
  520. package/lib/swipl/library/theme/auto.pl +61 -0
  521. package/lib/swipl/library/theme/dark.pl +455 -0
  522. package/lib/swipl/library/theme/light.pl +45 -0
  523. package/lib/swipl/library/thread.pl +810 -0
  524. package/lib/swipl/library/thread_pool.pl +508 -0
  525. package/lib/swipl/library/threadutil.pl +574 -0
  526. package/lib/swipl/library/tty.pl +316 -0
  527. package/lib/swipl/library/ugraphs.pl +644 -0
  528. package/lib/swipl/library/unicode/INDEX.pl +6 -0
  529. package/lib/swipl/library/unicode/blocks.pl +414 -0
  530. package/lib/swipl/library/url.pl +1129 -0
  531. package/lib/swipl/library/utf8.pl +145 -0
  532. package/lib/swipl/library/varnumbers.pl +221 -0
  533. package/lib/swipl/library/vm.pl +251 -0
  534. package/lib/swipl/library/wfs.pl +205 -0
  535. package/lib/swipl/library/when.pl +263 -0
  536. package/lib/swipl/library/writef.pl +278 -0
  537. package/lib/swipl/library/www_browser.pl +251 -0
  538. package/lib/swipl/library/yall.pl +574 -0
  539. package/lib/swipl/library/zip.pl +211 -0
  540. package/lib/swipl/swipl.home +1 -0
  541. package/lib/vendor/libcrypto.so.3 +0 -0
  542. package/lib/vendor/libpcre2-8.so.0 +0 -0
  543. package/lib/vendor/libssl.so.3 +0 -0
  544. package/lib/vendor/libz.so.1 +0 -0
  545. package/licenses/OpenSSL-LICENSE.txt +177 -0
  546. package/licenses/PCRE2-COPYING +5 -0
  547. package/licenses/README.md +12 -0
  548. package/licenses/SWI-Prolog-LICENSE +41 -0
  549. package/licenses/zlib-LICENSE +22 -0
  550. package/package.json +27 -0
@@ -0,0 +1,1102 @@
1
+ /* Part of SWI-Prolog
2
+
3
+ Author: Jan Wielemaker
4
+ E-mail: J.Wielemaker@vu.nl
5
+ WWW: http://www.swi-prolog.org
6
+ Copyright (c) 2000-2023, University of Amsterdam
7
+ VU University Amsterdam
8
+ CWI, Amsterdam
9
+ SWI-Prolog Solutions b.v.
10
+ All rights reserved.
11
+
12
+ Redistribution and use in source and binary forms, with or without
13
+ modification, are permitted provided that the following conditions
14
+ are met:
15
+
16
+ 1. Redistributions of source code must retain the above copyright
17
+ notice, this list of conditions and the following disclaimer.
18
+
19
+ 2. Redistributions in binary form must reproduce the above copyright
20
+ notice, this list of conditions and the following disclaimer in
21
+ the documentation and/or other materials provided with the
22
+ distribution.
23
+
24
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
+ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
+ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
+ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
+ ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
+ POSSIBILITY OF SUCH DAMAGE.
36
+ */
37
+
38
+ :- module(socket,
39
+ [ socket_create/2, % -Socket, +Options
40
+ tcp_socket/1, % -Socket
41
+ tcp_close_socket/1, % +Socket
42
+ tcp_open_socket/3, % +Socket, -Read, -Write
43
+ tcp_connect/2, % +Socket, +Address
44
+ tcp_connect/3, % +Address, -StreamPair, +Options
45
+ tcp_connect/4, % +Socket, +Address, -Read, -Write)
46
+ tcp_bind/2, % +Socket, +Address
47
+ tcp_accept/3, % +Master, -Slave, -PeerName
48
+ tcp_listen/2, % +Socket, +BackLog
49
+ tcp_fcntl/3, % +Socket, +Command, ?Arg
50
+ tcp_setopt/2, % +Socket, +Option
51
+ tcp_getopt/2, % +Socket, ?Option
52
+ host_address/3, % ?HostName, ?Address, +Options
53
+ tcp_host_to_address/2, % ?HostName, ?Ip-nr
54
+ tcp_select/3, % +Inputs, -Ready, +Timeout
55
+ gethostname/1, % -HostName
56
+
57
+ ip_name/2, % ?Ip, ?Name
58
+
59
+ tcp_open_socket/2, % +Socket, -StreamPair
60
+
61
+ udp_socket/1, % -Socket
62
+ udp_receive/4, % +Socket, -Data, -Sender, +Options
63
+ udp_send/4, % +Socket, +Data, +Sender, +Options
64
+
65
+ negotiate_socks_connection/2% +DesiredEndpoint, +StreamPair
66
+ ]).
67
+ :- use_module(library(debug), [assertion/1, debug/3]).
68
+ :- autoload(library(lists), [last/2, member/2, append/3, append/2]).
69
+ :- autoload(library(apply), [maplist/3, maplist/2]).
70
+ :- autoload(library(error),
71
+ [instantiation_error/1, syntax_error/1, must_be/2, domain_error/2]).
72
+ :- autoload(library(option), [option/2, option/3]).
73
+
74
+ :- multifile
75
+ rewrite_host/3. % +HostIn, -Host, +Socket
76
+
77
+ /** <module> Network socket (TCP and UDP) library
78
+
79
+ The library(socket) provides TCP and UDP inet-domain sockets from
80
+ SWI-Prolog, both client and server-side communication. The interface of
81
+ this library is very close to the Unix socket interface, also supported
82
+ by the MS-Windows _winsock_ API. SWI-Prolog applications that wish to
83
+ communicate with multiple sources have two options:
84
+
85
+ - Use I/O multiplexing based on wait_for_input/3. On Windows
86
+ systems this can only be used for sockets, not for general
87
+ (device-) file handles.
88
+ - Use multiple threads, handling either a single blocking socket
89
+ or a pool using I/O multiplexing as above.
90
+
91
+ ## Client applications {#socket-server}
92
+
93
+ Using this library to establish a TCP connection to a server is as
94
+ simple as opening a file. See also http_open/3.
95
+
96
+ ==
97
+ dump_swi_homepage :-
98
+ setup_call_cleanup(
99
+ tcp_connect('www.swi-prolog.org':http, Stream, []),
100
+ ( format(Stream,
101
+ 'GET / HTTP/1.1~n\c
102
+ Host: www.swi-prolog.org~n\c
103
+ Connection: close~n~n', []),
104
+ flush_output(Stream),
105
+ copy_stream_data(Stream, current_output)
106
+ ),
107
+ close(Stream)).
108
+ ==
109
+
110
+ To deal with timeouts and multiple connections, threads,
111
+ wait_for_input/3 and/or non-blocking streams (see tcp_fcntl/3) can be
112
+ used.
113
+
114
+ ## Server applications {#socket-client}
115
+
116
+ The typical sequence for generating a server application is given below.
117
+ To close the server, use close/1 on the `StreamPair`.
118
+
119
+ ==
120
+ create_server(Port) :-
121
+ tcp_socket(Socket),
122
+ tcp_bind(Socket, Port),
123
+ tcp_listen(Socket, 5),
124
+ tcp_open_socket(Socket, StreamPair),
125
+ stream_pair(StreamPair, AcceptFd, _),
126
+ <dispatch>
127
+ ==
128
+
129
+ There are various options for <dispatch>. The most commonly used option
130
+ is to start a Prolog thread to handle the connection. Alternatively,
131
+ input from multiple clients can be handled in a single thread by
132
+ listening to these clients using wait_for_input/3. Finally, on Unix
133
+ systems, we can use fork/1 to handle the connection in a new process.
134
+ Note that fork/1 and threads do not cooperate well. Combinations can be
135
+ realised but require good understanding of POSIX thread and
136
+ fork-semantics.
137
+
138
+ Below is the typical example using a thread. Note the use of
139
+ setup_call_cleanup/3 to guarantee that all resources are reclaimed, also
140
+ in case of failure or exceptions.
141
+
142
+ ==
143
+ dispatch(AcceptFd) :-
144
+ tcp_accept(AcceptFd, Socket, Peer),
145
+ thread_create(process_client(Socket, Peer), _,
146
+ [ detached(true)
147
+ ]),
148
+ dispatch(AcceptFd).
149
+
150
+ process_client(Socket, Peer) :-
151
+ setup_call_cleanup(
152
+ tcp_open_socket(Socket, StreamPair),
153
+ handle_service(StreamPair),
154
+ close(StreamPair)).
155
+
156
+ handle_service(StreamPair) :-
157
+ ...
158
+ ==
159
+
160
+ ## Socket exceptions {#socket-exceptions}
161
+
162
+ Errors that are trapped by the low-level library are mapped to an
163
+ exception of the shape below. In this term, `Code` is a lower case atom
164
+ that corresponds to the C macro name, e.g., `epipe` for a broken pipe.
165
+ `Message` is the human readable string for the error code returned by
166
+ the OS or the same as `Code` if the OS does not provide this
167
+ functionality. Note that `Code` is derived from a static set of macros
168
+ that may or may not be defines for the target OS. If the macro name is
169
+ not known, `Code` is =|ERROR_nnn|=, where _nnn_ is an integer.
170
+
171
+ error(socket_error(Code, Message), _)
172
+
173
+ Note that on Windows `Code` is a ``wsa*`` code which makes it hard to
174
+ write portable code that handles specific socket errors. Even on POSIX
175
+ systems the exact set of errors produced by the network stack is not
176
+ defined.
177
+
178
+ ## Socket addresses (families) {#socket-domains}
179
+
180
+ The library supports both IP4 and IP6 addresses. On Unix systems it also
181
+ supports _Unix domain sockets_ (``AF_UNIX``). The address of a Unix
182
+ domain sockets is a file name. Unix domain sockets are created using
183
+ socket_create/2 or unix_domain_socket/1.
184
+
185
+ IP4 or IP6 sockets can be created using socket_create/2 or tcp_connect/3
186
+ with the `inet` (default, IP3) or `inet6` domain option. Some of the
187
+ predicates produce or consume IP addresses as a Prolog term. The format
188
+ of this term is one of:
189
+
190
+ - ip(A,B,C,D)
191
+ Represents an IP4 address. Each field is an integer in the range
192
+ 0..255 (8 bit).
193
+ - ip(A,B,C,D,E,F,G,H)
194
+ Represents an IP6 address. Each field is an integer in the range
195
+ 0..65535 (16 bit).
196
+
197
+ The predicate ip_name/2 translates between the canonical textual
198
+ representation and the above defined address terms.
199
+
200
+ ## Socket predicate reference {#socket-predicates}
201
+ */
202
+
203
+ :- multifile
204
+ tcp_connect_hook/3, % +Socket, +Addr, -In, -Out
205
+ tcp_connect_hook/4, % +Socket, +Addr, -Stream
206
+ proxy_for_url/3, % +URL, +Host, -ProxyList
207
+ try_proxy/4. % +Proxy, +Addr, -Socket, -Stream
208
+
209
+ :- predicate_options(tcp_connect/3, 3,
210
+ [ bypass_proxy(boolean),
211
+ nodelay(boolean),
212
+ domain(oneof([inet,inet6]))
213
+ ]).
214
+
215
+ :- use_foreign_library(foreign(socket)).
216
+ :- public tcp_debug/1. % set debugging.
217
+
218
+ :- if(current_predicate(unix_domain_socket/1)).
219
+ :- export(unix_domain_socket/1). % -Socket
220
+ :- endif.
221
+
222
+ %! socket_create(-SocketId, +Options) is det.
223
+ %
224
+ % Create a socket according to Options. Supported Options are:
225
+ %
226
+ % - domain(+Domain)
227
+ % One of `inet` (default), `inet6`, `unix` or `local` (same
228
+ % as `unix`)
229
+ % - type(+Type)
230
+ % One of `stream` (default) to create a TCP connection or
231
+ % `dgram` to create a UDP socket.
232
+ %
233
+ % This predicate subsumes tcp_socket/1, udp_socket/1 and
234
+ % unix_domain_socket/1.
235
+
236
+ %! tcp_socket(-SocketId) is det.
237
+ %
238
+ % Equivalent to socket_create(SocketId, []) or, explicit,
239
+ % socket_create(SocketId, [domain(inet), type(stream)]).
240
+
241
+ %! unix_domain_socket(-SocketId) is det.
242
+ %
243
+ % Equivalent to socket_create(SocketId, [domain(unix)]) or,
244
+ % explicit, socket_create(SocketId, [domain(unix), type(stream)])
245
+ %
246
+ % Unix domain socket affect tcp_connect/2 (for clients) and
247
+ % tcp_bind/2 and tcp_accept/3 (for servers). The address is an atom
248
+ % or string that is handled as a file name. On most systems the
249
+ % length of this file name is limited to 128 bytes (including null
250
+ % terminator), but according to the Linux documentation (unix(7)),
251
+ % portable applications must keep the address below 92 bytes. Note
252
+ % that these lengths are in bytes. Non-ascii characters may be
253
+ % represented as multiple bytes. If the length limit is exceeded a
254
+ % representation_error(af_unix_name) exception is raised.
255
+
256
+ %! tcp_close_socket(+SocketId) is det.
257
+ %
258
+ % Closes the indicated socket, making SocketId invalid. Normally,
259
+ % sockets are closed by closing both stream handles returned by
260
+ % open_socket/3. There are two cases where tcp_close_socket/1 is
261
+ % used because there are no stream-handles:
262
+ %
263
+ % - If, after tcp_accept/3, the server uses fork/1 to handle the
264
+ % client in a sub-process. In this case the accepted socket is
265
+ % not longer needed from the main server and must be discarded
266
+ % using tcp_close_socket/1.
267
+ % - If, after discovering the connecting client with
268
+ % tcp_accept/3, the server does not want to accept the
269
+ % connection, it should discard the accepted socket
270
+ % immediately using tcp_close_socket/1.
271
+
272
+ %! tcp_open_socket(+SocketId, -StreamPair) is det.
273
+ %
274
+ % Create streams to communicate to SocketId. If SocketId is a
275
+ % master socket (see tcp_bind/2), StreamPair should be used for
276
+ % tcp_accept/3. If SocketId is a connected (see tcp_connect/2) or
277
+ % accepted socket (see tcp_accept/3), StreamPair is unified to a
278
+ % stream pair (see stream_pair/3) that can be used for reading and
279
+ % writing. The stream or pair must be closed with close/1, which
280
+ % also closes SocketId.
281
+
282
+ tcp_open_socket(Socket, Stream) :-
283
+ tcp_open_socket(Socket, In, Out),
284
+ ( var(Out)
285
+ -> Stream = In
286
+ ; stream_pair(Stream, In, Out)
287
+ ).
288
+
289
+ %! tcp_open_socket(+SocketId, -InStream, -OutStream) is det.
290
+ %
291
+ % Similar to tcp_open_socket/2, but creates two separate sockets
292
+ % where tcp_open_socket/2 would have created a stream pair.
293
+ %
294
+ % @deprecated New code should use tcp_open_socket/2 because
295
+ % closing a stream pair is much easier to perform safely.
296
+
297
+ %! tcp_bind(SocketId, ?Address) is det.
298
+ %
299
+ % Bind the socket to Address on the current machine. This
300
+ % operation, together with tcp_listen/2 and tcp_accept/3 implement
301
+ % the _server-side_ of the socket interface. Address is either an
302
+ % plain `Port` or a term HostPort. The first form binds the socket
303
+ % to the given port on all interfaces, while the second only binds
304
+ % to the matching interface. A typical example is below, causing
305
+ % the socket to listen only on port 8080 on the local machine's
306
+ % network.
307
+ %
308
+ % ==
309
+ % tcp_bind(Socket, localhost:8080)
310
+ % ==
311
+ %
312
+ % If `Port` is unbound, the system picks an arbitrary free port
313
+ % and unifies `Port` with the selected port number. `Port` is
314
+ % either an integer or the name of a registered service. See also
315
+ % tcp_connect/4.
316
+
317
+ %! tcp_listen(+SocketId, +BackLog) is det.
318
+ %
319
+ % Tells, after tcp_bind/2, the socket to listen for incoming
320
+ % requests for connections. Backlog indicates how many pending
321
+ % connection requests are allowed. Pending requests are requests
322
+ % that are not yet acknowledged using tcp_accept/3. If the
323
+ % indicated number is exceeded, the requesting client will be
324
+ % signalled that the service is currently not available. A
325
+ % commonly used default value for Backlog is 5.
326
+
327
+ %! tcp_accept(+Socket, -Slave, -Peer) is det.
328
+ %
329
+ % This predicate waits on a server socket for a connection request by
330
+ % a client. On success, it creates a new socket for the client and
331
+ % binds the identifier to Slave. Peer is bound to the IP-address of
332
+ % the client or the atom `af_unix` if Socket is an AF_UNIX socket (see
333
+ % unix_domain_socket/1).
334
+
335
+ %! tcp_connect(+SocketId, +Address) is det.
336
+ %
337
+ % Connect SocketId. After successful completion, tcp_open_socket/3
338
+ % can be used to create I/O-Streams to the remote socket. This
339
+ % predicate is part of the low level client API. A connection to a
340
+ % particular host and port is realised using these steps:
341
+ %
342
+ % ==
343
+ % tcp_socket(Socket),
344
+ % tcp_connect(Socket, Host:Port),
345
+ % tcp_open_socket(Socket, StreamPair)
346
+ % ==
347
+ %
348
+ % Typical client applications should use the high level interface
349
+ % provided by tcp_connect/3 which avoids resource leaking if a
350
+ % step in the process fails, and can be hooked to support proxies.
351
+ % For example:
352
+ %
353
+ % ==
354
+ % setup_call_cleanup(
355
+ % tcp_connect(Host:Port, StreamPair, []),
356
+ % talk(StreamPair),
357
+ % close(StreamPair))
358
+ % ==
359
+ %
360
+ % If SocketId is an AF_UNIX socket (see unix_domain_socket/1), Address
361
+ % is an atom or string denoting a file name.
362
+
363
+ tcp_connect(Socket, Host0:Port) =>
364
+ ( rewrite_host(Host0, Host, Socket)
365
+ -> true
366
+ ; Host = Host0
367
+ ),
368
+ tcp_connect_(Socket, Host:Port).
369
+ tcp_connect(Socket, Address) =>
370
+ tcp_connect_(Socket, Address).
371
+
372
+ %! rewrite_host(+HostIn, -HostOut, +Socket) is nondet.
373
+ %
374
+ % Allow rewriting the host for tcp_connect/2 and therefore all other
375
+ % predicates to connect a socket.
376
+ %
377
+ % This hook is currently defined in Windows to map `localhost` to
378
+ % ip(127,0,0,1) as resolving `localhost` on Windows is often very
379
+ % slow. Note that we do not want to do that in general as a system may
380
+ % prefer to map `localhost` to `::1`, i.e., the IPv6 loopback address.
381
+
382
+ :- if(current_prolog_flag(windows, true)).
383
+ rewrite_host(localhost, ip(127,0,0,1), _).
384
+ :- endif.
385
+
386
+
387
+ /*******************************
388
+ * HOOKABLE CONNECT *
389
+ *******************************/
390
+
391
+ %! tcp_connect(+Socket, +Address, -Read, -Write) is det.
392
+ %
393
+ % Connect a (client) socket to Address and return a bi-directional
394
+ % connection through the stream-handles Read and Write. This
395
+ % predicate may be hooked by defining socket:tcp_connect_hook/4
396
+ % with the same signature. Hooking can be used to deal with proxy
397
+ % connections. E.g.,
398
+ %
399
+ % ==
400
+ % :- multifile socket:tcp_connect_hook/4.
401
+ %
402
+ % socket:tcp_connect_hook(Socket, Address, Read, Write) :-
403
+ % proxy(ProxyAdress),
404
+ % tcp_connect(Socket, ProxyAdress),
405
+ % tcp_open_socket(Socket, Read, Write),
406
+ % proxy_connect(Address, Read, Write).
407
+ % ==
408
+ %
409
+ % @deprecated New code should use tcp_connect/3 called as
410
+ % tcp_connect(+Address, -StreamPair, +Options).
411
+
412
+ tcp_connect(Socket, Address, Read, Write) :-
413
+ tcp_connect_hook(Socket, Address, Read, Write),
414
+ !.
415
+ tcp_connect(Socket, Address, Read, Write) :-
416
+ tcp_connect(Socket, Address),
417
+ tcp_open_socket(Socket, Read, Write).
418
+
419
+
420
+
421
+ %! tcp_connect(+Address, -StreamPair, +Options) is det.
422
+ %! tcp_connect(+Socket, +Address, -StreamPair) is det.
423
+ %
424
+ % Establish a TCP communication as a client. The +,-,+ mode is the
425
+ % preferred way for a client to establish a connection. This predicate
426
+ % can be hooked to support network proxies. To use a proxy, the hook
427
+ % proxy_for_url/3 must be defined. Permitted options are:
428
+ %
429
+ % * bypass_proxy(+Boolean)
430
+ % Defaults to =false=. If =true=, do not attempt to use any
431
+ % proxies to obtain the connection
432
+ %
433
+ % * nodelay(+Boolean)
434
+ % Defaults to =false=. If =true=, set nodelay on the
435
+ % resulting socket using tcp_setopt(Socket, nodelay)
436
+ %
437
+ % * domain(+Domain)
438
+ % One of `inet' or `inet6`. When omitted we use host_address/2
439
+ % with type(stream) and try the returned addresses in order.
440
+ %
441
+ % The +,+,- mode is deprecated and does not support proxies. It
442
+ % behaves like tcp_connect/4, but creates a stream pair (see
443
+ % stream_pair/3).
444
+ %
445
+ % @arg Address is either a Host:Port term or a file name (atom or
446
+ % string). The latter connects to an AF_UNIX socket and requires
447
+ % unix_domain_socket/1.
448
+ %
449
+ % @error proxy_error(tried(ResultList)) is raised by mode (+,-,+) if
450
+ % proxies are defines by proxy_for_url/3 but no proxy can establsh the
451
+ % connection. `ResultList` contains one or more terms of the form
452
+ % false(Proxy) for a hook that simply failed or error(Proxy,
453
+ % ErrorTerm) for a hook that raised an exception.
454
+ %
455
+ % @see library(http/http_proxy) defines a hook that allows to connect
456
+ % through HTTP proxies that support the =CONNECT= method.
457
+
458
+ % Main mode: +,-,+
459
+ tcp_connect(Address, StreamPair, Options) :-
460
+ var(StreamPair),
461
+ !,
462
+ ( memberchk(bypass_proxy(true), Options)
463
+ -> tcp_connect_direct(Address, Socket, StreamPair, Options)
464
+ ; findall(Result,
465
+ try_a_proxy(Address, Result),
466
+ ResultList),
467
+ last(ResultList, Status)
468
+ -> ( Status = true(_Proxy, Socket, StreamPair)
469
+ -> true
470
+ ; throw(error(proxy_error(tried(ResultList)), _))
471
+ )
472
+ ; tcp_connect_direct(Address, Socket, StreamPair, Options)
473
+ ),
474
+ ( memberchk(nodelay(true), Options)
475
+ -> tcp_setopt(Socket, nodelay)
476
+ ; true
477
+ ).
478
+ % backward compatibility mode +,+,-
479
+ tcp_connect(Socket, Address, StreamPair) :-
480
+ tcp_connect_hook(Socket, Address, StreamPair0),
481
+ !,
482
+ StreamPair = StreamPair0.
483
+ tcp_connect(Socket, Address, StreamPair) :-
484
+ connect_stream_pair(Socket, Address, StreamPair).
485
+
486
+ :- public tcp_connect_direct/3. % used by HTTP proxy code.
487
+ tcp_connect_direct(Address, Socket, StreamPair) :-
488
+ tcp_connect_direct(Address, Socket, StreamPair, []).
489
+
490
+ %! tcp_connect_direct(+Address, +Socket, -StreamPair, +Options) is det.
491
+ %
492
+ % Make a direct connection to a TCP address, i.e., do not take proxy
493
+ % rules into account. If no explicit domain (`inet`, `inet6` is
494
+ % given, perform a getaddrinfo() call to obtain the relevant
495
+ % addresses.
496
+
497
+ tcp_connect_direct(Host0:Port, Socket, StreamPair, Options) :-
498
+ must_be(ground, Host0),
499
+ \+ option(domain(_), Options),
500
+ !,
501
+ ( rewrite_host(Host0, Host, Socket)
502
+ -> true
503
+ ; Host = Host0
504
+ ),
505
+ State = error(_),
506
+ ( ( is_ip(Host, Domain)
507
+ -> IP = Host
508
+ ; host_address(Host, Address, [type(stream)]),
509
+ Domain = Address.domain,
510
+ IP = Address.address
511
+ ),
512
+ socket_create(Socket, [domain(Domain)]),
513
+ E = error(_,_),
514
+ catch(connect_or_discard_socket(Socket, IP:Port, StreamPair),
515
+ E, store_error_and_fail(State, E)),
516
+ debug(socket, '~p: connected to ~p', [Host, IP])
517
+ -> true
518
+ ; arg(1, State, Error),
519
+ assertion(nonvar(Error)),
520
+ throw(Error)
521
+ ).
522
+ tcp_connect_direct(Address, Socket, StreamPair, Options) :-
523
+ make_socket(Address, Socket, Options),
524
+ connect_or_discard_socket(Socket, Address, StreamPair).
525
+
526
+ is_ip(ip(_,_,_,_), inet).
527
+ is_ip(ip(_,_,_,_, _,_,_,_), inet6).
528
+
529
+ connect_or_discard_socket(Socket, Address, StreamPair) :-
530
+ setup_call_catcher_cleanup(
531
+ true,
532
+ connect_stream_pair(Socket, Address, StreamPair),
533
+ Catcher, cleanup(Catcher, Socket)).
534
+
535
+ cleanup(exit, _) :- !.
536
+ cleanup(_, Socket) :-
537
+ tcp_close_socket(Socket).
538
+
539
+ connect_stream_pair(Socket, Address, StreamPair) :-
540
+ tcp_connect(Socket, Address, Read, Write),
541
+ stream_pair(StreamPair, Read, Write).
542
+
543
+ store_error_and_fail(State, E) :-
544
+ arg(1, State, E0),
545
+ var(E0),
546
+ nb_setarg(1, State, E),
547
+ fail.
548
+
549
+ :- if(current_predicate(unix_domain_socket/1)).
550
+ make_socket(Address, Socket, _Options) :-
551
+ ( atom(Address)
552
+ ; string(Address)
553
+ ),
554
+ !,
555
+ unix_domain_socket(Socket).
556
+ :- endif.
557
+ make_socket(_Address, Socket, Options) :-
558
+ option(domain(Domain), Options, inet),
559
+ socket_create(Socket, [domain(Domain)]).
560
+
561
+
562
+ %! tcp_select(+ListOfStreams, -ReadyList, +TimeOut)
563
+ %
564
+ % Same as the built-in wait_for_input/3. Used to allow for interrupts
565
+ % and timeouts on Windows. A redesign of the Windows socket interface
566
+ % makes it impossible to do better than Windows select() call
567
+ % underlying wait_for_input/3. As input multiplexing typically happens
568
+ % in a background thread anyway we accept the loss of timeouts and
569
+ % interrupts.
570
+ %
571
+ % @deprecated Use wait_for_input/3
572
+
573
+ tcp_select(ListOfStreams, ReadyList, TimeOut) :-
574
+ wait_for_input(ListOfStreams, ReadyList, TimeOut).
575
+
576
+
577
+ /*******************************
578
+ * PROXY SUPPORT *
579
+ *******************************/
580
+
581
+ try_a_proxy(Address, Result) :-
582
+ format(atom(URL), 'socket://~w', [Address]),
583
+ ( Address = Host:_
584
+ -> true
585
+ ; Host = Address
586
+ ),
587
+ proxy_for_url(URL, Host, Proxy),
588
+ debug(socket(proxy), 'Socket connecting via ~w~n', [Proxy]),
589
+ ( catch(try_proxy(Proxy, Address, Socket, Stream), E, true)
590
+ -> ( var(E)
591
+ -> !, Result = true(Proxy, Socket, Stream)
592
+ ; Result = error(Proxy, E)
593
+ )
594
+ ; Result = false(Proxy)
595
+ ),
596
+ debug(socket(proxy), 'Socket: ~w: ~p', [Proxy, Result]).
597
+
598
+ %! try_proxy(+Proxy, +TargetAddress, -Socket, -StreamPair) is semidet.
599
+ %
600
+ % Attempt a socket-level connection via the given proxy to
601
+ % TargetAddress. The Proxy argument must match the output argument
602
+ % of proxy_for_url/3. The predicate tcp_connect/3 (and http_open/3
603
+ % from the library(http/http_open)) collect the results of failed
604
+ % proxies and raise an exception no proxy is capable of realizing
605
+ % the connection.
606
+ %
607
+ % The default implementation recognises the values for Proxy
608
+ % described below. The library(http/http_proxy) adds
609
+ % proxy(Host,Port) which allows for HTTP proxies using the
610
+ % =CONNECT= method.
611
+ %
612
+ % - direct
613
+ % Do not use any proxy
614
+ % - socks(Host, Port)
615
+ % Use a SOCKS5 proxy
616
+
617
+ :- multifile
618
+ try_proxy/4.
619
+
620
+ try_proxy(direct, Address, Socket, StreamPair) :-
621
+ !,
622
+ tcp_connect_direct(Address, Socket, StreamPair).
623
+ try_proxy(socks(Host, Port), Address, Socket, StreamPair) :-
624
+ !,
625
+ tcp_connect_direct(Host:Port, Socket, StreamPair),
626
+ catch(negotiate_socks_connection(Address, StreamPair),
627
+ Error,
628
+ ( close(StreamPair, [force(true)]),
629
+ throw(Error)
630
+ )).
631
+
632
+ %! proxy_for_url(+URL, +Hostname, -Proxy) is nondet.
633
+ %
634
+ % This hook can be implemented to return a proxy to try when
635
+ % connecting to URL. Returned proxies are tried in the order in
636
+ % which they are returned by the multifile hook try_proxy/4.
637
+ % Pre-defined proxy methods are:
638
+ %
639
+ % * direct
640
+ % connect directly to the resource
641
+ % * proxy(Host, Port)
642
+ % Connect to the resource using an HTTP proxy. If the
643
+ % resource is not an HTTP URL, then try to connect using the
644
+ % CONNECT verb, otherwise, use the GET verb.
645
+ % * socks(Host, Port)
646
+ % Connect to the resource via a SOCKS5 proxy
647
+ %
648
+ % These correspond to the proxy methods defined by PAC [Proxy
649
+ % auto-config](http://en.wikipedia.org/wiki/Proxy_auto-config).
650
+ % Additional methods can be returned if suitable clauses for
651
+ % http:http_connection_over_proxy/6 or try_proxy/4 are defined.
652
+
653
+ :- multifile
654
+ proxy_for_url/3.
655
+
656
+ %! udp_socket(-SocketId) is det.
657
+ %
658
+ % Equivalent to socket_create(SocketId, [type(dgram)]) or, explicit,
659
+ % socket_create(SocketId, [domain(inet), type(dgram)]).
660
+
661
+ %! udp_receive(+Socket, -Data, -From, +Options) is det.
662
+ %
663
+ % Wait for and return the next datagram. The Data is returned as a
664
+ % Prolog term depending on Options. From is a term of the format
665
+ % Ip:Port indicating the sender of the message. Here, `Ip` is either
666
+ % an ip4 or ip6 structure. Socket can be waited for using
667
+ % wait_for_input/3. Defined Options:
668
+ %
669
+ % - as(+Type)
670
+ % Defines the type for Data. Possible values are `atom`, `codes`,
671
+ % `string` (default) or `term` (parse as Prolog term).
672
+ % - encoding(+Encoding)
673
+ % Specify the encoding used to interpret the message. It is one of
674
+ % `octet`. `iso_latin_1`, `text` or `utf8`.
675
+ % - max_message_size(+Size)
676
+ % Specify the maximum number of bytes to read from a UDP
677
+ % datagram. Size must be within the range 0-65535. If unspecified,
678
+ % a maximum of 4096 bytes will be read.
679
+ %
680
+ % For example:
681
+ %
682
+ % ```
683
+ % receive(Port) :-
684
+ % udp_socket(Socket),
685
+ % tcp_bind(Socket, Port),
686
+ % repeat,
687
+ % udp_receive(Socket, Data, From, [as(atom)]),
688
+ % format('Got ~q from ~q~n', [Data, From]),
689
+ % fail.
690
+ % ```
691
+
692
+
693
+ %! udp_send(+Socket, +Data, +To, +Options) is det.
694
+ %
695
+ % Send a UDP message. Data is a string, atom or code-list providing
696
+ % the data. To is an address of the form Host:Port where Host is
697
+ % either the hostname or an IP address. Defined Options are:
698
+ %
699
+ % - encoding(+Encoding)
700
+ % Specifies the encoding to use for the string. See
701
+ % udp_receive/4 for details
702
+ % - as(+Type)
703
+ % This uses the same values for Type as the as(Type) option of
704
+ % udp_receive/4. The are interpreted differently though. No Type
705
+ % corresponds to CVT_ALL of PL_get_chars(). Using atom
706
+ % corresponds to CVT_ATOM and any of string or codes is mapped
707
+ % to CVT_STRING|CVT_LIST, allowing for a SWI-Prolog string
708
+ % object, list of character codes or list of characters.
709
+ % Finally, `term` maps to CVT_WRITE_CANONICAL. This implies that
710
+ % arbitrary Prolog terms can be sent reliably using the option
711
+ % list `[as(term),encoding(utf8)])`, using the same option list
712
+ % for udp_receive/4.
713
+ %
714
+ % For example
715
+ %
716
+ % ```
717
+ % send(Host, Port, Message) :-
718
+ % udp_socket(S),
719
+ % udp_send(S, Message, Host:Port, []),
720
+ % tcp_close_socket(S).
721
+ % ```
722
+ %
723
+ % A broadcast is achieved by using tcp_setopt(Socket, broadcast)
724
+ % prior to sending the datagram and using the local network
725
+ % broadcast address as a ip/4 term.
726
+
727
+
728
+ /*******************************
729
+ * OPTIONS *
730
+ *******************************/
731
+
732
+ %! tcp_setopt(+SocketId, +Option) is det.
733
+ %
734
+ % Set options on the socket. Defined options are:
735
+ %
736
+ % - reuseaddr
737
+ % Allow servers to reuse a port without the system being
738
+ % completely sure the port is no longer in use.
739
+ %
740
+ % - bindtodevice(+Device)
741
+ % Bind the socket to Device (an atom). For example, the code
742
+ % below binds the socket to the _loopback_ device that is
743
+ % typically used to realise the _localhost_. See the manual
744
+ % pages for setsockopt() and the socket interface (e.g.,
745
+ % socket(7) on Linux) for details.
746
+ %
747
+ % ==
748
+ % tcp_socket(Socket),
749
+ % tcp_setopt(Socket, bindtodevice(lo))
750
+ % ==
751
+ %
752
+ % - nodelay
753
+ % - nodelay(true)
754
+ % If =true=, disable the Nagle optimization on this socket,
755
+ % which is enabled by default on almost all modern TCP/IP
756
+ % stacks. The Nagle optimization joins small packages, which is
757
+ % generally desirable, but sometimes not. Please note that the
758
+ % underlying TCP_NODELAY setting to setsockopt() is not
759
+ % available on all platforms and systems may require additional
760
+ % privileges to change this option. If the option is not
761
+ % supported, tcp_setopt/2 raises a domain_error exception. See
762
+ % [Wikipedia](http://en.wikipedia.org/wiki/Nagle's_algorithm)
763
+ % for details.
764
+ %
765
+ % - broadcast
766
+ % UDP sockets only: broadcast the package to all addresses
767
+ % matching the address. The address is normally the address of
768
+ % the local subnet (i.e. 192.168.1.255). See udp_send/4.
769
+ %
770
+ % - ip_add_membership(+MultiCastGroup)
771
+ % - ip_add_membership(+MultiCastGroup, +LocalInterface)
772
+ % - ip_add_membership(+MultiCastGroup, +LocalInterface, +InterfaceIndex)
773
+ % - ip_drop_membership(+MultiCastGroup)
774
+ % - ip_drop_membership(+MultiCastGroup, +LocalInterface)
775
+ % - ip_drop_membership(+MultiCastGroup, +LocalInterface, +InterfaceIndex)
776
+ % Join/leave a multicast group. Calls setsockopt() with the
777
+ % corresponding arguments.
778
+ %
779
+ % - dispatch(+Boolean)
780
+ % In GUI environments (using XPCE or the Windows =swipl-win.exe=
781
+ % executable) this flags defines whether or not any events are
782
+ % dispatched on behalf of the user interface. Default is
783
+ % =true=. Only very specific situations require setting
784
+ % this to =false=.
785
+ %
786
+ % - sndbuf(+Integer)
787
+ % Sets the send buffer size to Integer (bytes). On Windows this defaults
788
+ % (now) to 64kb. Higher latency links may benefit from increasing this
789
+ % further since the maximum theoretical throughput on a link is given by
790
+ % buffer-size / latency.
791
+ % See https://support.microsoft.com/en-gb/help/823764/slow-performance-occurs-when-you-copy-data-to-a-tcp-server-by-using-a
792
+ % for Microsoft's discussion
793
+
794
+ %! tcp_fcntl(+Stream, +Action, ?Argument) is det.
795
+ %
796
+ % Interface to the fcntl() call. Currently only suitable to deal
797
+ % switch stream to non-blocking mode using:
798
+ %
799
+ % ==
800
+ % tcp_fcntl(Stream, setfl, nonblock),
801
+ % ==
802
+ %
803
+ % An attempt to read from a non-blocking stream while there is no
804
+ % data available returns -1 (or =end_of_file= for read/1), but
805
+ % at_end_of_stream/1 fails. On actual end-of-input,
806
+ % at_end_of_stream/1 succeeds.
807
+
808
+ tcp_fcntl(Socket, setfl, nonblock) :-
809
+ !,
810
+ tcp_setopt(Socket, nonblock).
811
+
812
+ %! tcp_getopt(+Socket, ?Option) is semidet.
813
+ %
814
+ % Get information about Socket. Defined properties are below.
815
+ % Requesting an unknown option results in a `domain_error` exception.
816
+ %
817
+ % - file_no(-File)
818
+ % Get the OS file handle as an integer. This may be used for
819
+ % debugging and integration.
820
+
821
+ %! host_address(+HostName, -Address, +Options) is nondet.
822
+ %! host_address(-HostName, +Address, +Options) is det.
823
+ %
824
+ % Translate between a machines host-name and it's (IP-)address.
825
+ % Supported options:
826
+ %
827
+ % - domain(+Domain)
828
+ % One of `inet` or `inet6` to limit the results to the given
829
+ % family.
830
+ % - type(+Type)
831
+ % One of `stream` or `dgram`.
832
+ % - canonname(+Boolean)
833
+ % If `true` (default `false`), return the canonical host name
834
+ % in the frist answer
835
+ %
836
+ % In mode (+,-,+) Address is unified to a dict with the following keys:
837
+ %
838
+ % - address
839
+ % A Prolog terms describing the ip address.
840
+ % - domain
841
+ % One of `inet` or `inet6`. The underlying getaddrinfo() calls
842
+ % this `family`. We use `domain` for consistency with
843
+ % socket_create/2.
844
+ % - type
845
+ % Currently one of `stream` or `dgram`.
846
+ % - host
847
+ % Available if canonname(true) is specified on the first
848
+ % returned address. Holds the official canonical host name.
849
+
850
+ host_address(HostName, Address, Options), ground(HostName) =>
851
+ '$host_address'(HostName, Addresses, Options),
852
+ member(Address, Addresses).
853
+ host_address(HostName, Address, Options), is_dict(Address) =>
854
+ '$host_address'(HostName, Address.address, Options).
855
+ host_address(HostName, Address, Options), ground(Address) =>
856
+ '$host_address'(HostName, Address, Options).
857
+
858
+ %! tcp_host_to_address(?HostName, ?Address) is det.
859
+ %
860
+ % Translate between a machines host-name and it's (IP-)address. If
861
+ % HostName is an atom, it is resolved using getaddrinfo() and the
862
+ % IP-number is unified to Address using a term of the format
863
+ % ip(Byte1,Byte2,Byte3,Byte4). Otherwise, if Address is bound to an
864
+ % ip(Byte1,Byte2,Byte3,Byte4) term, it is resolved by gethostbyaddr()
865
+ % and the canonical hostname is unified with HostName.
866
+ %
867
+ % @deprecated New code should use host_address/3. This version is
868
+ % bootstrapped from host_address/3 and only searches for IP4 addresses
869
+ % that support TCP connections.
870
+
871
+ tcp_host_to_address(Host, Address), ground(Address) =>
872
+ host_address(Host, Address, []).
873
+ tcp_host_to_address(Host, Address), ground(Host) =>
874
+ host_address(Host, [Dict|_], [domain(inet), type(stream)]),
875
+ Address = Dict.address.
876
+
877
+
878
+ %! gethostname(-Hostname) is det.
879
+ %
880
+ % Return the canonical fully qualified name of this host. This is
881
+ % achieved by calling gethostname() and return the canonical name
882
+ % returned by getaddrinfo().
883
+
884
+
885
+ %! ip_name(?IP, ?Name) is det.
886
+ %
887
+ % Translate between the textual representation of an IP address and
888
+ % the Prolog data structure. Prolog represents ip4 addresses as
889
+ % ip(A,B,C,D) and ip6 addresses as ip(A,B,C,D,E,F,H). For example:
890
+ %
891
+ % ?- ip_name(ip(1,2,3,4), Name)
892
+ % Name = '1.2.3.4'.
893
+ % ?- ip_name(IP, '::').
894
+ % IP = ip(0,0,0,0,0,0,0,0).
895
+ % ?- ip_name(IP, '1:2::3').
896
+ % IP = ip(1,2,0,0,0,0,0,3).
897
+
898
+ ip_name(Ip, Atom), ground(Atom) =>
899
+ name_to_ip(Atom, Ip).
900
+ ip_name(Ip, Atom), ground(Ip) =>
901
+ ip_to_name(Ip, Atom).
902
+ ip_name(Ip, _) =>
903
+ instantiation_error(Ip).
904
+
905
+ name_to_ip(Atom, Ip4) :-
906
+ split_string(Atom, '.', '', Parts),
907
+ length(Parts, 4),
908
+ maplist(string_byte, Parts, Bytes),
909
+ !,
910
+ Ip4 =.. [ip|Bytes].
911
+ name_to_ip(Atom, Ip6) :-
912
+ split_string(Atom, ':', '', Parts0),
913
+ clean_ends(Parts0, Parts1),
914
+ length(Parts1, Len),
915
+ ( Len < 8
916
+ -> append(Pre, [""|Post], Parts1),
917
+ Zeros is 8-(Len-1),
918
+ length(ZList, Zeros),
919
+ maplist(=("0"), ZList),
920
+ append([Pre, ZList, Post], Parts)
921
+ ; Len == 8
922
+ -> Parts = Parts1
923
+ ),
924
+ !,
925
+ maplist(string_short, Parts, Shorts),
926
+ Ip6 =.. [ip|Shorts].
927
+ name_to_ip(Atom, _) :-
928
+ syntax_error(ip_address(Atom)).
929
+
930
+ clean_ends([""|T0], T) :-
931
+ !,
932
+ ( append(T1, [""], T0)
933
+ -> T = T1
934
+ ; T = T0
935
+ ).
936
+ clean_ends(T0, T) :-
937
+ append(T1, [""], T0),
938
+ !,
939
+ T = T1.
940
+ clean_ends(T, T).
941
+
942
+ string_byte(String, Byte) :-
943
+ number_string(Byte, String),
944
+ must_be(between(0, 255), Byte).
945
+
946
+ string_short(String, Short) :-
947
+ string_concat('0x', String, String1),
948
+ number_string(Short, String1),
949
+ must_be(between(0, 65535), Short).
950
+
951
+ ip_to_name(ip(A,B,C,D), Atom) :-
952
+ !,
953
+ atomic_list_concat([A,B,C,D], '.', Atom).
954
+ ip_to_name(IP, Atom) :-
955
+ compound(IP),
956
+ compound_name_arity(IP, ip, 8),
957
+ !,
958
+ IP =.. [ip|Parts],
959
+ ( zero_seq(Parts, Pre, Post, Len),
960
+ Len > 1,
961
+ \+ ( zero_seq(Post, _, _, Len2),
962
+ Len2 > Len
963
+ )
964
+ -> append([Pre, [''], Post], Parts1),
965
+ ( Pre == []
966
+ -> Parts2 = [''|Parts1]
967
+ ; Parts2 = Parts1
968
+ ),
969
+ ( Post == []
970
+ -> append(Parts2, [''], Parts3)
971
+ ; Parts3 = Parts2
972
+ )
973
+ ; Parts3 = Parts
974
+ ),
975
+ maplist(to_hex, Parts3, Parts4),
976
+ atomic_list_concat(Parts4, ':', Atom).
977
+ ip_to_name(IP, _) :-
978
+ domain_error(ip_address, IP).
979
+
980
+ zero_seq(List, Pre, Post, Count) :-
981
+ append(Pre, [0|Post0], List),
982
+ leading_zeros(Post0, Post, 1, Count).
983
+
984
+ leading_zeros([0|T0], T, C0, C) =>
985
+ C1 is C0+1,
986
+ leading_zeros(T0, T, C1, C).
987
+ leading_zeros(L0, L, C0, C) =>
988
+ L = L0,
989
+ C = C0.
990
+
991
+ to_hex('', '') :-
992
+ !.
993
+ to_hex(Num, Hex) :-
994
+ format(string(Hex), '~16r', [Num]).
995
+
996
+
997
+
998
+ /*******************************
999
+ * SOCKS *
1000
+ *******************************/
1001
+
1002
+ %! negotiate_socks_connection(+DesiredEndpoint, +StreamPair) is det.
1003
+ %
1004
+ % Negotiate a connection to DesiredEndpoint over StreamPair.
1005
+ % DesiredEndpoint should be in the form of either:
1006
+ %
1007
+ % * hostname : port
1008
+ % * ip(A,B,C,D) : port
1009
+ %
1010
+ % @error socks_error(Details) if the SOCKS negotiation failed.
1011
+
1012
+ negotiate_socks_connection(Host:Port, StreamPair):-
1013
+ format(StreamPair, '~s', [[0x5, % Version 5
1014
+ 0x1, % 1 auth method supported
1015
+ 0x0]]), % which is 'no auth'
1016
+ flush_output(StreamPair),
1017
+ get_byte(StreamPair, ServerVersion),
1018
+ get_byte(StreamPair, AuthenticationMethod),
1019
+ ( ServerVersion =\= 0x05
1020
+ -> throw(error(socks_error(invalid_version(5, ServerVersion)), _))
1021
+ ; AuthenticationMethod =:= 0xff
1022
+ -> throw(error(socks_error(invalid_authentication_method(
1023
+ 0xff,
1024
+ AuthenticationMethod)), _))
1025
+ ; true
1026
+ ),
1027
+ ( Host = ip(A,B,C,D)
1028
+ -> AddressType = 0x1, % IPv4 Address
1029
+ format(atom(Address), '~s', [[A, B, C, D]])
1030
+ ; AddressType = 0x3, % Domain
1031
+ atom_length(Host, Length),
1032
+ format(atom(Address), '~s~w', [[Length], Host])
1033
+ ),
1034
+ P1 is Port /\ 0xff,
1035
+ P2 is Port >> 8,
1036
+ format(StreamPair, '~s~w~s', [[0x5, % Version 5
1037
+ 0x1, % Please establish a connection
1038
+ 0x0, % reserved
1039
+ AddressType],
1040
+ Address,
1041
+ [P2, P1]]),
1042
+ flush_output(StreamPair),
1043
+ get_byte(StreamPair, _EchoedServerVersion),
1044
+ get_byte(StreamPair, Status),
1045
+ ( Status =:= 0 % Established!
1046
+ -> get_byte(StreamPair, _Reserved),
1047
+ get_byte(StreamPair, EchoedAddressType),
1048
+ ( EchoedAddressType =:= 0x1
1049
+ -> get_byte(StreamPair, _), % read IP4
1050
+ get_byte(StreamPair, _),
1051
+ get_byte(StreamPair, _),
1052
+ get_byte(StreamPair, _)
1053
+ ; get_byte(StreamPair, Length), % read host name
1054
+ forall(between(1, Length, _),
1055
+ get_byte(StreamPair, _))
1056
+ ),
1057
+ get_byte(StreamPair, _), % read port
1058
+ get_byte(StreamPair, _)
1059
+ ; throw(error(socks_error(negotiation_rejected(Status)), _))
1060
+ ).
1061
+
1062
+
1063
+ /*******************************
1064
+ * MESSAGES *
1065
+ *******************************/
1066
+
1067
+ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1068
+ The C-layer generates exceptions of the following format, where Message
1069
+ is extracted from the operating system.
1070
+
1071
+ error(socket_error(Code, Message), _)
1072
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
1073
+
1074
+ :- multifile
1075
+ prolog:error_message//1.
1076
+
1077
+ prolog:error_message(socket_error(_Code, Message)) -->
1078
+ [ 'Socket error: ~w'-[Message] ].
1079
+ prolog:error_message(socks_error(Error)) -->
1080
+ socks_error(Error).
1081
+ prolog:error_message(proxy_error(tried(Tried))) -->
1082
+ [ 'Failed to connect using a proxy. Tried:'-[], nl],
1083
+ proxy_tried(Tried).
1084
+
1085
+ socks_error(invalid_version(Supported, Got)) -->
1086
+ [ 'SOCKS: unsupported version: ~p (supported: ~p)'-
1087
+ [ Got, Supported ] ].
1088
+ socks_error(invalid_authentication_method(Supported, Got)) -->
1089
+ [ 'SOCKS: unsupported authentication method: ~p (supported: ~p)'-
1090
+ [ Got, Supported ] ].
1091
+ socks_error(negotiation_rejected(Status)) -->
1092
+ [ 'SOCKS: connection failed: ~p'-[Status] ].
1093
+
1094
+ proxy_tried([]) --> [].
1095
+ proxy_tried([H|T]) -->
1096
+ proxy_tried(H),
1097
+ proxy_tried(T).
1098
+ proxy_tried(error(Proxy, Error)) -->
1099
+ [ '~w: '-[Proxy] ],
1100
+ '$messages':translate_message(Error).
1101
+ proxy_tried(false(Proxy)) -->
1102
+ [ '~w: failed with unspecified error'-[Proxy] ].