master
  1const builtin = @import("builtin");
  2const std = @import("../std.zig");
  3const assert = std.debug.assert;
  4
  5const PATH_MAX = std.c.PATH_MAX;
  6const blkcnt_t = std.c.blkcnt_t;
  7const blksize_t = std.c.blksize_t;
  8const caddr_t = std.c.caddr_t;
  9const dev_t = std.c.dev_t;
 10const fd_t = std.c.fd_t;
 11const gid_t = std.c.gid_t;
 12const ino_t = std.c.ino_t;
 13const iovec_const = std.posix.iovec_const;
 14const mode_t = std.c.mode_t;
 15const nlink_t = std.c.nlink_t;
 16const off_t = std.c.off_t;
 17const pid_t = std.c.pid_t;
 18const sockaddr = std.c.sockaddr;
 19const time_t = std.c.time_t;
 20const timespec = std.c.timespec;
 21const uid_t = std.c.uid_t;
 22const sf_hdtr = std.c.sf_hdtr;
 23const clockid_t = std.c.clockid_t;
 24
 25comptime {
 26    assert(builtin.os.tag == .freebsd); // Prevent access of std.c symbols on wrong OS.
 27}
 28
 29pub extern "c" fn ptrace(request: c_int, pid: pid_t, addr: caddr_t, data: c_int) c_int;
 30
 31pub extern "c" fn kinfo_getfile(pid: pid_t, cntp: *c_int) ?[*]kinfo_file;
 32pub extern "c" fn copy_file_range(fd_in: fd_t, off_in: ?*off_t, fd_out: fd_t, off_out: ?*off_t, len: usize, flags: u32) isize;
 33
 34pub extern "c" fn sendfile(
 35    in_fd: fd_t,
 36    out_fd: fd_t,
 37    offset: off_t,
 38    nbytes: usize,
 39    sf_hdtr: ?*sf_hdtr,
 40    sbytes: ?*off_t,
 41    flags: u32,
 42) c_int;
 43
 44pub const UMTX_OP = enum(c_int) {
 45    LOCK = 0,
 46    UNLOCK = 1,
 47    WAIT = 2,
 48    WAKE = 3,
 49    MUTEX_TRYLOCK = 4,
 50    MUTEX_LOCK = 5,
 51    MUTEX_UNLOCK = 6,
 52    SET_CEILING = 7,
 53    CV_WAIT = 8,
 54    CV_SIGNAL = 9,
 55    CV_BROADCAST = 10,
 56    WAIT_UINT = 11,
 57    RW_RDLOCK = 12,
 58    RW_WRLOCK = 13,
 59    RW_UNLOCK = 14,
 60    WAIT_UINT_PRIVATE = 15,
 61    WAKE_PRIVATE = 16,
 62    MUTEX_WAIT = 17,
 63    MUTEX_WAKE = 18, // deprecated
 64    SEM_WAIT = 19, // deprecated
 65    SEM_WAKE = 20, // deprecated
 66    NWAKE_PRIVATE = 31,
 67    MUTEX_WAKE2 = 22,
 68    SEM2_WAIT = 23,
 69    SEM2_WAKE = 24,
 70    SHM = 25,
 71    ROBUST_LISTS = 26,
 72};
 73
 74pub const UMTX_ABSTIME = 0x01;
 75pub const _umtx_time = extern struct {
 76    timeout: timespec,
 77    flags: u32,
 78    clockid: clockid_t,
 79};
 80
 81pub extern "c" fn _umtx_op(obj: usize, op: c_int, val: c_ulong, uaddr: usize, uaddr2: usize) c_int;
 82
 83pub const fflags_t = u32;
 84
 85pub const Stat = extern struct {
 86    /// The inode's device.
 87    dev: dev_t,
 88    /// The inode's number.
 89    ino: ino_t,
 90    /// Number of hard links.
 91    nlink: nlink_t,
 92    /// Inode protection mode.
 93    mode: mode_t,
 94    __pad0: i16,
 95    /// User ID of the file's owner.
 96    uid: uid_t,
 97    /// Group ID of the file's group.
 98    gid: gid_t,
 99    __pad1: i32,
100    /// Device type.
101    rdev: dev_t,
102    /// Time of last access.
103    atim: timespec,
104    /// Time of last data modification.
105    mtim: timespec,
106    /// Time of last file status change.
107    ctim: timespec,
108    /// Time of file creation.
109    birthtim: timespec,
110    /// File size, in bytes.
111    size: off_t,
112    /// Blocks allocated for file.
113    blocks: blkcnt_t,
114    /// Optimal blocksize for I/O.
115    blksize: blksize_t,
116    /// User defined flags for file.
117    flags: fflags_t,
118    /// File generation number.
119    gen: u64,
120    __spare: [10]u64,
121
122    pub fn atime(self: @This()) timespec {
123        return self.atim;
124    }
125
126    pub fn mtime(self: @This()) timespec {
127        return self.mtim;
128    }
129
130    pub fn ctime(self: @This()) timespec {
131        return self.ctim;
132    }
133
134    pub fn birthtime(self: @This()) timespec {
135        return self.birthtim;
136    }
137};
138
139pub const fsblkcnt_t = u64;
140pub const fsfilcnt_t = u64;
141
142pub const CAP_RIGHTS_VERSION = 0;
143
144pub const cap_rights = extern struct {
145    rights: [CAP_RIGHTS_VERSION + 2]u64,
146};
147
148pub const kinfo_file = extern struct {
149    /// Size of this record.
150    /// A zero value is for the sentinel record at the end of an array.
151    structsize: c_int,
152    /// Descriptor type.
153    type: c_int,
154    /// Array index.
155    fd: fd_t,
156    /// Reference count.
157    ref_count: c_int,
158    /// Flags.
159    flags: c_int,
160    // 64bit padding.
161    _pad0: c_int,
162    /// Seek location.
163    offset: i64,
164    un: extern union {
165        socket: extern struct {
166            /// Sendq size.
167            sendq: u32,
168            /// Socket domain.
169            domain: c_int,
170            /// Socket type.
171            type: c_int,
172            /// Socket protocol.
173            protocol: c_int,
174            /// Socket address.
175            address: sockaddr.storage,
176            /// Peer address.
177            peer: sockaddr.storage,
178            /// Address of so_pcb.
179            pcb: u64,
180            /// Address of inp_ppcb.
181            inpcb: u64,
182            /// Address of unp_conn.
183            unpconn: u64,
184            /// Send buffer state.
185            snd_sb_state: u16,
186            /// Receive buffer state.
187            rcv_sb_state: u16,
188            /// Recvq size.
189            recvq: u32,
190        },
191        file: extern struct {
192            /// Vnode type.
193            type: i32,
194            // Reserved for future use
195            _spare1: [3]i32,
196            _spare2: [30]u64,
197            /// Vnode filesystem id.
198            fsid: u64,
199            /// File device.
200            rdev: u64,
201            /// Global file id.
202            fileid: u64,
203            /// File size.
204            size: u64,
205            /// fsid compat for FreeBSD 11.
206            fsid_freebsd11: u32,
207            /// rdev compat for FreeBSD 11.
208            rdev_freebsd11: u32,
209            /// File mode.
210            mode: u16,
211            // 64bit padding.
212            _pad0: u16,
213            _pad1: u32,
214        },
215        sem: extern struct {
216            _spare0: [4]u32,
217            _spare1: [32]u64,
218            /// Semaphore value.
219            value: u32,
220            /// Semaphore mode.
221            mode: u16,
222        },
223        pipe: extern struct {
224            _spare1: [4]u32,
225            _spare2: [32]u64,
226            addr: u64,
227            peer: u64,
228            buffer_cnt: u32,
229            // 64bit padding.
230            kf_pipe_pad0: [3]u32,
231        },
232        proc: extern struct {
233            _spare1: [4]u32,
234            _spare2: [32]u64,
235            pid: pid_t,
236        },
237        eventfd: extern struct {
238            value: u64,
239            flags: u32,
240        },
241    },
242    /// Status flags.
243    status: u16,
244    // 32-bit alignment padding.
245    _pad1: u16,
246    // Reserved for future use.
247    _spare: c_int,
248    /// Capability rights.
249    cap_rights: cap_rights,
250    /// Reserved for future cap_rights
251    _cap_spare: u64,
252    /// Path to file, if any.
253    path: [PATH_MAX - 1:0]u8,
254
255    comptime {
256        assert(@sizeOf(@This()) == KINFO_FILE_SIZE);
257        assert(@alignOf(@This()) == @sizeOf(u64));
258    }
259};
260
261pub const KINFO_FILE_SIZE = 1392;
262
263pub const MFD = struct {
264    pub const CLOEXEC = 0x0001;
265    pub const ALLOW_SEALING = 0x0002;
266};
267
268pub const E = enum(u16) {
269    /// No error occurred.
270    SUCCESS = 0,
271
272    PERM = 1, // Operation not permitted
273    NOENT = 2, // No such file or directory
274    SRCH = 3, // No such process
275    INTR = 4, // Interrupted system call
276    IO = 5, // Input/output error
277    NXIO = 6, // Device not configured
278    @"2BIG" = 7, // Argument list too long
279    NOEXEC = 8, // Exec format error
280    BADF = 9, // Bad file descriptor
281    CHILD = 10, // No child processes
282    DEADLK = 11, // Resource deadlock avoided
283    // 11 was AGAIN
284    NOMEM = 12, // Cannot allocate memory
285    ACCES = 13, // Permission denied
286    FAULT = 14, // Bad address
287    NOTBLK = 15, // Block device required
288    BUSY = 16, // Device busy
289    EXIST = 17, // File exists
290    XDEV = 18, // Cross-device link
291    NODEV = 19, // Operation not supported by device
292    NOTDIR = 20, // Not a directory
293    ISDIR = 21, // Is a directory
294    INVAL = 22, // Invalid argument
295    NFILE = 23, // Too many open files in system
296    MFILE = 24, // Too many open files
297    NOTTY = 25, // Inappropriate ioctl for device
298    TXTBSY = 26, // Text file busy
299    FBIG = 27, // File too large
300    NOSPC = 28, // No space left on device
301    SPIPE = 29, // Illegal seek
302    ROFS = 30, // Read-only filesystem
303    MLINK = 31, // Too many links
304    PIPE = 32, // Broken pipe
305
306    // math software
307    DOM = 33, // Numerical argument out of domain
308    RANGE = 34, // Result too large
309
310    // non-blocking and interrupt i/o
311
312    /// Resource temporarily unavailable
313    /// This code is also used for `WOULDBLOCK`: operation would block.
314    AGAIN = 35,
315    INPROGRESS = 36, // Operation now in progress
316    ALREADY = 37, // Operation already in progress
317
318    // ipc/network software -- argument errors
319    NOTSOCK = 38, // Socket operation on non-socket
320    DESTADDRREQ = 39, // Destination address required
321    MSGSIZE = 40, // Message too long
322    PROTOTYPE = 41, // Protocol wrong type for socket
323    NOPROTOOPT = 42, // Protocol not available
324    PROTONOSUPPORT = 43, // Protocol not supported
325    SOCKTNOSUPPORT = 44, // Socket type not supported
326    /// Operation not supported
327    /// This code is also used for `NOTSUP`.
328    OPNOTSUPP = 45,
329    PFNOSUPPORT = 46, // Protocol family not supported
330    AFNOSUPPORT = 47, // Address family not supported by protocol family
331    ADDRINUSE = 48, // Address already in use
332    ADDRNOTAVAIL = 49, // Can't assign requested address
333
334    // ipc/network software -- operational errors
335    NETDOWN = 50, // Network is down
336    NETUNREACH = 51, // Network is unreachable
337    NETRESET = 52, // Network dropped connection on reset
338    CONNABORTED = 53, // Software caused connection abort
339    CONNRESET = 54, // Connection reset by peer
340    NOBUFS = 55, // No buffer space available
341    ISCONN = 56, // Socket is already connected
342    NOTCONN = 57, // Socket is not connected
343    SHUTDOWN = 58, // Can't send after socket shutdown
344    TOOMANYREFS = 59, // Too many references: can't splice
345    TIMEDOUT = 60, // Operation timed out
346    CONNREFUSED = 61, // Connection refused
347
348    LOOP = 62, // Too many levels of symbolic links
349    NAMETOOLONG = 63, // File name too long
350
351    // should be rearranged
352    HOSTDOWN = 64, // Host is down
353    HOSTUNREACH = 65, // No route to host
354    NOTEMPTY = 66, // Directory not empty
355
356    // quotas & mush
357    PROCLIM = 67, // Too many processes
358    USERS = 68, // Too many users
359    DQUOT = 69, // Disc quota exceeded
360
361    // Network File System
362    STALE = 70, // Stale NFS file handle
363    REMOTE = 71, // Too many levels of remote in path
364    BADRPC = 72, // RPC struct is bad
365    RPCMISMATCH = 73, // RPC version wrong
366    PROGUNAVAIL = 74, // RPC prog. not avail
367    PROGMISMATCH = 75, // Program version wrong
368    PROCUNAVAIL = 76, // Bad procedure for program
369
370    NOLCK = 77, // No locks available
371    NOSYS = 78, // Function not implemented
372
373    FTYPE = 79, // Inappropriate file type or format
374    AUTH = 80, // Authentication error
375    NEEDAUTH = 81, // Need authenticator
376    IDRM = 82, // Identifier removed
377    NOMSG = 83, // No message of desired type
378    OVERFLOW = 84, // Value too large to be stored in data type
379    CANCELED = 85, // Operation canceled
380    ILSEQ = 86, // Illegal byte sequence
381    NOATTR = 87, // Attribute not found
382
383    DOOFUS = 88, // Programming error
384
385    BADMSG = 89, // Bad message
386    MULTIHOP = 90, // Multihop attempted
387    NOLINK = 91, // Link has been severed
388    PROTO = 92, // Protocol error
389
390    NOTCAPABLE = 93, // Capabilities insufficient
391    CAPMODE = 94, // Not permitted in capability mode
392    NOTRECOVERABLE = 95, // State not recoverable
393    OWNERDEAD = 96, // Previous owner died
394    INTEGRITY = 97, // Integrity check failed
395    _,
396};
397
398// https://github.com/freebsd/freebsd-src/blob/9bfbc6826f72eb385bf52f4cde8080bccf7e3ebd/sys/netinet/in.h#L436
399pub const IP = struct {
400    pub const OPTIONS = 1;
401    pub const HDRINCL = 2;
402    pub const TOS = 3;
403    pub const TTL = 4;
404    pub const RECVOPTS = 5;
405    pub const RECVRETOPTS = 6;
406    pub const RECVDSTADDR = 7;
407    pub const SENDSRCADDR = RECVDSTADDR;
408    pub const RETOPTS = 8;
409    pub const MULTICAST_IF = 9;
410    pub const MULTICAST_TTL = 10;
411    pub const MULTICAST_LOOP = 11;
412    pub const ADD_MEMBERSHIP = 12;
413    pub const DROP_MEMBERSHIP = 13;
414    pub const MULTICAST_VIF = 14;
415    pub const RSVP_ON = 15;
416    pub const RSVP_OFF = 16;
417    pub const RSVP_VIF_ON = 17;
418    pub const RSVP_VIF_OFF = 18;
419    pub const PORTRANGE = 19;
420    pub const RECVIF = 20;
421    pub const IPSEC_POLICY = 21;
422    pub const ONESBCAST = 23;
423    pub const BINDANY = 24;
424    pub const ORIGDSTADDR = 27;
425    pub const RECVORIGDSTADDR = ORIGDSTADDR;
426    pub const FW_TABLE_ADD = 40;
427    pub const FW_TABLE_DEL = 41;
428    pub const FW_TABLE_FLUSH = 42;
429    pub const FW_TABLE_GETSIZE = 43;
430    pub const FW_TABLE_LIST = 44;
431    pub const FW3 = 48;
432    pub const DUMMYNET3 = 49;
433    pub const FW_ADD = 50;
434    pub const FW_DEL = 51;
435    pub const FW_FLUSH = 52;
436    pub const FW_ZERO = 53;
437    pub const FW_GET = 54;
438    pub const FW_RESETLOG = 55;
439    pub const FW_NAT_CFG = 56;
440    pub const FW_NAT_DEL = 57;
441    pub const FW_NAT_GET_CONFIG = 58;
442    pub const FW_NAT_GET_LOG = 59;
443    pub const DUMMYNET_CONFIGURE = 60;
444    pub const DUMMYNET_DEL = 61;
445    pub const DUMMYNET_FLUSH = 62;
446    pub const DUMMYNET_GET = 64;
447    pub const RECVTTL = 65;
448    pub const MINTTL = 66;
449    pub const DONTFRAG = 67;
450    pub const RECVTOS = 68;
451    pub const ADD_SOURCE_MEMBERSHIP = 70;
452    pub const DROP_SOURCE_MEMBERSHIP = 71;
453    pub const BLOCK_SOURCE = 72;
454    pub const UNBLOCK_SOURCE = 73;
455    pub const MSFILTER = 74;
456    pub const VLAN_PCP = 75;
457    pub const FLOWID = 90;
458    pub const FLOWTYPE = 91;
459    pub const RSSBUCKETID = 92;
460    pub const RECVFLOWID = 93;
461    pub const RECVRSSBUCKETID = 94;
462    // Same namespace, but these are arguments rather than option names
463    pub const DEFAULT_MULTICAST_TTL = 1;
464    pub const DEFAULT_MULTICAST_LOOP = 1;
465    pub const MAX_MEMBERSHIPS = 4095;
466    pub const MAX_GROUP_SRC_FILTER = 512;
467    pub const MAX_SOCK_SRC_FILTER = 128;
468    pub const MAX_SOCK_MUTE_FILTER = 128;
469    pub const PORTRANGE_DEFAULT = 0;
470    pub const PORTRANGE_HIGH = 1;
471    pub const PORTRANGE_LOW = 2;
472};
473
474// https://github.com/freebsd/freebsd-src/blob/9bfbc6826f72eb385bf52f4cde8080bccf7e3ebd/sys/netinet6/in6.h#L402
475pub const IPV6 = struct {
476    pub const UNICAST_HOPS = 4;
477    pub const MULTICAST_IF = 9;
478    pub const MULTICAST_HOPS = 10;
479    pub const MULTICAST_LOOP = 11;
480    pub const JOIN_GROUP = 12;
481    pub const LEAVE_GROUP = 13;
482    pub const PORTRANGE = 14;
483    pub const @"2292PKTINFO" = 19;
484    pub const @"2292HOPLIMIT" = 20;
485    pub const @"2292NEXTHOP" = 21;
486    pub const @"2292HOPOPTS" = 22;
487    pub const @"2292DSTOPTS" = 23;
488    pub const @"2292RTHDR" = 24;
489    pub const @"2292PKTOPTIONS" = 25;
490    pub const CHECKSUM = 26;
491    pub const V6ONLY = 27;
492    pub const BINDV6ONLY = V6ONLY;
493    pub const IPSEC_POLICY = 28;
494    pub const FW_ADD = 30;
495    pub const FW_DEL = 31;
496    pub const FW_FLUSH = 32;
497    pub const FW_ZERO = 33;
498    pub const FW_GET = 34;
499    pub const RTHDRDSTOPTS = 35;
500    pub const RECVPKTINFO = 36;
501    pub const RECVHOPLIMIT = 37;
502    pub const RECVRTHDR = 38;
503    pub const RECVHOPOPTS = 39;
504    pub const RECVDSTOPTS = 40;
505    pub const RECVRTHDRDSTOPTS = 41;
506    pub const USE_MIN_MTU = 42;
507    pub const RECVPATHMTU = 43;
508    pub const PATHMTU = 44;
509    pub const REACHCONF = 45;
510    pub const PKTINFO = 46;
511    pub const HOPLIMIT = 47;
512    pub const NEXTHOP = 48;
513    pub const HOPOPTS = 49;
514    pub const DSTOPTS = 50;
515    pub const RTHDR = 51;
516    pub const PKTOPTIONS = 52;
517    pub const RECVTCLASS = 57;
518    pub const AUTOFLOWLABEL = 59;
519    pub const TCLASS = 61;
520    pub const DONTFRAG = 62;
521    pub const PREFER_TEMPADDR = 63;
522    pub const BINDANY = 64;
523    pub const FLOWID = 67;
524    pub const FLOWTYPE = 68;
525    pub const RSSBUCKETID = 69;
526    pub const RECVFLOWID = 70;
527    pub const RECVRSSBUCKETID = 71;
528    pub const ORIGDSTADDR = 72;
529    pub const RECVORIGDSTADDR = ORIGDSTADDR;
530    pub const MSFILTER = 74;
531    pub const VLAN_PCP = 75;
532    // Same namespace, but these are arguments rather than option names
533    pub const RTHDR_LOOSE = 0;
534    pub const RTHDR_STRICT = 1;
535    pub const RTHDR_TYPE_0 = 0;
536    pub const DEFAULT_MULTICAST_HOPS = 1;
537    pub const DEFAULT_MULTICAST_LOOP = 1;
538    pub const MAX_MEMBERSHIPS = 4095;
539    pub const MAX_GROUP_SRC_FILTER = 512;
540    pub const MAX_SOCK_SRC_FILTER = 128;
541    pub const PORTRANGE_DEFAULT = 0;
542    pub const PORTRANGE_HIGH = 1;
543    pub const PORTRANGE_LOW = 2;
544};
545
546// https://github.com/freebsd/freebsd-src/blob/9bfbc6826f72eb385bf52f4cde8080bccf7e3ebd/sys/netinet/ip.h#L77
547pub const IPTOS = struct {
548    pub const LOWDELAY = 0x10;
549    pub const THROUGHPUT = 0x08;
550    pub const RELIABILITY = 0x04;
551    pub const MINCOST = DSCP_CS0;
552    pub const PREC_ROUTINE = DSCP_CS0;
553    pub const PREC_PRIORITY = DSCP_CS1;
554    pub const PREC_IMMEDIATE = DSCP_CS2;
555    pub const PREC_FLASH = DSCP_CS3;
556    pub const PREC_FLASHOVERRIDE = DSCP_CS4;
557    pub const PREC_CRITIC_ECP = DSCP_CS5;
558    pub const PREC_INTERNETCONTROL = DSCP_CS6;
559    pub const PREC_NETCONTROL = DSCP_CS7;
560    pub const DSCP_OFFSET = 2;
561    pub const DSCP_CS0 = 0x00;
562    pub const DSCP_CS1 = 0x20;
563    pub const DSCP_AF11 = 0x28;
564    pub const DSCP_AF12 = 0x30;
565    pub const DSCP_AF13 = 0x38;
566    pub const DSCP_CS2 = 0x40;
567    pub const DSCP_AF21 = 0x48;
568    pub const DSCP_AF22 = 0x50;
569    pub const DSCP_AF23 = 0x58;
570    pub const DSCP_CS3 = 0x60;
571    pub const DSCP_AF31 = 0x68;
572    pub const DSCP_AF32 = 0x70;
573    pub const DSCP_AF33 = 0x78;
574    pub const DSCP_CS4 = 0x80;
575    pub const DSCP_AF41 = 0x88;
576    pub const DSCP_AF42 = 0x90;
577    pub const DSCP_AF43 = 0x98;
578    pub const DSCP_CS5 = 0xa0;
579    pub const DSCP_VA = 0xb0;
580    pub const DSCP_EF = 0xb8;
581    pub const DSCP_CS6 = 0xc0;
582    pub const DSCP_CS7 = 0xe0;
583    pub const ECN_NOTECT = 0x00;
584    pub const ECN_ECT1 = 0x01;
585    pub const ECN_ECT0 = 0x02;
586    pub const ECN_CE = 0x03;
587    pub const ECN_MASK = 0x03;
588};