vquic.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef ENABLE_QUIC
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #endif
  29. #include "urldata.h"
  30. #include "dynbuf.h"
  31. #include "curl_printf.h"
  32. #include "vquic.h"
  33. #ifdef O_BINARY
  34. #define QLOGMODE O_WRONLY|O_CREAT|O_BINARY
  35. #else
  36. #define QLOGMODE O_WRONLY|O_CREAT
  37. #endif
  38. /*
  39. * If the QLOGDIR environment variable is set, open and return a file
  40. * descriptor to write the log to.
  41. *
  42. * This function returns error if something failed outside of failing to
  43. * create the file. Open file success is deemed by seeing if the returned fd
  44. * is != -1.
  45. */
  46. CURLcode Curl_qlogdir(struct Curl_easy *data,
  47. unsigned char *scid,
  48. size_t scidlen,
  49. int *qlogfdp)
  50. {
  51. const char *qlog_dir = getenv("QLOGDIR");
  52. *qlogfdp = -1;
  53. if(qlog_dir) {
  54. struct dynbuf fname;
  55. CURLcode result;
  56. unsigned int i;
  57. Curl_dyn_init(&fname, DYN_QLOG_NAME);
  58. result = Curl_dyn_add(&fname, qlog_dir);
  59. if(!result)
  60. result = Curl_dyn_add(&fname, "/");
  61. for(i = 0; (i < scidlen) && !result; i++) {
  62. char hex[3];
  63. msnprintf(hex, 3, "%02x", scid[i]);
  64. result = Curl_dyn_add(&fname, hex);
  65. }
  66. if(!result)
  67. result = Curl_dyn_add(&fname, ".sqlog");
  68. if(!result) {
  69. int qlogfd = open(Curl_dyn_ptr(&fname), QLOGMODE,
  70. data->set.new_file_perms);
  71. if(qlogfd != -1)
  72. *qlogfdp = qlogfd;
  73. }
  74. Curl_dyn_free(&fname);
  75. if(result)
  76. return result;
  77. }
  78. return CURLE_OK;
  79. }
  80. #endif