/** * This functions handles an incoming ECSS packet. * If the ECSS packet is part of a large data transfer consisting from * several sequential ECSS packets, it handles them automatically. * * In other words, there is no need to explicitly check for a fragmented data * transfer. * * @param payload the received payload * @param payload_size the size of the payload * @return SATR_OK if all went ok or appropriate error code */ SAT_returnState rx_ecss (uint8_t *payload, const uint16_t payload_size) { SAT_returnState ret; tc_tm_pkt *pkt; pkt = get_pkt (payload_size); if (!C_ASSERT(pkt != NULL)) { return SATR_ERROR; } if (unpack_pkt (payload, pkt, payload_size) == SATR_OK) { ret = route_pkt (pkt); } TC_TM_app_id dest = 0; if(pkt->type == TC) { dest = pkt->app_id; } else if(pkt->type == TM) { dest = pkt->dest_id; } if(dest == SYSTEM_APP_ID) { free_pkt(pkt); } return ret; }
SAT_returnState hk_crt_empty_pkt_TM(tc_tm_pkt **pkt, const TC_TM_app_id app_id, const HK_struct_id sid) { *pkt = get_pkt(PKT_NORMAL); if(!C_ASSERT(*pkt != NULL) == true) { return SATR_ERROR; } hk_crt_pkt_TM(*pkt, app_id, sid); return SATR_OK; }
bool write_pkt(buf_ptr &buf_in) { buf_ptr buf_out = super::buffer(); put_pkt(buf_in); do { get_pkt(buf_out); super::write_pkt(buf_out); buf_out->reset(); } while (super::decrease_budget()); return true; }
bool read_pkt(buf_ptr &buf_out) { buf_ptr buf_in = super::buffer(); if (!is_done() && is_partial_complete()) { get_pkt(buf_out); return true; } else if (is_done()) { increment(); return false; } while (true) { super::rlnc_hdr_reserve(buf_in); if (!super::read_pkt(buf_in)) break; if (!validate_type(buf_in)) continue; put_pkt(buf_in); process_rank(); buf_in->reset(); if (is_complete()) { send_ack(super::rlnc_hdr_block(), base::m_coder->rank()); break; } } if (!is_partial_complete()) return false; get_pkt(buf_out); return true; }
void timer() { buf_ptr buf; super::timer(); if (base::m_coder->symbols_initialized() == 0) return; if (m_decoder_rank == base::m_coder->rank()) return; if (m_stopped) return; super::increase_budget(); buf = super::buffer(); do { get_pkt(buf); super::write_pkt(buf); buf->reset(); } while (super::decrease_budget()); }
void tcp_send_recv(int server_socket) { int max_sock; fd_set all_sockets; fd_set read_sockets; printf("$: "); fflush(stdout); FD_ZERO(&all_sockets); FD_ZERO(&read_sockets); FD_SET(server_socket, &all_sockets); FD_SET(0, &all_sockets); max_sock = server_socket; while(1) { read_sockets = all_sockets; if (select(server_socket + 1, &read_sockets, NULL, NULL, NULL) < 0) { perror("select"); exit(1); } if (FD_ISSET(server_socket, &read_sockets)) { if (get_pkt(server_socket) != 0) { FD_CLR(server_socket, &all_sockets); exit(0); } else { printf("$: "); fflush(stdout); } } if (FD_ISSET(0, &read_sockets)) { tcp_send_message(server_socket); } } }
int server::join_game(out_socket *os, char *name, char *server_name) { char *re="Error occurred while reading from server\n"; packet pk; if (!get_pkt(os,pk)) // read join status packet, 0 means we can't join { fputs(re,stderr); exit(0); } int32_t nfs_port; if (pk.read((uint8_t *)&nfs_port,4)!=4) { fputs(re,stderr); exit(0); } // connect_to_nfs_server(server_name,lltl(nfs_port)); pk.write((uint8_t *)name,strlen(name)+1); // send or name and see if it's ok to join in if (!send_pkt(os,pk)) { printf("Unable to write to server\n"); exit(0); } if (!get_pkt(os,pk)) // read join status packet, 0 means we can't join { fputs(re,stderr); exit(0); } uint8_t stat; if (pk.read((uint8_t *)&stat,1)!=1) { fputs(re,stderr); exit(0); } if (stat==0) { printf("Sorry, this server is refusing you (%s)\n",name); exit(0); } if (current_level) delete current_level; int32_t vs[4]={ lltl(320/2-155),lltl(200/2-95),lltl(320/2+155),lltl(200/2+70)}; pk.write((uint8_t *)vs,4*4); if (!send_pkt(os,pk)) { printf("Unable to write to server\n"); exit(0); } current_level=new level(os); if (current_level->load_failed()) { printf("Error occurred while downloading level\n"); exit(1); } if (!get_pkt(os,pk)) { printf("Unable to read views from server\n"); exit(0); } uint16_t tv; if (pk.read((uint8_t *)&tv,2)!=2) { fputs(re,stderr); exit(0); } tv=lstl(tv); view *last=NULL; for (int i=0; i<tv; i++) { if (!get_pkt(os,pk)) { fputs(re,stderr); exit(0); } view *v=add_view(pk); if (v) { printf("added view %d\n",v->player_number); if (last) last->next=v; else player_list=v; last=v; } else printf("no view created, why?\n"); } if (!get_pkt(os,pk)) { fputs(re,stderr); exit(0); } if (pk.read((uint8_t *)&rand_on,2)!=2) // read the current random seed used by the server. { fputs(re,stderr); exit(0); } rand_on=lstl(rand_on); uint16_t rtab[1024]; if (!pk.read((uint8_t *)rtab,1024*2)) { fputs(re,stderr); exit(0); } // read the rand table for (int j=0; j<1024*2; j++) if (((uint8_t *)rtab)[j]!=((uint8_t *)rtable)[j]) { printf("rtables differ on byte %d\n",j); exit(0); } if (last) { last->Drawable=1; last->connect=os; } start_running=1; is_server=0; return 1; }
void server::check_for_new_players() { if (is_server && has_net) { out_socket *nd=in->check_for_connect(); if (nd) { packet pk; // pk.write_uint32(file_server->get_port()); if (!send_pkt(nd,pk)) { printf("error writing to connection\n"); return ; } // while (!file_server->service_request()) milli_wait(1000); if (!get_pkt(nd,pk)) { printf("error reading from connection\n"); return ; } else { char name[100]; pk.get_string(name,100); printf("Joined by player %s\n",name); pk.reset(); uint8_t ok=1; pk.write((uint8_t *)&ok,1); // write ok to join send_pkt(nd,pk); /**************** Read suggested view size from client ****/ if (!get_pkt(nd,pk)) { printf("error reading view info from connection\n"); return ; } int32_t cx1,cy1,cx2,cy2; if (pk.read((uint8_t *)&cx1,4)!=4) return ; cx1=lltl(cx1); if (pk.read((uint8_t *)&cy1,4)!=4) return ; cy1=lltl(cy1); if (pk.read((uint8_t *)&cx2,4)!=4) return ; cx2=lltl(cx2); if (pk.read((uint8_t *)&cy2,4)!=4) return ; cy2=lltl(cy2); /**************** Create the player *******************/ for (view *f=player_list; f && f->next; f=f->next); // find last player, add one for pn int i,st=0; for (i=0; i<total_objects; i++) if (!strcmp(object_names[i],"START")) st=i; game_object *o=create(current_start_type,0,0); game_object *start=current_level->get_random_start(320,NULL); if (start) { o->x=start->x; o->y=start->y; } else { o->x=100; o->y=100; } f->next=new view(o,NULL,f->player_number+1); o->set_controller(f->next); current_level->add_object(o); view *v=f->next; v->cx1=cx1; v->cy1=cy1; v->cx2=cx2; v->cy2=cy2; v->connect=nd; strcpy(v->name,name); if (current_level->send(nd)) { uint8_t cmd=SCMD_ADD_VIEW; next_out.write((uint8_t *)&cmd,1); v->write_packet(next_out); /********** Send all of the views to the player **********/ pk.reset(); uint16_t tv=0; for (f=player_list; f; f=f->next) tv++; tv=lstl(tv); pk.write((uint8_t *)&tv,2); if (!send_pkt(nd,pk)) return ; for (f=player_list; f; f=f->next) { pk.reset(); f->write_packet(pk); if (!send_pkt(nd,pk)) return ; } pk.reset(); uint16_t r=lstl(rand_on); pk.write((uint8_t *)&r,2); // write current random seed pk.write((uint8_t *)rtable,1024*2); send_pkt(nd,pk); } } } } }
void server::collect_inputs() { out_socket *collect_server=NULL; for (view *f=player_list; f; ) { view *next=f->next; if (is_server) { if (f->connect) { packet pk; if (get_pkt(f->connect,pk)) { while (!pk.eop()) { uint8_t cmd; if (pk.read((uint8_t *)&cmd,1)==1) if (!process_command(f,cmd,pk)) { remove_player(f); f=NULL; } } } else { remove_player(f); f=NULL; } } else { f->get_input(); add_change_log(f,next_out,1); } } else { if (f->local_player()) { f->get_input(); if (f->connect && !send_inputs(f)) remove_from_server(f); else if (f->connect) collect_server=f->connect; // take note that we should collect the input back from the server } } f=next; } if (collect_server) { packet pk; if (!get_pkt(collect_server,pk)) { for (view *f=player_list; f; f=f->next) if (f->local_player()) remove_from_server(f); } if (!client_do_packet(pk)) printf("Error occurred while processing packet from server\n"); } if (is_server && in) distribute_changes(); }
int tcp_send_setup(char *host_name, char *port) { int socket_num; struct sockaddr_in remote; // socket address for remote side struct hostent *hp; // address of remote host char *pktTail; char *fullPkt; int fullPktLen; int sent = 0; // create the socket if ((socket_num = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket call"); exit(-1); } // designate the addressing family remote.sin_family= AF_INET; // get the address of the remote host and store if ((hp = gethostbyname(host_name)) == NULL) { printf("Error getting hostname: %s\n", host_name); exit(-1); } memcpy((char*)&remote.sin_addr, (char*)hp->h_addr, hp->h_length); // get the port used on the remote side and store remote.sin_port= htons(atoi(port)); if(connect(socket_num, (struct sockaddr*)&remote, sizeof(struct sockaddr_in)) < 0) { perror("connect call"); exit(-1); } //printf("len of handle %d\n", (int)strlen(handle)); fullPktLen = strlen(handle) + 1; pktTail = malloc(fullPktLen); pktTail[0] = (uint8_t)(fullPktLen - 1); memcpy(pktTail + 1, handle, strlen(handle)); fullPkt = create_full_packet((int8_t)1, pktTail, fullPktLen); fullPktLen = fullPktLen + NRML_HDR_LEN; free(pktTail); sent = send(socket_num, fullPkt, fullPktLen, 0); if(sent < 0) { perror("send call"); exit(-1); } free(fullPkt); // wait for ack get_pkt(socket_num); return socket_num; }
int rdt_connect(struct in_addr dst, int scid, int dcid) { int n, fd; pid_t pid; struct sockaddr_un un; struct in_addr src; struct conn_info conn_info; if (signal(SIGINT, sig_hand) == SIG_ERR || signal(SIGHUP, sig_hand) == SIG_ERR || signal(SIGQUIT, sig_hand) == SIG_ERR) { err_sys("signal() error"); } src = get_addr(dst); pid = getpid(); /* allocate share area for send * and recv process. */ conn_alloc(); conn_info.cact = ACTIVE; conn_info.pid = pid; conn_info.src = conn_user->src = src; conn_info.dst = conn_user->dst = dst; conn_info.scid = conn_user->scid = scid; conn_info.dcid = conn_user->dcid = dcid; conn_user->sndfd = make_fifo(pid, "snd"); conn_user->rcvfd = make_fifo(pid, "rcv"); conn_user->sfd = make_sock(); conn_user->seq = conn_user->ack = 0; if (!mtu) { if (dev[0] == 0 && !get_dev(src, dev)) err_quit("can't get dev name"); mtu = get_mtu(dev); } n = min(mtu, 1500); conn_user->mss = n; if ((conn_user->sndpkt = malloc(n)) == NULL) err_sys("malloc() sndpkt error"); if ((conn_user->rcvpkt = malloc(n)) == NULL) err_sys("malloc() rcvpkt error"); if ((fd = ux_cli(RDT_UX_SOCK, &un)) < 0) err_sys("ux_cli() error"); n = sizeof(struct conn_info); if (sendto(fd, &conn_info, n, 0, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) != n) { err_sys("sendto() error"); } get_pkt(conn_user->sndfd, &conn_info, NULL, 0); conn_user->scid = conn_info.scid; if (rexmt_pkt(conn_user, RDT_REQ, NULL, 0) < 0) err_sys("rexmt_pkt() error"); fprintf(stderr, "rdt_connect() succeed\n"); pkt_debug((struct rdthdr *)conn_user->sndpkt); return(0); }