Advertisement
Guest User

Untitled

a guest
Sep 21st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.67 KB | None | 0 0
  1. //
  2. // Created by Dennis Dominkovic
  3. //
  4.  
  5. #include <arpa/inet.h>
  6. #include <dirent.h>
  7. #include <fcntl.h>
  8. #include <math.h>
  9. #include <limits.h>
  10. #include <netinet/in.h>
  11. #include <pthread.h>
  12. #include <semaphore.h>
  13. #include <signal.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <string.h>
  18. #include <sys/fcntl.h>
  19. #include <sys/socket.h>
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <sys/wait.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25.  
  26. #define MAXLINELEN 2048
  27.  
  28. #define CMDLINESIZE 2048
  29. #define MAXARGS 100
  30. #define MAXDIRSIZE 100
  31. #define ADDITIONALDIRS "/bin/:/usr/bin/:/sbin/:/usr/sbin/"
  32. #define LOGFILE "/tmp/is141007"
  33. //#define SEMNAME "/is141007"
  34.  
  35. //sem_t *sem_logfile;
  36.  
  37.  
  38.  
  39.  
  40. typedef struct filestats_t{
  41. long long inode;
  42. char name[CMDLINESIZE];
  43. char perm[5];
  44. time_t mtime;
  45. } t_filestats;
  46.  
  47. void* shellthread(void* sockfd);
  48. int do_log(int sockfd, const char* cmdline);
  49.  
  50. int original_stdout;
  51.  
  52. pthread_mutex_t strtok_m;
  53. pthread_mutex_t stdout_m;
  54. pthread_mutex_t logfile_m;
  55.  
  56.  
  57.  
  58. //logging methode
  59. /*void logging(char *cmdline, sem_t *semfile) {
  60. int fileDescriptor;
  61.  
  62. //error wenn sem_wait failed
  63. if ( sem_wait(semfile) == -1 ) {
  64. perror("sem_wait");
  65. }
  66.  
  67. mode_t mask = umask(0);
  68. //logging file öffnen bzw erstellen wenn noch nicht existiert, ansonsten anhängen
  69. //bei fehler anhängen an vorhandnes file
  70. if( (fileDescriptor=open(LOGGINGFILE, O_RDWR | O_APPEND | O_CREAT, 0600)) == -1 ) {
  71. perror("open or create of logging file failed");
  72. if ( sem_post(semfile) ) {
  73. perror("sem_post");
  74. }
  75. umask(mask);
  76. return;
  77. }
  78. umask(mask);
  79.  
  80. //commandline in file schreiben
  81. if( write(fileDescriptor, cmdline, strlen(cmdline)) == -1) {
  82. perror("write");
  83. close(fileDescriptor);
  84. if ( sem_post(semfile) ) {
  85. perror("sem_post");
  86. }
  87. return;
  88. }
  89. //abschließendes \n anzeile anhängen
  90. if( write(fileDescriptor, "\n", strlen("\n")) == -1) {
  91. perror("write");
  92. close(fileDescriptor);
  93. if ( sem_post(semfile) ) {
  94. perror("sem_post");
  95. }
  96. return;
  97. }
  98. //file schließen
  99. close(fileDescriptor);
  100.  
  101. //semaphore unlocken
  102. if ( sem_post(semfile) ) {
  103. perror("sem_post");
  104. }
  105.  
  106. return;
  107. }*/
  108.  
  109.  
  110. int do_log(int sockfd, const char* cmdline) {
  111. struct sockaddr_in peer;
  112. unsigned int socklen = sizeof(peer);
  113. char ip[MAXLINELEN];
  114. FILE *logfile;
  115.  
  116. //logfile mutex
  117. pthread_mutex_lock(&logfile_m);
  118.  
  119. //File öffnen und hinten anhängen, falls es nicht exisitiert neu erstellen
  120. logfile = fopen(LOGFILE, "a+");
  121.  
  122. //falls beim öffnen ein Fehler auftritt --> Error
  123. if(logfile == NULL) {
  124. perror("logfile");
  125. pthread_mutex_unlock(&logfile_m);
  126. return -1;
  127. }
  128.  
  129. //Peernamen von Client bekommen.
  130. if(getpeername(sockfd, (struct sockaddr*)&peer, &socklen) == -1) {
  131. perror("peername");
  132. return -1;
  133. }
  134.  
  135. //Von Peer IP in Variable kopieren
  136. strcpy(ip, inet_ntoa(peer.sin_addr));
  137. strcat(ip, ": ");
  138. //In Logfile schreiben <IP>: <cmdline>
  139. fwrite(ip, sizeof(char), strlen(ip), logfile);
  140. fwrite(cmdline, sizeof(char), strlen(cmdline), logfile);
  141. fwrite("\n", sizeof(char), 1, logfile);
  142. //logfile schließen
  143. fclose(logfile);
  144. //mutex unlocken
  145. pthread_mutex_unlock(&logfile_m);
  146.  
  147. return 0;
  148. }
  149.  
  150. // ENV(PATH) -> ADDITIONALDIRS -> CWD
  151. int execvc(char**cmdv) {
  152. char cmd[CMDLINESIZE];
  153. char path[CMDLINESIZE];
  154. char* t;
  155.  
  156. // checking ENV(PATH)
  157. strcpy(path, getenv("PATH"));
  158. //nach jedem doppelpunkt den Pfad trennen und schauen ob execute rechte vorhanden sind und existenz prüfen
  159. for (t=strtok(path,":"); t; t=strtok(NULL,":")){
  160. strcpy(cmd,t);
  161. strcat(cmd,"/");
  162. strcat(cmd, cmdv[0]);
  163. if ( access(cmd, F_OK) == 0 && access(cmd, X_OK) == 0) {
  164. return execv(cmd, cmdv);
  165. }
  166. }
  167.  
  168. // checking ADDITIONALDIRS
  169. //nach jedem doppelpunkt den Pfad trennen und schauen ob execute rechte vorhanden sind und existenz prüfen
  170. strcpy(path, ADDITIONALDIRS);
  171. for (t=strtok(path,":"); t; t=strtok(NULL,":")){
  172. strcpy(cmd,t);
  173. strcat(cmd, cmdv[0]);
  174. if ( access(cmd, F_OK) == 0 && access(cmd, X_OK) == 0) {
  175. return execv(cmd, cmdv);
  176. }
  177. }
  178.  
  179. // checking CWD
  180. //aktuelles directory checken
  181. getcwd(path, CMDLINESIZE);
  182. strcpy(cmd,path);
  183. strcat(cmd,"/");
  184. strcat(cmd, cmdv[0]);
  185. if ( access(cmd, F_OK) == 0 && access(cmd, X_OK) == 0) {
  186. return execv(cmd, cmdv);
  187. }
  188.  
  189. return -1;
  190. }
  191.  
  192. //vergleichsfunktion
  193. int compare(const void* s1, const void* s2) {
  194. struct filestats_t* f1 = (struct filestats_t*) s1;
  195. struct filestats_t* f2 = (struct filestats_t*) s2;
  196.  
  197. if(f1->mtime > f2->mtime) {
  198. return 1;
  199. }
  200. //wenn files gleich groß sind anhand des filenamens sortieren.
  201. else if (f1->mtime == f2->mtime) {
  202. return strcmp(f1->name, f2->name);
  203. }
  204. else {
  205. return -1;
  206. }
  207. }
  208.  
  209. void execute(char **argv, int bg, char* cwd, int sockfd){
  210.  
  211. char** cmdv = argv;
  212. int pid;
  213. int status;
  214. int hintergrund=bg;
  215.  
  216. //mittels & befehl im hintergrund starten
  217. /*if ( cmdline[len-1] == '&' )
  218. {
  219. hintergrund=1;
  220. cmdline[len-1]='\0';
  221. }*/
  222.  
  223. //logging starten
  224. //logging(cmdline, sem_logfile);
  225.  
  226. //abspeichern der argumente in cmdv
  227.  
  228. switch( pid=fork() )
  229. {
  230. case -1: perror("Prozesserzeugung");
  231. break;
  232. // child
  233. case 0:
  234. //Interrupt Signal nach "default" behandeln
  235. signal(SIGINT,SIG_DFL);
  236. //working directory auf cwd ändern
  237. chdir(cwd);
  238. //stdin, out und err auf den sockfd FileDescriptor legen
  239. dup2(sockfd, 0);
  240. dup2(sockfd, 1);
  241. dup2(sockfd, 2);
  242. //command ausführen
  243. execvp(cmdv[0],cmdv);
  244. perror(cmdv[0]);
  245. exit(1);
  246. default:
  247. //Wenn prozess nicht im hintergrundläuft
  248. if (!hintergrund)
  249. {
  250. //auf prozess warten
  251. waitpid(pid, &status, 0);
  252. pthread_mutex_lock(&stdout_m);
  253.  
  254. //check of Prozess normal beendet wurde oder getötet wurde
  255. if (WIFEXITED(status))
  256. {
  257. printf("Kind hat Arbeit erledigt mit Ret Code %d\n", WEXITSTATUS(status));
  258. }
  259. else
  260. {
  261. printf("Kind wurde getoetet mit Signal %d\n", status);
  262. }
  263.  
  264. pthread_mutex_unlock(&stdout_m);
  265. }
  266.  
  267. }
  268.  
  269. }
  270.  
  271.  
  272. void* shellthread(void* param) {
  273. char line[MAXLINELEN];
  274. int anz, tries, sockfd;
  275. char* cmdv[MAXARGS];
  276. char cwd[MAXLINELEN];
  277. int hintergrund = 0;
  278.  
  279. //Interrupt Signal ignorieren.
  280. signal(SIGINT,SIG_IGN);
  281.  
  282. sockfd = (int)(param);
  283. writesock(sockfd, "WELCOME\n");
  284.  
  285. //Passwort Abfrage (3 Versuche)
  286. for(tries = 0; tries < 3; tries++) {
  287. writesock(sockfd, "Enter Password: ");
  288. anz = (int) read(sockfd, line, MAXLINELEN);
  289. if(!anz) {
  290. perror("read");
  291. }
  292. else {
  293. //letztes zeichen sollte es ein Return sein in eine 0 umwandeln
  294. line[anz]=0; // make c-string
  295. if (anz>0 && (line[anz-1]=='\n'||line[anz-1]==10||line[anz-1]==13))
  296. line[--anz]=0;
  297. if (anz>0 && (line[anz-1]=='\r'||line[anz-1]==10||line[anz-1]==13))
  298. line[--anz]=0;
  299. sleep(1); // benutzer nach eingabe von passwort 1 Sekunde warten lassen, bis Passwort gechecked wird.
  300. if (!strcmp("dominkovic", line)) {
  301. break;
  302. } else {
  303. writesock(sockfd, "wrong password. try again\n");
  304. }
  305. }
  306. }
  307.  
  308. //Wenn 3 Versuche erreicht worden sind --> Client raushauen, Funktion beenden
  309. if(tries == 3) {
  310. writesock(sockfd, "Nice try");
  311. shutdown(sockfd, SHUT_RDWR); // socket "freigeben"
  312. close(sockfd);
  313. return NULL;
  314. }
  315.  
  316. //Wenn Loginversuch gestimmt hat, prompt ausgeben
  317. for(;;) {
  318. printf("PID: %d\n", getpid());
  319. writesock(sockfd, "remoteshell >>>> ");
  320. anz=(int)read(sockfd,line,MAXLINELEN-1);
  321. line[anz]=0; // make c-string
  322. //eingabe am server ausgeben
  323. printf("line: %s", line);
  324.  
  325. //letztes zeichen sollte es ein Return sein in eine 0 umwandeln
  326. if (anz>0 && (line[anz-1]=='\n'||line[anz-1]==10||line[anz-1]==13))
  327. line[--anz]=0;
  328. if (anz>0 && (line[anz-1]=='\r'||line[anz-1]==10||line[anz-1]==13))
  329. line[--anz]=0;
  330.  
  331. //logging starten
  332. do_log(sockfd, line);
  333.  
  334. // check if background
  335. if (line[anz-1] == '&')
  336. {
  337. hintergrund=1;
  338. line[anz-1] = 0;
  339. }
  340.  
  341. //eingabe in argumente zerlegen
  342. int i;
  343. pthread_mutex_lock(&strtok_m);
  344. for (cmdv[i=0]=strtok(line," \n\t");
  345. cmdv[i] != NULL;
  346. cmdv[++i]=strtok(NULL," \n\t"))
  347. ;
  348. pthread_mutex_unlock(&strtok_m);
  349.  
  350. if(cmdv[0] == NULL) { // nur <enter> gedrückt
  351. printf("skipping due to empty line\n");
  352. continue; // zurück zur prompt
  353. }
  354.  
  355. //Server runterfahren
  356. else if(!strcmp("exit-server", cmdv[0])) {
  357. writesock(sockfd, "Shutting Down Sever\n");
  358. shutdown(sockfd, SHUT_RDWR);
  359. close(sockfd);
  360. exit(0);
  361. }
  362. //Nur client session beenden
  363. else if (!strcmp("exit",cmdv[0]))
  364. {
  365. writesock(sockfd, "ByeBye\n");
  366. shutdown(sockfd, SHUT_RDWR);
  367. close(sockfd);
  368. break;
  369. }else if(!strcmp("info",cmdv[0]))
  370. {
  371.  
  372. getcwd(cwd,sizeof(cwd));
  373. mode_t mask;
  374.  
  375. mask = umask(0);
  376. umask(mask);
  377.  
  378. writesock(sockfd, "UID: %d\nUSR: %s\nCWD: %s\nUMASK: %04o\nENV PATH: %s\n", getuid(),getenv("USER"),cwd,mask,getenv("PATH"));
  379.  
  380. //chdir für jeden Thread einzeln in "cwd" speichern
  381. }else if(!strcmp("pwd", cmdv[0])){
  382. //char cwd[CMDLINESIZE];
  383. getcwd(cwd,sizeof(cwd));
  384. writesock(sockfd,"%s\n", cwd);
  385.  
  386. }/*else if (!strcmp("id",cmdv[0])){
  387. struct group *grp;
  388. grp = getgrgid(getgid());
  389. //print data of current user
  390. writesock(sockfd, "user=%u(%s)\ngroup=%u(%s)\ngroups=", getuid(), getlogin(), getgid(), grp->gr_name);
  391.  
  392. //get all groups of user and print id/name
  393. int ngroups, i;
  394. gid_t groups[NGROUPS_MAX];
  395.  
  396. ngroups = NGROUPS_MAX;
  397. if ( getgrouplist( getlogin(), getegid(), groups, &ngroups) == -1) {
  398. writesock(sockfd,"Groups array is too small: %d\n", ngroups);
  399. }
  400. for (i=0; i < ngroups; i++) {
  401. grp = getgrgid(groups[i]);
  402. writesock(sockfd, "%d (%s), ", groups[i], grp->gr_name);
  403. }
  404. printf ("\n");
  405.  
  406.  
  407. }*/else if (!strcmp("chdir",cmdv[0]) || !strcmp("cd",cmdv[0]) || !strcmp("chd",cmdv[0]))
  408. {
  409. if (cmdv[1]!=NULL) {
  410. strncpy(cwd, cmdv[1], MAXLINELEN-1); // working direktory für thread sichern
  411. //chdir(cmdv[1]);
  412. } else {
  413. writesock(sockfd, "Syntax ist: chd Verzeichnis!\n");
  414. }
  415. } else {
  416. //befehl ausführen innerhalb von "cwd"
  417. execute(cmdv, hintergrund, cwd, sockfd);
  418. }
  419.  
  420.  
  421.  
  422. }
  423.  
  424.  
  425. /**
  426.  
  427. cmdv[0] = strtok( cmdline, " " );
  428. for( i = 0; i < MAXARGS - 1 && cmdv[i] != NULL; i++ )
  429. {
  430. cmdv[i+1] = strtok( NULL, " " );
  431. }
  432.  
  433. //cd command & check arguments
  434. else if (strcmp("cd",cmdv[0]) == 0)
  435. {
  436. if (!cmdv[1])
  437. fprintf(stderr,"Syntax: cd <directory>\n");
  438. else
  439. chdir(cmdv[1]);
  440. strcpy(cwd,cmdv[1]);
  441. }
  442. //current working directory path command
  443.  
  444.  
  445. //identification command
  446. else if(strcmp("id", cmdv[0]) == 0) {
  447. struct group *grp;
  448. grp = getgrgid(getgid());
  449. //print data of current user
  450. fprintf(stdout, "user=%u(%s) group=%u(%s), groups=", getuid(), getlogin(), getgid(), grp->gr_name);
  451.  
  452. //get all groups of user and print id/name
  453. int ngroups, i;
  454. gid_t groups[NGROUPS_MAX];
  455.  
  456. ngroups = NGROUPS_MAX;
  457. if ( getgrouplist( getlogin(), getegid(), groups, &ngroups) == -1) {
  458. printf ("Groups array is too small: %d\n", ngroups);
  459. }
  460. for (i=0; i < ngroups; i++) {
  461. grp = getgrgid(groups[i]);
  462. printf("%d (%s), ", groups[i], grp->gr_name);
  463. }
  464. printf ("\n");
  465. }
  466.  
  467. //umask command
  468. else if(strcmp("umask", cmdv[0]) == 0) {
  469. mode_t mask;
  470. mask = umask(0);
  471. umask(mask);
  472. printf("%04o\n", mask);
  473. }
  474.  
  475. //setpath command
  476. else if(strcmp("setpath",cmdv[0]) == 0) {
  477. if(!cmdv[1]) {
  478. fprintf(stderr, "Syntax: setpath </my/lovely/path:/some/directory/>\n");
  479. }
  480. else {
  481. //set PATH var in current process enviroment
  482. setenv("PATH", cmdv[1], 1);
  483. }
  484. }
  485. //info command
  486.  
  487. //list command
  488. else if(strcmp("list", cmdv[0]) == 0) {
  489. if(!cmdv[1]) {
  490. fprintf(stderr, "Syntax: list </path/to/directory>\n");
  491. }
  492. else {
  493. char dir[CMDLINESIZE];
  494. DIR* directory;
  495. t_filestats content[CMDLINESIZE];
  496. struct dirent* file;
  497. int filecount = 0;
  498. char filename[CMDLINESIZE];
  499. struct stat inode;
  500. char buffer[26];
  501. struct tm* tm_info;
  502.  
  503. //copy argument to dir var
  504. strcpy(dir, cmdv[1]);
  505.  
  506. //check if we can stat dir
  507. if ( stat(dir, &inode) == -1 ) {
  508. perror(dir);
  509. return;
  510. }
  511.  
  512. //check if the inode is a directory
  513. if ( !S_ISDIR(inode.st_mode) ) {
  514. fprintf(stderr, "%s is no directory.\n", dir);
  515. return;
  516. }
  517.  
  518. //check if we can open dir
  519. if ( (directory=opendir(dir)) == NULL ) {
  520. fprintf(stderr, "error when opening %s.\n", dir);
  521. return;
  522. }
  523.  
  524. //read whole directory until we reach NULL or filecount is equal MAXDIRSIZE
  525. while ( (file=readdir(directory)) != NULL && filecount <= MAXDIRSIZE ) {
  526. t_filestats currfile;
  527.  
  528. //concat filename of current direntry
  529. strcpy(filename,dir);
  530. if ( filename[strlen(filename)-1] != '/' ) {
  531. strcat(filename,"/");
  532. }
  533. strcat(filename, file->d_name);
  534.  
  535. //try to stat currentfile
  536. if ( stat(filename, &inode)==-1 ) {
  537. perror(filename);
  538. continue;
  539. }
  540.  
  541. //write data in currfile struct
  542. currfile.inode = file->d_ino;
  543. strcpy(currfile.name, file->d_name);
  544.  
  545. //check files types
  546. if ( S_ISBLK(inode.st_mode) ) {
  547. strcpy(currfile.perm,"b");
  548. } else if ( S_ISCHR(inode.st_mode) ) {
  549. strcpy(currfile.perm,"c");
  550. } else if ( S_ISDIR(inode.st_mode) ) {
  551. strcpy(currfile.perm,"d");
  552. } else if ( S_ISFIFO(inode.st_mode) ) {
  553. strcpy(currfile.perm,"p");
  554. } else if ( S_ISREG(inode.st_mode) ) {
  555. strcpy(currfile.perm,"-");
  556. } else if ( S_ISLNK(inode.st_mode) ) {
  557. strcpy(currfile.perm,"l");
  558. //} else if ( S_ISSOCK(inode.st_mode) ) {
  559. // strcpy(currfile.perm,"s");
  560. } else {
  561. strcpy(currfile.perm,"u");
  562. }
  563.  
  564. //append to permissions if we can read write or execute
  565. strcat(currfile.perm, access(filename, R_OK) != -1 ? "r" : "-");
  566. strcat(currfile.perm, access(filename, W_OK) != -1 ? "w" : "-");
  567. strcat(currfile.perm, access(filename, X_OK) != -1 ? "x" : "-");
  568.  
  569. //assign mtime to struct
  570. currfile.mtime=inode.st_mtime;
  571.  
  572. content[filecount]=currfile;
  573.  
  574. filecount++;
  575.  
  576.  
  577. }
  578.  
  579.  
  580. //print content array
  581. printf("%4s\t", "perm");
  582. printf("%-10s\t", "inode");
  583. printf("%-25s\t", "name");
  584. printf("%-35s\n", "mtime");
  585.  
  586. //sort array depending on mtime
  587. qsort(content, filecount, sizeof(struct filestats_t), compare);
  588.  
  589.  
  590. for(int i = 0; i < filecount; i++) {
  591.  
  592. tm_info = localtime(&content[i].mtime);
  593. strftime(buffer,26,"%Y:%m:%d %H:%M:%S", tm_info);
  594.  
  595. printf("%4s\t", content[i].perm);
  596. printf("%-10llu\t", content[i].inode);
  597. printf("%-25s\t", content[i].name);
  598. printf("%-35s\n", buffer);
  599.  
  600. }
  601.  
  602. //close dir, on error, print an awesome message
  603. if ( closedir(directory) == -1 ) {
  604. fprintf(stderr, "error when closing %s.", dir);
  605. return;
  606. }
  607. }
  608. }
  609. else
  610.  
  611. switch ( pid=fork() )
  612. {
  613. case -1: perror ("Prozesserzeugung");
  614. break;
  615. case 0: //child
  616. //execvp(cmdv[0], cmdv);
  617. execvc(cmdv);
  618. perror(cmdv[0]);
  619. exit(1);
  620. default: // parent
  621. if (!hintergrund)
  622. waitpid(pid,&state,0);
  623. }
  624.  
  625.  
  626.  
  627. */
  628.  
  629. return NULL;
  630. }
  631.  
  632. void writesock(int sockfd, const char * fmt, ...)
  633. {
  634. //buffer erstellen
  635. char buffer[MAXLINELEN];
  636. //liste die übergeben wurde als "args definieren"
  637. va_list args;
  638. va_start(args, fmt);
  639. //alle args in den buffer schreiben
  640. vsprintf(buffer, fmt, args);
  641. va_end(args);
  642. //damit stdout nicht von anderen Threads überschrieben wird lock
  643. pthread_mutex_lock(&stdout_m);
  644. //stdout auf sockfd kopieren und dann flushen, sodass es am socket geschrieben wird.
  645. //dann wieder den originalen darauf legen
  646. dup2(sockfd, 1);
  647. printf("%s", buffer);
  648. fflush(stdout);
  649. dup2(original_stdout, 1);
  650. pthread_mutex_unlock(&stdout_m);
  651. }
  652.  
  653. int main(int argc, char *argv[])
  654. {
  655. //char comline[CMDLINESIZE ];
  656. //char *userInput;
  657.  
  658. int sock, clientsock;
  659. struct sockaddr clientaddr;
  660. struct sockaddr_in srvaddr;
  661. socklen_t addrlen = sizeof(srvaddr);
  662. pthread_t thrid;
  663.  
  664. //kopieren den fileDescriptor das stdouts
  665. original_stdout = dup(1);
  666.  
  667. int yes = 1;
  668.  
  669. //deklarationen für den Sever
  670. srvaddr.sin_family = AF_INET; //IPv4 Adresse
  671. srvaddr.sin_port = htons(8081);
  672. srvaddr.sin_addr.s_addr = INADDR_ANY; //auf allen adressen zuhöhren
  673.  
  674. //Mutexes initialisieren
  675. pthread_mutex_init(&stdout_m, NULL);
  676. pthread_mutex_init(&strtok_m, NULL);
  677. pthread_mutex_init(&logfile_m, NULL);
  678.  
  679. //Socket erstellung, Fehlerausgabe sollte nicht funktionieren
  680. if ( (sock=socket(AF_INET,SOCK_STREAM, 0)) == -1)
  681. perror("Socket Erzeugung");
  682.  
  683. //Falls die Adresse schon verwendet wird fehler.
  684. if ( setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) {
  685. perror("setsockopt");
  686. }
  687.  
  688. //Wenn wir unseren Socket nicht binden können --> Fehler.
  689. else if ( bind(sock, (struct sockaddr *)(&srvaddr), sizeof(struct sockaddr_in) ) == -1 )
  690. perror("Bind");
  691. else if ( listen(sock,5) == -1)
  692. perror("Server Aktivierung");
  693.  
  694. else
  695. {
  696. pthread_mutex_lock(&stdout_m);
  697. printf("Shell Server is running and listening on %s:%d\n", inet_ntoa(srvaddr.sin_addr), ntohs(srvaddr.sin_port));
  698. pthread_mutex_unlock(&stdout_m);
  699.  
  700. for(;;)
  701. { //Auf einen Client warten
  702. if ( ( clientsock = accept(sock,&clientaddr, &addrlen) ) == -1 )
  703. perror("Client Connect");
  704. else
  705. {
  706. //Wenn sich der Client verbunden hat, neuen Thread erstellen.
  707. pthread_mutex_lock(&stdout_m);
  708. printf("Client hat sich verbunden\n");
  709. pthread_mutex_unlock(&stdout_m);
  710. pthread_create(&thrid,NULL,shellthread,(void*)(clientsock));
  711. }
  712. }
  713. }
  714.  
  715.  
  716. /**for (;;)
  717. {
  718. printf("dsh >>>> ");
  719. //einlesen von Kommando vom stdin
  720. userInput=fgets(comline,CMDLINESIZE,stdin);
  721. // expandieren der comline ....
  722.  
  723. //schaun ob das erste zeichen ein "Return" ist und dann schleife von vorne anfangen.
  724. if(userInput && *comline && (strcmp(&comline[0], "\n") == 0)) {
  725. continue;
  726. }
  727.  
  728. else if(userInput && *comline)
  729. {
  730. comline[strlen(comline)-1]=0; // \n am Ende entfernen
  731. execute(comline);
  732. }
  733. }*/
  734.  
  735. return 0;
  736. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement