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