/* Small echo service. It listen on a port and copy back what is sent */ #include #include #include #include #include #include #include #include #include struct HANDLE_INFO: public ARRAY_OBJ{ long long bytes; HANDLE_INFO(){ bytes = 0; } }; int main (int argc, char *argv[]) { glocal const char *bind = NULL; glocal const char *port = NULL; glocal const char *message = "ECHO"; glocal const char *pidfile = "/var/run/horizon.pid"; glocal const char *user = "blackhole"; glocal bool show = false; glocal bool daemon = false; glocal bool rawmode = false; int ret = (argc, argv); setarg ('b',"bind","bind on this IP",glocal.bind,false); setarg ('p',"port","Setup the service on this port",glocal.port,true); setarg ('m',"message","Greeting message",glocal.message,false); setarg ('v',"showinput","Show lines received",glocal.show,false); setarg ('R',"rawmode","Raw mode, not line",glocal.rawmode,false); setgrouparg ("Daemon options"); setarg (' ',"daemon","Run in background",glocal.daemon,false); setarg (' ',"pidfile","Write the daemon pid in this file",glocal.pidfile,false); setarg (' ',"user","Run as this user",glocal.user,false); glocal int nbcon = 0; (glocal.bind,glocal.port,1); settcpnodelay (true); sendf ("Welcome to service %s\r\n",glocal.message); info.data = new HANDLE_INFO; if (glocal.rawmode) setrawmode(true); glocal.nbcon++; if (glocal.show) fprintf (stderr,"receive %s\n",line); HANDLE_INFO *n = (HANDLE_INFO*)info.data; n->bytes += info.linelen; if (strcmp(line,"quit")==0){ endclient = true; send ("bye\r\n"); }else if (strcmp(line,"sleep")==0){ send ("Sleeping 100 seconds\r\n"); //closeclient(no); //sleep (100); endclient = true; }else if (strcmp(line,"hang")==0){ send ("Hanging forever\r\n"); while (1){ sleep (1); int v = 0; int n = ioctl (no,FIONREAD,&v); printf ("n=%d v=%d errno=%d (%s)\n" ,n,v,errno,strerror(errno)); } endclient = true; }else if (strcmp(line,"status")==0){ sendf ("Received %d connections (ended)\n",glocal.nbcon); void *data; int fd = iter_init(data); while (fd != -1){ HANDLE_INFO *c = (HANDLE_INFO*)data; sendf ("handle %d%s: %Ld bytes %s\n",fd ,fd==no ? "(me)" : " " ,c->bytes ,islistening(fd) ? "" : "stopped"); fd = iter_next (data); } }else if (strncmp(line,"stop",4)==0){ int fd = atoi(str_skip(line+4)); if (getclientdata(fd)!=NULL){ setlisten (fd,false); sendf ("Handle %d is not listening anymore\n",fd); }else{ sendf ("Handle %d not valid\n",fd); } }else if (strncmp(line,"start",5)==0){ int fd = atoi(str_skip(line+5)); if (getclientdata(fd)!=NULL){ setlisten (fd,true); sendf ("Handle %d is resuming operation\n",fd); }else{ sendf ("Handle %d not valid\n",fd); } }else if (strncmp(line,"close",5)==0){ int fd = atoi(str_skip(line+5)); if (getclientdata(fd)!=NULL){ closeclient (fd); sendf ("Handle %d is closed\n",fd); }else{ sendf ("Handle %d not valid\n",fd); } }else{ sendf ("echo %s\r\n",line); } signal (SIGPIPE,SIG_IGN); if (s.is_ok()){ if (glocal.daemon){ daemon_init (glocal.pidfile,glocal.user); } s.loop(); } return 0; return ret; }