[rtmpdump] r404 - in trunk: librtmp/parseurl.c librtmp/rtmp.c librtmp/rtmp.h rtmpdump.c rtmpgw.c rtmpsuck.c

hyc subversion at mplayerhq.hu
Fri Mar 26 22:07:05 CET 2010


Author: hyc
Date: Fri Mar 26 22:07:03 2010
New Revision: 404

Log:
Use AVal for hostname and sockshost, don't allocate hostnames

Modified:
   trunk/librtmp/parseurl.c
   trunk/librtmp/rtmp.c
   trunk/librtmp/rtmp.h
   trunk/rtmpdump.c
   trunk/rtmpgw.c
   trunk/rtmpsuck.c

Modified: trunk/librtmp/parseurl.c
==============================================================================
--- trunk/librtmp/parseurl.c	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/librtmp/parseurl.c	Fri Mar 26 22:07:03 2010	(r404)
@@ -29,7 +29,7 @@
 #include "rtmp_sys.h"
 #include "log.h"
 
-bool RTMP_ParseURL(const char *url, int *protocol, char **host, unsigned int *port,
+bool RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port,
 	AVal *playpath, AVal *app)
 {
 	char *p, *end, *col, *ques, *slash;
@@ -99,11 +99,9 @@ parsehost:
 		hostlen = col - p;
 
 	if(hostlen < 256) {
-		*host = malloc(hostlen+1);
-		strncpy(*host, p, hostlen);
-		(*host)[hostlen]=0;
-
-		RTMP_Log(RTMP_LOGDEBUG, "Parsed host    : %s", *host);
+		host->av_val = p;
+		host->av_len = hostlen;
+		RTMP_Log(RTMP_LOGDEBUG, "Parsed host    : %.*s", hostlen, host->av_val);
 	} else {
 		RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!");
 	}

Modified: trunk/librtmp/rtmp.c
==============================================================================
--- trunk/librtmp/rtmp.c	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/librtmp/rtmp.c	Fri Mar 26 22:07:03 2010	(r404)
@@ -283,9 +283,9 @@ const AVal RTMP_DefaultFlashVer =
 void
 RTMP_SetupStream(RTMP *r,
 		 int protocol,
-		 const char *hostname,
+		 AVal *host,
 		 unsigned int port,
-		 const char *sockshost,
+		 AVal *sockshost,
 		 AVal *playpath,
 		 AVal *tcUrl,
 		 AVal *swfUrl,
@@ -300,7 +300,7 @@ RTMP_SetupStream(RTMP *r,
 		 uint32_t dLength, bool bLiveStream, long int timeout)
 {
   RTMP_Log(RTMP_LOGDEBUG, "Protocol : %s", RTMPProtocolStrings[protocol&7]);
-  RTMP_Log(RTMP_LOGDEBUG, "Hostname : %s", hostname);
+  RTMP_Log(RTMP_LOGDEBUG, "Hostname : %.*s", host->av_len, host->av_val);
   RTMP_Log(RTMP_LOGDEBUG, "Port     : %d", port);
   RTMP_Log(RTMP_LOGDEBUG, "Playpath : %s", playpath->av_val);
 
@@ -343,22 +343,24 @@ RTMP_SetupStream(RTMP *r,
     }
 #endif
 
-  if (sockshost)
+  if (sockshost->av_len)
     {
-      const char *socksport = strchr(sockshost, ':');
-      char *hostname = strdup(sockshost);
+      const char *socksport = strchr(sockshost->av_val, ':');
+      char *hostname = strdup(sockshost->av_val);
 
       if (socksport)
-	hostname[socksport - sockshost] = '\0';
-      r->Link.sockshost = hostname;
+	hostname[socksport - sockshost->av_val] = '\0';
+      r->Link.sockshost.av_val = hostname;
+      r->Link.sockshost.av_len = strlen(hostname);
 
       r->Link.socksport = socksport ? atoi(socksport + 1) : 1080;
-      RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost,
+      RTMP_Log(RTMP_LOGDEBUG, "Connecting via SOCKS proxy: %s:%d", r->Link.sockshost.av_val,
 	  r->Link.socksport);
     }
   else
     {
-      r->Link.sockshost = NULL;
+      r->Link.sockshost.av_val = NULL;
+      r->Link.sockshost.av_len = 0;
       r->Link.socksport = 0;
     }
 
@@ -384,7 +386,7 @@ RTMP_SetupStream(RTMP *r,
   r->Link.timeout = timeout;
 
   r->Link.protocol = protocol;
-  r->Link.hostname = hostname;
+  r->Link.hostname = *host;
   r->Link.port = port;
   r->Link.playpath = *playpath;
 
@@ -400,8 +402,21 @@ RTMP_SetupStream(RTMP *r,
 }
 
 static bool
-add_addr_info(struct sockaddr_in *service, const char *hostname, int port)
+add_addr_info(struct sockaddr_in *service, AVal *host, int port)
 {
+  char *hostname;
+  bool ret = true;
+  if (host->av_val[host->av_len])
+    {
+      hostname = malloc(host->av_len+1);
+      memcpy(hostname, host->av_val, host->av_len);
+      hostname[host->av_len] = '\0';
+    }
+  else
+    {
+      hostname = host->av_val;
+    }
+
   service->sin_addr.s_addr = inet_addr(hostname);
   if (service->sin_addr.s_addr == INADDR_NONE)
     {
@@ -409,13 +424,17 @@ add_addr_info(struct sockaddr_in *servic
       if (host == NULL || host->h_addr == NULL)
 	{
 	  RTMP_Log(RTMP_LOGERROR, "Problem accessing the DNS. (addr: %s)", hostname);
-	  return false;
+	  ret = false;
+	  goto finish;
 	}
       service->sin_addr = *(struct in_addr *)host->h_addr;
     }
 
   service->sin_port = htons(port);
-  return true;
+finish:
+  if (hostname != host->av_val)
+    free(hostname);
+  return ret;
 }
 
 bool
@@ -525,7 +544,7 @@ bool
 RTMP_Connect(RTMP *r, RTMPPacket *cp)
 {
   struct sockaddr_in service;
-  if (!r->Link.hostname)
+  if (!r->Link.hostname.av_len)
     return false;
 
   memset(&service, 0, sizeof(struct sockaddr_in));
@@ -534,13 +553,13 @@ RTMP_Connect(RTMP *r, RTMPPacket *cp)
   if (r->Link.socksport)
     {
       // Connect via SOCKS
-      if (!add_addr_info(&service, r->Link.sockshost, r->Link.socksport))
+      if (!add_addr_info(&service, &r->Link.sockshost, r->Link.socksport))
 	return false;
     }
   else
     {
       // Connect directly
-      if (!add_addr_info(&service, r->Link.hostname, r->Link.port))
+      if (!add_addr_info(&service, &r->Link.hostname, r->Link.port))
 	return false;
     }
 
@@ -558,7 +577,7 @@ SocksNegotiate(RTMP *r)
   struct sockaddr_in service;
   memset(&service, 0, sizeof(struct sockaddr_in));
 
-  add_addr_info(&service, r->Link.hostname, r->Link.port);
+  add_addr_info(&service, &r->Link.hostname, r->Link.port);
   unsigned long addr = htonl(service.sin_addr.s_addr);
 
   char packet[] = {
@@ -3024,7 +3043,7 @@ HTTP_Post(RTMP *r, RTMPTCmd cmd, const c
 {
   char hbuf[512];
   int hlen = snprintf(hbuf, sizeof(hbuf), "POST /%s%s/%d HTTP/1.1\r\n"
-    "Host: %s:%d\r\n"
+    "Host: %.*s:%d\r\n"
     "Accept: */*\r\n"
     "User-Agent: Shockwave Flash\n"
     "Connection: Keep-Alive\n"
@@ -3032,7 +3051,8 @@ HTTP_Post(RTMP *r, RTMPTCmd cmd, const c
     "Content-type: application/x-fcs\r\n"
     "Content-length: %d\r\n\r\n", RTMPT_cmds[cmd],
     r->m_clientID.av_val ? r->m_clientID.av_val : "",
-    r->m_msgCounter, r->Link.hostname, r->Link.port, len);
+    r->m_msgCounter, r->Link.hostname.av_len, r->Link.hostname.av_val,
+    r->Link.port, len);
   RTMPSockBuf_Send(&r->m_sb, hbuf, hlen);
   hlen = RTMPSockBuf_Send(&r->m_sb, buf, len);
   r->m_msgCounter++;

Modified: trunk/librtmp/rtmp.h
==============================================================================
--- trunk/librtmp/rtmp.h	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/librtmp/rtmp.h	Fri Mar 26 22:07:03 2010	(r404)
@@ -119,7 +119,7 @@ extern "C"
 
   typedef struct RTMP_LNK
   {
-    const char *hostname;
+    AVal hostname;
     unsigned int port;
     int protocol;
 
@@ -142,7 +142,7 @@ extern "C"
 
     int timeout;		// number of seconds before connection times out
 
-    const char *sockshost;
+    AVal sockshost;
     unsigned short socksport;
 
 #ifdef CRYPTO
@@ -234,16 +234,16 @@ extern "C"
     RTMPSockBuf m_sb;
   } RTMP;
 
-  bool RTMP_ParseURL(const char *url, int *protocol, char **host,
+  bool RTMP_ParseURL(const char *url, int *protocol, AVal *host,
 		     unsigned int *port, AVal *playpath, AVal *app);
   void RTMP_ParsePlaypath(AVal *in, AVal *out);
   void RTMP_SetBufferMS(RTMP *r, int size);
   void RTMP_UpdateBufferMS(RTMP *r);
 
   void RTMP_SetupStream(RTMP *r, int protocol,
-			const char *hostname,
+			AVal *hostname,
 			unsigned int port,
-			const char *sockshost,
+			AVal *sockshost,
 			AVal *playpath,
 			AVal *tcUrl,
 			AVal *swfUrl,

Modified: trunk/rtmpdump.c
==============================================================================
--- trunk/rtmpdump.c	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/rtmpdump.c	Fri Mar 26 22:07:03 2010	(r404)
@@ -738,7 +738,7 @@ main(int argc, char **argv)
   uint32_t nInitialFrameSize = 0;
   int initialFrameType = 0;	// tye: audio or video
 
-  char *hostname = 0;
+  AVal hostname = { 0, 0 };
   AVal playpath = { 0, 0 };
   AVal subscribepath = { 0, 0 };
   int port = -1;
@@ -762,7 +762,7 @@ main(int argc, char **argv)
   uint32_t swfSize = 0;
   AVal flashVer = { 0, 0 };
   AVal token = { 0, 0 };
-  char *sockshost = 0;
+  AVal sockshost = { 0, 0 };
   AMFObject extras = {0};
   int edepth = 0;
 
@@ -1007,7 +1007,7 @@ main(int argc, char **argv)
 	  STR2AVAL(subscribepath, optarg);
 	  break;
 	case 'n':
-	  hostname = optarg;
+	  STR2AVAL(hostname, optarg);
 	  break;
 	case 'c':
 	  port = atoi(optarg);
@@ -1026,9 +1026,7 @@ main(int argc, char **argv)
 	  break;
 	case 'r':
 	  {
-		AVal parsedApp, parsedPlaypath;
-
-	    char *parsedHost = 0;
+	    AVal parsedHost, parsedApp, parsedPlaypath;
 	    unsigned int parsedPort = 0;
 	    int parsedProtocol = RTMP_PROTOCOL_UNDEFINED;
 
@@ -1042,7 +1040,7 @@ main(int argc, char **argv)
 	      }
 	    else
 	      {
-		if (hostname == 0)
+		if (!hostname.av_len)
 		  hostname = parsedHost;
 		if (port == -1)
 		  port = parsedPort;
@@ -1118,7 +1116,7 @@ main(int argc, char **argv)
 	  RTMP_debuglevel = RTMP_LOGALL;
 	  break;
 	case 'S':
-	  sockshost = optarg;
+	  STR2AVAL(sockshost, optarg);
 	  break;
 	default:
 	  RTMP_LogPrintf("unknown option: %c\n", opt);
@@ -1127,7 +1125,7 @@ main(int argc, char **argv)
 	}
     }
 
-  if (hostname == 0)
+  if (!hostname.av_len)
     {
       RTMP_Log(RTMP_LOGERROR,
 	  "You must specify a hostname (--host) or url (-r \"rtmp://host[:port]/playpath\") containing a hostname");
@@ -1212,8 +1210,9 @@ main(int argc, char **argv)
     {
       char str[512] = { 0 };
 
-      tcUrl.av_len = snprintf(str, 511, "%s://%s:%d/%.*s",
-	  	   RTMPProtocolStringsLower[protocol], hostname, port, app.av_len, app.av_val);
+      tcUrl.av_len = snprintf(str, 511, "%s://%.*s:%d/%.*s",
+	  	   RTMPProtocolStringsLower[protocol], hostname.av_len,
+		   hostname.av_val, port, app.av_len, app.av_val);
       tcUrl.av_val = (char *) malloc(tcUrl.av_len + 1);
       strcpy(tcUrl.av_val, str);
     }
@@ -1234,7 +1233,7 @@ main(int argc, char **argv)
 
   RTMP rtmp = { 0 };
   RTMP_Init(&rtmp);
-  RTMP_SetupStream(&rtmp, protocol, hostname, port, sockshost, &playpath,
+  RTMP_SetupStream(&rtmp, protocol, &hostname, port, &sockshost, &playpath,
 		   &tcUrl, &swfUrl, &pageUrl, &app, &auth, &swfHash, swfSize,
 		   &flashVer, &subscribepath, dSeek, 0, bLiveStream, timeout);
 

Modified: trunk/rtmpgw.c
==============================================================================
--- trunk/rtmpgw.c	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/rtmpgw.c	Fri Mar 26 22:07:03 2010	(r404)
@@ -75,7 +75,7 @@ void stopStreaming(STREAMING_SERVER * se
 
 typedef struct
 {
-  char *hostname;
+  AVal hostname;
   int rtmpport;
   int protocol;
   bool bLiveStream;		// is it a live stream? then we can't seek/resume
@@ -94,7 +94,7 @@ typedef struct
   AVal flashVer;
   AVal token;
   AVal subscribepath;
-  char *sockshost;
+  AVal sockshost;
   AMFObject extras;
   int edepth;
   uint32_t swfSize;
@@ -468,7 +468,7 @@ void processTCPrequest(STREAMING_SERVER 
     }
 
   // do necessary checks right here to make sure the combined request of default values and GET parameters is correct
-  if (req.hostname == 0)
+  if (!req.hostname.av_len)
     {
       RTMP_Log(RTMP_LOGERROR,
 	  "You must specify a hostname (--host) or url (-r \"rtmp://host[:port]/playpath\") containing a hostname");
@@ -508,9 +508,9 @@ void processTCPrequest(STREAMING_SERVER 
   if (req.tcUrl.av_len == 0)
     {
       char str[512] = { 0 };
-      req.tcUrl.av_len = snprintf(str, 511, "%s://%s:%d/%.*s",
-	RTMPProtocolStringsLower[req.protocol],
-	req.hostname, req.rtmpport, req.app.av_len, req.app.av_val);
+      req.tcUrl.av_len = snprintf(str, 511, "%s://%.*s:%d/%.*s",
+	RTMPProtocolStringsLower[req.protocol], req.hostname.av_len,
+	req.hostname.av_val, req.rtmpport, req.app.av_len, req.app.av_val);
       req.tcUrl.av_val = (char *) malloc(req.tcUrl.av_len + 1);
       strcpy(req.tcUrl.av_val, str);
     }
@@ -556,7 +556,7 @@ void processTCPrequest(STREAMING_SERVER 
   RTMP_Log(RTMP_LOGDEBUG, "Setting buffer time to: %dms", req.bufferTime);
   RTMP_Init(&rtmp);
   RTMP_SetBufferMS(&rtmp, req.bufferTime);
-  RTMP_SetupStream(&rtmp, req.protocol, req.hostname, req.rtmpport, req.sockshost,
+  RTMP_SetupStream(&rtmp, req.protocol, &req.hostname, req.rtmpport, &req.sockshost,
 		   &req.playpath, &req.tcUrl, &req.swfUrl, &req.pageUrl, &req.app, &req.auth, &req.swfHash, req.swfSize, &req.flashVer, &req.subscribepath, dSeek, dLength,
 		   req.bLiveStream, req.timeout);
   /* backward compatibility, we always sent this as true before */
@@ -856,7 +856,7 @@ ParseOption(char opt, char *arg, RTMP_RE
       STR2AVAL(req->subscribepath, arg);
       break;
     case 'n':
-      req->hostname = arg;
+      STR2AVAL(req->hostname, arg);
       break;
     case 'c':
       req->rtmpport = atoi(arg);
@@ -883,8 +883,7 @@ ParseOption(char opt, char *arg, RTMP_RE
       {
 	req->rtmpurl = arg;
 
-	AVal parsedPlaypath, parsedApp;
-	char *parsedHost = 0;
+	AVal parsedHost, parsedPlaypath, parsedApp;
 	unsigned int parsedPort = 0;
 	int parsedProtocol = RTMP_PROTOCOL_UNDEFINED;
 
@@ -896,7 +895,7 @@ ParseOption(char opt, char *arg, RTMP_RE
 	  }
 	else
 	  {
-	    if (req->hostname == 0)
+	    if (!req->hostname.av_len)
 	      req->hostname = parsedHost;
 	    if (req->rtmpport == -1)
 	      req->rtmpport = parsedPort;
@@ -949,7 +948,7 @@ ParseOption(char opt, char *arg, RTMP_RE
       STR2AVAL(req->token, arg);
       break;
     case 'S':
-	  req->sockshost = arg;
+      STR2AVAL(req->sockshost, arg);
     case 'q':
       RTMP_debuglevel = RTMP_LOGCRIT;
       break;

Modified: trunk/rtmpsuck.c
==============================================================================
--- trunk/rtmpsuck.c	Thu Mar 25 11:11:39 2010	(r403)
+++ trunk/rtmpsuck.c	Fri Mar 26 22:07:03 2010	(r404)
@@ -250,15 +250,17 @@ ServeInvoke(STREAMING_SERVER *server, in
                   r2 = malloc(len+1);
                   memcpy(r2, r1, len);
                   r2[len] = '\0';
-                  server->rc.Link.hostname = (const char *)r2;
-                  r1 = strrchr(server->rc.Link.hostname, ':');
+                  server->rc.Link.hostname.av_val = r2;
+                  r1 = strrchr(r2, ':');
                   if (r1)
                     {
+		      server->rc.Link.hostname.av_len = r1 - r2;
                       *r1++ = '\0';
                       server->rc.Link.port = atoi(r1);
                     }
                   else
                     {
+		      server->rc.Link.hostname.av_len = len;
                       server->rc.Link.port = 1935;
                     }
                 }
@@ -965,8 +967,7 @@ cleanup:
   server->f_cur = NULL;
   free(buf);
   /* Should probably be done by RTMP_Close() ... */
-  free((void *)server->rc.Link.hostname);
-  server->rc.Link.hostname = NULL;
+  server->rc.Link.hostname.av_val = NULL;
   server->rc.Link.tcUrl.av_val = NULL;
   server->rc.Link.swfUrl.av_val = NULL;
   server->rc.Link.pageUrl.av_val = NULL;


More information about the rtmpdump mailing list