[rtmpdump] [PATCH] Initialize winsock in librtmp

LRN lrn1986 at gmail.com
Sat May 5 19:54:09 CEST 2012


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

subj is attached

Initializes winsock internally in librtmp (due to preservation of
RTMP_Init() interface application calls for InitSockets() are
retained, although a failure to initialize winsock 1.1 is pretty much
unheard of).
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJPpWk/AAoJEOs4Jb6SI2CwFTYIALfKQy+Ax8KBTpRR0067Vntz
e76k/n8CWKdeMALRCcCAcab3GrdjK2nhqZjkHbfWS9L44XInyx0JEer9SyDrMoQf
B4Lr5u3LpidFRMZM5iupvptqV/oxcho055cU5fxnnEY7I19HhIl9LPThOfH43qjt
vlwRHyaUAOcam0b/4NRolrv97ZAowKX93RNxQSjOAmJENh7AwtuWtYlOd6XTuwYm
P5HHb319pdVNE4Je8eF4VDHNrz6n7BSST9scR1bC9C6C6ZctBTo8fp6nYlBne5qP
2INrzu8LlK3pbuhQ6c4lyRiQT1i4wghRnrRM2fjSN3uvIcvXXTeUzicsmObpVRw=
=MyLb
-----END PGP SIGNATURE-----
-------------- next part --------------
From 157c15198f53e0a58284d8fc4893324effff6e31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1=D1?=
 =?UTF-8?q?=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= <lrn1986 at gmail.com>
Date: Sat, 5 May 2012 21:37:16 +0400
Subject: [PATCH] Initialize winsock in librtmp

---
 librtmp/rtmp.c     |    5 +++++
 librtmp/rtmp_sys.h |   16 ++++++++++++++++
 rtmpdump.c         |   27 +++------------------------
 rtmpgw.c           |   23 ++++++++---------------
 rtmpsrv.c          |   23 ++++++++---------------
 rtmpsuck.c         |   23 ++++++++---------------
 6 files changed, 48 insertions(+), 69 deletions(-)

diff --git a/librtmp/rtmp.c b/librtmp/rtmp.c
index 52d0254..373b0c8 100644
--- a/librtmp/rtmp.c
+++ b/librtmp/rtmp.c
@@ -242,6 +242,9 @@ RTMP_Free(RTMP *r)
 void
 RTMP_Init(RTMP *r)
 {
+  int nInitSocketsResult = 1;
+  /* FIXME: result is not checked, and RTMP_Init() never fails... */
+  InitSockets(&nInitSocketsResult);
 #ifdef CRYPTO
   if (!RTMP_TLS_ctx)
     RTMP_TLS_Init();
@@ -3546,6 +3549,8 @@ RTMP_Close(RTMP *r)
       r->Link.rc4keyOut = NULL;
     }
 #endif
+
+  CleanupSockets();
 }
 
 int
diff --git a/librtmp/rtmp_sys.h b/librtmp/rtmp_sys.h
index c3fd4a6..821fea8 100644
--- a/librtmp/rtmp_sys.h
+++ b/librtmp/rtmp_sys.h
@@ -34,6 +34,22 @@
 #define vsnprintf _vsnprintf
 #endif
 
+#ifdef WIN32
+#define InitSockets(init_result)	{\
+        WORD version;			\
+        WSADATA wsaData;		\
+					\
+        version = MAKEWORD(1,1);	\
+        *(init_result) = WSAStartup(version, &wsaData); \
+        if (*(init_result) == 0) *(init_result) = TRUE; \
+        else *(init_result) = FALSE; }
+
+#define	CleanupSockets()	WSACleanup()
+#else
+#define InitSockets(init_result) (*(init_result) = TRUE)
+#define	CleanupSockets()
+#endif
+
 #define GetSockError()	WSAGetLastError()
 #define SetSockError(e)	WSASetLastError(e)
 #define setsockopt(a,b,c,d,e)	(setsockopt)(a,b,c,(const char *)d,(int)e)
diff --git a/rtmpdump.c b/rtmpdump.c
index 34bfdba..a1bcd4e 100644
--- a/rtmpdump.c
+++ b/rtmpdump.c
@@ -52,29 +52,6 @@
 #define DEF_BUFTIME	(10 * 60 * 60 * 1000)	/* 10 hours default */
 #define DEF_SKIPFRM	0
 
-// starts sockets
-int
-InitSockets()
-{
-#ifdef WIN32
-  WORD version;
-  WSADATA wsaData;
-
-  version = MAKEWORD(1, 1);
-  return (WSAStartup(version, &wsaData) == 0);
-#else
-  return TRUE;
-#endif
-}
-
-inline void
-CleanupSockets()
-{
-#ifdef WIN32
-  WSACleanup();
-#endif
-}
-
 #ifdef _DEBUG
 uint32_t debugTS = 0;
 int pnum = 0;
@@ -717,6 +694,7 @@ int
 main(int argc, char **argv)
 {
   extern char *optarg;
+  int nInitSocketsResult = 0;
 
   int nStatus = RD_SUCCESS;
   double percent = 0;
@@ -797,7 +775,8 @@ main(int argc, char **argv)
   RTMP_LogPrintf
     ("(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL\n");
 
-  if (!InitSockets())
+  InitSockets(&nInitSocketsResult);
+  if (!nInitSocketsResult)
     {
       RTMP_Log(RTMP_LOGERROR,
 	  "Couldn't load sockets support on your platform, exiting!");
diff --git a/rtmpgw.c b/rtmpgw.c
index 0cf56bb..8f48ecb 100644
--- a/rtmpgw.c
+++ b/rtmpgw.c
@@ -40,20 +40,6 @@
 
 #define PACKET_SIZE 1024*1024
 
-#ifdef WIN32
-#define InitSockets()	{\
-        WORD version;			\
-        WSADATA wsaData;		\
-					\
-        version = MAKEWORD(1,1);	\
-        WSAStartup(version, &wsaData);	}
-
-#define	CleanupSockets()	WSACleanup()
-#else
-#define InitSockets()
-#define	CleanupSockets()
-#endif
-
 enum
 {
   STREAMING_ACCEPTING,
@@ -968,6 +954,7 @@ int
 main(int argc, char **argv)
 {
   int nStatus = RD_SUCCESS;
+  int nInitSocketsResult = 0;
 
   // http streaming server
   char DEFAULT_HTTP_STREAMING_DEVICE[] = "0.0.0.0";	// 0.0.0.0 is any device
@@ -1036,7 +1023,13 @@ main(int argc, char **argv)
   signal(SIGPIPE, SIG_IGN);
 #endif
 
-  InitSockets();
+  InitSockets(&nInitSocketsResult);
+  if (!nInitSocketsResult)
+    {
+      RTMP_Log(RTMP_LOGERROR,
+	  "Couldn't load sockets support on your platform, exiting!");
+      return RD_FAILED;
+    }
 
   while ((opt =
 	  getopt_long(argc, argv,
diff --git a/rtmpsrv.c b/rtmpsrv.c
index 9aa62f3..a679cc7 100644
--- a/rtmpsrv.c
+++ b/rtmpsrv.c
@@ -54,20 +54,6 @@
 
 #define PACKET_SIZE 1024*1024
 
-#ifdef WIN32
-#define InitSockets()	{\
-	WORD version;			\
-	WSADATA wsaData;		\
-					\
-	version = MAKEWORD(1,1);	\
-	WSAStartup(version, &wsaData);	}
-
-#define	CleanupSockets()	WSACleanup()
-#else
-#define InitSockets()
-#define	CleanupSockets()
-#endif
-
 #define DUPTIME	5000	/* interval we disallow duplicate requests, in msec */
 
 enum
@@ -1071,6 +1057,7 @@ int
 main(int argc, char **argv)
 {
   int nStatus = RD_SUCCESS;
+  int nInitSocketsResult = 0;
 
   // http streaming server
   char DEFAULT_HTTP_STREAMING_DEVICE[] = "0.0.0.0";	// 0.0.0.0 is any device
@@ -1107,7 +1094,13 @@ main(int argc, char **argv)
   netstackdump_read = fopen("netstackdump_read", "wb");
 #endif
 
-  InitSockets();
+  InitSockets(&nInitSocketsResult);
+  if (!nInitSocketsResult)
+    {
+      RTMP_Log(RTMP_LOGERROR,
+	  "Couldn't load sockets support on your platform, exiting!");
+      return RD_FAILED;
+    }
 
   // start text UI
   ThreadCreate(controlServerThread, 0);
diff --git a/rtmpsuck.c b/rtmpsuck.c
index e886179..4dc2c88 100644
--- a/rtmpsuck.c
+++ b/rtmpsuck.c
@@ -49,20 +49,6 @@
 
 #define PACKET_SIZE 1024*1024
 
-#ifdef WIN32
-#define InitSockets()	{\
-        WORD version;			\
-        WSADATA wsaData;		\
-					\
-        version = MAKEWORD(1,1);	\
-        WSAStartup(version, &wsaData);	}
-
-#define	CleanupSockets()	WSACleanup()
-#else
-#define InitSockets()
-#define	CleanupSockets()
-#endif
-
 enum
 {
   STREAMING_ACCEPTING,
@@ -1138,6 +1124,7 @@ int
 main(int argc, char **argv)
 {
   int nStatus = RD_SUCCESS;
+  int nInitSocketsResult = 0;
 
   // rtmp streaming server
   char DEFAULT_RTMP_STREAMING_DEVICE[] = "0.0.0.0";	// 0.0.0.0 is any device
@@ -1163,7 +1150,13 @@ main(int argc, char **argv)
   netstackdump_read = fopen("netstackdump_read", "wb");
 #endif
 
-  InitSockets();
+  InitSockets(&nInitSocketsResult);
+  if (!nInitSocketsResult)
+    {
+      RTMP_Log(RTMP_LOGERROR,
+	  "Couldn't load sockets support on your platform, exiting!");
+      return RD_FAILED;
+    }
 
   // start text UI
   ThreadCreate(controlServerThread, 0);
-- 
1.7.4


More information about the rtmpdump mailing list