static void client() { int drmfd, ret; unsigned int time; wait_event(0, SERVER_READY); /* XXX: Should make sure we open the same DRM as the master */ drmfd = drm_open_any(); client_auth(drmfd); /* Wait for the server to grab the lock, then grab it ourselves (to * contest it). Hopefully we hit it within the window of when the * server locks. */ wait_event(0, SERVER_LOCKED); ret = drmGetLock(drmfd, lock2, 0); time = get_millis(); if (ret != 0) err(1, "Failed to get lock on client\n"); drmUnlock(drmfd, lock2); /* Tell the server that our locking completed, and when it did */ send_event(0, CLIENT_LOCKED); ret = write(commfd[0], &time, sizeof(time)); close(drmfd); exit(0); }
/** * @brief Displays shell and parses input from user. * * Only allows integer input. * Call a function based on user selection (1-6). * If connected to a server, it disconnects before exiting. * @param r A pointer to a record structure. * @return Returns 0 on success, -1 otherwise. */ int command_parser() { printf("\n\n------------------------------------------\n" "\t1) Connect\n" "\t2) Authenticate\n" "\t3) Get\n" "\t4) Set\n" "\t5) Query\n" "\t6) Disconnect\n" "\t7) Exit\n" "------------------------------------------\n\n\n"); int option, status; struct storage_record r; bool read_success = false; while(read_success == false) { printf("Please enter your selection: "); char *l = fgets(input_buffer, sizeof input_buffer, stdin); if(l != input_buffer || (sscanf(input_buffer, "%d %s", &option, trash) != 1) || option > 7 || option < 1) printf("Invalid selection. Please enter a valid option number (1-6).\n"); else read_success = true; } switch (option) { case 1: // Connect to server status = client_connect(); break; case 2: // Authenticate the client. status = client_auth(); break; case 3: // Issue storage_get client_get(&r); n_gets++; break; case 4: // Issue storage_set client_set(&r); n_sets++; break; case 5: //Issue client_query client_query(); break; case 6: // Disconnect from server status = client_disconnect(); break; case 7: //Exit if (connected) status = client_disconnect(); status = -1; // To stop main loop printf("Goodbye!\n"); break; default: status = -1; printf("Invalid selection.\n"); break; } return status; }
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; }