/* * @brief open and return a listening socket on port * * @param port the server port number * @return int socket discriptor */ int Socket::open_ListenSocket(int port) { int listenfd, optval=1; struct sockaddr_in serveraddr; /* Create a socket descriptor */ if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { fprintf(stderr, "Error: Create server socket failed\n"); return -1; } /* Eliminates "Address already in use" error from bind. */ if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)) < 0) { fprintf(stderr, "Error: set server socket failed\n"); return -1; } /* Listenfd will be an endpoint for all requests to port on any IP address for this host */ bzero((char *) &serveraddr, sizeof(serveraddr)); serveraddr.sin_family = AF_INET; serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); serveraddr.sin_port = htons((unsigned short)port); if (bind(listenfd, (SA *)&serveraddr, sizeof(serveraddr)) < 0) { fprintf(stderr, "Error: bind server socket to port %d failed\n", port); Close_Socket(listenfd); return -1; } /* Make it a listening socket ready to accept connection requests */ if (listen(listenfd, LISTENQ) < 0) { fprintf(stderr, "Error: listen to port %d failed\n", port); Close_Socket(listenfd); return -1; } return listenfd; }
int main (void) { int loop=0, werbung=0, tnet=-1,blit=0,cnum=-1,tv,wflag=1,runtime=0, mut=0, vol=0, tmin=15; char tstr[40],zapchan[40],msgchan[40]; FILE *fh1; time_t t1,t2,t3,t4; printf("Blockad Version %s\n",P_VERSION); if (!ReadConf()) { printf("Blockad <Configuration failed>\n"); } else { if((fh1=fopen(FLG_FILE,"r"))!=NULL) { if(fgets(zapchan, sizeof(zapchan), fh1)) { Trim_String(zapchan); if(fgets(tstr, sizeof(tstr), fh1)) { sscanf(tstr,"%d",&cnum); if(fgets(tstr, sizeof(tstr), fh1)) { sscanf(tstr,"%d",&rezap); if(fgets(msgchan, sizeof(msgchan), fh1)) { Trim_String(msgchan); } else { sprintf(msgchan,"Unbekannt"); } if(fgets(tstr, sizeof(tstr), fh1)) { if(sscanf(tstr,"%d",&vol)==1) { if(volume) { if(fgets(tstr, sizeof(tstr), fh1)) { if(sscanf(tstr,"%d",&mut)!=1) { mut=0; } } } } } if(fgets(tstr, sizeof(tstr), fh1)) { if(sscanf(tstr,"%d",&tmin)!=1) { tmin=15; } } } } else { rezap=420; } loop=1; if(cnum<0) { inet=-1; } } fclose(fh1); } time(&t1); t4=t3=t1; #ifdef HAVE_DBOX_HARDWARE LCD_Init(); #endif if(inet>=0) { if(Open_Socket()==-1) { inet=-1; } } while(loop) { time(&t2); if(inet>=0) { tnet=-1; if(inet>=0) { tv=Check_Socket(cnum,&werbung); if(!werbung) { if((t2-t4)>tmin) { Do_Rezap(zapchan,vol,mut); loop=0; } } else { t4=t2; } if(tv>=0) { tnet=inet; } } inet=tnet; } if(loop) { if((fh1=fopen(FLG_FILE,"r"))!=NULL) { fclose(fh1); if(inet>=0) { if(wflag) { fh1=fopen(STS_FILE,"w"); fprintf(fh1,"%s\n",msgchan); fprintf(fh1,"Auto\n"); fclose(fh1); wflag=0; } if(t3!=t2) { sprintf(tstr,"%s",(blit^=1)?" ":" WZ"); #ifdef HAVE_DBOX_HARDWARE LCD_Read(); LCD_draw_string(97, 13, tstr); LCD_update(); #endif t3=t2; } sleep(1); } else { runtime=t2-t1; if(runtime>rezap) { Do_Rezap(zapchan,vol,mut); loop=0; } else { if(t3!=t2) { runtime=rezap-runtime; sprintf(tstr,"%2d:%02d",runtime/60,runtime%60); fh1=fopen(STS_FILE,"w"); fprintf(fh1,"%s\n",msgchan); fprintf(fh1,"%s\n",tstr); fclose(fh1); #ifdef HAVE_DBOX_HARDWARE LCD_Read(); LCD_draw_string(81, 13, tstr); LCD_update(); #endif sleep(1); t3=t2; } } } } else { loop=0; } } } } #ifdef HAVE_DBOX_HARDWARE LCD_Close(); #endif if(inet>=0) { Close_Socket(); } sprintf(tstr,"cd /tmp\nrm blockads.*"); system(tstr); return 0; }
void A_Connection_Manager_Socket::Run_Handler() { // Log Entry CLI_LOG_CLASS_ENTRY(); // Get the length const std::string log_tag = "SessionType: " + CORE::SessionTypeToString(m_configuration->Get_Session_Type()); socklen_t clilen; struct sockaddr_in cli_addr; clilen = sizeof(cli_addr); // Iteratively load connections while( m_is_running ) { // Spawn a new instance int client_fd; // Accept the socket LOG_TRACE( log_tag + ", Waiting for connection. (Socket FD: " + std::to_string(m_sock_fd) + ")"); client_fd = accept( m_sock_fd, (struct sockaddr*)&cli_addr, &clilen); // Check if we need to exit if( m_is_running != true ){ close(client_fd); continue; } // Check if the operation failed if( client_fd < 0 ){ sleep(1); continue; } // Make the socket non-blocking fcntl( client_fd, F_SETFL, O_NONBLOCK ); // Log char host[NI_MAXHOST]; getnameinfo( (struct sockaddr *)&cli_addr, sizeof(cli_addr), host, sizeof(host), NULL, 0, NI_NUMERICHOST); // Log Connection LOG_DEBUG(log_tag + ", Connection has been made by " + std::string(host)); // Call the process method int next_position = Get_Next_Client_Slot(); // Make sure we are not past the max number if( next_position < 0 ){ LOG_DEBUG( log_tag + ", Connection is rejected as max number of connections reached."); continue; } // Log the Creation LOG_DEBUG( log_tag + ", Starting the Socket Connection for ID " + std::to_string(next_position) + "."); // Add new Connection Instance CORE::Session session( next_position, CORE::ConnectionType::SOCKET ); session.Add_Connection_Data_Entry("IP_ADDRESS", std::string(host)); A_Socket_Base_Instance::ptr_t new_connection = nullptr; switch(m_configuration->Get_Session_Type()) { case CORE::SessionType::JSON: new_connection = std::make_shared<A_Socket_JSON_Instance>( m_configuration->Get_Instance_Config(), next_position, session, client_fd ); break; case CORE::SessionType::TELNET: new_connection = std::make_shared<A_Socket_Telnet_Instance>( m_configuration->Get_Instance_Config(), next_position, session, client_fd ); break; default: LOG_FATAL("Unsuported Session-Type: " + CORE::SessionTypeToString(m_configuration->Get_Session_Type())); } // Process Event //CORE::SessionEvent new_session_event( session, // CORE::SessionEventType::CONNECT ); //EVT::Event_Manager::Process_Event( // Start new_connection->Start(); m_connection_list[next_position] = new_connection; } // Set the running flag m_is_running = false; // Stop each connection for( auto conn : m_connection_list ) { conn.second->Set_Connection_Flag(false); conn.second->Join(); } // Close Socket Close_Socket(); // Log Exit CLI_LOG_CLASS_EXIT(); }
/****************************************************************************** * Blockads Main ******************************************************************************/ int main (void) { int loop=0,found,werbung=0,tnet, tv, cnum=-1; char tstr[512],line_buffer[512],zapchan[40],srcchan[40],*cpt1=NULL; FILE *fh1,*fh2; printf("Blockads Version %s\n",P_VERSION); if((fh1=fopen(FLG_FILE,"r"))==NULL) { // system("ping -c 5 google.com &"); fb = open(FB_DEVICE, O_RDWR); rc = open(RC_DEVICE, O_RDONLY); fcntl(rc, F_SETFL, (fcntl(rc, F_GETFL) | O_EXCL) & ~O_NONBLOCK); if((sx=Read_Neutrino_Cfg("screen_StartX"))<0) sx=80; if((ex=Read_Neutrino_Cfg("screen_EndX"))<0) ex=620; if((sy=Read_Neutrino_Cfg("screen_StartY"))<0) sy=80; if((ey=Read_Neutrino_Cfg("screen_EndY"))<0) ey=505; if(Read_Neutrino_Cfg("rounded_corners")>0) radius=9; else radius=0; //init framebuffer if(ioctl(fb, FBIOGET_FSCREENINFO, &fix_screeninfo) == -1) { printf("Blockads <FBIOGET_FSCREENINFO failed>\n"); return -1; } if(ioctl(fb, FBIOGET_VSCREENINFO, &var_screeninfo) == -1) { printf("Blockads <FBIOGET_VSCREENINFO failed>\n"); return -1; } if(!(lfb = (unsigned char*)mmap(0, fix_screeninfo.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0))) { printf("Blockads <mapping of Framebuffer failed>\n"); return -1; } //init fontlibrary if((error = FT_Init_FreeType(&library))) { printf("Tuxwetter <FT_Init_FreeType failed with Errorcode 0x%.2X>", error); munmap(lfb, fix_screeninfo.smem_len); return -1; } if((error = FTC_Manager_New(library, 1, 2, 0, &MyFaceRequester, NULL, &manager))) { printf("Tuxwetter <FTC_Manager_New failed with Errorcode 0x%.2X>\n", error); FT_Done_FreeType(library); munmap(lfb, fix_screeninfo.smem_len); return -1; } if((error = FTC_SBitCache_New(manager, &cache))) { printf("Tuxwetter <FTC_SBitCache_New failed with Errorcode 0x%.2X>\n", error); FTC_Manager_Done(manager); FT_Done_FreeType(library); munmap(lfb, fix_screeninfo.smem_len); return -1; } if((error = FTC_Manager_Lookup_Face(manager, FONT, &face))) { printf("Tuxwetter <FTC_Manager_Lookup_Face failed with Errorcode 0x%.2X>\n", error); FTC_Manager_Done(manager); FT_Done_FreeType(library); munmap(lfb, fix_screeninfo.smem_len); return -1; } use_kerning = FT_HAS_KERNING(face); #ifdef FT_NEW_CACHE_API desc.face_id = FONT; desc.flags = FT_LOAD_MONOCHROME; #else desc.font.face_id = FONT; desc.image_type = ftc_image_mono; #endif //init backbuffer if(!(lbb = malloc(var_screeninfo.xres*var_screeninfo.yres))) { printf("Blockads <allocating of Backbuffer failed>\n"); munmap(lfb, fix_screeninfo.smem_len); return -1; } memset(lbb, TRANSP, var_screeninfo.xres*var_screeninfo.yres); if (!ReadConf()) { printf("Blockads <Configuration failed>\n"); return -1; } found=0; loop=0; if(!HTTP_downloadFile("localhost",80,"/control/channellist", LST_FILE, 0, 1)) { if(!HTTP_downloadFile("localhost",80,"/control/zapto", ZAP_FILE, 0, 1)) { if((fh1=fopen(ZAP_FILE,"r"))!=NULL) { while((fgets(line_buffer, sizeof(line_buffer), fh1)>0) && (strlen(line_buffer)<4)); if(strlen(line_buffer)>1) { Trim_String(line_buffer); strcpy(zapchan,line_buffer); if((fh2=fopen(LST_FILE,"r"))!=NULL) { while(!found && (fgets(line_buffer, sizeof(line_buffer), fh2))) { if(strstr(line_buffer,zapchan)==line_buffer) { if((cpt1=strchr(line_buffer,' '))!=NULL) { cpt1++; Trim_String(cpt1); strcpy(msgchan,cpt1); loop=1; if(!Translate_Channel(cpt1, tstr)) { cnum=Get_ChannelNumber(tstr); strcpy(srcchan,tstr); found=1; } } } } if(!found) { inet=-1; } fclose(fh2); } } fclose(fh1); } } } if(!loop) { sprintf(tstr,"Fehler beim Auslesen der Kanalliste.\nPlugin konnte nicht gestartet werden."); Msg_Popup(tstr); return -1; } if(volume) { volume=0; if(!HTTP_downloadFile("localhost",80,"/control/volume?status", ZAP_FILE, 0, 1)) { if((fh1=fopen(ZAP_FILE,"r"))!=NULL) { while((fgets(line_buffer, sizeof(line_buffer), fh1)>0) && !strlen(line_buffer)); if(strlen(line_buffer)>=1) { Trim_String(line_buffer); sscanf(line_buffer,"%d",&mute); } fclose(fh1); } } if(!HTTP_downloadFile("localhost",80,"/control/volume", ZAP_FILE, 0, 1)) { if((fh1=fopen(ZAP_FILE,"r"))!=NULL) { while((fgets(line_buffer, sizeof(line_buffer), fh1)>0) && !strlen(line_buffer)); if(strlen(line_buffer)>=1) { Trim_String(line_buffer); sscanf(line_buffer,"%d",&volume); } fclose(fh1); } } } if(inet>=0) { tnet=-1; if(Open_Socket()!=-1) { tv=Check_Socket(cnum,&werbung); if(tv>0) { tnet=inet; if(!werbung) { if(!zapalways) { int adf=0; sprintf(tstr,"Für Kanal %s wird\nmomentan keine Werbung gemeldet.\nWerbung läuft im Augenblick auf:\n",msgchan); for(tv=0; tv<NUM_CHANNELS; tv++) { Check_Channel(tv,&found); if(found) { sprintf(tstr+strlen(tstr),"\n%s",Get_ChannelName(tv)); adf=1; } } if(!adf) { sprintf(tstr+strlen(tstr),"\nkeinem anderen Sender"); } Msg_Popup(tstr); loop=0; } else { tnet=-1; } } else { if((fh2=fopen(FLG_FILE,"w"))!=NULL) { int adf=0; fprintf(fh2,"%s\n%d\n%d\n%s\n%d\n%d\n%d\n",zapchan,cnum,rezap*60,msgchan,volume,mute,debounce); fclose(fh2); system("/bin/blockad &"); sprintf(tstr,"Werbezapper fr Kanal %s aktiviert.\nSie können jetzt umschalten.\n\nWerbung läuft im Augenblick auch auf:\n",msgchan); for(tv=0; tv<NUM_CHANNELS; tv++) { Check_Channel(tv,&found); if(found && (tv != cnum)) { sprintf(tstr+strlen(tstr),"\n%s",Get_ChannelName(tv)); adf=1; } } if(!adf) { sprintf(tstr+strlen(tstr),"\nkeinem anderen Sender"); } Msg_Popup(tstr); } } } Close_Socket(); } inet=tnet; } if(inet==-1 && loop) { Start_NoInet(zapchan); } } else { Msg_Popup("Werbezapper deaktiviert"); remove(FLG_FILE); sleep(2); } // clear Display memset(lbb, TRANSP, var_screeninfo.xres*var_screeninfo.yres); memcpy(lfb, lbb, var_screeninfo.xres*var_screeninfo.yres); munmap(lfb, fix_screeninfo.smem_len); close(fb); free(lbb); FTC_Manager_Done(manager); FT_Done_FreeType(library); fcntl(rc, F_SETFL, O_NONBLOCK); close(rc); return 0; }