Fix the build with GCC and libstdc++

crypto.h's min/max macros broke <algorithm> in C++ sources that include
it first, netutils.h's linux/tcp.h clashed with the netinet/tcp.h libuv
includes, and GCC rejects memcpy into crypto_t. Apple clang accepted all
three.
This commit is contained in:
Max Lv
2026-10-14 22:53:09 +08:00
parent 46f5ddcf80
commit c5c8e5ebaf
8 changed files with 15 additions and 1825 deletions
+3 -3
View File
@@ -57,7 +57,7 @@ brealloc(buffer_t *ptr, size_t len, size_t capacity)
{
if (ptr == NULL)
return -1;
size_t real_capacity = max(len, capacity);
size_t real_capacity = len > capacity ? len : capacity;
if (ptr->capacity < real_capacity) {
ptr->data = (char *)ss_realloc(ptr->data, real_capacity);
ptr->capacity = real_capacity;
@@ -171,7 +171,7 @@ crypto_init(const char *password, const char *key, const char *method)
.ctx_init = &stream_ctx_init,
.ctx_release = &stream_ctx_release,
};
memcpy(crypto, &tmp, sizeof(crypto_t));
memcpy((void *)crypto, &tmp, sizeof(crypto_t)); /* const members: copy the bytes */
return crypto;
}
@@ -200,7 +200,7 @@ crypto_init(const char *password, const char *key, const char *method)
.udp_session_release = aead_is_2022(cipher)
? &aead_2022_udp_session_release : NULL,
};
memcpy(crypto, &tmp, sizeof(crypto_t));
memcpy((void *)crypto, &tmp, sizeof(crypto_t)); /* const members: copy the bytes */
return crypto;
}
}
+3
View File
@@ -69,8 +69,11 @@ typedef mbedtls_md_info_t digest_type_t;
#define CRYPTO_NEED_MORE -1
#define CRYPTO_OK 0
/* Not for C++, where they would break <algorithm>. */
#ifndef __cplusplus
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#endif
#define SUBKEY_INFO "ss-subkey"
#define IV_INFO "ss-iv"
+1 -1
View File
@@ -1042,7 +1042,7 @@ manager_recv_cb(SS_P_ ss_io *w, int revents)
}
size_t pos = strlen(buf);
strcpy(buf + max(pos - 1, 1), "\n]"); // Remove trailing ","
strcpy(buf + (pos > 2 ? pos - 1 : 1), "\n]"); // Remove trailing ","
pos = strlen(buf);
if (sendto(manager->fd, buf, pos, 0, (struct sockaddr *)&claddr, len)
!= pos) {
+5 -3
View File
@@ -31,10 +31,12 @@
#include <netinet/in.h>
#endif
#if defined(HAVE_LINUX_TCP_H)
#include <linux/tcp.h>
#elif defined(HAVE_NETINET_TCP_H)
/* netinet/tcp.h first: libuv includes it, and linux/tcp.h redefines its
* structures. */
#if defined(HAVE_NETINET_TCP_H)
#include <netinet/tcp.h>
#elif defined(HAVE_LINUX_TCP_H)
#include <linux/tcp.h>
#elif defined(HAVE_NETDB_H)
#include <netdb.h>
#endif
-86
View File
@@ -1,86 +0,0 @@
/* * redir.h - Define the redirector's buffers and callbacks
*
* Copyright (C) 2013 - 2019, Max Lv <max.c.lv@gmail.com>
*
* This file is part of the shadowsocks-libev.
*
* shadowsocks-libev is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* shadowsocks-libev is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with shadowsocks-libev; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef _REDIR_H
#define _REDIR_H
#include "ss_event.h"
#include "crypto.h"
#include "jconf.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct listen_ctx {
ss_io io;
int remote_num;
int timeout;
int fd;
int mptcp;
int tos;
struct sockaddr **remote_addr;
} listen_ctx_t;
typedef struct server_ctx {
ss_io io;
int connected;
struct server *server;
} server_ctx_t;
typedef struct server {
int fd;
buffer_t *buf;
cipher_ctx_t *e_ctx;
cipher_ctx_t *d_ctx;
struct server_ctx *recv_ctx;
struct server_ctx *send_ctx;
struct remote *remote;
struct sockaddr_storage destaddr;
ss_timer delayed_connect_watcher;
} server_t;
typedef struct remote_ctx {
ss_io io;
ss_timer watcher;
int connected;
struct remote *remote;
} remote_ctx_t;
typedef struct remote {
int fd;
buffer_t *buf;
struct remote_ctx *recv_ctx;
struct remote_ctx *send_ctx;
struct server *server;
uint32_t counter;
struct sockaddr *addr;
} remote_t;
#ifdef __cplusplus
}
#endif
#endif // _REDIR_H
+3 -2
View File
@@ -504,8 +504,9 @@ stream_decrypt(buffer_t *ciphertext, cipher_ctx_t *cipher_ctx, size_t capacity)
balloc(cipher_ctx->chunk, cipher->nonce_len);
}
size_t left_len = min(cipher->nonce_len - cipher_ctx->chunk->len,
ciphertext->len);
size_t left_len = cipher->nonce_len - cipher_ctx->chunk->len;
if (left_len > ciphertext->len)
left_len = ciphertext->len;
if (left_len > 0) {
memcpy(cipher_ctx->chunk->data + cipher_ctx->chunk->len, ciphertext->data, left_len);
-1626
View File
File diff suppressed because it is too large Load Diff
-104
View File
@@ -1,104 +0,0 @@
/*
* udprelay.h - Define UDP relay's buffers and callbacks
*
* Copyright (C) 2013 - 2019, Max Lv <max.c.lv@gmail.com>
*
* This file is part of the shadowsocks-libev.
*
* shadowsocks-libev is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* shadowsocks-libev is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with shadowsocks-libev; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef _UDPRELAY_H
#define _UDPRELAY_H
#include <time.h>
#include "ss_event.h"
#include "crypto.h"
#include "jconf.h"
#ifdef MODULE_REMOTE
#include "resolv.h"
#endif
#include "cache.h"
#include "common.h"
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_UDP_PACKET_SIZE (65507)
#define PACKET_HEADER_SIZE (1 + 28 + 2 + 64)
#define DEFAULT_PACKET_SIZE 1397 // 1492 - PACKET_HEADER_SIZE = 1397, the default MTU for UDP relay
#define MAX_ADDR_HEADER_SIZE (1 + 256 + 2) // 1-byte atyp + 256-byte hostname + 2-byte port
typedef struct server_ctx {
ss_io io;
int fd;
crypto_t *crypto;
int timeout;
const char *iface;
struct cache *conn_cache;
#ifdef MODULE_LOCAL
const struct sockaddr *remote_addr;
int remote_addr_len;
#ifdef MODULE_TUNNEL
ss_addr_t tunnel_addr;
#endif
#endif
#ifdef MODULE_REMOTE
struct ss_loop *loop;
#endif
} server_ctx_t;
#ifdef MODULE_REMOTE
typedef struct query_ctx {
struct sockaddr_storage src_addr;
buffer_t *buf;
int addr_header_len;
char addr_header[MAX_ADDR_HEADER_SIZE];
struct server_ctx *server_ctx;
struct remote_ctx *remote_ctx;
void *udp_session; /* Owned until transferred to the resolved relay. */
} query_ctx_t;
#endif
typedef struct remote_ctx {
ss_io io;
ss_timer watcher;
int af;
int fd;
struct sockaddr_storage src_addr;
#ifdef MODULE_REMOTE
struct sockaddr_storage dst_addr;
#endif
/*
* AEAD-2022 UDP session state for this conversation, owned by the
* crypto layer and released with crypto->udp_session_release.
* NULL for legacy ciphers.
*/
void *udp_session;
struct server_ctx *server_ctx;
} remote_ctx_t;
#ifdef __cplusplus
}
#endif
#endif // _UDPRELAY_H