]> granicus.if.org Git - rtmpdump/commitdiff
Simplify the logic for freeing reused strings in RTMP_Close
authorMartin Storsjo <martin@martin.st>
Sat, 5 Jul 2014 21:58:26 +0000 (00:58 +0300)
committerMartin Storsjo <martin@martin.st>
Mon, 7 Jul 2014 07:15:59 +0000 (10:15 +0300)
Instead of having a plethora of flags indicating the current
state (just to avoid freeing them when closing the previous
connection when reconnecting), split RTMP_Close into CloseInternal,
taking a flag saying whether this is a reconnect or a normal,
final RTMP_Close call.

This also avoids leaks of some strings, if the whole session is
closed while the authentication process is underway.

librtmp/rtmp.c
librtmp/rtmp.h

index 75906be96da9998e614a7b1ad71a5809d0d23f65..32aaa297fd22056481a398b088ed93a392898c68 100644 (file)
@@ -142,6 +142,8 @@ static void DecodeTEA(AVal *key, AVal *text);
 static int HTTP_Post(RTMP *r, RTMPTCmd cmd, const char *buf, int len);
 static int HTTP_read(RTMP *r, int fill);
 
+static void CloseInternal(RTMP *r, int reconnect);
+
 #ifndef _WIN32
 static int clk_tck;
 #endif
@@ -2533,7 +2535,6 @@ PublisherAuth(RTMP *r, AVal *description)
   AVal pubToken;
 
   // If tcUrl is allocated, set RTMP_PUB_ALLOC instead to simplify checks below.
-  // This also avoids accidentally freeing tcUrl in RTMP_Close when reconnecting.
   if (r->Link.lFlags & RTMP_LF_FTCU && !(r->Link.pFlags & RTMP_PUB_ALLOC))
     {
       ptr = malloc(r->Link.app.av_len + 1);
@@ -2551,7 +2552,6 @@ PublisherAuth(RTMP *r, AVal *description)
         {
             if (strstr(r->Link.app.av_val, av_authmod_adobe.av_val) != NULL) {
               RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__);
-              r->Link.pFlags |= RTMP_PUB_CLEAN;
               return 0;
             } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) {
               pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_adobe.av_len + 8);
@@ -2559,10 +2559,8 @@ PublisherAuth(RTMP *r, AVal *description)
                       av_authmod_adobe.av_val,
                       r->Link.pubUser.av_val);
               RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val);
-              r->Link.pFlags |= RTMP_PUB_NAME;
             } else {
               RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__);
-              r->Link.pFlags |= RTMP_PUB_CLEAN;
               return 0;
             }
         }
@@ -2652,25 +2650,21 @@ PublisherAuth(RTMP *r, AVal *description)
                     opaque.av_len ? opaque.av_val : "");
             RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val);
             free(orig_ptr);
-            r->Link.pFlags |= RTMP_PUB_RESP|RTMP_PUB_CLATE;
         }
       else if(strstr(description->av_val, "?reason=authfailed") != NULL)
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: wrong password", __FUNCTION__);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
       else if(strstr(description->av_val, "?reason=nosuchuser") != NULL)
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
       else
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s",
                   __FUNCTION__, description->av_val);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
 
@@ -2706,7 +2700,6 @@ PublisherAuth(RTMP *r, AVal *description)
 
             if (strstr(r->Link.app.av_val, av_authmod_llnw.av_val) != NULL) {
               RTMP_Log(RTMP_LOGERROR, "%s, wrong pubUser & pubPasswd for publisher auth", __FUNCTION__);
-              r->Link.pFlags |= RTMP_PUB_CLEAN;
               return 0;
             } else if(r->Link.pubUser.av_len && r->Link.pubPasswd.av_len) {
               pubToken.av_val = malloc(r->Link.pubUser.av_len + av_authmod_llnw.av_len + 8);
@@ -2714,10 +2707,8 @@ PublisherAuth(RTMP *r, AVal *description)
                       av_authmod_llnw.av_val,
                       r->Link.pubUser.av_val);
               RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken1: %s", __FUNCTION__, pubToken.av_val);
-              r->Link.pFlags |= RTMP_PUB_NAME;
             } else {
               RTMP_Log(RTMP_LOGERROR, "%s, need to set pubUser & pubPasswd for publisher auth", __FUNCTION__);
-              r->Link.pFlags |= RTMP_PUB_CLEAN;
               return 0;
             }
         }
@@ -2841,27 +2832,23 @@ PublisherAuth(RTMP *r, AVal *description)
                   nonce.av_val, cnonce, nchex, hash3);
           pubToken.av_len = strlen(pubToken.av_val);
           RTMP_Log(RTMP_LOGDEBUG, "%s, pubToken2: %s", __FUNCTION__, pubToken.av_val);
-          r->Link.pFlags |= RTMP_PUB_RESP|RTMP_PUB_CLATE;
 
           free(orig_ptr);
         }
       else if(strstr(description->av_val, "?reason=authfail") != NULL)
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed", __FUNCTION__);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
       else if(strstr(description->av_val, "?reason=nosuchuser") != NULL)
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: no such user", __FUNCTION__);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
       else
         {
           RTMP_Log(RTMP_LOGERROR, "%s, Authentication failed: unknown auth mode: %s",
                   __FUNCTION__, description->av_val);
-          r->Link.pFlags |= RTMP_PUB_CLEAN;
           return 0;
         }
 
@@ -3106,9 +3093,7 @@ HandleInvoke(RTMP *r, const char *body, unsigned int nBodySize)
               /* if PublisherAuth returns 1, then reconnect */
               if (PublisherAuth(r, &description) == 1)
               {
-                RTMP_Close(r);
-                if (r->Link.pFlags & RTMP_PUB_CLATE)
-                    r->Link.pFlags |= RTMP_PUB_CLEAN;
+                CloseInternal(r, 1);
                 if (!RTMP_Connect(r, NULL) || !RTMP_ConnectStream(r, 0))
                   goto leave;
               }
@@ -4139,6 +4124,12 @@ RTMP_Serve(RTMP *r)
 
 void
 RTMP_Close(RTMP *r)
+{
+  CloseInternal(r, 0);
+}
+
+static void
+CloseInternal(RTMP *r, int reconnect)
 {
   int i;
 
@@ -4226,15 +4217,14 @@ RTMP_Close(RTMP *r)
       r->Link.lFlags ^= RTMP_LF_FTCU;
     }
 
-#ifdef CRYPTO
-  if (!(r->Link.protocol & RTMP_FEATURE_WRITE) || (r->Link.pFlags & RTMP_PUB_CLEAN))
+  if (!reconnect)
     {
       free(r->Link.playpath0.av_val);
       r->Link.playpath0.av_val = NULL;
     }
+#ifdef CRYPTO
   if ((r->Link.protocol & RTMP_FEATURE_WRITE) &&
-      (r->Link.pFlags & RTMP_PUB_CLEAN) &&
-      (r->Link.pFlags & RTMP_PUB_ALLOC))
+      (r->Link.pFlags & RTMP_PUB_ALLOC) && !reconnect)
     {
       free(r->Link.app.av_val);
       r->Link.app.av_val = NULL;
@@ -4256,9 +4246,6 @@ RTMP_Close(RTMP *r)
       RC4_free(r->Link.rc4keyOut);
       r->Link.rc4keyOut = NULL;
     }
-#else
-  free(r->Link.playpath0.av_val);
-  r->Link.playpath0.av_val = NULL;
 #endif
 }
 
index d723070dabca2757df2df66cbbc762d67a67fc6c..1a080c1ebf5745a713b7d5a5ade0f0800af21a62 100644 (file)
@@ -178,11 +178,7 @@ extern "C"
     int protocol;
     int timeout;               /* connection timeout in seconds */
 
-#define RTMP_PUB_NAME   0x0001  /* send login to server */
-#define RTMP_PUB_RESP   0x0002  /* send salted password hash */
 #define RTMP_PUB_ALLOC  0x0004  /* allocated data for new tcUrl & app */
-#define RTMP_PUB_CLEAN  0x0008  /* need to free allocated data for newer tcUrl & app at exit */
-#define RTMP_PUB_CLATE  0x0010  /* late clean tcUrl & app at exit */
     int pFlags;
 
     unsigned short socksport;