Mercurial
comparison seobeo/s_network.c @ 244:b8aa08503378
[tools] Add sandboxed online LaTeX editor
Co-authored-by: Copilot <[email protected]>
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Mon, 03 Aug 2026 16:56:25 -0700 |
| parents | 0e7b9464248d |
| children | 117c4d53c9a4 |
comparison
equal
deleted
inserted
replaced
| 243:823f2a8b16c8 | 244:b8aa08503378 |
|---|---|
| 15 #else | 15 #else |
| 16 (void)socket_fd; | 16 (void)socket_fd; |
| 17 #endif | 17 #endif |
| 18 } | 18 } |
| 19 | 19 |
| 20 static void Seobeo_Socket_Set_Close_On_Exec(int socket_fd) | |
| 21 { | |
| 22 int flags = fcntl(socket_fd, F_GETFD, 0); | |
| 23 if (flags >= 0) | |
| 24 fcntl(socket_fd, F_SETFD, flags | FD_CLOEXEC); | |
| 25 } | |
| 26 | |
| 20 static ssize_t Seobeo_Socket_Write( | 27 static ssize_t Seobeo_Socket_Write( |
| 21 int socket_fd, | 28 int socket_fd, |
| 22 const void *buffer, | 29 const void *buffer, |
| 23 size_t length) | 30 size_t length) |
| 24 { | 31 { |
| 50 free_server_info = server_infos; | 57 free_server_info = server_infos; |
| 51 free_server_info != NULL; | 58 free_server_info != NULL; |
| 52 free_server_info = free_server_info->ai_next | 59 free_server_info = free_server_info->ai_next |
| 53 ) | 60 ) |
| 54 { | 61 { |
| 62 int socket_type = free_server_info->ai_socktype; | |
| 63 #ifdef SOCK_CLOEXEC | |
| 64 socket_type |= SOCK_CLOEXEC; | |
| 65 #endif | |
| 55 if((socket_fd = socket(free_server_info->ai_family, | 66 if((socket_fd = socket(free_server_info->ai_family, |
| 56 free_server_info->ai_socktype, free_server_info->ai_protocol)) == -1) | 67 socket_type, free_server_info->ai_protocol)) == -1) |
| 57 { perror("socket"); continue; } | 68 { perror("socket"); continue; } |
| 69 Seobeo_Socket_Set_Close_On_Exec(socket_fd); | |
| 58 Seobeo_Socket_Disable_Sigpipe(socket_fd); | 70 Seobeo_Socket_Disable_Sigpipe(socket_fd); |
| 59 | 71 |
| 60 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) | 72 if (setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1) |
| 61 { perror("setsockopt SO_REUSEADDR"); continue; } | 73 { perror("setsockopt SO_REUSEADDR"); continue; } |
| 62 | 74 |
| 124 | 136 |
| 125 if (getaddrinfo(host, port, &hints, &server_infos) != 0) | 137 if (getaddrinfo(host, port, &hints, &server_infos) != 0) |
| 126 { perror("getaddrinfo"); return NULL; } | 138 { perror("getaddrinfo"); return NULL; } |
| 127 | 139 |
| 128 | 140 |
| 141 int socket_type = server_infos->ai_socktype; | |
| 142 #ifdef SOCK_CLOEXEC | |
| 143 socket_type |= SOCK_CLOEXEC; | |
| 144 #endif | |
| 129 if((socket_fd = socket(server_infos->ai_family, | 145 if((socket_fd = socket(server_infos->ai_family, |
| 130 server_infos->ai_socktype, server_infos->ai_protocol)) == -1) | 146 socket_type, server_infos->ai_protocol)) == -1) |
| 131 { perror("socket"); return NULL; } | 147 { perror("socket"); return NULL; } |
| 148 Seobeo_Socket_Set_Close_On_Exec(socket_fd); | |
| 132 Seobeo_Socket_Disable_Sigpipe(socket_fd); | 149 Seobeo_Socket_Disable_Sigpipe(socket_fd); |
| 133 | 150 |
| 134 if (connect(socket_fd, server_infos->ai_addr, server_infos->ai_addrlen) != 0) | 151 if (connect(socket_fd, server_infos->ai_addr, server_infos->ai_addrlen) != 0) |
| 135 { perror("connect"); return NULL; } | 152 { perror("connect"); return NULL; } |
| 136 freeaddrinfo(server_infos); | 153 freeaddrinfo(server_infos); |
| 176 { | 193 { |
| 177 struct sockaddr_storage addr; | 194 struct sockaddr_storage addr; |
| 178 socklen_t addrlen = sizeof addr; | 195 socklen_t addrlen = sizeof addr; |
| 179 char client_inet_addr[INET6_ADDRSTRLEN]; | 196 char client_inet_addr[INET6_ADDRSTRLEN]; |
| 180 | 197 |
| 181 int client_fd = accept(p_server_handle->socket, | 198 int client_fd = accept( |
| 182 (struct sockaddr*)&addr, | 199 p_server_handle->socket, |
| 183 &addrlen); | 200 (struct sockaddr*)&addr, |
| 201 &addrlen); | |
| 184 inet_ntop( | 202 inet_ntop( |
| 185 addr.ss_family, | 203 addr.ss_family, |
| 186 Seobeo_Get_IP4_Or_IP6((struct sockaddr *)&addr), | 204 Seobeo_Get_IP4_Or_IP6((struct sockaddr *)&addr), |
| 187 client_inet_addr, sizeof client_inet_addr); | 205 client_inet_addr, sizeof client_inet_addr); |
| 188 if (client_fd == -1) return NULL; | 206 if (client_fd == -1) return NULL; |
| 207 Seobeo_Socket_Set_Close_On_Exec(client_fd); | |
| 189 Seobeo_Socket_Disable_Sigpipe(client_fd); | 208 Seobeo_Socket_Disable_Sigpipe(client_fd); |
| 190 | 209 |
| 191 // Set non blocking... | 210 // Set non blocking... |
| 192 int flags = fcntl(client_fd, F_GETFL, 0); | 211 int flags = fcntl(client_fd, F_GETFL, 0); |
| 193 if (flags == -1) return NULL; | 212 if (flags == -1) return NULL; |