static void tcp_received(struct net_app_ctx *ctx, struct net_pkt *pkt, int status, void *user_data) { struct data *data = ctx->user_data; ARG_UNUSED(user_data); ARG_UNUSED(status); if (!pkt || net_pkt_appdatalen(pkt) == 0) { if (pkt) { net_pkt_unref(pkt); } return; } NET_DBG("Sent %d bytes, received %u bytes", data->expecting_tcp, net_pkt_appdatalen(pkt)); if (!compare_tcp_data(pkt, data->expecting_tcp, data->received_tcp)) { NET_DBG("Data mismatch"); } else { data->received_tcp += net_pkt_appdatalen(pkt); } if (data->expecting_tcp <= data->received_tcp) { /* Send more data */ send_tcp_data(ctx, data); } net_pkt_unref(pkt); }
static void tcp_connected(struct net_app_ctx *ctx, int status, void *user_data) { if (status < 0) { return; } connected_count++; if (IS_ENABLED(CONFIG_NET_UDP)) { if (IS_ENABLED(CONFIG_NET_IPV6) && IS_ENABLED(CONFIG_NET_IPV4)) { if (connected_count > 1) { k_sem_give(&tcp_ready); } } else { k_sem_give(&tcp_ready); } } send_tcp_data(ctx, user_data); }
/* * Authors: Weiyan Lin * * Purpose: It receives data from the Authentication Center * then call handle_buy() * function to handle the buy packet. * * Expected input: It takes file descriptor data from accept(), * machine info as input * * Implementation details: * It receives the first byte of the first byte from the * Authentication Center, if the packet is * action_packet, we woulc check the code of this packet. * * CODE == BUYCODE => send pricecheck_packet with price to * the Authentication Center and wait the buyack_packet * * CODE == PRICE_CODE or STATUS CODE => * translate action_packet to ser_tran and finish the handle_client function * * if the the packet is buyack_packet * we translate the buyack_packet to a ser_tran packet and then finish * handle_client part * */ void handle_client(int client,machine* p_machine,struct ser_tran *torecv) { // set two struct to receive the packets from the Authentication Center char buffer[30]; struct action_packet act_buffer; struct buyack_packet buychk_buffer; // associate *rx with Authentication Center FILE *rx = fdopen(client, "r"); // receive the first byte of the first packet(password packet) size_t bufs_received= fread(buffer, 1, 1, rx); // if receive failed, exit with an error if(bufs_received <0) { fclose(rx); vending_exit_with_error("recv failed",p_machine); } // check the first CODE of the packet else if (buffer[0]==1) { //it deal with the buy action //got the last data from the rx fread(buffer+1,sizeof(struct action_packet)-1,1,rx); memcpy(&act_buffer,buffer, sizeof(struct action_packet)); struct pricecheck_packet prichk; prichk.code=PRICECHECK_CODE; prichk.price=ntohl(p_machine->item[ntohl(act_buffer.item_index)-1].price); prichk.seq_num=act_buffer.seq_num; //send price to the Authentication Center send_tcp_data(client,&prichk,sizeof(struct pricecheck_packet)); //get the buyack from the Authentication Center fread(buffer,sizeof(struct buyack_packet),1,rx); memcpy(&buychk_buffer,buffer, sizeof(struct buyack_packet)); torecv->code=buychk_buffer.code; torecv->seq_num=buychk_buffer.seq_num; torecv->item_index=ntohl(buychk_buffer.index); //if status is success, we would put enough money to //the ser_tran packet, so our buying process would be success if ( buychk_buffer.status_code==1) { torecv->money_available=p_machine->item[ntohl(buychk_buffer.index)-1].price; } //if status is wrong , we would put 0 money to the ser_tran packet //so the status would be fail and the user would get the price of //the item that they want else { torecv->money_available=0; } } else { //if CODE is PRICE_CODE or STATUS_CODE //we would copy the CODE to ser_tran //and set the money_available is 0. fread(buffer+1,sizeof(struct action_packet)-1,1,rx); memcpy(&act_buffer,buffer, sizeof(struct action_packet)); torecv->code=act_buffer.code; torecv->seq_num=act_buffer.seq_num; torecv->item_index=ntohl(act_buffer.item_index); torecv->money_available=0; } //close the connection fclose(rx); }
void EtherShield::ES_send_tcp_data(uint8_t *buf,uint16_t dlen ) { send_tcp_data(buf, dlen); }
/* * Authors: Weiyan Lin * * Purpose: It handle the ser_tran , which we translate it from the * Authentication Center message , and use cmd_buy to do the buying * process and use send_tcp_data to send result to the user * * Expected input: It takes the client of the user , information of the machine , * and the ser_tran packet as input * * our receive_client would translate the message from * Authentication Center to the ser_tran packet, which can be * handle easily by handle_buy and cmd_buy * * Implementation details: * * It would show the packet we received on the server, * then use the index information from the ser_tran packet * to find the target item * * If the item index is in our machine, we would continue buying process * else we would return a specific respond packet to notice Certification * Center * * Continuing our buying process, we would check the seq_num from buy packet, * If this seq_num exists in our machine, we would send respond packet from the memory * directly to the user. * Else we would continue our buying process with cmd_buy(). * * Finially, we got a handled respond packet. Then we would use send_tcp_data function * to send our respond packet */ void handle_buy(struct ser_tran *torecv, int sock, machine *p_machine) { // show what we got from the Authentication Center printf("code: %u\n", torecv->code); printf("index: A%u\n", torecv->item_index); uint32_t send_length; uint8_t i; //get the seq_num uint32_t seq_num = torecv->seq_num; uint32_t name_length; uint32_t index=p_machine->pg_index; struct respond_packet *tosend = NULL; //check if the item_index exists if(torecv->item_index <= p_machine->item_num && torecv->item_index >0 ) { //check if the seq_num exists if (sock > 0) { for (i=0; i <99; i++) { if ( p_machine->ptr_bg[i] != NULL) { //if seq_num exists, get the address from pointer array //then send the data directly if (p_machine->ptr_bg[i]->seq_num == seq_num) { name_length=p_machine->ptr_bg[i]->name_length; send_length = sizeof(struct respond_packet)+name_length-1; send_tcp_data(sock, p_machine->ptr_bg[i],send_length); return; } } } } //make sure the length of our respond because item's name is different name_length = strlen(p_machine->item[torecv->item_index-1].label); send_length = sizeof(struct respond_packet)+name_length-1; //malloc for respond packet tosend = malloc(send_length); cmd_buy(p_machine,torecv,tosend); //input our respond packet in to a pointer array // so we could check the seq_num and know whether we go the seq_num before p_machine->ptr_bg[index] = tosend; p_machine->pg_index+=1; if(p_machine->pg_index > 99) p_machine->pg_index=0; } else { // if we got something wrong about the title // we would make a specific respond packet, which name is '\0' and // status is 2 to the user send_length = sizeof(struct respond_packet); tosend = malloc(send_length); tosend->seq_num = torecv->seq_num; if (torecv->code == BUYACK_CODE) { tosend->code = BUYRES_CODE; } else { tosend->code = torecv->code+3; } tosend->price = 0; tosend->name_length = 0; tosend->status_code = 2; tosend->name = '\0'; } send_tcp_data(sock, tosend, send_length); if(sock == 0 ) { free(tosend); } }