[xiph-commits] r10257 - trunk/icecast2yp/src

oddsock at svn.xiph.org oddsock at svn.xiph.org
Sat Oct 22 21:07:46 PDT 2005


Author: oddsock
Date: 2005-10-22 21:07:44 -0700 (Sat, 22 Oct 2005)
New Revision: 10257

Added:
   trunk/icecast2yp/src/check_socket.c
   trunk/icecast2yp/src/yp_check.c
Modified:
   trunk/icecast2yp/src/Makefile.am
   trunk/icecast2yp/src/yp_cachegen.c
   trunk/icecast2yp/src/yp_cgi.c
   trunk/icecast2yp/src/yp_routines.c
Log:
added new checker for validating streams listed on the YP..


Modified: trunk/icecast2yp/src/Makefile.am
===================================================================
--- trunk/icecast2yp/src/Makefile.am	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/Makefile.am	2005-10-23 04:07:44 UTC (rev 10257)
@@ -4,13 +4,14 @@
 
 EXTRA_DIST= log.h yp_db.h yp_routines.h run_cachegen.sh
 
-bin_PROGRAMS = yp_cgi yp_cachegen yp_clean yp_random
+bin_PROGRAMS = yp_cgi yp_cachegen yp_clean yp_random yp_check
 
 
 yp_cgi_SOURCES = yp_cgi.c log.c yp_routines.c
 yp_cachegen_SOURCES = yp_cachegen.c log.c yp_routines.c
 yp_clean_SOURCES = yp_clean.c log.c yp_routines.c
 yp_random_SOURCES = yp_random.c log.c yp_routines.c
+yp_check_SOURCES = yp_check.c log.c check_socket.c yp_routines.c
     
 CFLAGS = @CFLAGS@ @MYSQL_CFLAGS@ @XML_CFLAGS@ -I${prefix}/include -I/usr/include/libcgi `xml2-config --cflags`
 LIBS = @LIBS@ @MYSQL_LIBS@ @XML_LIBS@ -L${prefix}/lib `xml2-config --libs`

Added: trunk/icecast2yp/src/check_socket.c
===================================================================
--- trunk/icecast2yp/src/check_socket.c	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/check_socket.c	2005-10-23 04:07:44 UTC (rev 10257)
@@ -0,0 +1,107 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include	<netdb.h>
+#include	<netinet/in.h>
+#include	<sys/types.h>
+#include	<sys/socket.h>
+#include	<stdio.h>
+#include	<time.h>
+#include	<signal.h>
+#include	<string.h>
+
+
+int connect_w_to(char *ip, int port) { 
+  int res; 
+  struct sockaddr_in addr; 
+  long arg; 
+  fd_set myset; 
+  struct timeval tv; 
+  int valopt; 
+  socklen_t lon; 
+  int soc = 0;
+  struct hostent *server;
+  char buff[1024] = "";
+
+
+  // Create socket 
+  soc = socket(AF_INET, SOCK_STREAM, 0); 
+  if (soc < 0) { 
+     fprintf(stderr, "Error creating socket (%d %s)\n", errno, strerror(errno)); 
+     exit(0); 
+  } 
+
+  addr.sin_family = AF_INET; 
+  addr.sin_port = htons(port); 
+
+  server = gethostbyname(ip);
+  if (server == NULL) {
+      //fprintf(stderr,"ERROR, no such host (%s)\n", ip);
+      return(0);
+  }
+  strcpy(buff, (char *)inet_ntoa(*(struct in_addr *)server->h_addr));
+
+  addr.sin_addr.s_addr = inet_addr(buff); 
+
+  //bzero((char *) &addr, sizeof(addr));
+  //bcopy((char *)server->h_addr, (char *)&addr.sin_addr.s_addr, server->h_length);
+
+  // Set non-blocking 
+  if( (arg = fcntl(soc, F_GETFL, NULL)) < 0) { 
+     fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); 
+     return(0);
+  } 
+  arg |= O_NONBLOCK; 
+  if( fcntl(soc, F_SETFL, arg) < 0) { 
+     fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); 
+     return(0);
+  } 
+  // Trying to connect with timeout 
+  res = connect(soc, (struct sockaddr *)&addr, sizeof(addr)); 
+  if (res < 0) { 
+     if (errno == EINPROGRESS) { 
+        do { 
+           tv.tv_sec = 10; 
+           tv.tv_usec = 0; 
+           FD_ZERO(&myset); 
+           FD_SET(soc, &myset); 
+           res = select(soc+1, NULL, &myset, NULL, &tv); 
+           if (res < 0 && errno != EINTR) { 
+              //fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
+		 close(soc);
+		return(0);
+           } 
+           else if (res > 0) { 
+              // Socket selected for write 
+              lon = sizeof(int); 
+              if (getsockopt(soc, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon) < 0) { 
+                 fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno)); 
+		 close(soc);
+                 return(0);
+              } 
+              // Check the value returned... 
+              if (valopt) { 
+                 //fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt));
+		 close(soc);
+                 return(0);
+              } 
+              break; 
+           } 
+           else { 
+              //fprintf(stderr, "Timeout!\n");
+		 close(soc);
+              return(0);
+           } 
+        } while (1); 
+     } 
+     else { 
+        //fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); 
+        return(0);
+     } 
+  } 
+ close(soc);
+  return(1);
+} 

Modified: trunk/icecast2yp/src/yp_cachegen.c
===================================================================
--- trunk/icecast2yp/src/yp_cachegen.c	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/yp_cachegen.c	2005-10-23 04:07:44 UTC (rev 10257)
@@ -26,7 +26,9 @@
 	char *out, *pin, *pout;
         int ret,size,out_size,temp;
 	iconv_t	iconv_handle;
+	int i =0;
 
+
         size = (int)strlen(in)+1; 
         out_size = size*2-1; 
         out = malloc((size_t)out_size); 
@@ -46,6 +48,11 @@
         } else {
                 printf("no mem\n");
         }
+	for (i=0;i<out_size;i++) {
+		if ((out[i] < 16)) {
+			out[i] = '.';
+		}
+	}
         return (out);
 }	
 
@@ -77,7 +84,7 @@
 
 	memset(sql, '\000', sizeof(sql));
 
-	sprintf(sql,"select a.server_name, b.listen_url, b.server_type, b.bitrate, b.channels, b.samplerate, b.genre, b.current_song from servers a, server_details b where a.id = b.parent_id order by server_name");
+	sprintf(sql,"select a.server_name, b.listen_url, b.server_type, b.bitrate, b.channels, b.samplerate, b.genre, b.current_song from servers a, server_details b where a.id = b.parent_id and yp_status = 'verified' order by server_name");
 	if(mysql_real_query(&dbase,sql,strlen(sql))) {
 		strcpy(error, mysql_error(&dbase));
 		return(ERROR);

Modified: trunk/icecast2yp/src/yp_cgi.c
===================================================================
--- trunk/icecast2yp/src/yp_cgi.c	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/yp_cgi.c	2005-10-23 04:07:44 UTC (rev 10257)
@@ -16,15 +16,15 @@
 	char	server_type[26];
 	char	server_subtype[255];
 	char	bitrate[26];
-	char	desc[256];
-	char	st[256];
+	char	desc[150];
+	char	st[150];
 	char	genre[101];
 	char	sid[101];
 	char	cluster_password[51];
 	char	url[256];
-	char	current_song[256];
-	char	listenurl[201];
-	char	listenurl2[201];
+	char	current_song[150];
+	char	listenurl[2046];
+	char	listenurl2[2046];
 	char	listeners[256];
 	char	samplerate[256];
 	char	channels[256];
@@ -64,6 +64,10 @@
 	cgi_process_form();
 
 	//sendOK();
+	if (!strcmp(listing_ip, "200.122.53.27")) {	
+		sendYPResponse(0, "banned", ICECAST2_RESPONSE);
+		goto endofcall;
+	}
 
 	/* Was there an error initializing the CGI??? */
 /*

Added: trunk/icecast2yp/src/yp_check.c
===================================================================
--- trunk/icecast2yp/src/yp_check.c	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/yp_check.c	2005-10-23 04:07:44 UTC (rev 10257)
@@ -0,0 +1,153 @@
+/*********************************************
+YP-CLEAN by oddsock
+**********************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <mysql.h>
+#include <unistd.h>
+#include "log.h"
+
+
+extern MYSQL		dbase;
+MYSQL_RES	*result;
+MYSQL_RES	*result2;
+
+#define YP_ERROR 0
+#define YP_SUCCESS 1
+
+
+int checkServers(char *error)
+{
+	char	sql[8096];
+	char	sid[2046];
+	int	i;
+	int	j;
+	MYSQL_ROW	row;
+	int	existing = 0;
+	int	nrows = 0;
+        char    detail_id[255] = "";
+        char    parent_id[255] = "";
+	char	*p1;
+	int	randomNumber = 0;
+	int	goodCount = 0;
+	int	badCount = 0;
+	char	badSeen[2096][255];
+	int	numBadSeen = 0;
+
+	memset(&badSeen, '\000', sizeof(badSeen));
+	srand(time() + getpid());
+
+	memset(sql, '\000', sizeof(sql));
+
+	sprintf(sql,"select a.id, listen_url from servers a, server_details b where a.id = b.parent_id and a.yp_status = 'notverified' order by listen_url");
+	if(mysql_real_query(&dbase,sql,strlen(sql))) {
+		strcpy(error, mysql_error(&dbase));
+		return(YP_ERROR);
+	}
+	result = mysql_store_result(&dbase);
+	nrows = mysql_num_rows(result);
+	
+	if(nrows == 0) {
+		return(YP_SUCCESS);
+	}
+	else {
+		for (i=0;i<nrows;i++) {
+			//printf("%d servers left\n", nrows-i);
+			row = mysql_fetch_row(result);
+			if (row[1]) {
+				//printf("(%s)\n", row[1]);
+				char *p1 = row[1] + strlen("http://");
+				char *p2 = strchr(p1, '/');
+				if (p2) {
+					*p2 = '\000';
+				}
+				char host[1024] = "";
+				int port = 0;
+				char *p3 = strchr(p1, ':');
+				if (p3) {
+					memset(host, '\000', sizeof(host));
+					if (p3-p1 < 1024) {
+						char addr[255] = "";
+						int seen = 0;
+						int i = 0;
+
+						strncpy(host, p1, p3-p1);
+						port = atoi(p3+1);
+						//printf("Checking host (%s) port (%d)\n", host, port);
+
+						snprintf(addr, 254, "%s:%d", host, port);
+
+						for (i=0;i<numBadSeen;i++) {
+							if (!strcmp(badSeen[i], addr)) {
+								seen = 1;
+								break;
+							}
+						}
+						if (seen) {
+							//printf("Seen this already...bad\n");
+							badCount++;
+						}
+						else {
+							if (connect_w_to(host, port)) {
+							    //printf("%s is good\n", host);
+							    sprintf(sql,"update servers set yp_status = 'verified' where id = %s", row[0]);
+							    if (mysql_real_query(&dbase,sql,strlen(sql))) {
+								sprintf(error, "servers: %s", mysql_error(&dbase));
+								sprintf(sql,"ROLLBACK");
+								mysql_real_query(&dbase,sql,strlen(sql));
+							    }
+							    goodCount++;
+							}
+							else {
+							    sprintf(sql,"delete from servers where id = %s", row[0]);
+							    if (mysql_real_query(&dbase,sql,strlen(sql))) {
+								sprintf(error, "servers: %s", mysql_error(&dbase));
+								sprintf(sql,"ROLLBACK");
+								mysql_real_query(&dbase,sql,strlen(sql));
+							    }
+								//printf("%s is bad\n", host);
+								strcpy(badSeen[numBadSeen], addr);
+								numBadSeen++;
+								badCount++;
+							}
+						}
+					}
+				}
+			}
+		}
+		mysql_free_result(result);
+	}
+	//printf("Good: %d\n", goodCount);
+	//printf("Bad: %d\n", badCount);
+	//for (j=0;j<numBadSeen;j++) {
+		//printf("\tBAD: %s\n", badSeen[j]);
+	//}
+	return(YP_SUCCESS);
+}
+
+int main(int argc, char * argv[])
+{
+	int res;
+	int	ret = 0;
+	int	ok = 0;
+	int	pid = 0;
+	char	error[2046];
+	int	loop = 1;
+
+	memset(error, '\000', sizeof(error));
+
+	setErrorType(LM_INFO);
+	setLogFile(YP_LOGDIR"yp-check.log");
+
+	if (connectToDB()) {
+		ret = checkServers(error);
+		if (ret == YP_ERROR) {
+			LogMessage(LOG_ERROR, "Error: %s", error);
+		}
+	}
+	return(0);
+}
+
+

Modified: trunk/icecast2yp/src/yp_routines.c
===================================================================
--- trunk/icecast2yp/src/yp_routines.c	2005-10-23 03:41:06 UTC (rev 10256)
+++ trunk/icecast2yp/src/yp_routines.c	2005-10-23 04:07:44 UTC (rev 10257)
@@ -540,9 +540,12 @@
 	char	detail_id[255] = "";
 	int	ret = 0;
 
+	double	startTime = 0.0;
+	double	endTime = 0.0;
 	/* Check for Dupes */
 	memset(sql, '\000', sizeof(sql));
 
+	startTime = GetCurrentTime();
 	server_name_esc = malloc(strlen(server_name)*2 + 1);
 	memset(server_name_esc, '\000', strlen(server_name)*2 + 1);
 	mysql_real_escape_string(&dbase, server_name_esc, server_name, strlen(server_name));
@@ -650,7 +653,7 @@
 
 
 	if (!cluster_flag) {
-		snprintf(sql, sizeof(sql)-1, "insert into servers (server_name, listing_ip) values  ('%s', '%s')", server_name_esc, listing_ip);
+		snprintf(sql, sizeof(sql)-1, "insert into servers (server_name, listing_ip, yp_status) values  ('%s', '%s', 'notverified')", server_name_esc, listing_ip);
 		Log(LOG_DEBUG, "This isn't a cluster, it's just a new stream");
 		if (mysql_real_query(&dbase,sql,strlen(sql))) {
 			sprintf(error, "servers: %s", mysql_error(&dbase));
@@ -788,6 +791,9 @@
 	if (channels_esc) {
 		free(channels_esc);
 	}
+	endTime = GetCurrentTime();
+	//Log(LOG_ERROR, "ADD: %f", endTime - startTime);
+		
 	return(YP_ADDED);
 Error:
 	if (server_name_esc) {
@@ -844,8 +850,11 @@
 	char	detail_id[255] = "";
 	char	parent_id[255] = "";
 	char	*p1;
+	double	startTime = 0.0;
+	double	endTime = 0.0;
 
 	memset(sql, '\000', sizeof(sql));
+	startTime = GetCurrentTime();
 
 	memset(detail_id, '\000', sizeof(detail_id));
 	memset(parent_id, '\000', sizeof(parent_id));
@@ -980,6 +989,8 @@
 		if (server_name_esc) {
 			free(server_name_esc);
 		}
+		endTime = GetCurrentTime();
+		//Log(LOG_ERROR, "TOUCH: %f", endTime - startTime);
 		return(YP_TOUCHED);
 	}
 TouchError:



More information about the commits mailing list