1{
2   This file is part of the Free Pascal run time library.
3   (c) 2000-2003 by Marco van de Voort
4   member of the Free Pascal development team.
5
6   See the file COPYING.FPC, included in this distribution,
7   for details about the copyright.
8
9   socket call implementations for FreeBSD
10
11   This program is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY;without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14}
15
16type
17  Pucred = ^ucred;
18
19  ucred = record
20    pid: cuint32;
21    uid: cuint32;
22    gid: cuint32;
23  end;
24
25const
26
27  SCM_RIGHTS      = $01; { rw: access rights (array of int) }
28  SCM_CREDENTIALS = $02; { rw: struct ucred                 }
29  SCM_SECURITY    = $03; { rw: security label               }
30
31{* Supported address families. *}
32  AF_UNSPEC     = 0;
33  AF_UNIX       = 1;     { Unix domain sockets          }
34  AF_LOCAL      = 1;     { POSIX name for AF_UNIX       }
35  AF_INET       = 2;     { Internet IP Protocol         }
36  AF_AX25       = 3;     { Amateur Radio AX.25          }
37  AF_IPX        = 4;     { Novell IPX                   }
38  AF_APPLETALK  = 5;     { AppleTalk DDP                }
39  AF_NETROM     = 6;     { Amateur Radio NET/ROM        }
40  AF_BRIDGE     = 7;     { Multiprotocol bridge         }
41  AF_ATMPVC     = 8;     { ATM PVCs                     }
42  AF_X25        = 9;     { Reserved for X.25 project    }
43  AF_INET6      = 10;    { IP version 6                 }
44  AF_ROSE       = 11;    { Amateur Radio X.25 PLP       }
45  AF_DECnet     = 12;    { Reserved for DECnet project  }
46  AF_NETBEUI    = 13;    { Reserved for 802.2LLC project}
47  AF_SECURITY   = 14;    { Security callback pseudo AF }
48  AF_KEY        = 15;    { PF_KEY key management API }
49  AF_NETLINK    = 16;
50  AF_ROUTE      = AF_NETLINK; { Alias to emulate 4.4BSD }
51  AF_PACKET     = 17;    { Packet family                }
52  AF_ASH        = 18;    { Ash                          }
53  AF_ECONET     = 19;    { Acorn Econet                 }
54  AF_ATMSVC     = 20;    { ATM SVCs                     }
55  AF_RDS        = 21;    { RDS sockets                  }
56  AF_SNA        = 22;	 { Linux SNA Project (nutters!) }
57  AF_IRDA       = 23;    { IRDA sockets                 }
58  AF_PPPOX	= 24;    { PPPoX sockets                }
59  AF_WANPIPE    = 25;    { Wanpipe API Sockets }
60  AF_LLC        = 26;    { Linux LLC                    }
61  AF_IB         = 27;    { Native InfiniBand address    }
62  AF_MPLS       = 28;	   { MPLS                         }
63  AF_CAN        = 29;    { Controller Area Network      }
64  AF_TIPC       = 30;    { TIPC sockets                 }
65  AF_BLUETOOTH  = 31;    { Bluetooth sockets            }
66  AF_IUCV       = 32;    { IUCV sockets                 }
67  AF_RXRPC      = 33;    { RxRPC sockets                }
68  AF_ISDN       = 34;    { mISDN sockets                }
69  AF_PHONET     = 35;    { Phonet sockets               }
70  AF_IEEE802154 = 36;    { IEEE802154 sockets           }
71  AF_CAIF       = 37;    { CAIF sockets                 }
72  AF_ALG        = 38;    { Algorithm sockets            }
73  AF_NFC        = 39;    { NFC sockets                  }
74  AF_VSOCK      = 40;    { vSockets                     }
75  AF_KCM        = 41;    { Kernel Connection Multiplexor}
76  AF_QIPCRTR    = 42;    { Qualcomm IPC Router          }
77  AF_SMC        = 43;    { smc sockets: reserve number for
78                           PF_SMC protocol family that
79                           reuses AF_INET address family}
80  AF_XDP        = 44;    { XDP sockets                  }
81  AF_MAX        = 45;    { For now..                    }
82
83  SOCK_MAXADDRLEN = 255;             { longest possible addresses }
84
85{
86* Protocol families, same as address families for now.
87}
88
89  PF_UNSPEC     = AF_UNSPEC;
90  PF_UNIX       = AF_UNIX;
91  PF_LOCAL      = AF_LOCAL;
92  PF_INET       = AF_INET;
93  PF_AX25       = AF_AX25;
94  PF_IPX        = AF_IPX;
95  PF_APPLETALK  = AF_APPLETALK;
96  PF_NETROM     = AF_NETROM;
97  PF_BRIDGE     = AF_BRIDGE;
98  PF_ATMPVC     = AF_ATMPVC;
99  PF_X25        = AF_X25;
100  PF_INET6      = AF_INET6;
101  PF_ROSE       = AF_ROSE;
102  PF_DECnet     = AF_DECnet;
103  PF_NETBEUI    = AF_NETBEUI;
104  PF_SECURITY   = AF_SECURITY;
105  PF_KEY        = AF_KEY;
106  PF_NETLINK    = AF_NETLINK;
107  PF_ROUTE      = AF_ROUTE;
108  PF_PACKET     = AF_PACKET;
109  PF_ASH        = AF_ASH;
110  PF_ECONET     = AF_ECONET;
111  PF_ATMSVC     = AF_ATMSVC;
112  PF_RDS        = AF_RDS;
113  PF_SNA        = AF_SNA;
114  PF_IRDA       = AF_IRDA;
115  PF_PPPOX	= AF_PPPOX;
116  PF_WANPIPE    = AF_WANPIPE;
117  PF_LLC        = AF_LLC;
118  PF_IB         = AF_IB;
119  PF_MPLS       = AF_MPLS;
120  PF_CAN        = AF_CAN;
121  PF_TIPC       = AF_TIPC;
122  PF_BLUETOOTH  = AF_BLUETOOTH;
123  PF_IUCV       = AF_IUCV;
124  PF_RXRPC      = AF_RXRPC;
125  PF_ISDN       = AF_ISDN;
126  PF_PHONET     = AF_PHONET;
127  PF_IEEE802154 = AF_IEEE802154;
128  PF_CAIF       = AF_CAIF;
129  PF_ALG        = AF_ALG;
130  PF_NFC        = AF_NFC;
131  PF_VSOCK      = AF_VSOCK;
132  PF_KCM        = AF_KCM;
133  PF_QIPCRTR    = AF_QIPCRTR;
134  PF_SMC        = AF_SMC;
135  PF_XDP        = AF_XDP;
136  PF_MAX        = AF_MAX;
137
138
139{ Maximum queue length specifiable by listen.  }
140  SOMAXCONN = 4096;
141
142{ For setsockoptions(2) }
143         SOL_SOCKET  =   1;
144         SO_DEBUG    =   1;
145         SO_REUSEADDR=   2;
146         SO_TYPE     =   3;
147         SO_ERROR    =   4;
148         SO_DONTROUTE=   5;
149         SO_BROADCAST=   6;
150         SO_SNDBUF   =   7;
151         SO_RCVBUF   =   8;
152         SO_KEEPALIVE=   9;
153         SO_OOBINLINE=   10;
154         SO_NO_CHECK =   11;
155         SO_PRIORITY =   12;
156         SO_LINGER   =   13;
157         SO_BSDCOMPAT=   14;
158         SO_REUSEPORT = 15;
159         SO_PASSCRED=    16;
160         SO_PEERCRED=    17;
161         SO_RCVLOWAT=    18;
162         SO_SNDLOWAT=    19;
163         SO_RCVTIMEO=    20;
164         SO_SNDTIMEO=    21;
165
166{ Security levels - as per NRL IPv6 - don't actually do anything }
167
168         SO_SECURITY_AUTHENTICATION      =   22;
169         SO_SECURITY_ENCRYPTION_TRANSPORT=   23;
170         SO_SECURITY_ENCRYPTION_NETWORK  =   24;
171
172         SO_BINDTODEVICE=   25;
173
174{ Socket filtering }
175
176         SO_ATTACH_FILTER=  26;
177         SO_DETACH_FILTER=  27;
178         SO_PEERNAME     =  28;
179         SO_TIMESTAMP    = 29;
180         SCM_TIMESTAMP   = SO_TIMESTAMP;
181         SO_ACCEPTCONN   = 30;
182
183// Following from kernel 2.6.14-1.1637_FC4
184
185        SHUT_RD          = 0;             { shut down the reading side }
186        SHUT_WR          = 1;             { shut down the writing side }
187        SHUT_RDWR        = 2;             { shut down both sides }
188
189//from /usr/include/netinet/in.h
190
191        IPPROTO_IP       = 0;       { Dummy protocol for TCP.  }
192        IPPROTO_HOPOPTS  = 0;      { IPv6 Hop-by-Hop options.  }
193
194        IPPROTO_ICMP     = 1;       { Internet Control Message Protocol.  }
195        IPPROTO_IGMP     = 2;       { Internet Group Management Protocol. }
196        IPPROTO_IPIP     = 4;       { IPIP tunnels (older KA9Q tunnels use 94).  }
197        IPPROTO_TCP      = 6;       { Transmission Control Protocol.  }
198        IPPROTO_EGP      = 8;       { Exterior Gateway Protocol.  }
199
200        IPPROTO_PUP      = 12;       { PUP protocol.  }
201        IPPROTO_UDP      = 17;       { User Datagram Protocol.  }
202        IPPROTO_IDP      = 22;       { XNS IDP protocol.  }
203        IPPROTO_TP       = 29;       { SO Transport Protocol Class 4.  }
204        IPPROTO_IPV6     = 41;     { IPv6 header.  }
205
206         IPPROTO_ROUTING = 43;  { IPv6 routing header.  }
207         IPPROTO_FRAGMENT = 44; { IPv6 fragmentation header.  }
208         IPPROTO_RSVP    = 46;       { Reservation Protocol.  }
209         IPPROTO_GRE     = 47;       { General Routing Encapsulation.  }
210         IPPROTO_ESP     = 50;     { encapsulating security payload.  }
211         IPPROTO_AH      = 51;     { authentication header.  }
212         IPPROTO_ICMPV6  = 58;     { ICMPv6.  }
213         IPPROTO_NONE    = 59;     { IPv6 no next header.  }
214         IPPROTO_DSTOPTS = 60;     { IPv6 destination options.  }
215         IPPROTO_MTP     = 92;       { Multicast Transport Protocol.  }
216         IPPROTO_ENCAP   = 98;       { Encapsulation Header.  }
217         IPPROTO_PIM     = 103;       { Protocol Independent Multicast.  }
218         IPPROTO_COMP    = 108;       { Compression Header Protocol.  }
219         IPPROTO_SCTP    = 132;       { Stream Control Transmission Protocol.  }
220         IPPROTO_RAW     = 255;       { Raw IP packets.  }
221         IPPROTO_MAX     = 255;
222//from /usr/include/bits/in.h
223{ Options for use with getsockopt' and setsockopt' at the IP level.
224   The first word in the comment at the right is the data type used;
225   "bool" means a boolean value stored in an int'.  }
226
227        IP_OPTIONS              = 4;               { ip_opts; IP per-packet options.  }
228        IP_HDRINCL              = 3;               { int; Header is included with data.  }
229        IP_TOS                  = 1;               { int; IP type of service and precedence.  }
230        IP_TTL                  = 2;               { int; IP time to live.  }
231        IP_RECVOPTS             = 6;               { bool; Receive all IP options w/datagram.  }
232{ For BSD compatibility.  }
233        IP_RETOPTS              = 7;               { ip_opts; Set/get IP per-packet options.  }
234        IP_RECVRETOPTS          = IP_RETOPTS;      { bool; Receive IP options for response.  }
235
236        IP_MULTICAST_IF         = 32;                { in_addr; set/get IP multicast i/f }
237        IP_MULTICAST_TTL        = 33;               { u_char; set/get IP multicast ttl }
238        IP_MULTICAST_LOOP       = 34;              { i_char; set/get IP multicast loopback }
239        IP_ADD_MEMBERSHIP       = 35;               { ip_mreq; add an IP group membership }
240        IP_DROP_MEMBERSHIP      = 36;            { ip_mreq; drop an IP group membership }
241        IP_UNBLOCK_SOURCE       = 37;                 { ip_mreq_source: unblock data from source }
242        IP_BLOCK_SOURCE         = 38;              { ip_mreq_source: block data from source }
243        IP_ADD_SOURCE_MEMBERSHIP = 39;             { ip_mreq_source: join source group }
244        IP_DROP_SOURCE_MEMBERSHIP = 40;            { ip_mreq_source: leave source group }
245        IP_MSFILTER             = 41;
246        MCAST_JOIN_GROUP        = 42;    { group_req: join any-source group }
247        MCAST_BLOCK_SOURCE      = 43;    { group_source_req: block from given group }
248        MCAST_UNBLOCK_SOURCE    = 44;    { group_source_req: unblock from given group}
249        MCAST_LEAVE_GROUP       = 45;    { group_req: leave any-source group }
250        MCAST_JOIN_SOURCE_GROUP = 46;   { group_source_req: join source-spec gr }
251        MCAST_LEAVE_SOURCE_GROUP = 47;  { group_source_req: leave source-spec gr}
252        MCAST_MSFILTER          = 48;
253
254        MCAST_EXCLUDE           = 0;
255        MCAST_INCLUDE           = 1;
256
257        IP_ROUTER_ALERT         = 5;    { bool }
258        IP_PKTINFO              = 8;    { bool }
259        IP_PKTOPTIONS           = 9;
260        IP_PMTUDISC             = 10;    { obsolete name? }
261        IP_MTU_DISCOVER         = 10;   { int; see below }
262        IP_RECVERR              = 11;    { bool }
263        IP_RECVTTL              = 12;   { bool }
264        IP_RECVTOS              = 13;    { bool }
265
266
267{ IP_MTU_DISCOVER arguments.  }
268        IP_PMTUDISC_DONT        = 0;    { Never send DF frames.  }
269        IP_PMTUDISC_WANT        = 1;    { Use per route hints.  }
270        IP_PMTUDISC_DO          = 2;    { Always DF.  }
271
272{ To select the IP level.  }
273{ Setsockoptions(2) level. Thanks to BSD these must match IPPROTO_xxx }
274        SOL_IP                  = 0;
275        // SOL_ICMP	  =   1;  { No-no-no! Due to Linux :-) we cannot use SOL_ICMP=1 }
276	      SOL_TCP       =   6;
277	      SOL_UDP       =  17;
278
279{ Socket level values for IPv6.  }
280	      SOL_IPV6      =  41;
281	      SOL_ICMPV6    =  58;
282
283      	SOL_SCTP      = 132;
284	      SOL_UDPLITE   = 136;   { UDP-Lite (RFC 3828) }
285	      SOL_RAW       = 255;
286	      SOL_IPX       = 256;
287	      SOL_AX25      = 257;
288	      SOL_ATALK     = 258;
289	      SOL_NETROM    = 259;
290	      SOL_ROSE      = 260;
291	      SOL_DECNET    = 261;
292	      SOL_X25       = 262;
293	      SOL_PACKET    = 263;
294	      SOL_ATM       = 264;   { ATM layer (cell level) }
295	      SOL_AAL       = 265;   { ATM Adaption Layer (packet level) }
296	      SOL_IRDA      = 266;
297	      SOL_NETBEUI   = 267;
298	      SOL_LLC       = 268;
299	      SOL_DCCP      = 269;
300	      SOL_NETLINK   = 270;
301	      SOL_TIPC      = 271;
302	      SOL_RXRPC     = 272;
303	      SOL_PPPOL2TP  = 273;
304	      SOL_BLUETOOTH = 274;
305	      SOL_PNPIPE    = 275;
306	      SOL_RDS       = 276;
307	      SOL_IUCV      = 277;
308	      SOL_CAIF      = 278;
309	      SOL_ALG       = 279;
310	      SOL_NFC       = 280;
311	      SOL_KCM       = 281;
312	      SOL_TLS       = 282;
313	      SOL_XDP       = 283;
314
315
316{ IPX options }
317        IPX_TYPE = 1;
318
319
320        IP_DEFAULT_MULTICAST_TTL = 1;
321        IP_DEFAULT_MULTICAST_LOOP = 1;
322        IP_MAX_MEMBERSHIPS       = 20;
323
324
325{  Options for use with getsockopt' and setsockopt' at the IPv6 level.
326   The first word in the comment at the right is the data type used;
327   "bool" means a boolean value stored in an int'.  }
328        IPV6_ADDRFORM         = 1;
329        IPV6_PKTINFO             = 2;
330        IPV6_HOPOPTS             = 3;
331        IPV6_DSTOPTS             = 4;
332        IPV6_RTHDR          = 5;
333        IPV6_RXSRCRT             = IPV6_RTHDR;
334        //this may be an old name, I couldn't find it in my include files but
335        //I found it with google.  It may have been depreciated because I only
336        //saw it in earlier files.
337        IPV6_PKTOPTIONS         = 6;
338        IPV6_CHECKSUM            = 7;
339        IPV6_HOPLIMIT            = 8;
340
341        SCM_SRCRT                = IPV6_RXSRCRT;
342
343        IPV6_NEXTHOP         = 9;
344        IPV6_AUTHHDR         = 10;
345        IPV6_UNICAST_HOPS        = 16;
346        IPV6_MULTICAST_IF     = 17;
347        IPV6_MULTICAST_HOPS     = 18;
348        IPV6_MULTICAST_LOOP      = 19;
349        IPV6_JOIN_GROUP             = 20;
350        IPV6_LEAVE_GROUP     = 21;
351        IPV6_ROUTER_ALERT     = 22;
352        IPV6_MTU_DISCOVER     = 23;
353        IPV6_MTU         = 24;
354        IPV6_RECVERR         = 25;
355        IPV6_V6ONLY         = 26;
356        IPV6_JOIN_ANYCAST     = 27;
357        IPV6_LEAVE_ANYCAST     = 28;
358        IPV6_IPSEC_POLICY     = 34;
359        IPV6_XFRM_POLICY     = 35;
360
361{ Obsolete synonyms for the above.  }
362        IPV6_ADD_MEMBERSHIP     = IPV6_JOIN_GROUP;
363        IPV6_DROP_MEMBERSHIP     = IPV6_LEAVE_GROUP;
364        IPV6_RXHOPOPTS         = IPV6_HOPOPTS;
365        IPV6_RXDSTOPTS           = IPV6_DSTOPTS;
366
367{ IPV6_MTU_DISCOVER values.  }
368        IPV6_PMTUDISC_DONT     = 0;    { Never send DF frames.  }
369        IPV6_PMTUDISC_WANT      = 1;    { Use per route hints.  }
370        IPV6_PMTUDISC_DO     = 2;    { Always DF.  }
371
372{ Routing header options for IPv6.  }
373        IPV6_RTHDR_LOOSE         = 0;   { Hop doesn't need to be neighbour. }
374        IPV6_RTHDR_STRICT     = 1;    { Hop must be a neighbour.  }
375
376        IPV6_RTHDR_TYPE_0        = 0;    { IPv6 Routing header type 0.  }
377
378  { Flags for send, recv etc. }
379  MSG_OOB                = $00000001;              { Process out-of-band data}
380  MSG_PEEK               = $00000002;              { Peek at incoming messages }
381  MSG_DONTROUTE          = $00000004;              { Don't use local routing }
382  MSG_TRYHARD  = MSG_DONTROUTE;
383  MSG_CTRUNC             = $00000008;              { Control data lost before delivery }
384  MSG_PROXY              = $00000010;              { Supply or ask second address }
385  MSG_PROBE              = MSG_PROXY;              { Do not send. Only probe path f.e. for MTU }
386  MSG_TRUNC              = $00000020;
387  MSG_DONTWAIT           = $00000040;              { Non-blocking I/O }
388  MSG_EOR                = $00000080;              { End of record }
389  MSG_WAITALL            = $00000100;              { Wait for a full request }
390  MSG_FIN                = $00000200;
391  MSG_SYN                = $00000400;
392  MSG_CONFIRM            = $00000800;              { Confirm path validity }
393  MSG_RST                = $00001000;
394  MSG_ERRQUERE           = $00002000;              { Fetch message from error queue }
395  MSG_NOSIGNAL           = $00004000;              { Do not generate SIGPIPE }
396  MSG_MORE               = $00008000;              { Sender will send more }
397  MSG_WAITFORONE         = $00010000;              { recvmmsg(): block until 1+ packets avail }
398  MSG_SENDPAGE_NOPOLICY  = $00010000;              { sendpage() internal : do no apply policy }
399  MSG_SENDPAGE_NOTLAST   = $00020000;              { sendpage() internal : not the last page }
400  MSG_BATCH              = $00040000;              { sendmmsg(): more messages coming }
401  MSG_EOF      = MSG_FIN;
402  MSG_NO_SHARED_FRAGS    = $00080000;              { sendpage() internal : page frags are not shared }
403  MSG_SENDPAGE_DECRYPTED = $00100000;              { sendpage() internal : page may carry
404                                                     plain text and require encryption }
405  MSG_ZEROCOPY           = $04000000;              { Use user data in kernel path }
406  MSG_FASTOPEN           = $20000000;              { Send data in TCP SYN }
407  MSG_CMSG_CLOEXEC       = $40000000;              { Set close_on_exec for file
408                                                     descriptor received through SCM_RIGHTS }
409  MSG_CMSG_COMPAT        = $0; {$80000000}         { This message would need 32 bit fixups, so use 0 instead }
410
411
412     TCP_NODELAY = 1;
413  { Limit MSS  }
414     TCP_MAXSEG = 2;
415  { Never send partially complete segments  }
416     TCP_CORK = 3;
417  { Start keeplives after this period  }
418     TCP_KEEPIDLE = 4;
419  { Interval between keepalives  }
420     TCP_KEEPINTVL = 5;
421  { Number of keepalives before death  }
422     TCP_KEEPCNT = 6;
423  { Number of SYN retransmits  }
424     TCP_SYNCNT = 7;
425  { Life time of orphaned FIN-WAIT-2 state  }
426     TCP_LINGER2 = 8;
427  { Wake up listener only when data arrive  }
428     TCP_DEFER_ACCEPT = 9;
429  { Bound advertised window  }
430     TCP_WINDOW_CLAMP = 10;
431  { Information about this connection.  }
432     TCP_INFO = 11;
433  { Block/reenable quick acks  }
434     TCP_QUICKACK = 12;
435  { Congestion control algorithm  }
436     TCP_CONGESTION = 13;
437  { TCP MD5 Signature (RFC2385)  }
438     TCP_MD5SIG = 14;
439
440     UDP_CORK = 1;
441  { Set the socket to accept encapsulated packets  }
442     UDP_ENCAP = 100;
443  { UDP encapsulation types  }
444  { draft-ietf-ipsec-nat-t-ike-00/01  }
445     UDP_ENCAP_ESPINUDP_NON_IKE = 1;
446  { draft-ietf-ipsec-udp-encaps-06  }
447     UDP_ENCAP_ESPINUDP = 2;
448  { rfc2661  }
449     UDP_ENCAP_L2TPINUDP = 3;
450
451