quic_record_rx_wrap.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "internal/refcount.h"
  11. #include "internal/quic_record_rx_wrap.h"
  12. OSSL_QRX_PKT_WRAP *ossl_qrx_pkt_wrap_new(OSSL_QRX_PKT *pkt)
  13. {
  14. CRYPTO_RWLOCK *refcount_lock = NULL;
  15. OSSL_QRX_PKT_WRAP *res = NULL;
  16. if (pkt == NULL)
  17. return NULL;
  18. #ifdef HAVE_ATOMICS
  19. refcount_lock = CRYPTO_THREAD_lock_new();
  20. if (refcount_lock == NULL) {
  21. ERR_raise(ERR_LIB_SSL, ERR_R_CRYPTO_LIB);
  22. return NULL;
  23. }
  24. #endif
  25. if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
  26. CRYPTO_THREAD_lock_free(refcount_lock);
  27. return NULL;
  28. }
  29. res->pkt = pkt;
  30. res->handle = pkt->handle;
  31. res->references = 1;
  32. res->lock = refcount_lock;
  33. return res;
  34. }
  35. int ossl_qrx_pkt_wrap_up_ref(OSSL_QRX_PKT_WRAP *pkt_wrap)
  36. {
  37. int ref = 0;
  38. if (pkt_wrap == NULL || pkt_wrap->pkt == NULL)
  39. return 0;
  40. CRYPTO_UP_REF(&pkt_wrap->references, &ref, pkt_wrap->lock);
  41. return 1;
  42. }
  43. void ossl_qrx_pkt_wrap_free(OSSL_QRX *qrx, OSSL_QRX_PKT_WRAP *pkt_wrap)
  44. {
  45. int ref = 0;
  46. if (pkt_wrap == NULL)
  47. return;
  48. CRYPTO_DOWN_REF(&pkt_wrap->references, &ref, pkt_wrap->lock);
  49. if (ref > 0)
  50. return;
  51. ossl_qrx_release_pkt(qrx, pkt_wrap->handle);
  52. CRYPTO_THREAD_lock_free(pkt_wrap->lock);
  53. OPENSSL_free(pkt_wrap);
  54. }