core_foundation_sys/
socket.rs

1// Copyright 2023 The Servo Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10use std::os::raw::c_void;
11
12use crate::base::{Boolean, CFAllocatorRef, CFIndex, CFOptionFlags, CFTypeID, SInt32, UInt16};
13use crate::data::CFDataRef;
14use crate::date::CFTimeInterval;
15use crate::propertylist::CFPropertyListRef;
16use crate::runloop::CFRunLoopSourceRef;
17use crate::string::CFStringRef;
18
19#[repr(C)]
20pub struct __CFSocket(c_void);
21
22pub type CFSocketRef = *mut __CFSocket;
23
24pub type CFSocketError = CFIndex;
25pub type CFSocketCallBackType = CFOptionFlags;
26pub type CFSocketCallBack = extern "C" fn(
27    s: CFSocketRef,
28    _type: CFSocketCallBackType,
29    address: CFDataRef,
30    cdata: *const c_void,
31    info: *mut c_void,
32);
33#[cfg(not(target_os = "windows"))]
34pub type CFSocketNativeHandle = std::os::raw::c_int;
35#[cfg(target_os = "windows")]
36pub type CFSocketNativeHandle = std::os::raw::c_ulong;
37
38pub const kCFSocketSuccess: CFSocketError = 0;
39pub const kCFSocketError: CFSocketError = -1;
40pub const kCFSocketTimeout: CFSocketError = -2;
41
42#[derive(Debug, Copy, Clone)]
43#[repr(C)]
44pub struct CFSocketSignature {
45    pub protocolFamily: SInt32,
46    pub socketType: SInt32,
47    pub protocol: SInt32,
48    pub address: CFDataRef,
49}
50
51#[derive(Debug, Copy, Clone)]
52#[repr(C)]
53pub struct CFSocketContext {
54    pub version: CFIndex,
55    pub info: *mut c_void,
56    pub retain: extern "C" fn(info: *const c_void) -> *const c_void,
57    pub release: extern "C" fn(info: *const c_void),
58    pub copyDescription: extern "C" fn(info: *const c_void) -> CFStringRef,
59}
60
61pub const kCFSocketNoCallBack: CFSocketError = 0;
62pub const kCFSocketReadCallBack: CFSocketError = 1;
63pub const kCFSocketAcceptCallBack: CFSocketError = 2;
64pub const kCFSocketDataCallBack: CFSocketError = 3;
65pub const kCFSocketConnectCallBack: CFSocketError = 4;
66pub const kCFSocketWriteCallBack: CFSocketError = 8;
67
68pub const kCFSocketAutomaticallyReenableReadCallBack: CFOptionFlags = 1;
69pub const kCFSocketAutomaticallyReenableAcceptCallBack: CFOptionFlags = 2;
70pub const kCFSocketAutomaticallyReenableDataCallBack: CFOptionFlags = 3;
71pub const kCFSocketAutomaticallyReenableWriteCallBack: CFOptionFlags = 8;
72pub const kCFSocketLeaveErrors: CFOptionFlags = 64;
73pub const kCFSocketCloseOnInvalidate: CFOptionFlags = 128;
74
75extern "C" {
76    /*
77     * CFSocket.h
78     */
79
80    /* CFSocket Name Server Keys: Not used */
81    pub static kCFSocketCommandKey: CFStringRef;
82    pub static kCFSocketNameKey: CFStringRef;
83    pub static kCFSocketValueKey: CFStringRef;
84    pub static kCFSocketResultKey: CFStringRef;
85    pub static kCFSocketErrorKey: CFStringRef;
86    pub static kCFSocketRegisterCommand: CFStringRef;
87    pub static kCFSocketRetrieveCommand: CFStringRef;
88
89    /* Creating Sockets */
90    pub fn CFSocketCreate(
91        allocator: CFAllocatorRef,
92        protocolFamily: SInt32,
93        socketType: SInt32,
94        protocol: SInt32,
95        callBackTypes: CFOptionFlags,
96        callout: CFSocketCallBack,
97        context: *const CFSocketContext,
98    ) -> CFSocketRef;
99    pub fn CFSocketCreateConnectedToSocketSignature(
100        allocator: CFAllocatorRef,
101        signature: *const CFSocketSignature,
102        callBackTypes: CFOptionFlags,
103        callout: CFSocketCallBack,
104        context: *const CFSocketContext,
105        timeout: CFTimeInterval,
106    ) -> CFSocketRef;
107    pub fn CFSocketCreateWithNative(
108        allocator: CFAllocatorRef,
109        sock: CFSocketNativeHandle,
110        callBackTypes: CFOptionFlags,
111        callout: CFSocketCallBack,
112        context: *const CFSocketContext,
113    ) -> CFSocketRef;
114    pub fn CFSocketCreateWithSocketSignature(
115        allocator: CFAllocatorRef,
116        signature: *const CFSocketSignature,
117        callBackTypes: CFOptionFlags,
118        callout: CFSocketCallBack,
119        context: *const CFSocketContext,
120    ) -> CFSocketRef;
121
122    /* Configuring Sockets */
123    pub fn CFSocketCopyAddress(s: CFSocketRef) -> CFDataRef;
124    pub fn CFSocketCopyPeerAddress(s: CFSocketRef) -> CFDataRef;
125    pub fn CFSocketDisableCallBacks(s: CFSocketRef, callBackTypes: CFOptionFlags);
126    pub fn CFSocketEnableCallBacks(s: CFSocketRef, callBackTypes: CFOptionFlags);
127    pub fn CFSocketGetContext(s: CFSocketRef, context: *mut CFSocketContext);
128    pub fn CFSocketGetNative(s: CFSocketRef) -> CFSocketNativeHandle;
129    pub fn CFSocketGetSocketFlags(s: CFSocketRef) -> CFOptionFlags;
130    pub fn CFSocketSetAddress(s: CFSocketRef, address: CFDataRef) -> CFSocketError;
131    pub fn CFSocketSetSocketFlags(s: CFSocketRef, flags: CFOptionFlags);
132
133    /* Using Sockets */
134    pub fn CFSocketConnectToAddress(
135        s: CFSocketRef,
136        address: CFDataRef,
137        timeout: CFTimeInterval,
138    ) -> CFSocketError;
139    pub fn CFSocketCreateRunLoopSource(
140        allocator: CFAllocatorRef,
141        s: CFSocketRef,
142        order: CFIndex,
143    ) -> CFRunLoopSourceRef;
144    pub fn CFSocketGetTypeID() -> CFTypeID;
145    pub fn CFSocketInvalidate(s: CFSocketRef);
146    pub fn CFSocketIsValid(s: CFSocketRef) -> Boolean;
147    pub fn CFSocketSendData(
148        s: CFSocketRef,
149        address: CFDataRef,
150        data: CFDataRef,
151        timeout: CFTimeInterval,
152    ) -> CFSocketError;
153
154    /* Socket Name Server Utilities */
155    pub fn CFSocketCopyRegisteredSocketSignature(
156        nameServerSignature: *const CFSocketSignature,
157        timeout: CFTimeInterval,
158        name: CFStringRef,
159        signature: *mut CFSocketSignature,
160        nameServerAddress: *mut CFDataRef,
161    ) -> CFSocketError;
162    pub fn CFSocketCopyRegisteredValue(
163        nameServerSignature: *const CFSocketSignature,
164        timeout: CFTimeInterval,
165        name: CFStringRef,
166        value: *mut CFPropertyListRef,
167        nameServerAddress: *mut CFDataRef,
168    ) -> CFSocketError;
169    pub fn CFSocketGetDefaultNameRegistryPortNumber() -> UInt16;
170    pub fn CFSocketRegisterSocketSignature(
171        nameServerSignature: *const CFSocketSignature,
172        timeout: CFTimeInterval,
173        name: CFStringRef,
174        signature: *const CFSocketSignature,
175    ) -> CFSocketError;
176    pub fn CFSocketRegisterValue(
177        nameServerSignature: *const CFSocketSignature,
178        timeout: CFTimeInterval,
179        name: CFStringRef,
180        value: CFPropertyListRef,
181    ) -> CFSocketError;
182    pub fn CFSocketSetDefaultNameRegistryPortNumber(port: UInt16);
183    pub fn CFSocketUnregister(
184        nameServerSignature: *const CFSocketSignature,
185        timeout: CFTimeInterval,
186        name: CFStringRef,
187    ) -> CFSocketError;
188}