void play_scenario(struct scenario *scn) { /* TODO remove hardcode */ char *ip = "10.40.0.2"; int port = 9; struct sockaddr_in sa; int option_value = 1; int s; char *buf = malloc(65535); int pc; struct cmd cmd; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(port); inet_aton(ip, &sa.sin_addr); s = socket(PF_INET, SOCK_STREAM, 0); if (connect(s, &sa, sizeof(sa)) < 0) perror("connect"); pc = 0; while (pc < scn->cmd_count) { cmd = scn->cmds[pc++]; switch (cmd.cmd) { case CMD_NOOP: break; case CMD_SYNTAX_ERROR: break; case CMD_GOTO: pc = cmd.arg; break; case CMD_CORK: set_socket_option(s, TCP_CORK, cmd.arg); break; case CMD_NODELAY: set_socket_option(s, TCP_NODELAY, cmd.arg); break; case CMD_DELAY: sprintf(buf, "/etc/delay %d", cmd.arg); if(system(buf) != 0) { fprintf(stderr, "Error on system(\"%s\")\n", buf); } break; case CMD_WAIT: usleep(cmd.arg * 1000); break; case CMD_SEND: if (send(s, buf, cmd.arg, 0) < 0) { perror("send"); } break; } } printf("Replay finished.\n"); }
int udp_data_handler::init(base_reactor* reactor,const char* host,int port) { if(m_fd >= 0 || reactor == NULL || host == NULL ) return -1 ; sa_in_t service_addr ; init_sa_in(&service_addr,host,port) ; int sfd = create_udp_service(&service_addr) ; if(sfd <0 ) return -2 ; set_socket_option(sfd,SO_RCVBUF,MAX_BUF_SIZE * 1024) ; set_socket_option(sfd,SO_SNDBUF,MAX_BUF_SIZE * 1024) ; if( reactor->add_handler(sfd, this, base_reactor::EVENT_READ)!=0 ) { close(sfd) ; return -3 ; } m_reactor = reactor ; m_fd = sfd ; return 0 ; }
static int open_tcp_socket () { struct sockaddr_in insock; int request; int retry; #ifndef SO_DONTLINGER #ifdef SO_LINGER static int linger[2] = { 0, 0 }; #endif /* SO_LINGER */ #endif /* SO_DONTLINGER */ #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE) #ifndef FORCE_DISPLAY_NUM extern int AIXTCPSocket; if (AIXTCPSocket>=0) { request= AIXTCPSocket; } else #endif /* FORCE_DISPLAY_NUM */ #endif /* AIX && etc. */ if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) { Error ("Creating TCP socket"); return -1; } #ifdef SO_REUSEADDR /* Necesary to restart the server without a reboot */ #ifdef hpux set_socket_option (request, SO_REUSEADDR); #else { int one = 1; setsockopt(request, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int)); } #endif #endif /* SO_REUSEADDR */ #if defined(AIX) && defined(IBM_OS_HAS_HFT) && !defined(IBM_OS_HAS_X_QUEUE) #ifndef FORCE_DISPLAY_NUMBER if (AIXTCPSocket<0) #endif #endif { bzero ((char *)&insock, sizeof (insock)); insock.sin_family = AF_INET; insock.sin_port = htons ((unsigned short)(X_TCP_PORT + atoi (display))); insock.sin_addr.s_addr = htonl(INADDR_ANY); retry = 20; while (bind(request, (struct sockaddr *) &insock, sizeof (insock))) { if (--retry == 0) { Error ("Binding TCP socket"); close (request); return -1; } #ifdef SO_REUSEADDR sleep (1); #else sleep (10); #endif /* SO_REUSEDADDR */ } } #ifdef SO_DONTLINGER if(setsockopt (request, SOL_SOCKET, SO_DONTLINGER, (char *)NULL, 0)) Error ("Setting TCP SO_DONTLINGER"); #else #ifdef SO_LINGER if(setsockopt (request, SOL_SOCKET, SO_LINGER, (char *)linger, sizeof(linger))) Error ("Setting TCP SO_LINGER"); #endif /* SO_LINGER */ #endif /* SO_DONTLINGER */ if (listen (request, 5)) { Error ("TCP Listening"); close (request); return -1; } return request; }
void eStreamClient::notifier(int what) { if (!(what & eSocketNotifier::Read)) return; ePtr<eStreamClient> ref = this; char buf[512]; int len; if ((len = singleRead(streamFd, buf, sizeof(buf))) <= 0) { rsn->stop(); stop(); parent->connectionLost(this); return; } request.append(buf, len); if (running || (request.find('\n') == std::string::npos)) return; if (request.substr(0, 5) == "GET /") { size_t pos; size_t posdur; if (eConfigManager::getConfigBoolValue("config.streaming.authentication")) { bool authenticated = false; if ((pos = request.find("Authorization: Basic ")) != std::string::npos) { std::string authentication, username, password; std::string hash = request.substr(pos + 21); pos = hash.find('\r'); hash = hash.substr(0, pos); authentication = base64decode(hash); pos = authentication.find(':'); if (pos != std::string::npos) { char *buffer = (char*)malloc(4096); if (buffer) { struct passwd pwd; struct passwd *pwdresult = NULL; std::string crypt; username = authentication.substr(0, pos); password = authentication.substr(pos + 1); getpwnam_r(username.c_str(), &pwd, buffer, 4096, &pwdresult); if (pwdresult) { struct crypt_data cryptdata; char *cryptresult = NULL; cryptdata.initialized = 0; crypt = pwd.pw_passwd; if (crypt == "*" || crypt == "x") { struct spwd spwd; struct spwd *spwdresult = NULL; getspnam_r(username.c_str(), &spwd, buffer, 4096, &spwdresult); if (spwdresult) { crypt = spwd.sp_pwdp; } } cryptresult = crypt_r(password.c_str(), crypt.c_str(), &cryptdata); authenticated = cryptresult && cryptresult == crypt; } free(buffer); } } } if (!authenticated) { const char *reply = "HTTP/1.0 401 Authorization Required\r\nWWW-Authenticate: Basic realm=\"streamserver\"\r\n\r\n"; writeAll(streamFd, reply, strlen(reply)); rsn->stop(); parent->connectionLost(this); return; } } pos = request.find(' ', 5); if (pos != std::string::npos) { std::string serviceref = urlDecode(request.substr(5, pos - 5)); if (!serviceref.empty()) { const char *reply = "HTTP/1.0 200 OK\r\nConnection: Close\r\nContent-Type: video/mpeg\r\nServer: streamserver\r\n\r\n"; writeAll(streamFd, reply, strlen(reply)); /* We don't expect any incoming data, so set a tiny buffer */ set_socket_option(streamFd, SO_RCVBUF, 1 * 1024); /* We like 188k packets, so set the TCP window size to that */ set_socket_option(streamFd, SO_SNDBUF, 188 * 1024); /* activate keepalive */ set_socket_option(streamFd, SO_KEEPALIVE, 1); /* configure keepalive */ set_tcp_option(streamFd, TCP_KEEPINTVL, 10); // every 10 seconds set_tcp_option(streamFd, TCP_KEEPIDLE, 1); // after 1 second of idle set_tcp_option(streamFd, TCP_KEEPCNT, 2); // drop connection after second miss /* also set 10 seconds data push timeout */ set_tcp_option(streamFd, TCP_USER_TIMEOUT, 10 * 1000); if (serviceref.substr(0, 10) == "file?file=") /* convert openwebif stream reqeust back to serviceref */ serviceref = "1:0:1:0:0:0:0:0:0:0:" + serviceref.substr(10); /* Strip session ID from URL if it exists, PLi streaming can not handle it */ pos = serviceref.find("&sessionid="); if (pos != std::string::npos) { serviceref.erase(pos, std::string::npos); } pos = serviceref.find("?sessionid="); if (pos != std::string::npos) { serviceref.erase(pos, std::string::npos); } pos = serviceref.find('?'); if (pos == std::string::npos) { eDebug("[eDVBServiceStream] stream ref: %s", serviceref.c_str()); if (eDVBServiceStream::start(serviceref.c_str(), streamFd) >= 0) { running = true; m_serviceref = serviceref; m_useencoder = false; } } else { request = serviceref.substr(pos); serviceref = serviceref.substr(0, pos); pos = request.find("?bitrate="); posdur = request.find("?duration="); eDebug("[eDVBServiceStream] stream ref: %s", serviceref.c_str()); if (posdur != std::string::npos) { if (eDVBServiceStream::start(serviceref.c_str(), streamFd) >= 0) { running = true; m_serviceref = serviceref; m_useencoder = false; } int timeout = 0; sscanf(request.substr(posdur).c_str(), "?duration=%d", &timeout); eDebug("[eDVBServiceStream] duration: %d seconds", timeout); if (timeout) { m_timeout->startLongTimer(timeout); } } else if (pos != std::string::npos) { /* we need to stream transcoded data */ int bitrate = 1024 * 1024; int width = 720; int height = 576; int framerate = 25000; int interlaced = 0; int aspectratio = 0; sscanf(request.substr(pos).c_str(), "?bitrate=%d", &bitrate); pos = request.find("?width="); if (pos != std::string::npos) sscanf(request.substr(pos).c_str(), "?width=%d", &width); pos = request.find("?height="); if (pos != std::string::npos) sscanf(request.substr(pos).c_str(), "?height=%d", &height); pos = request.find("?framerate="); if (pos != std::string::npos) sscanf(request.substr(pos).c_str(), "?framerate=%d", &framerate); pos = request.find("?interlaced="); if (pos != std::string::npos) sscanf(request.substr(pos).c_str(), "?interlaced=%d", &interlaced); pos = request.find("?aspectratio="); if (pos != std::string::npos) sscanf(request.substr(pos).c_str(), "?aspectratio=%d", &aspectratio); encoderFd = -1; if (eEncoder::getInstance()) encoderFd = eEncoder::getInstance()->allocateEncoder(serviceref, bitrate, width, height, framerate, !!interlaced, aspectratio); if (encoderFd >= 0) { running = true; streamThread = new eDVBRecordStreamThread(188); if (streamThread) { streamThread->setTargetFD(streamFd); streamThread->start(encoderFd); } m_serviceref = serviceref; m_useencoder = true; } } } } } } if (!running) { const char *reply = "HTTP/1.0 400 Bad Request\r\n\r\n"; writeAll(streamFd, reply, strlen(reply)); rsn->stop(); parent->connectionLost(this); return; } request.clear(); }
void CreateWellKnownSockets() { int request, i; int whichbyte; /* used to figure out whether this is LSB or MSB */ #ifdef TCPCONN struct sockaddr_in insock; int tcpportReg; /* port with same byte order as server */ #ifdef SO_LINGER static int linger[2] = { 0, 0 }; #endif /* SO_LINGER */ #endif /* TCPCONN */ #ifdef DNETCONN struct sockaddr_dn dnsock; #endif /* DNETCONN */ #ifdef ISOCONN struct TSAPdisconnect tds; struct TSAPdisconnect *td = &tds; struct TSAPaddr tas; struct TSAPaddr *ta = &tas; struct PSAPaddr *pa; AEI aei; #endif /* ISOCONN */ int retry; #ifdef ISOCONN #ifdef ISODEBUG isodetcpath = ISODEPATH; #endif #endif /* ISOCONN */ CLEARBITS(AllSockets); CLEARBITS(AllClients); CLEARBITS(LastSelectMask); CLEARBITS(ClientsWithInput); for (i=0; i<MAXSOCKS; i++) ConnectionTranslation[i] = (ClientPtr)NULL; #ifdef hpux lastfdesc = _NFILE - 1; #else lastfdesc = getdtablesize() - 1; #endif /* hpux */ if (lastfdesc > MAXSOCKS) { lastfdesc = MAXSOCKS; if (debug_conns) ErrorF( "GOT TO END OF SOCKETS %d\n", MAXSOCKS); } WellKnownConnections = 0; whichbyte = 1; if (*(char *) &whichbyte) whichByteIsFirst = 'l'; else whichByteIsFirst = 'B'; #ifdef TCPCONN tcpportReg = atoi (display); tcpportReg += X_TCP_PORT; if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0) { Notice ("Creating TCP socket"); } else { bzero ((char *)&insock, sizeof (insock)); insock.sin_family = AF_INET; insock.sin_port = htons (tcpportReg); insock.sin_addr.s_addr = htonl(INADDR_ANY); retry = 20; while (i = bind(request, (struct sockaddr *) &insock, sizeof (insock))) { #ifdef hpux /* Necesary to restart the server without a reboot */ if (errno == EADDRINUSE) set_socket_option (request, SO_REUSEADDR); if (--retry == 0) Error ("Binding TCP socket"); sleep (1); #else if (--retry == 0) Error ("Binding MSB TCP socket"); sleep (10); #endif /* hpux */ } #ifdef hpux /* return the socket option to the original */ if (errno) unset_socket_option (request, SO_REUSEADDR); #endif /* hpux */ #ifdef SO_LINGER if(setsockopt (request, SOL_SOCKET, SO_LINGER, (char *)linger, sizeof(linger))) Notice ("Setting TCP SO_LINGER\n"); #endif /* SO_LINGER */ if (listen (request, 5)) Error ("Reg TCP Listening"); WellKnownConnections |= (1 << request); DefineSelf (request); #ifdef ISOCONN fd2family[request] = UNIX_IO; #endif /* ISOCONN */ } #endif /* TCPCONN */ #ifdef UNIXCONN if ((request = open_unix_socket ()) != -1) { WellKnownConnections |= (1L << request); unixDomainConnection = request; #ifdef ISOCONN fd2family[request] = UNIX_IO; #endif /* ISOCONN */ } #endif /*UNIXCONN */ #ifdef DNETCONN if ((request = socket (AF_DECnet, SOCK_STREAM, 0)) < 0) { Notice ("Creating DECnet socket"); } else { bzero ((char *)&dnsock, sizeof (dnsock)); dnsock.sdn_family = AF_DECnet; sprintf(dnsock.sdn_objname, "X$X%d", atoi (display)); dnsock.sdn_objnamel = strlen(dnsock.sdn_objname); if (bind (request, (struct sockaddr *) &dnsock, sizeof (dnsock))) Error ("Binding DECnet socket"); if (listen (request, 5)) Error ("DECnet Listening"); WellKnownConnections |= (1 << request); DefineSelf (request); #ifdef ISOCONN fd2family[request] = UNIX_IO; #endif /* ISOCONN */ } #endif /* DNETCONN */ #ifdef ISOCONN /* * If display is set, its the string after the Colon: * i.e. X0 or X1 or T0 or T1... */ if ((display == NULLCP) || (atoi(display) == 0)) aei = str2aei(TLocalHostName(), DEFAULTTSERVICE); else aei = str2aei(TLocalHostName(), display); if (aei == NULLAEI) { ErrorF("No AEI for me:"); FatalError(TLocalHostName()); } /* * This hack only works if the PSAPaddr and SSAP addrsd are null!! */ if ((pa = aei2addr (aei)) == NULLPA) FatalError("address translation failed"); ta = (struct TSAPaddr *)&(pa->pa_addr.sa_addr); /* * Just put out a listen for now */ if ((request = TNetListen(ta, td)) != OK) { Error(TErrString(td->td_reason)); FatalError("TNetListen"); } WellKnownConnections |= (1 << request); DefineSelf (request); fd2family[request] = ISODE_IO; #endif /* ISOCONN */ if (WellKnownConnections == 0) Error ("No Listeners, nothing to do"); signal (SIGPIPE, SIG_IGN); signal (SIGHUP, AutoResetServer); signal (SIGINT, GiveUp); signal (SIGTERM, GiveUp); FirstClient = request + 1; AllSockets[0] = WellKnownConnections; ResetHosts(display); for (i=0; i<MaxClients; i++) { inputBuffers[i].buffer = (char *) NULL; inputBuffers[i].bufptr = (char *) NULL; inputBuffers[i].bufcnt = 0; inputBuffers[i].lenLastReq = 0; inputBuffers[i].size = 0; } }