1

我正在学习如何使用 Apple 的安全传输框架为我的网络实现 TLS,这有点令人困惑。我相信我应该只是将我的套接字 fds 转换为 SSLConnectionRefs,但是当我这样做时会收到警告Cast to 'SSLConnectionRef' (aka 'const void *') from smaller integer type 'int'

int sockfd = socket(...);
...
SSLContextRef sslContext = SSLCreateContext(...);

// This line gives the warning
SSLSetConnection(sslContext, (SSLConnectionRef)sockfd);

我不会在这里丢失任何信息,因为void *大于int,对吗?所以这应该是安全的。我只是担心编译器警告。感谢您的任何建议。

4

2 回答 2

3

你应该做

SSLSetConnection(sslContext, (SSLConnectionRef)(intptr_t)sockfd);
于 2015-06-04T15:44:45.433 回答
1

(SSLConnectionRef)(long)sockfd工作并且应该是安全的,只要sizeof(void*) > sizeof(int)这对所有当前的编译器都是正确的,但不一定保证。

另一种方法是暂时禁用警告:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wint-to-point-cast"
    SSLSetConnection(sslContext, (SSLConnectionRef)sockfd);
#pragma clang diagnostic pop

最终,“正确”的解决方案是实际传递一个指向您通过 malloc 或其他方案分配的整数的指针。如果这一切都在一个对象中,则将 fd 存储在实例变量中并传入 &_sockfd;

于 2014-05-09T15:42:40.167 回答