vquic.c 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef ENABLE_QUIC
  24. #ifdef HAVE_FCNTL_H
  25. #include <fcntl.h>
  26. #endif
  27. #include "urldata.h"
  28. #include "dynbuf.h"
  29. #include "curl_printf.h"
  30. #include "vquic.h"
  31. #ifdef O_BINARY
  32. #define QLOGMODE O_WRONLY|O_CREAT|O_BINARY
  33. #else
  34. #define QLOGMODE O_WRONLY|O_CREAT
  35. #endif
  36. /*
  37. * If the QLOGDIR environment variable is set, open and return a file
  38. * descriptor to write the log to.
  39. *
  40. * This function returns error if something failed outside of failing to
  41. * create the file. Open file success is deemed by seeing if the returned fd
  42. * is != -1.
  43. */
  44. CURLcode Curl_qlogdir(struct Curl_easy *data,
  45. unsigned char *scid,
  46. size_t scidlen,
  47. int *qlogfdp)
  48. {
  49. const char *qlog_dir = getenv("QLOGDIR");
  50. *qlogfdp = -1;
  51. if(qlog_dir) {
  52. struct dynbuf fname;
  53. CURLcode result;
  54. unsigned int i;
  55. Curl_dyn_init(&fname, DYN_QLOG_NAME);
  56. result = Curl_dyn_add(&fname, qlog_dir);
  57. if(!result)
  58. result = Curl_dyn_add(&fname, "/");
  59. for(i = 0; (i < scidlen) && !result; i++) {
  60. char hex[3];
  61. msnprintf(hex, 3, "%02x", scid[i]);
  62. result = Curl_dyn_add(&fname, hex);
  63. }
  64. if(!result)
  65. result = Curl_dyn_add(&fname, ".qlog");
  66. if(!result) {
  67. int qlogfd = open(Curl_dyn_ptr(&fname), QLOGMODE,
  68. data->set.new_file_perms);
  69. if(qlogfd != -1)
  70. *qlogfdp = qlogfd;
  71. }
  72. Curl_dyn_free(&fname);
  73. if(result)
  74. return result;
  75. }
  76. return CURLE_OK;
  77. }
  78. #endif