diff --git a/Makefile b/Makefile index 92eaa83..e1d751a 100644 --- a/Makefile +++ b/Makefile @@ -17,28 +17,28 @@ #Objects OBJS = socket.o amf.o rtmp.o -LIBRARIES = +LIBRARIES = -lpthread +CFLAGS = -Wall -O2 # Link The Binary $(BINARY) : $(OBJS) $(CC) $(CFLAGS) -o $@ $(STARTUP) $(LIBRARIES) $(OBJS) - strip $(BINARY) # Compile the source files .cc.o: - $(CXX) -Wall -O $(CFLAGS) $(INCLUDES) -c -o $@ $< + $(CXX) $(CFLAGS) $(INCLUDES) -c -o $@ $< .cc.s: - $(CXX) -Wall -O $(CFLAGS) $(INCLUDES) -S -o $@ $< + $(CXX) $(CFLAGS) $(INCLUDES) -S -o $@ $< .c.o: - $(CC) -Wall -O $(CFLAGS) $(INCLUDES) -c -o $@ $< + $(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< .c.s: - $(CC) -Wall -O $(CFLAGS) $(INCLUDES) -S -o $@ $< + $(CC) $(CFLAGS) $(INCLUDES) -S -o $@ $< .S.o: - $(CC) -Wall $(CLFAGS) $(INCLUDES) -c -o $@ $< + $(CC) $(CLFAGS) $(INCLUDES) -c -o $@ $< # Clean Up The junk clean: diff --git a/amf.c b/amf.c index 49d382f..870a768 100644 --- a/amf.c +++ b/amf.c @@ -1,9 +1,48 @@ #include #include +#include #include "rtmp.h" -int doAccept(int fd) { +int amfDoHandshake(int fd) { + int recLen = 0x0; + int i = 0x0; + int cmd = 0x0; + char data1[1536]; + char data2[1536]; + + recLen = recv(fd,&cmd,1,0); + if (cmd != 0x03) { + return(-1); + } + + recLen = recv(fd,&data1,1536,0); + + for (i = 0;i < 1536;i++) { + data2[i] = (char)i; + } + if (send(fd,&cmd,1,0) != 1) { + printf("Error Sending Header\n"); + return(-1); + } + if (send(fd,&data2,1536,0) != 1536) { + printf("Error Sending Hand2\n"); + return(-1); + } + if (send(fd,&data1,1536,0) != 1536) { + printf("Error Sending Hand1\n"); + return(-1); + } + if (recv(fd,&data1,1536,MSG_WAITALL) != 1536) { + printf("Error getting handshake\n"); + return(-1); + } + + return(0x0); + } + + +int amfDoAccept(int fd) { char raw1[] = {0x02,0x00,0x00,0x00,0x00,0x00,0x04,0x05,0x00,0x00,0x00,0x00,0x07,0xeb,0x58,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x96,0x14,0x00,0x00,0x00,0x00,0x02,0x00,0x07,0x5f,0x72,0x65,0x73,0x75,0x6c,0x74,0x00,0x3f,0xf0,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x03,0x00,0x04,0x63,0x6f,0x64,0x65,0x02,0x00,0x1d,0x4e,0x65,0x74,0x43,0x6f,0x6e,0x6e,0x65,0x63,0x74,0x69,0x6f,0x6e,0x2e,0x43,0x6f,0x6e,0x6e,0x65,0x63,0x74,0x2e,0x53,0x75,0x63,0x63,0x65,0x73,0x73,0x00,0x05,0x6c,0x65,0x76,0x65,0x6c,0x02,0x00,0x06,0x73,0x74,0x61,0x74,0x75,0x73,0x00,0x07,0x64,0x65,0x74,0x61,0x69,0x6c,0x73,0x05,0x00,0x0b,0x64,0x65,0x73,0x63,0x72,0x69,0x70,0x74,0x69,0x6f,0x6e,0x02,0x00,0x15,0x43,0x6f,0x6e,0x6e,0x65,0x63,0x74,0x69,0x6f,0x6e,0x20,0x73,0x75,0x63,0x63,0x65,0x65,0x64,0x65,0x64,0x2e,0x00,0x0e,0x6f,0x62,0x6a,0x65,0x02,0x00,0x00,0x00,0x00,0x00,0x05,0x06,0x00,0x00,0x00,0x00,0x00,0x03,0xd0,0x90,0x02,0xc3,0x63,0x74,0x45,0x6e,0x63,0x6f,0x64,0x69,0x6e,0x67,0x00,0x40,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09}; int recLen = 0x0; diff --git a/rtmp.c b/rtmp.c index b693fe6..ca63f4b 100644 --- a/rtmp.c +++ b/rtmp.c @@ -3,73 +3,57 @@ */ #include - -/* -#include -#include #include +#include +#include +#include +#include +#include #include #include -#include -#include -#include -*/ + #include "rtmp.h" amfHeader_real *AMFS = 0x0; +int getAMF(int); +int doProcessPacket(int,int); +int doAmfFunction(int fd,int amfID,int packetSize); + int main(void) { - int sockFD = 0x0; //Listener Socket int newFD = 0x0; //New Socket; - - int yes=1; - + //socklen_t sin_size; + //struct sockaddr_in remoteAddr; // connector's address information AMFS = (amfHeader_real *)malloc(sizeof(amfHeader_real) * 128); + sStartListener(); + while(1) { // main accept() loop + while (1); +/* sin_size = sizeof(struct sockaddr_in); - if ((newFD = accept(sockFD, (struct sockaddr *)&their_addr, &sin_size)) == -1) { + if ((newFD = accept(listenerFD, (struct sockaddr *)&remoteAddr, &sin_size)) == -1) { perror("accept"); continue; } fcntl(newFD, F_SETFL, fcntl(newFD, F_GETFL, 0) | O_NONBLOCK); - printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr)); -/* - if (!fork()) { // this is the child process - close(sockFD); // child doesn't need the listener -*/ - - if (doHandshake(newFD) != 0) { - close(newFD); - exit(0); - } -/* - if (doAccept(newFD) != 0) { - close(newFD); - exit(0); - } -*/ - - while (1) { - //bzero(&data,4096); - //recv(new_fd,&data,1,MSG_WAITALL); - /* - if (getAMF(newFD,data[0]) != 0x0) { - */ - if (getAMF(newFD) != 0x0) { - printf("Error"); - close(newFD); - exit(0); - } - } -/* + printf("server: got connection from %s\n",inet_ntoa(remoteAddr.sin_addr)); + if (doHandshake(newFD) != 0) { + close(newFD); + exit(0); } */ + while (1) { + if (getAMF(newFD) != 0x0) { + printf("Error"); + close(newFD); + exit(0); + } + } close(newFD); // parent doesn't need this } - return 0; } @@ -79,7 +63,6 @@ int amfID = 0x0; int headerSize = 0x0; int packetSize = 0x0; - int i = 0x0; char data[12]; amfHeader *amfHdr = (amfHeader *)&data; fd_set readset; @@ -102,10 +85,10 @@ switch(headerSize) { case 0: - while (select(fd + 1,&readset,0x0,0x0,0x0)) { - if (FD_ISSET(fd,&readset)) - break; - } + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } recLen = recv(fd,&data,11,0); printf("12 Byte Header\n"); @@ -116,11 +99,10 @@ AMFS[amfID].body = (char *)malloc(packetSize); AMFS[amfID].amfType = amfHdr->amfType[0]; if (packetSize > 128) { - while (select(fd + 1,&readset,0x0,0x0,0x0)) { - if (FD_ISSET(fd,&readset)) - break; - } - + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } recv(fd,AMFS[amfID].body + AMFS[amfID].bodyCount,128,0); AMFS[amfID].bodyCount = 128; } @@ -128,26 +110,46 @@ case 3: printf("4 Byte Header\n"); if ((AMFS[amfID].bodySize - AMFS[amfID].bodyCount) > 128) { - while (select(fd + 1,&readset,0x0,0x0,0x0)) { - if (FD_ISSET(fd,&readset)) - break; - } - + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } recv(fd,AMFS[amfID].body + (AMFS[amfID].bodyCount - 1),128,0); AMFS[amfID].bodyCount += 128; } else { printf("Bal: (%i)\n",(AMFS[amfID].bodySize - AMFS[amfID].bodyCount)); - while (select(fd + 1,&readset,0x0,0x0,0x0)) { - if (FD_ISSET(fd,&readset)) - break; - } - + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } recLen = recv(fd,AMFS[amfID].body + (AMFS[amfID].bodyCount - 1),(AMFS[amfID].bodySize - AMFS[amfID].bodyCount)-1,0); printf("Received: [%i]\n",recLen); AMFS[amfID].bodyCount += (AMFS[amfID].bodySize - AMFS[amfID].bodyCount)-1; } break; + case 2: + printf("2 Byte Header\n"); + if ((AMFS[amfID].bodySize - AMFS[amfID].bodyCount) > 128) { + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } + recv(fd,AMFS[amfID].body + (AMFS[amfID].bodyCount - 1),128,0); + AMFS[amfID].bodyCount += 128; + } + else { + printf("Bal: (%i)\n",(AMFS[amfID].bodySize - AMFS[amfID].bodyCount)); + while (select(fd + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(fd,&readset)) + break; + } + recLen = recv(fd,AMFS[amfID].body + (AMFS[amfID].bodyCount - 1),(AMFS[amfID].bodySize - AMFS[amfID].bodyCount)-1,0); + printf("Received: [%i]\n",recLen); + AMFS[amfID].bodyCount += (AMFS[amfID].bodySize - AMFS[amfID].bodyCount)-1; + } + break; + default: printf("Unhandled Header Size: [%i]\n",headerSize); return(-1); @@ -247,7 +249,7 @@ function[i] = '\0'; printf("Function: [%s]\n",function); if (!strcmp(function,"connect")) - doAccept(fd); + amfDoAccept(fd); for (i=0;i #include #include +#include +#include +#include +#include #include "rtmp.h" +int listenerFD = 0x0; +pthread_t listenerThread; +pthread_mutex_t sConnectionsMutex; + +myConnections_t *connections = 0x0; int sStartListener() { - int listenerFD = 0x0; int optVal = 0x1; struct sockaddr_in myAddr; // my address information - struct sockaddr_in remoteAddr; // connector's address information - socklen_t sin_size; if ((listenerFD = socket(PF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); @@ -50,6 +56,73 @@ } + pthread_mutex_init(&sConnectionsMutex,NULL); + pthread_create(&listenerThread,NULL,(void *)sListenerThread,(void *)listenerFD); + + return(0x0); + } + +ssize_t sReadSocket(int socketFD,void *buffer,size_t lenght) { + ssize_t recLen = 0x0; + fd_set readset; + + FD_ZERO(&readset); + FD_SET(socketFD,&readset); + + while (select(socketFD + 1,&readset,0x0,0x0,0x0)) { + if (FD_ISSET(socketFD,&readset)) + break; + } + //recv(socketFD,AMFS[amfID].body + (AMFS[amfID].bodyCount - 1),128,0); + return(recLen); + } + +void sListenerThread(int listenerFD) { + int newFD = 0x0; //New Socket; + socklen_t sin_size; + struct sockaddr_in remoteAddr; // connector's address information + + while (1) { + sin_size = sizeof(struct sockaddr_in); + + if ((newFD = accept(listenerFD, (struct sockaddr *)&remoteAddr, &sin_size)) == -1) { + perror("accept"); + continue; + } + + printf("server: got connection from %s\n",inet_ntoa(remoteAddr.sin_addr)); + + if (amfDoHandshake(newFD) == -1) { + printf("Error: Bad Handshake\n"); + close(newFD); + } + printf("Boobies"); + fcntl(newFD, F_SETFL, fcntl(newFD, F_GETFL, 0) | O_NONBLOCK); + sAddConnection(newFD); + } + } + +int sAddConnection(int socketFD) { + myConnections_t *tmpConnection = 0x0; + + pthread_mutex_lock(&sConnectionsMutex); + + if (connections == 0x0) { + connections = (myConnections_t *)malloc(sizeof(myConnections_t)); + connections->prev = 0x0; + connections->next = 0x0; + connections->socketFD = socketFD; + } + else { + tmpConnection = (myConnections_t *)malloc(sizeof(myConnections_t)); + tmpConnection->socketFD = socketFD; + tmpConnection->prev = 0x0; + tmpConnection->next = connections; + connections->prev = tmpConnection; + connections = tmpConnection; + } + printf("Socket Added"); + pthread_mutex_unlock(&sConnectionsMutex); return(0x0); }