posix_other.nim 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. when defined(nimHasStyleChecks):
  10. {.push styleChecks: off.}
  11. when defined(freertos) or defined(zephyr):
  12. const
  13. hasSpawnH = false # should exist for every Posix system nowadays
  14. hasAioH = false
  15. else:
  16. const
  17. hasSpawnH = true # should exist for every Posix system nowadays
  18. hasAioH = defined(linux)
  19. when defined(linux) and not defined(android):
  20. # On Linux:
  21. # timer_{create,delete,settime,gettime},
  22. # clock_{getcpuclockid, getres, gettime, nanosleep, settime} lives in librt
  23. {.passl: "-lrt".}
  24. when defined(solaris):
  25. # On Solaris hstrerror lives in libresolv
  26. {.passl: "-lresolv".}
  27. type
  28. DIR* {.importc: "DIR", header: "<dirent.h>",
  29. incompleteStruct.} = object
  30. ## A type representing a directory stream.
  31. type
  32. SocketHandle* = distinct cint # The type used to represent socket descriptors
  33. type
  34. Time* {.importc: "time_t", header: "<time.h>".} = distinct (
  35. when defined(nimUse64BitCTime):
  36. int64
  37. else:
  38. clong
  39. )
  40. Timespec* {.importc: "struct timespec",
  41. header: "<time.h>", final, pure.} = object ## struct timespec
  42. tv_sec*: Time ## Seconds.
  43. tv_nsec*: int ## Nanoseconds.
  44. Dirent* {.importc: "struct dirent",
  45. header: "<dirent.h>", final, pure.} = object ## dirent_t struct
  46. when defined(haiku):
  47. d_dev*: Dev ## Device (not POSIX)
  48. d_pdev*: Dev ## Parent device (only for queries) (not POSIX)
  49. d_ino*: Ino ## File serial number.
  50. when defined(dragonfly):
  51. # DragonflyBSD doesn't have `d_reclen` field.
  52. d_type*: uint8
  53. elif defined(linux) or defined(macosx) or defined(freebsd) or
  54. defined(netbsd) or defined(openbsd) or defined(genode):
  55. d_reclen*: cshort ## Length of this record. (not POSIX)
  56. d_type*: int8 ## Type of file; not supported by all filesystem types.
  57. ## (not POSIX)
  58. when defined(linux) or defined(openbsd):
  59. d_off*: Off ## Not an offset. Value that `telldir()` would return.
  60. elif defined(haiku):
  61. d_pino*: Ino ## Parent inode (only for queries) (not POSIX)
  62. d_reclen*: cushort ## Length of this record. (not POSIX)
  63. d_name*: array[0..255, char] ## Name of entry.
  64. Tflock* {.importc: "struct flock", final, pure,
  65. header: "<fcntl.h>".} = object ## flock type
  66. l_type*: cshort ## Type of lock; F_RDLCK, F_WRLCK, F_UNLCK.
  67. l_whence*: cshort ## Flag for starting offset.
  68. l_start*: Off ## Relative offset in bytes.
  69. l_len*: Off ## Size; if 0 then until EOF.
  70. l_pid*: Pid ## Process ID of the process holding the lock;
  71. ## returned with F_GETLK.
  72. FTW* {.importc: "struct FTW", header: "<ftw.h>", final, pure.} = object
  73. base*: cint
  74. level*: cint
  75. Glob* {.importc: "glob_t", header: "<glob.h>",
  76. final, pure.} = object ## glob_t
  77. gl_pathc*: int ## Count of paths matched by pattern.
  78. gl_pathv*: cstringArray ## Pointer to a list of matched pathnames.
  79. gl_offs*: int ## Slots to reserve at the beginning of gl_pathv.
  80. Group* {.importc: "struct group", header: "<grp.h>",
  81. final, pure.} = object ## struct group
  82. gr_name*: cstring ## The name of the group.
  83. gr_gid*: Gid ## Numerical group ID.
  84. gr_mem*: cstringArray ## Pointer to a null-terminated array of character
  85. ## pointers to member names.
  86. Iconv* {.importc: "iconv_t", header: "<iconv.h>", final, pure.} =
  87. object ## Identifies the conversion from one codeset to another.
  88. Lconv* {.importc: "struct lconv", header: "<locale.h>", final,
  89. pure.} = object
  90. currency_symbol*: cstring
  91. decimal_point*: cstring
  92. frac_digits*: char
  93. grouping*: cstring
  94. int_curr_symbol*: cstring
  95. int_frac_digits*: char
  96. int_n_cs_precedes*: char
  97. int_n_sep_by_space*: char
  98. int_n_sign_posn*: char
  99. int_p_cs_precedes*: char
  100. int_p_sep_by_space*: char
  101. int_p_sign_posn*: char
  102. mon_decimal_point*: cstring
  103. mon_grouping*: cstring
  104. mon_thousands_sep*: cstring
  105. negative_sign*: cstring
  106. n_cs_precedes*: char
  107. n_sep_by_space*: char
  108. n_sign_posn*: char
  109. positive_sign*: cstring
  110. p_cs_precedes*: char
  111. p_sep_by_space*: char
  112. p_sign_posn*: char
  113. thousands_sep*: cstring
  114. Mqd* {.importc: "mqd_t", header: "<mqueue.h>", final, pure.} = object
  115. MqAttr* {.importc: "struct mq_attr",
  116. header: "<mqueue.h>",
  117. final, pure.} = object ## message queue attribute
  118. mq_flags*: int ## Message queue flags.
  119. mq_maxmsg*: int ## Maximum number of messages.
  120. mq_msgsize*: int ## Maximum message size.
  121. mq_curmsgs*: int ## Number of messages currently queued.
  122. Passwd* {.importc: "struct passwd", header: "<pwd.h>",
  123. final, pure.} = object ## struct passwd
  124. pw_name*: cstring ## User's login name.
  125. pw_uid*: Uid ## Numerical user ID.
  126. pw_gid*: Gid ## Numerical group ID.
  127. pw_dir*: cstring ## Initial working directory.
  128. pw_shell*: cstring ## Program to use as shell.
  129. Blkcnt* {.importc: "blkcnt_t", header: "<sys/types.h>".} = int
  130. ## used for file block counts
  131. Blksize* {.importc: "blksize_t", header: "<sys/types.h>".} = int
  132. ## used for block sizes
  133. Clock* {.importc: "clock_t", header: "<sys/types.h>".} = int
  134. ClockId* {.importc: "clockid_t", header: "<sys/types.h>".} = int
  135. Dev* {.importc: "dev_t", header: "<sys/types.h>".} = int
  136. Fsblkcnt* {.importc: "fsblkcnt_t", header: "<sys/types.h>".} = int
  137. Fsfilcnt* {.importc: "fsfilcnt_t", header: "<sys/types.h>".} = int
  138. Gid* {.importc: "gid_t", header: "<sys/types.h>".} = int
  139. Id* {.importc: "id_t", header: "<sys/types.h>".} = int
  140. Ino* {.importc: "ino_t", header: "<sys/types.h>".} = int
  141. Key* {.importc: "key_t", header: "<sys/types.h>".} = int
  142. Mode* {.importc: "mode_t", header: "<sys/types.h>".} = (
  143. when defined(android) or defined(macos) or defined(macosx) or
  144. (defined(bsd) and not defined(openbsd) and not defined(netbsd)):
  145. uint16
  146. else:
  147. uint32
  148. )
  149. Nlink* {.importc: "nlink_t", header: "<sys/types.h>".} = int
  150. Off* {.importc: "off_t", header: "<sys/types.h>".} = int64
  151. Pid* {.importc: "pid_t", header: "<sys/types.h>".} = int32
  152. Pthread_attr* {.importc: "pthread_attr_t", header: "<sys/types.h>".} = int
  153. Pthread_barrier* {.importc: "pthread_barrier_t",
  154. header: "<sys/types.h>".} = int
  155. Pthread_barrierattr* {.importc: "pthread_barrierattr_t",
  156. header: "<sys/types.h>".} = int
  157. Pthread_cond* {.importc: "pthread_cond_t", header: "<sys/types.h>".} = int
  158. Pthread_condattr* {.importc: "pthread_condattr_t",
  159. header: "<sys/types.h>".} = int
  160. Pthread_key* {.importc: "pthread_key_t", header: "<sys/types.h>".} = int
  161. Pthread_mutex* {.importc: "pthread_mutex_t", header: "<sys/types.h>".} = int
  162. Pthread_mutexattr* {.importc: "pthread_mutexattr_t",
  163. header: "<sys/types.h>".} = int
  164. Pthread_once* {.importc: "pthread_once_t", header: "<sys/types.h>".} = int
  165. Pthread_rwlock* {.importc: "pthread_rwlock_t",
  166. header: "<sys/types.h>".} = int
  167. Pthread_rwlockattr* {.importc: "pthread_rwlockattr_t",
  168. header: "<sys/types.h>".} = int
  169. Pthread_spinlock* {.importc: "pthread_spinlock_t",
  170. header: "<sys/types.h>".} = int
  171. Pthread* {.importc: "pthread_t", header: "<sys/types.h>".} = int
  172. Suseconds* {.importc: "suseconds_t", header: "<sys/types.h>".} = int
  173. #Ttime* {.importc: "time_t", header: "<sys/types.h>".} = int
  174. Timer* {.importc: "timer_t", header: "<sys/types.h>".} = int
  175. Trace_attr* {.importc: "trace_attr_t", header: "<sys/types.h>".} = int
  176. Trace_event_id* {.importc: "trace_event_id_t",
  177. header: "<sys/types.h>".} = int
  178. Trace_event_set* {.importc: "trace_event_set_t",
  179. header: "<sys/types.h>".} = int
  180. Trace_id* {.importc: "trace_id_t", header: "<sys/types.h>".} = int
  181. Uid* {.importc: "uid_t", header: "<sys/types.h>".} = int
  182. Useconds* {.importc: "useconds_t", header: "<sys/types.h>".} = int
  183. Utsname* {.importc: "struct utsname",
  184. header: "<sys/utsname.h>",
  185. final, pure.} = object ## struct utsname
  186. sysname*, ## Name of this implementation of the operating system.
  187. nodename*, ## Name of this node within the communications
  188. ## network to which this node is attached, if any.
  189. release*, ## Current release level of this implementation.
  190. version*, ## Current version level of this release.
  191. machine*: array[0..255, char] ## Name of the hardware type on which the
  192. ## system is running.
  193. Sem* {.importc: "sem_t", header: "<semaphore.h>", final, pure.} = object
  194. Ipc_perm* {.importc: "struct ipc_perm",
  195. header: "<sys/ipc.h>", final, pure.} = object ## struct ipc_perm
  196. uid*: Uid ## Owner's user ID.
  197. gid*: Gid ## Owner's group ID.
  198. cuid*: Uid ## Creator's user ID.
  199. cgid*: Gid ## Creator's group ID.
  200. mode*: Mode ## Read/write permission.
  201. Stat* {.importc: "struct stat",
  202. header: "<sys/stat.h>", final, pure.} = object ## struct stat
  203. st_dev*: Dev ## Device ID of device containing file.
  204. st_ino*: Ino ## File serial number.
  205. st_mode*: Mode ## Mode of file (see below).
  206. st_nlink*: Nlink ## Number of hard links to the file.
  207. st_uid*: Uid ## User ID of file.
  208. st_gid*: Gid ## Group ID of file.
  209. st_rdev*: Dev ## Device ID (if file is character or block special).
  210. st_size*: Off ## For regular files, the file size in bytes.
  211. ## For symbolic links, the length in bytes of the
  212. ## pathname contained in the symbolic link.
  213. ## For a shared memory object, the length in bytes.
  214. ## For a typed memory object, the length in bytes.
  215. ## For other file types, the use of this field is
  216. ## unspecified.
  217. when StatHasNanoseconds:
  218. st_atim*: Timespec ## Time of last access.
  219. st_mtim*: Timespec ## Time of last data modification.
  220. st_ctim*: Timespec ## Time of last status change.
  221. else:
  222. st_atime*: Time ## Time of last access.
  223. st_mtime*: Time ## Time of last data modification.
  224. st_ctime*: Time ## Time of last status change.
  225. st_blksize*: Blksize ## A file system-specific preferred I/O block size
  226. ## for this object. In some file system types, this
  227. ## may vary from file to file.
  228. st_blocks*: Blkcnt ## Number of blocks allocated for this object.
  229. Statvfs* {.importc: "struct statvfs", header: "<sys/statvfs.h>",
  230. final, pure.} = object ## struct statvfs
  231. f_bsize*: int ## File system block size.
  232. f_frsize*: int ## Fundamental file system block size.
  233. f_blocks*: Fsblkcnt ## Total number of blocks on file system
  234. ## in units of f_frsize.
  235. f_bfree*: Fsblkcnt ## Total number of free blocks.
  236. f_bavail*: Fsblkcnt ## Number of free blocks available to
  237. ## non-privileged process.
  238. f_files*: Fsfilcnt ## Total number of file serial numbers.
  239. f_ffree*: Fsfilcnt ## Total number of free file serial numbers.
  240. f_favail*: Fsfilcnt ## Number of file serial numbers available to
  241. ## non-privileged process.
  242. f_fsid*: int ## File system ID.
  243. f_flag*: int ## Bit mask of f_flag values.
  244. f_namemax*: int ## Maximum filename length.
  245. Posix_typed_mem_info* {.importc: "struct posix_typed_mem_info",
  246. header: "<sys/mman.h>", final, pure.} = object
  247. posix_tmi_length*: int
  248. Tm* {.importc: "struct tm", header: "<time.h>",
  249. final, pure.} = object ## struct tm
  250. tm_sec*: cint ## Seconds [0,60].
  251. tm_min*: cint ## Minutes [0,59].
  252. tm_hour*: cint ## Hour [0,23].
  253. tm_mday*: cint ## Day of month [1,31].
  254. tm_mon*: cint ## Month of year [0,11].
  255. tm_year*: cint ## Years since 1900.
  256. tm_wday*: cint ## Day of week [0,6] (Sunday =0).
  257. tm_yday*: cint ## Day of year [0,365].
  258. tm_isdst*: cint ## Daylight Savings flag.
  259. Itimerspec* {.importc: "struct itimerspec", header: "<time.h>",
  260. final, pure.} = object ## struct itimerspec
  261. it_interval*: Timespec ## Timer period.
  262. it_value*: Timespec ## Timer expiration.
  263. Sig_atomic* {.importc: "sig_atomic_t", header: "<signal.h>".} = cint
  264. ## Possibly volatile-qualified integer type of an object that can be
  265. ## accessed as an atomic entity, even in the presence of asynchronous
  266. ## interrupts.
  267. Sigset* {.importc: "sigset_t", header: "<signal.h>", final, pure.} = object
  268. SigEvent* {.importc: "struct sigevent",
  269. header: "<signal.h>", final, pure.} = object ## struct sigevent
  270. sigev_notify*: cint ## Notification type.
  271. sigev_signo*: cint ## Signal number.
  272. sigev_value*: SigVal ## Signal value.
  273. sigev_notify_function*: proc (x: SigVal) {.noconv.} ## Notification func.
  274. sigev_notify_attributes*: ptr Pthread_attr ## Notification attributes.
  275. SigVal* {.importc: "union sigval",
  276. header: "<signal.h>", final, pure.} = object ## struct sigval
  277. sival_ptr*: pointer ## pointer signal value;
  278. ## integer signal value not defined!
  279. Sigaction* {.importc: "struct sigaction",
  280. header: "<signal.h>", final, pure.} = object ## struct sigaction
  281. sa_handler*: proc (x: cint) {.noconv.} ## Pointer to a signal-catching
  282. ## function or one of the macros
  283. ## SIG_IGN or SIG_DFL.
  284. sa_mask*: Sigset ## Set of signals to be blocked during execution of
  285. ## the signal handling function.
  286. sa_flags*: cint ## Special flags.
  287. sa_sigaction*: proc (x: cint, y: ptr SigInfo, z: pointer) {.noconv.}
  288. Stack* {.importc: "stack_t",
  289. header: "<signal.h>", final, pure.} = object ## stack_t
  290. ss_sp*: pointer ## Stack base or pointer.
  291. ss_size*: int ## Stack size.
  292. ss_flags*: cint ## Flags.
  293. SigStack* {.importc: "struct sigstack",
  294. header: "<signal.h>", final, pure.} = object ## struct sigstack
  295. ss_onstack*: cint ## Non-zero when signal stack is in use.
  296. ss_sp*: pointer ## Signal stack pointer.
  297. SigInfo* {.importc: "siginfo_t",
  298. header: "<signal.h>", final, pure.} = object ## siginfo_t
  299. si_signo*: cint ## Signal number.
  300. si_code*: cint ## Signal code.
  301. si_errno*: cint ## If non-zero, an errno value associated with
  302. ## this signal, as defined in <errno.h>.
  303. si_pid*: Pid ## Sending process ID.
  304. si_uid*: Uid ## Real user ID of sending process.
  305. si_addr*: pointer ## Address of faulting instruction.
  306. si_status*: cint ## Exit value or signal.
  307. si_band*: int ## Band event for SIGPOLL.
  308. si_value*: SigVal ## Signal value.
  309. Nl_item* {.importc: "nl_item", header: "<nl_types.h>".} = cint
  310. Nl_catd* {.importc: "nl_catd", header: "<nl_types.h>".} = cint
  311. Sched_param* {.importc: "struct sched_param",
  312. header: "<sched.h>",
  313. final, pure.} = object ## struct sched_param
  314. sched_priority*: cint
  315. sched_ss_low_priority*: cint ## Low scheduling priority for
  316. ## sporadic server.
  317. sched_ss_repl_period*: Timespec ## Replenishment period for
  318. ## sporadic server.
  319. sched_ss_init_budget*: Timespec ## Initial budget for sporadic server.
  320. sched_ss_max_repl*: cint ## Maximum pending replenishments for
  321. ## sporadic server.
  322. Timeval* {.importc: "struct timeval", header: "<sys/select.h>",
  323. final, pure.} = object ## struct timeval
  324. tv_sec*: Time ## Seconds.
  325. tv_usec*: Suseconds ## Microseconds.
  326. TFdSet* {.importc: "fd_set", header: "<sys/select.h>",
  327. final, pure.} = object
  328. Mcontext* {.importc: "mcontext_t", header: "<ucontext.h>",
  329. final, pure.} = object
  330. Ucontext* {.importc: "ucontext_t", header: "<ucontext.h>",
  331. final, pure.} = object ## ucontext_t
  332. uc_link*: ptr Ucontext ## Pointer to the context that is resumed
  333. ## when this context returns.
  334. uc_sigmask*: Sigset ## The set of signals that are blocked when this
  335. ## context is active.
  336. uc_stack*: Stack ## The stack used by this context.
  337. uc_mcontext*: Mcontext ## A machine-specific representation of the saved
  338. ## context.
  339. when hasAioH:
  340. type
  341. Taiocb* {.importc: "struct aiocb", header: "<aio.h>",
  342. final, pure.} = object ## struct aiocb
  343. aio_fildes*: cint ## File descriptor.
  344. aio_offset*: Off ## File offset.
  345. aio_buf*: pointer ## Location of buffer.
  346. aio_nbytes*: int ## Length of transfer.
  347. aio_reqprio*: cint ## Request priority offset.
  348. aio_sigevent*: SigEvent ## Signal number and value.
  349. aio_lio_opcode: cint ## Operation to be performed.
  350. when hasSpawnH:
  351. type
  352. Tposix_spawnattr* {.importc: "posix_spawnattr_t",
  353. header: "<spawn.h>", final, pure.} = object
  354. Tposix_spawn_file_actions* {.importc: "posix_spawn_file_actions_t",
  355. header: "<spawn.h>", final, pure.} = object
  356. when defined(linux):
  357. const Sockaddr_max_length* = 255
  358. # from sys/un.h
  359. const Sockaddr_un_path_length* = 108
  360. elif defined(zephyr):
  361. when defined(net_ipv6):
  362. const Sockaddr_max_length* = 24
  363. elif defined(net_raw):
  364. const Sockaddr_max_length* = 20
  365. elif defined(net_ipv4):
  366. const Sockaddr_max_length* = 8
  367. else:
  368. const Sockaddr_max_length* = 255 # just for compilation purposes
  369. const Sockaddr_un_path_length* = Sockaddr_max_length
  370. # Zephyr is heavily customizable so it's easy to get to a state
  371. # where Nim & Zephyr IPv6 settings are out of sync, causing painful runtime failures.
  372. when defined(net_ipv4) or defined(net_ipv6) or defined(net_raw):
  373. {.emit: ["NIM_STATIC_ASSERT(NET_SOCKADDR_MAX_SIZE == ",
  374. Sockaddr_max_length,
  375. ",\"NET_SOCKADDR_MAX_SIZE and Sockaddr_max_length size mismatch!",
  376. " Check that Nim and Zephyr IPv4/IPv6 settings match.",
  377. " Try adding -d:net_ipv6 to enable IPv6 for Nim on Zephyr.\" );"].}
  378. elif defined(freertos) or defined(lwip):
  379. const Sockaddr_max_length* = 14
  380. const Sockaddr_un_path_length* = 108
  381. else:
  382. const Sockaddr_max_length* = 255
  383. # according to http://pubs.opengroup.org/onlinepubs/009604499/basedefs/sys/un.h.html
  384. # this is >=92
  385. const Sockaddr_un_path_length* = 92
  386. type
  387. SockLen* {.importc: "socklen_t", header: "<sys/socket.h>".} = cuint
  388. TSa_Family* {.importc: "sa_family_t", header: "<sys/socket.h>".} = cushort
  389. when defined(lwip):
  390. type
  391. SockAddr* {.importc: "struct sockaddr", header: "<sys/socket.h>",
  392. pure, final.} = object ## struct sockaddr
  393. sa_len*: uint8 ## Address family.
  394. sa_family*: TSa_Family ## Address family.
  395. sa_data*: array[0..Sockaddr_max_length-sizeof(uint8)-sizeof(TSa_Family), char] ## Socket address (variable-length data).
  396. Sockaddr_storage* {.importc: "struct sockaddr_storage",
  397. header: "<sys/socket.h>",
  398. pure, final.} = object ## struct sockaddr_storage
  399. s2_len*: uint8 ## Address family.
  400. ss_family*: TSa_Family ## Address family.
  401. s2_data1*: array[2, char] ## Address family.
  402. s2_data2*: array[3, uint32] ## Address family.
  403. when defined(lwip6) or defined(net_ipv6):
  404. s2_data3*: array[3, uint32] ## Address family.
  405. elif defined(zephyr):
  406. type
  407. SockAddr* {.importc: "struct sockaddr", header: "<sys/socket.h>",
  408. pure, final.} = object ## struct sockaddr
  409. sa_family*: TSa_Family ## Address family.
  410. data*: array[0..Sockaddr_max_length-sizeof(TSa_Family), char] ## Socket address (variable-length data).
  411. Sockaddr_storage* {.importc: "struct sockaddr_storage",
  412. header: "<sys/socket.h>",
  413. pure, final.} = object ## struct sockaddr_storage
  414. ss_family*: TSa_Family ## Address family.
  415. data*: array[0..Sockaddr_max_length-sizeof(TSa_Family), char] ## Socket address (variable-length data).
  416. {.emit: ["NIM_STATIC_ASSERT(sizeof(struct sockaddr) == ", sizeof(Sockaddr), ",\"struct size mismatch\" );"].}
  417. {.emit: ["NIM_STATIC_ASSERT(sizeof(struct sockaddr_storage) == ", sizeof(Sockaddr_storage), ",\"struct size mismatch\" );"].}
  418. else:
  419. type
  420. SockAddr* {.importc: "struct sockaddr", header: "<sys/socket.h>",
  421. pure, final.} = object ## struct sockaddr
  422. sa_family*: TSa_Family ## Address family.
  423. sa_data*: array[0..Sockaddr_max_length-sizeof(TSa_Family), char] ## Socket address (variable-length data).
  424. Sockaddr_storage* {.importc: "struct sockaddr_storage",
  425. header: "<sys/socket.h>",
  426. pure, final.} = object ## struct sockaddr_storage
  427. ss_family*: TSa_Family ## Address family.
  428. type
  429. Sockaddr_un* {.importc: "struct sockaddr_un", header: "<sys/un.h>",
  430. pure, final.} = object ## struct sockaddr_un
  431. sun_family*: TSa_Family ## Address family.
  432. sun_path*: array[0..Sockaddr_un_path_length-sizeof(TSa_Family), char] ## Socket path
  433. type
  434. Tif_nameindex* {.importc: "struct if_nameindex", final,
  435. pure, header: "<net/if.h>".} = object ## struct if_nameindex
  436. if_index*: cint ## Numeric index of the interface.
  437. if_name*: cstring ## Null-terminated name of the interface.
  438. IOVec* {.importc: "struct iovec", pure, final,
  439. header: "<sys/uio.h>".} = object ## struct iovec
  440. iov_base*: pointer ## Base address of a memory region for input or output.
  441. iov_len*: csize_t ## The size of the memory pointed to by iov_base.
  442. Tmsghdr* {.importc: "struct msghdr", pure, final,
  443. header: "<sys/socket.h>".} = object ## struct msghdr
  444. msg_name*: pointer ## Optional address.
  445. msg_namelen*: SockLen ## Size of address.
  446. msg_iov*: ptr IOVec ## Scatter/gather array.
  447. msg_iovlen*: cint ## Members in msg_iov.
  448. msg_control*: pointer ## Ancillary data; see below.
  449. msg_controllen*: SockLen ## Ancillary data buffer len.
  450. msg_flags*: cint ## Flags on received message.
  451. Tcmsghdr* {.importc: "struct cmsghdr", pure, final,
  452. header: "<sys/socket.h>".} = object ## struct cmsghdr
  453. cmsg_len*: SockLen ## Data byte count, including the cmsghdr.
  454. cmsg_level*: cint ## Originating protocol.
  455. cmsg_type*: cint ## Protocol-specific type.
  456. TLinger* {.importc: "struct linger", pure, final,
  457. header: "<sys/socket.h>".} = object ## struct linger
  458. l_onoff*: cint ## Indicates whether linger option is enabled.
  459. l_linger*: cint ## Linger time, in seconds.
  460. InPort* = uint16
  461. InAddrScalar* = uint32
  462. InAddrT* {.importc: "in_addr_t", pure, final,
  463. header: "<netinet/in.h>".} = uint32
  464. InAddr* {.importc: "struct in_addr", pure, final,
  465. header: "<netinet/in.h>".} = object ## struct in_addr
  466. s_addr*: InAddrScalar
  467. # TODO: Fixme for FreeRTOS/LwIP, these are incorrect
  468. Sockaddr_in* {.importc: "struct sockaddr_in", pure, final,
  469. header: "<netinet/in.h>".} = object ## struct sockaddr_in
  470. sin_family*: TSa_Family ## AF_INET.
  471. sin_port*: InPort ## Port number.
  472. sin_addr*: InAddr ## IP address.
  473. In6Addr* {.importc: "struct in6_addr", pure, final,
  474. header: "<netinet/in.h>".} = object ## struct in6_addr
  475. s6_addr*: array[0..15, char]
  476. Sockaddr_in6* {.importc: "struct sockaddr_in6", pure, final,
  477. header: "<netinet/in.h>".} = object ## struct sockaddr_in6
  478. sin6_family*: TSa_Family ## AF_INET6.
  479. sin6_port*: InPort ## Port number.
  480. sin6_flowinfo*: int32 ## IPv6 traffic class and flow information.
  481. sin6_addr*: In6Addr ## IPv6 address.
  482. sin6_scope_id*: int32 ## Set of interfaces for a scope.
  483. Tipv6_mreq* {.importc: "struct ipv6_mreq", pure, final,
  484. header: "<netinet/in.h>".} = object ## struct ipv6_mreq
  485. ipv6mr_multiaddr*: In6Addr ## IPv6 multicast address.
  486. ipv6mr_interface*: cint ## Interface index.
  487. Hostent* {.importc: "struct hostent", pure, final,
  488. header: "<netdb.h>".} = object ## struct hostent
  489. h_name*: cstring ## Official name of the host.
  490. h_aliases*: cstringArray ## A pointer to an array of pointers to
  491. ## alternative host names, terminated by a
  492. ## null pointer.
  493. h_addrtype*: cint ## Address type.
  494. h_length*: cint ## The length, in bytes, of the address.
  495. h_addr_list*: cstringArray ## A pointer to an array of pointers to network
  496. ## addresses (in network byte order) for the
  497. ## host, terminated by a null pointer.
  498. Tnetent* {.importc: "struct netent", pure, final,
  499. header: "<netdb.h>".} = object ## struct netent
  500. n_name*: cstring ## Official, fully-qualified (including the
  501. ## domain) name of the host.
  502. n_aliases*: cstringArray ## A pointer to an array of pointers to
  503. ## alternative network names, terminated by a
  504. ## null pointer.
  505. n_addrtype*: cint ## The address type of the network.
  506. n_net*: int32 ## The network number, in host byte order.
  507. Protoent* {.importc: "struct protoent", pure, final,
  508. header: "<netdb.h>".} = object ## struct protoent
  509. p_name*: cstring ## Official name of the protocol.
  510. p_aliases*: cstringArray ## A pointer to an array of pointers to
  511. ## alternative protocol names, terminated by
  512. ## a null pointer.
  513. p_proto*: cint ## The protocol number.
  514. Servent* {.importc: "struct servent", pure, final,
  515. header: "<netdb.h>".} = object ## struct servent
  516. s_name*: cstring ## Official name of the service.
  517. s_aliases*: cstringArray ## A pointer to an array of pointers to
  518. ## alternative service names, terminated by
  519. ## a null pointer.
  520. s_port*: cint ## The port number at which the service
  521. ## resides, in network byte order.
  522. s_proto*: cstring ## The name of the protocol to use when
  523. ## contacting the service.
  524. AddrInfo* {.importc: "struct addrinfo", pure, final,
  525. header: "<netdb.h>".} = object ## struct addrinfo
  526. ai_flags*: cint ## Input flags.
  527. ai_family*: cint ## Address family of socket.
  528. ai_socktype*: cint ## Socket type.
  529. ai_protocol*: cint ## Protocol of socket.
  530. ai_addrlen*: SockLen ## Length of socket address.
  531. ai_addr*: ptr SockAddr ## Socket address of socket.
  532. ai_canonname*: cstring ## Canonical name of service location.
  533. ai_next*: ptr AddrInfo ## Pointer to next in list.
  534. when not defined(lwip):
  535. type
  536. TPollfd* {.importc: "struct pollfd", pure, final,
  537. header: "<poll.h>".} = object ## struct pollfd
  538. fd*: cint ## The following descriptor being polled.
  539. events*: cshort ## The input event flags (see below).
  540. revents*: cshort ## The output event flags (see below).
  541. when defined(zephyr):
  542. type
  543. Tnfds* = distinct cint
  544. else:
  545. type
  546. Tnfds* {.importc: "nfds_t", header: "<poll.h>".} = cint
  547. var
  548. errno* {.importc, header: "<errno.h>".}: cint ## error variable
  549. h_errno* {.importc, header: "<netdb.h>".}: cint
  550. daylight* {.importc, header: "<time.h>".}: cint
  551. timezone* {.importc, header: "<time.h>".}: int
  552. # Regenerate using detect.nim!
  553. when defined(lwip):
  554. include posix_freertos_consts
  555. else:
  556. include posix_other_consts
  557. when defined(linux):
  558. var
  559. MAP_POPULATE* {.importc, header: "<sys/mman.h>".}: cint
  560. ## Populate (prefault) page tables for a mapping.
  561. else:
  562. var
  563. MAP_POPULATE*: cint = 0
  564. when defined(linux) or defined(nimdoc):
  565. when defined(alpha) or defined(mips) or defined(mipsel) or
  566. defined(mips64) or defined(mips64el) or defined(parisc) or
  567. defined(sparc) or defined(sparc64) or defined(nimdoc):
  568. const SO_REUSEPORT* = cint(0x0200)
  569. ## Multiple binding: load balancing on incoming TCP connections
  570. ## or UDP packets. (Requires Linux kernel > 3.9)
  571. else:
  572. const SO_REUSEPORT* = cint(15)
  573. elif defined(nuttx):
  574. # Not supported, use SO_REUSEADDR to avoid compilation errors.
  575. var SO_REUSEPORT* {.importc: "SO_REUSEADDR", header: "<sys/socket.h>".}: cint
  576. else:
  577. var SO_REUSEPORT* {.importc, header: "<sys/socket.h>".}: cint
  578. when defined(linux) or defined(bsd) or defined(nuttx):
  579. var SOCK_CLOEXEC* {.importc, header: "<sys/socket.h>".}: cint
  580. when defined(macosx):
  581. # We can't use the NOSIGNAL flag in the `send` function, it has no effect
  582. # Instead we should use SO_NOSIGPIPE in setsockopt
  583. const
  584. MSG_NOSIGNAL* = 0'i32
  585. var
  586. SO_NOSIGPIPE* {.importc, header: "<sys/socket.h>".}: cint
  587. elif defined(solaris):
  588. # Solaris doesn't have MSG_NOSIGNAL
  589. const
  590. MSG_NOSIGNAL* = 0'i32
  591. elif defined(zephyr) or defined(freertos) or defined(lwip):
  592. # LwIP/FreeRTOS doesn't have MSG_NOSIGNAL
  593. const
  594. MSG_NOSIGNAL* = 0x20'i32
  595. else:
  596. var
  597. MSG_NOSIGNAL* {.importc, header: "<sys/socket.h>".}: cint
  598. ## No SIGPIPE generated when an attempt to send is made on a stream-oriented socket that is no longer connected.
  599. when defined(haiku):
  600. const
  601. SIGKILLTHR* = 21 ## BeOS specific: Kill just the thread, not team
  602. when hasSpawnH:
  603. when defined(linux):
  604. # better be safe than sorry; Linux has this flag, macosx and NuttX don't,
  605. # don't know about the other OSes
  606. # Non-GNU systems like TCC and musl-libc don't define __USE_GNU, so we
  607. # can't get the magic number from spawn.h
  608. const POSIX_SPAWN_USEVFORK* = cint(0x40)
  609. else:
  610. # macosx and NuttX lack this, so we define the constant to be 0 to not affect
  611. # OR'ing of flags:
  612. const POSIX_SPAWN_USEVFORK* = cint(0)
  613. # <sys/wait.h>
  614. proc WEXITSTATUS*(s: cint): cint {.importc, header: "<sys/wait.h>".}
  615. ## Exit code, if WIFEXITED(s)
  616. proc WTERMSIG*(s: cint): cint {.importc, header: "<sys/wait.h>".}
  617. ## Termination signal, if WIFSIGNALED(s)
  618. proc WSTOPSIG*(s: cint): cint {.importc, header: "<sys/wait.h>".}
  619. ## Stop signal, if WIFSTOPPED(s)
  620. proc WIFEXITED*(s: cint): bool {.importc, header: "<sys/wait.h>".}
  621. ## True if child exited normally.
  622. proc WIFSIGNALED*(s: cint): bool {.importc, header: "<sys/wait.h>".}
  623. ## True if child exited due to uncaught signal.
  624. proc WIFSTOPPED*(s: cint): bool {.importc, header: "<sys/wait.h>".}
  625. ## True if child is currently stopped.
  626. proc WIFCONTINUED*(s: cint): bool {.importc, header: "<sys/wait.h>".}
  627. ## True if child has been continued.
  628. when defined(nimHasStyleChecks):
  629. {.pop.}