CURLOPT_UNIX_SOCKET_PATH.md (2357B)
1 --- 2 c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 SPDX-License-Identifier: curl 4 Title: CURLOPT_UNIX_SOCKET_PATH 5 Section: 3 6 Source: libcurl 7 See-also: 8 - CURLOPT_ABSTRACT_UNIX_SOCKET (3) 9 - CURLOPT_OPENSOCKETFUNCTION (3) 10 - unix (7) 11 Protocol: 12 - All 13 Added-in: 7.40.0 14 --- 15 16 # NAME 17 18 CURLOPT_UNIX_SOCKET_PATH - Unix domain socket 19 20 # SYNOPSIS 21 22 ~~~c 23 #include <curl/curl.h> 24 25 CURLcode curl_easy_setopt(CURL *handle, CURLOPT_UNIX_SOCKET_PATH, char *path); 26 ~~~ 27 28 # DESCRIPTION 29 30 Enables the use of Unix domain sockets as connection endpoint and sets the 31 path to *path*. If *path* is NULL, then Unix domain sockets are 32 disabled. 33 34 When enabled, curl connects to the Unix domain socket instead of establishing 35 a TCP connection to the host. Since no network connection is created, curl 36 does not resolve the DNS hostname in the URL. 37 38 The maximum path length on Cygwin, Linux and Solaris is 107. On other platforms 39 it might be even less. 40 41 Proxy and TCP options such as CURLOPT_TCP_NODELAY(3) are not supported. Proxy 42 options such as CURLOPT_PROXY(3) have no effect either as these are 43 TCP-oriented, and asking a proxy server to connect to a certain Unix domain 44 socket is not possible. 45 46 The application does not have to keep the string around after setting this 47 option. 48 49 Using this option multiple times makes the last set string override the 50 previous ones. Set it to NULL to disable its use again. 51 52 # DEFAULT 53 54 NULL - no Unix domain sockets are used. 55 56 # %PROTOCOLS% 57 58 # EXAMPLE 59 60 ~~~c 61 int main(void) 62 { 63 CURL *curl = curl_easy_init(); 64 if(curl) { 65 curl_easy_setopt(curl, CURLOPT_UNIX_SOCKET_PATH, "/tmp/httpd.sock"); 66 curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/"); 67 68 curl_easy_perform(curl); 69 } 70 } 71 ~~~ 72 73 If you are on Linux and somehow have a need for paths larger than 107 bytes, 74 you can use the *proc* filesystem to bypass the limitation: 75 76 ~~~c 77 int dirfd = open(long_directory_path_to_socket, O_DIRECTORY | O_RDONLY); 78 char path[108]; 79 snprintf(path, sizeof(path), "/proc/self/fd/%d/httpd.sock", dirfd); 80 curl_easy_setopt(curl_handle, CURLOPT_UNIX_SOCKET_PATH, path); 81 /* Be sure to keep dirfd valid until you discard the handle */ 82 ~~~ 83 84 # %AVAILABILITY% 85 86 # RETURN VALUE 87 88 curl_easy_setopt(3) returns a CURLcode indicating success or error. 89 90 CURLE_OK (0) means everything was OK, non-zero means an error occurred, see 91 libcurl-errors(3).