int main(int argc, char* argv[]) { char* vip = NULL; //IP-Adresse des Servers, Variable muss später freigegeben werden int vport = -1; //Portnummer // int error = 0; //nur temorär verwendet zum debugging memset(username,'\0',sizeof(username)); /* Start der GETOPT behandlung */ { int c; int counter = 0; int sl = 0; if(argc <3) print_usage(); while( (c = getopt(argc, argv, "i:p:")) != EOF ) { switch (c) { case 'p': if(vport!=-1) { print_usage(); break; } vport = strtol(optarg,(char**)NULL,10); counter++; break; case 'i': if(vip!=NULL) { print_usage(); break; } sl = strlen(optarg); vip = (char*) malloc(sizeof(char)* (sl+1)); strcpy(vip, optarg); counter++; break; case '?': print_usage(); break; } } // printf("\nIP: %s, Port: %d\n",vip, vport); // printf("\nCounter: %d; optind: %d; argc: %d\n", counter, optind, argc); if(counter!= 2 || optind!=argc || vport < 1) print_usage(); //printf("\n FERTIG\n "); /* ENDE GETOPT */ } /* Kommunikation mit server */ { int create_socket; char buffer[BUF]; struct sockaddr_in address; char* command; char* file; memset(buffer,'\0',sizeof(buffer)); /* Socket erstellen */ { if ((create_socket = socket (AF_INET, SOCK_STREAM, 0)) == -1) { perror("Socket error"); return EXIT_FAILURE; } memset(&address,0,sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons (vport); inet_aton(vip, &address.sin_addr); free(vip); } /* connect */ { if (connect ( create_socket, (struct sockaddr *) &address, sizeof (address)) == 0) { clrscr(); do { if(read(create_socket,buffer,BUF)==-1) perror("Error reading stuff"); if(!strcmp(buffer,"fail")) { perror("Server failed, shutting down client..."); close (create_socket); return EXIT_FAILURE; } if(!strncasecmp(buffer,"block",5)) { strtok(buffer, "|"); char* printmsg = strtok(NULL, "|"); printf("\n%s\nShutting down client..\n",printmsg); close(create_socket); return EXIT_FAILURE; } } while(strcmp(buffer,"win")); printf ("Connection with server (%s) established\nPlease log in first with comand: \"LOGIN\"\n", inet_ntoa (address.sin_addr)); } else { perror("Connect error - no server available"); return EXIT_FAILURE; } } do { login = 0; printf ("Send command: "); s_gets(buffer, BUF); /* String trennen */ { char buf2[BUF]; strcpy(buf2,buffer); command = strtok(buf2, " "); file = strtok(NULL, "\n"); // printf("Command: %s\nFile: %s\nbuf2: %s\n\n",command,file,buf2); } // printf("Command = %s\n", command); // if(file!=NULL) //printf("File = %s\n", file); /* Befehl ausführen */ { if(!strcasecmp(command, "LIST")) { if(send(create_socket, command, strlen (command), 0)==-1) perror("Error sending stuff"); do { if(read(create_socket,buffer,BUF)==-1) perror("Error reading stuff"); if(!strcmp(buffer,"log")) { // printf("Server refused, login first\n"); login=1; break; } } while(strcmp(buffer,"server ready")); if(!login) { printf("List wird ausgeführt\n\nListe:\n"); memset(buffer,'\0',sizeof(buffer)); { strcpy(buffer,"start"); if(write(create_socket,buffer,BUF)==-1) perror("Error writing stuff"); memset(buffer,'\0',sizeof(buffer)); } do { if(read(create_socket,buffer, BUF)==-1) perror("Error writing stuff"); //buffer[BUF-1] = '\0'; printf("%s",buffer); /* if(!strcmp(buffer,"list")) { perror("\”Server error, shut down client\n"); return EXIT_FAILURE; }*/ } while (strlen(buffer)!=0); printf("\n"); } else { printf("Error in LIST, you must login first, send command: \"LOGIN\"\n\n"); } } else if(!strcasecmp(command, "GET")) { if(strstr(file, "..")) printf("Wrong filename, filename must not contain \"..\"\n"); else c_get(create_socket, file); } else if(!strcasecmp(command, "PUT")) { c_put(create_socket, file); } else if(!strcasecmp(command, "LOGIN")) { //printf("Put wird ausgeführt\n"); if(send(create_socket, command, strlen (command), 0)==-1) perror("Error sending stuff"); do { if(read(create_socket,buffer,BUF)==-1) perror("Error reading stuff"); if(!strcmp(buffer,"log")) { login = 1; break; } } while(strcmp(buffer,"server ready")); if(!login) { clrscr(); client_auth(create_socket); } else { printf("Login refused\nAlready logged in as: %s\n\n",username); } } else if(!strcasecmp(command, "QUIT")) { printf("Client wird beendet\n"); } else { printf("Unknown command: %s\n\n", command); //error = 1; } } } while (strcasecmp (command, "QUIT") != 0); close (create_socket); } /* Ende Verbindung */ return 0; }
void print(char *str) { char *ch = str; while ( *ch!= '\0') c_put(USART2, (*ch++)); }
void docmd(void) { int c; struct ww *w; char out = 0; while (!out && !quit) { if ((c = wwgetc()) < 0) { if (terse) wwsetcursor(0, 0); else { wwputs("Command: ", cmdwin); wwcurtowin(cmdwin); } do wwiomux(); while ((c = wwgetc()) < 0); } if (!terse) wwputc('\n', cmdwin); switch (c) { default: if (c != escapec) break; case 'h': case 'j': case 'k': case 'l': case 'y': case 'p': case ctrl('y'): case ctrl('e'): case ctrl('u'): case ctrl('d'): case ctrl('b'): case ctrl('f'): case ctrl('s'): case ctrl('q'): case ctrl('['): if (selwin == 0) { error("No window."); continue; } } switch (c) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': if ((w = window[c - '1']) == 0) { error("%c: No such window.", c); break; } setselwin(w); if (checkproc(selwin) >= 0) out = 1; break; case '%': if ((w = getwin()) != 0) setselwin(w); break; case ctrl('^'): if (lastselwin != 0) { setselwin(lastselwin); if (checkproc(selwin) >= 0) out = 1; } else error("No previous window."); break; case 'c': if ((w = getwin()) != 0) closewin(w); break; case 'w': c_window(); break; case 'm': if ((w = getwin()) != 0) c_move(w); break; case 'M': if ((w = getwin()) != 0) movewin(w, w->ww_alt.t, w->ww_alt.l); break; case 's': if ((w = getwin()) != 0) c_size(w); break; case 'S': if ((w = getwin()) != 0) sizewin(w, w->ww_alt.nr, w->ww_alt.nc); break; case 'y': c_yank(); break; case 'p': c_put(); break; case ':': c_colon(); break; case 'h': (void) wwwrite(selwin, "\b", 1); break; case 'j': (void) wwwrite(selwin, "\n", 1); break; case 'k': (void) wwwrite(selwin, "\033A", 2); break; case 'l': (void) wwwrite(selwin, "\033C", 2); break; case ctrl('e'): wwscroll(selwin, 1); break; case ctrl('y'): wwscroll(selwin, -1); break; case ctrl('d'): wwscroll(selwin, selwin->ww_w.nr / 2); break; case ctrl('u'): wwscroll(selwin, - selwin->ww_w.nr / 2); break; case ctrl('f'): wwscroll(selwin, selwin->ww_w.nr); break; case ctrl('b'): wwscroll(selwin, - selwin->ww_w.nr); break; case ctrl('s'): stopwin(selwin); break; case ctrl('q'): startwin(selwin); break; case ctrl('l'): wwredraw(); break; case '?': c_help(); break; case ctrl('['): if (checkproc(selwin) >= 0) out = 1; break; case ctrl('z'): wwsuspend(); break; case 'q': c_quit(); break; /* debugging stuff */ case '&': if (debug) { c_debug(); break; } default: if (c == escapec) { if (checkproc(selwin) >= 0) { (void) write(selwin->ww_pty, &escapec, 1); out = 1; } } else { if (!terse) wwbell(); error("Type ? for help."); } } } if (!quit) setcmd(0); }
/**F*****************************************************************/ int dispatch (spp_ctx *c) /** * PURPOSE : receives commands from remote server * * RETURN : 1 to terminate else 0 * * NOTES : * *F*/ { DWORD e, terminate=0, end=0; spp_blk in; DWORD sck_evt; HANDLE evt[MAXIMUM_WAIT_OBJECTS]; DWORD evt_cnt=0; evt[sck_evt = evt_cnt++] = WSACreateEvent(); do { // wait for event e=wait_evt(evt, evt_cnt, sck_evt, c->s); // failure? exit if (e == -1) { printf ("[ wait_evt() failure : returned %08X\n", e); return 0; } // receive packet if (spp_recv(c, &in) != SPP_ERR_OK) { break; } // inspect packet switch (in.cmd.opt) { // terminate client case SPP_CMD_TERM : { DEBUG_PRINT("received command to terminate"); terminate=1; break; } // close the connection case SPP_CMD_CLOSE : { DEBUG_PRINT("received command to close connection"); end=1; break; } // execute cmd.exe for remote server case SPP_CMD_SHELL : { DEBUG_PRINT("received command to execute cmd.exe"); cmd(c); break; } // send a file to remote server // buf should contain the name of file to open case SPP_CMD_GET : { DEBUG_PRINT("received command to send file"); c_get(c, in.cmd.buf); break; } // receive a file from remote server // buf should contain the name of file to create case SPP_CMD_PUT : { DEBUG_PRINT("received command to receive file"); c_put(c, in.cmd.buf); break; } default: DEBUG_PRINT("unknown command received %08X", in.cmd.opt); break; } // continue until close or terminate } while (!end && !terminate); return terminate; }