utils_net.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2012-2019 UCloud. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. //based on Alibaba c-sdk
  16. /*
  17. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  18. */
  19. #ifndef C_SDK_UTILS_NET_H_
  20. #define C_SDK_UTILS_NET_H_
  21. #include <stdint.h>
  22. #include <stddef.h>
  23. /**
  24. * @brief The structure of network connection(TCP or SSL).
  25. * The user has to allocate memory for this structure.
  26. */
  27. struct utils_network;
  28. typedef struct utils_network utils_network_t, *utils_network_pt;
  29. typedef enum
  30. {
  31. SSL_CA_VERIFY_NONE = 0,
  32. SSL_CA_VERIFY_OPTIONAL = 1,
  33. SSL_CA_VERIFY_REQUIRED = 2,
  34. SSL_CA_VERIFY_UNSET = 3,
  35. }SSL_AUTH_MODE;
  36. struct utils_network {
  37. const char *pHostAddress;
  38. uint16_t port;
  39. uint16_t ca_crt_len;
  40. uint16_t authmode;
  41. /**< NULL, TCP connection; NOT NULL, SSL connection */
  42. const char *ca_crt;
  43. /**< connection handle: 0, NOT connection; NOT 0, handle of the connection */
  44. uintptr_t handle;
  45. /**< Read data from server function pointer. */
  46. int (*read)(utils_network_pt,unsigned char *, size_t, uint32_t);
  47. /**< Send data to server function pointer. */
  48. int (*write)(utils_network_pt,unsigned char *, size_t, uint32_t);
  49. /**< Disconnect the network */
  50. int (*disconnect)(utils_network_pt);
  51. /**< Establish the network */
  52. int (*connect)(utils_network_pt);
  53. };
  54. int utils_net_read(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms);
  55. int utils_net_write(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms);
  56. int utils_net_disconnect(utils_network_pt pNetwork);
  57. int utils_net_connect(utils_network_pt pNetwork);
  58. int utils_net_init(utils_network_pt pNetwork, const char *host, uint16_t port, uint16_t authmode, const char *ca_crt);
  59. #endif /* C_SDK_UTILS_NET_H_ */