【问题标题】:How can I cast a socket to an SSLConnectionRef without getting a warning?如何在不收到警告的情况下将套接字转换为 SSLConnectionRef?
【发布时间】:2014-06-27 10:07:56
【问题描述】:

我正在学习如何使用 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,对吧?所以这应该是安全的。我只是担心编译器警告。感谢您的任何建议。

【问题讨论】:

    标签: ios macos sockets secure-transport


    【解决方案1】:

    你应该这样做

    SSLSetConnection(sslContext, (SSLConnectionRef)(intptr_t)sockfd);
    

    【讨论】:

      【解决方案2】:

      (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;

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-02-16
        • 2019-11-14
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        相关资源
        最近更新 更多