]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
smtp: add tracing feature
authorStefan Eissing <stefan@eissing.org>
Tue, 13 Aug 2024 11:49:07 +0000 (13:49 +0200)
committerDaniel Stenberg <daniel@haxx.se>
Wed, 14 Aug 2024 09:51:55 +0000 (11:51 +0200)
Add `smtp` as tracing feature, use CURL_TRC_SMTP() in code to
trace operations.

Closes #14531

docs/libcurl/curl_global_trace.md
lib/curl_trc.c
lib/curl_trc.h
lib/smtp.c

index 51b10fb41361f6f6950803e6fe3aa55ea060e446..bf722ae6f93db3c17a52986376b9db684191cb32 100644 (file)
@@ -105,6 +105,10 @@ Tracing of DNS-over-HTTP operations to resolve hostnames.
 
 Traces reading of upload data from the application in order to send it to the server.
 
+## `smtp`
+
+Tracing of SMTP operations when this protocol is enabled in your build.
+
 ## `write`
 
 Traces writing of download data, received from the server, to the application.
index 73748729ef1824a9f5695b42e2ff6483d1731bcb..58512d74d058628a1699433591f0fca86f1d37fc 100644 (file)
@@ -221,6 +221,24 @@ void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
 }
 #endif /* !CURL_DISABLE_FTP */
 
+#ifndef CURL_DISABLE_SMTP
+struct curl_trc_feat Curl_trc_feat_smtp = {
+  "SMTP",
+  CURL_LOG_LVL_NONE,
+};
+
+void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...)
+{
+  DEBUGASSERT(!strchr(fmt, '\n'));
+  if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_smtp)) {
+    va_list ap;
+    va_start(ap, fmt);
+    trc_infof(data, &Curl_trc_feat_smtp, fmt, ap);
+    va_end(ap);
+  }
+}
+#endif /* !CURL_DISABLE_SMTP */
+
 #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
 struct curl_trc_feat Curl_trc_feat_ws = {
   "WS",
@@ -258,6 +276,9 @@ static struct trc_feat_def trc_feats[] = {
 #ifndef CURL_DISABLE_DOH
   { &Curl_doh_trc,            TRC_CT_NETWORK },
 #endif
+#ifndef CURL_DISABLE_SMTP
+  { &Curl_trc_feat_smtp,      TRC_CT_PROTOCOL },
+#endif
 #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
   { &Curl_trc_feat_ws,        TRC_CT_PROTOCOL },
 #endif
index c98234c6ac2e7d7748e2d38f50e6cc2ad097ccf5..5f675b453fd3fbf277ae70cafa544c6d60852c31 100644 (file)
@@ -89,6 +89,11 @@ void Curl_failf(struct Curl_easy *data,
   do { if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ftp)) \
          Curl_trc_ftp(data, __VA_ARGS__); } while(0)
 #endif /* !CURL_DISABLE_FTP */
+#ifndef CURL_DISABLE_SMTP
+#define CURL_TRC_SMTP(data, ...) \
+  do { if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_smtp)) \
+         Curl_trc_smtp(data, __VA_ARGS__); } while(0)
+#endif /* !CURL_DISABLE_SMTP */
 #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
 #define CURL_TRC_WS(data, ...) \
   do { if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_ws)) \
@@ -105,6 +110,9 @@ void Curl_failf(struct Curl_easy *data,
 #ifndef CURL_DISABLE_FTP
 #define CURL_TRC_FTP   Curl_trc_ftp
 #endif
+#ifndef CURL_DISABLE_SMTP
+#define CURL_TRC_SMTP  Curl_trc_smtp
+#endif
 #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
 #define CURL_TRC_WS    Curl_trc_ws
 #endif
@@ -156,6 +164,11 @@ extern struct curl_trc_feat Curl_trc_feat_ftp;
 void Curl_trc_ftp(struct Curl_easy *data,
                   const char *fmt, ...) CURL_PRINTF(2, 3);
 #endif
+#ifndef CURL_DISABLE_SMTP
+extern struct curl_trc_feat Curl_trc_feat_smtp;
+void Curl_trc_smtp(struct Curl_easy *data,
+                   const char *fmt, ...) CURL_PRINTF(2, 3);
+#endif
 #if defined(USE_WEBSOCKETS) && !defined(CURL_DISABLE_HTTP)
 extern struct curl_trc_feat Curl_trc_feat_ws;
 void Curl_trc_ws(struct Curl_easy *data,
@@ -207,6 +220,12 @@ static void Curl_trc_ftp(struct Curl_easy *data, const char *fmt, ...)
   (void)data; (void)fmt;
 }
 #endif
+#ifndef CURL_DISABLE_SMTP
+static void Curl_trc_smtp(struct Curl_easy *data, const char *fmt, ...)
+{
+  (void)data; (void)fmt;
+}
+#endif
 
 #endif /* !defined(CURL_DISABLE_VERBOSE_STRINGS) */
 
index 5ee1b5ea37ac4c97a3483a326f3c1b15f556086d..0e7140f357e896c79947442c8800d2356f4cd940 100644 (file)
@@ -288,7 +288,7 @@ static CURLcode smtp_get_message(struct Curl_easy *data, struct bufref *out)
 static void smtp_state(struct Curl_easy *data, smtpstate newstate)
 {
   struct smtp_conn *smtpc = &data->conn->proto.smtpc;
-#if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
+#if !defined(CURL_DISABLE_VERBOSE_STRINGS)
   /* for debug purposes */
   static const char * const names[] = {
     "STOP",
@@ -308,8 +308,8 @@ static void smtp_state(struct Curl_easy *data, smtpstate newstate)
   };
 
   if(smtpc->state != newstate)
-    infof(data, "SMTP %p state change from %s to %s",
-          (void *)smtpc, names[smtpc->state], names[newstate]);
+    CURL_TRC_SMTP(data, "state change from %s to %s",
+                  names[smtpc->state], names[newstate]);
 #endif
 
   smtpc->state = newstate;
@@ -1422,7 +1422,8 @@ static CURLcode smtp_done(struct Curl_easy *data, CURLcode status,
 
   /* Clear the transfer mode for the next request */
   smtp->transfer = PPTRANSFER_BODY;
-
+  CURL_TRC_SMTP(data, "smtp_done(status=%d, premature=%d) -> %d",
+                status, premature, result);
   return result;
 }
 
@@ -1440,7 +1441,7 @@ static CURLcode smtp_perform(struct Curl_easy *data, bool *connected,
   CURLcode result = CURLE_OK;
   struct SMTP *smtp = data->req.p.smtp;
 
-  DEBUGF(infof(data, "DO phase starts"));
+  CURL_TRC_SMTP(data, "smtp_perform(), start");
 
   if(data->req.no_body) {
     /* Requested no body means no transfer */
@@ -1472,16 +1473,16 @@ static CURLcode smtp_perform(struct Curl_easy *data, bool *connected,
     result = smtp_perform_command(data);
 
   if(result)
-    return result;
+    goto out;
 
   /* Run the state-machine */
   result = smtp_multi_statemach(data, dophase_done);
 
   *connected = Curl_conn_is_connected(data->conn, FIRSTSOCKET);
 
-  if(*dophase_done)
-    DEBUGF(infof(data, "DO phase is complete"));
-
+out:
+  CURL_TRC_SMTP(data, "smtp_perform() -> %d, connected=%d, done=%d",
+                result, *connected, *dophase_done);
   return result;
 }
 
@@ -1507,7 +1508,7 @@ static CURLcode smtp_do(struct Curl_easy *data, bool *done)
     return result;
 
   result = smtp_regular_transfer(data, done);
-
+  CURL_TRC_SMTP(data, "smtp_do() -> %d, done=%d", result, *done);
   return result;
 }
 
@@ -1542,6 +1543,7 @@ static CURLcode smtp_disconnect(struct Curl_easy *data,
 
   /* Cleanup our connection based variables */
   Curl_safefree(smtpc->domain);
+  CURL_TRC_SMTP(data, "smtp_disconnect(), finished");
 
   return CURLE_OK;
 }
@@ -1573,6 +1575,7 @@ static CURLcode smtp_doing(struct Curl_easy *data, bool *dophase_done)
     DEBUGF(infof(data, "DO phase is complete"));
   }
 
+  CURL_TRC_SMTP(data, "smtp_doing() -> %d, done=%d", result, *dophase_done);
   return result;
 }
 
@@ -1607,6 +1610,8 @@ static CURLcode smtp_regular_transfer(struct Curl_easy *data,
   if(!result && *dophase_done)
     result = smtp_dophase_done(data, connected);
 
+  CURL_TRC_SMTP(data, "smtp_regular_transfer() -> %d, done=%d",
+                result, *dophase_done);
   return result;
 }
 
@@ -1620,10 +1625,8 @@ static CURLcode smtp_setup_connection(struct Curl_easy *data,
 
   /* Initialise the SMTP layer */
   result = smtp_init(data);
-  if(result)
-    return result;
-
-  return CURLE_OK;
+  CURL_TRC_SMTP(data, "smtp_setup_connection() -> %d", result);
+  return result;
 }
 
 /***********************************************************************