static int recv_message(void *buf, size_t size) { uint8_t *iter = buf; struct ste_message *msg = buf; do { DBG_VERBOSE("size %zu msg->len %u.", size, msg->len); if (size < sizeof(*msg)) { sap_error("invalid message received (%zu bytes)", size); return -EBADMSG; } /* Message must be complete. */ if (size < (msg->len + sizeof(msg->len))) { sap_error("incomplete message received (%zu bytes)", size); return -EBADMSG; } recv_response(msg); /* Reduce total buffer size by just handled frame size. */ size -= msg->len + sizeof(msg->len); /* Move msg pointer to the next message if any. */ iter += msg->len + sizeof(msg->len); msg = (struct ste_message *)iter; } while (size > 0); return 0; }
static void handle_query() { char from_station[32]; char to_station[32]; fputs("\n================== QUERY ========================\n\n", stdout); send_request(LIST_ALL, 0, NULL); response * rs1 = recv_response(); if (rs1->status == SUCCESS) { char *data = strtok(rs1->result, " "); fprintf(stdout, "\tfrom\t\tto\n"); int count =1; while(data != NULL) { fprintf(stdout, "\t%s", data); if(count %2 ==0) fputc('\n',stdout); count ++; data = strtok(NULL, " "); } fputc('\n',stdout); }else{ error_msg("query error",NULL); } fputs("\n********* ONLY THE STATIONS LISTED IS AVAILABLE**********\n\n", stdout); get_str("from: ", from_station); get_str("to: ", to_station); char * params[2]; params[0] = from_station; params[1] = to_station; send_request(QUERY, 2, params); response * rs2 = recv_response(); if (rs2->status == SUCCESS) { char *data = strtok(rs2->result, " "); fprintf(stdout, "\n\tid\tform\t\tto\t\tprice\tdays_left\n"); while(data != NULL) { fprintf(stdout, "\t%s", data); data = strtok(NULL, " "); } fputc('\n',stdout); }else{ error_msg("query error",NULL); } status = S_VERIFIED; }
static void process_download(struct server_attr *attr) { int fd; unsigned int length, n; char str[BUFSZ]; char filename[NAME_MAX + 1]; download_next: /* Format: | uint file-length | uint file-name-length | file-name */ length = *((int *)attr->data); n = *((int *)(attr->data + sizeof(length))); strncpy(filename, (attr->data + sizeof(length) * 2), n); *(filename + n) = 0; create_download_dir(filename); /* create directory if necessary */ /* Overwrite exist file */ fd = open(filename, O_WRONLY | O_CREAT, DEFAULT_DL_FILE_MODE); if (fd == -1) { err_msg(errno, "Error for open %s", filename); return; } SET_PROGRESS_DOWNLOAD(str, filename, length); n = 0; while (n < length) { recv_response(attr); writen(fd, attr->data, attr->resp.len); n += attr->resp.len; PRINT_PROGRESS(str, length, n); } printf("\n"); close(fd); /* Confirm does recived the RESP_DATA_FINISH flag */ recv_response(attr); if (attr->resp.code != RESP_DATA_FINISH) goto download_next; }
//Begin ClientCode int main(int argc, char ** argv) { #ifdef CSEC_VERIFY assume_string("clientID"); assume_string("serverID"); assume_string("port_ascii"); // Assumption that the corresponding argv fields indeed contains the correct ids: readenvL(argv[1], strlen(argv[1]), "clientID"); readenvL(argv[2], strlen(argv[2]), "serverID"); readenvL(argv[3], strlen(argv[3]), "port_ascii"); #endif RPCstate clState; clState.end = CLIENT; if (parseargs(argc,argv,&clState)) { #ifdef VERBOSE fprintf(stdout, "Usage: client clientAddress serverAddress [port] request\n"); #endif exit(-1); } #ifdef VERBOSE printf("Client: Now connecting to "); print_text_buffer(clState.other,clState.other_len); printf(", port %d.\n", clState.port); fflush(stdout); #endif // Getting arguments if (socket_connect(&(clState.conn_fd),(char*) clState.other, clState.other_len, clState.port)) return -1; clState.k_ab = get_shared_key(clState.self, clState.self_len, clState.other, clState.other_len, &(clState.k_ab_len)); clState.k = mk_session_key(&(clState.k_len)); clState.response = NULL; #ifdef CSEC_VERIFY event3("client_begin", clState.self, clState.self_len, clState.other, clState.other_len, clState.request, clState.request_len); #endif /* Send request */ if (send_request(&clState) < 0) return -1; /* Receive response */ if (recv_response(&clState) < 0) return -1; #ifdef CSEC_VERIFY event4("client_accept", clState.self, clState.self_len, clState.other, clState.other_len, clState.request, clState.request_len, clState.response, clState.response_len); #endif return 0; }
static void process_ls(struct server_attr *attr) { char *p, *p2; do { p = attr->data; while ((p2 = strchr(p, ':')) != NULL) { *p2++ = 0; printf("%s\n", p); p = p2; } recv_response(attr); } while(attr->resp.code != RESP_DATA_FINISH); }
/* * Wait for request response */ static int serial_term_wait_response(struct ipmi_intf * intf, struct serial_term_request_ctx * req_ctx, uint8_t * msg, size_t max_len) { struct serial_term_hdr * hdr = (struct serial_term_hdr *) msg; int msg_len; /* wait for response(s) */ do { /* receive message */ msg_len = recv_response(intf, msg, max_len); /* check if valid message received */ if (msg_len > 0) { /* validate message size */ if (msg_len < 4) { /* either bad response or non-related message */ continue; } /* check for the waited response */ if (hdr->netFn == (req_ctx->netFn|4) && (hdr->seq & ~3) == req_ctx->seq && hdr->cmd == req_ctx->cmd) { /* check if something new has been parsed */ if (verbose > 3) { fprintf(stderr, "Got response:\n"); fprintf(stderr, " NetFN/rsLUN = 0x%x\n", msg[0]); fprintf(stderr, " rqSeq/Bridge = 0x%x\n", msg[1]); fprintf(stderr, " cmd = 0x%x\n", msg[2]); fprintf(stderr, " completion code = 0x%x\n", msg[3]); if (msg_len > 8) { fprintf(stderr, " data_len = %d\n", msg_len - 4); fprintf(stderr, " data = %s\n", buf2str(msg + 4, msg_len - 4)); } } /* move to start from completion code */ memmove(msg, hdr + 1, msg_len - sizeof (*hdr)); /* the waited one */ return msg_len - sizeof (*hdr); } } } while (msg_len > 0); return 0; }
int main(int argc, char* argv[]) { const int BUF_SIZE = 50; char url[BUF_SIZE]; char filepath[BUF_SIZE]; char filename[BUF_SIZE]; int sock; struct addrinfo* addrinf; //* GET url, filepath, AND filename if( argc>1) get_url_file( argv[1], url, filepath, filename, BUF_SIZE); else finish("Too few arguments\n", 1); if( !strlen( filename)) strcpy( filename, "index.html"); printf( "url: [%s]\nfilepath: [%s]\nfilename: [%s]\n\n", url, filepath, filename); //*/ //* GET REMOTE addrinf addrinf = getaddr( url, "80", AF_UNSPEC); if( !addrinf) finish( "getaddrinfo() returned no results\n", 1); //*/ //* CREATE AND CONNECT THE SOCKET sock = socket( addrinf->ai_family, SOCK_STREAM, 0); if( sock == -1) { printf( "couldn't create the socket, errno(%d): %s\n", errno, strerror( errno)); exit(1); } if( connect( sock, addrinf->ai_addr, addrinf->ai_addrlen) != 0) { printf( "couldn't connect, errno(%d): %s\n", errno, strerror( errno)); exit(1); }//*/ //* DO IT send_request( sock, url, filepath); recv_response( sock, filename); //*/ //* CLEAN UP AND EXIT assert( shutdown( sock, SHUT_RDWR)==0); //*/ return 0; }
static void handle_order() { fputs("\n================== ORDER =========================\n\n", stdout); char ticket_id[8]; get_str("ticket_id: ", ticket_id); char * params[1]; params[0] = ticket_id; send_request(ORDER, 1, params); response * rs = recv_response(); if (rs->status == SUCCESS) { fputs("\n############# ORDER SUCCESS ##############\n", stdout); } else { error_msg("order error",NULL); } status = S_VERIFIED; }
static int authenticate_with_cookie(IPC_Channel *chan, cl_uuid_t *cookie) { struct ha_msg * request; struct ha_msg * reply; assert(chan != NULL); assert(cookie != NULL); if (!(request = create_basic_reqmsg_fields(ST_SIGNON))) { return ST_FAIL; } if (ha_msg_adduuid(request, F_STONITHD_COOKIE, cookie) != HA_OK) { stdlib_log(LOG_ERR, "cannot add field to ha_msg."); ZAPMSG(request); return ST_FAIL; } /* Send request/read response */ if (send_request(chan, request, DEFAULT_TIMEOUT) != ST_OK) { ZAPMSG(request); return ST_FAIL; } ZAPMSG(request); if (!(reply = recv_response(chan, DEFAULT_TIMEOUT))) { return ST_FAIL; } /* Are we signed on this time? */ if ( TRUE == is_expected_msg(reply, F_STONITHD_TYPE, ST_APIRPL, F_STONITHD_APIRPL, ST_RSIGNON, TRUE) ) { if ( !STRNCMP_CONST( cl_get_string(reply,F_STONITHD_APIRET), ST_APIOK) ) { ZAPMSG(reply); return ST_OK; } } ZAPMSG(reply); return ST_FAIL; }
static void handle_query_orders() { fputs("\n================== QUERY ORDERS====================\n\n", stdout); send_request(QUERY_ORDER, 0, NULL); response * rs = recv_response(); if (rs->status == SUCCESS) { char *data = strtok(rs->result, " "); fprintf(stdout, "\tid\tfrom\t\tto\t\tprice\tusername\n"); int count = 1; while(data != NULL) { fprintf(stdout, "\t%s", data); if(count % 5 == 0) fputc('\n',stdout); count ++; data = strtok(NULL, " "); } fputc('\n',stdout); }else{ error_msg("query error",NULL); } status = S_VERIFIED; }
char AD_to_server (int sockfd,char* tIP, short unsigned int port) { //connecting active data socket to server char temp1[4]={0}; char * ptr=strstr(tIP, "."); strncpy(temp1,tIP, ptr-tIP); strcpy(tIP,ptr+1); char temp2[4]={0}; ptr=strstr(tIP, "."); strncpy(temp2,tIP, ptr-tIP); strcpy(tIP,ptr+1); char temp3[4]={0}; ptr=strstr(tIP, "."); strncpy(temp3,tIP, ptr-tIP); strcpy(tIP,ptr+1); char temp4[4]={0}; strcpy(temp4,tIP); short unsigned int first_half=0x00FF; short unsigned int second_half=0xFF00; second_half=(second_half & port)>>8; first_half=first_half & port; char port_message[64]={0}; sprintf(port_message,"%s, %s, %s, %s, %d, %d",temp1,temp2,temp3,temp4,second_half,first_half); char std_message[36]={0}; char status=create_command ("PORT", port_message, std_message); status= send(sockfd , std_message , strlen(std_message), 0); //send_command(sockfd,std_message,strlen(std_message)); if (status!=-1) { status=recv_response(sockfd); } return status; }
static void handle_register() { char username[32]; char passwd[32]; char passwd_again[32]; int try = 3; fputs("\n================== REGISTER ==========================\n\n", stdout); while(try > 0){ fputs("Please enter your username and password. \n", stdout); get_str("username: "******"password: "******"password again: ", passwd_again); if (strcmp(passwd,passwd_again) == 0) break; else{ try--; fputs("Opps, not the same password.\n", stdout); } } if(try == 0){ fputs("redirect to init\n", stdout); status = S_INIT; return; } char * params[2]; params[0] = username; params[1] = passwd; send_request(REGISTER, 2, params); response * rs = recv_response(); if (rs->status == SUCCESS) { status = S_LOGIN; } else { fputs("Can't register. perhaps: already exists", stdout); status = S_INIT; } } static void handle_login() { char username[32]; char passwd[32]; fputs("\n================== LOG IN =======================\n\n", stdout); fputs("Please enter your username and password.\n", stdout); get_str("username: "******"password: "******"Can't LOGIN. perhaps: username or password wrong", stdout); status = S_INIT; } }