int udp_init_listen(char *port) { struct addrinfo *addr = create_udp_addr(nullptr, port); int sock = create_udp_socket(addr); bind_udp(sock, addr); freeaddrinfo(addr); return sock; }
int init_rtp_connection(struct rtp_connection *connection, unsigned long send_interval_sec, unsigned long send_interval_usec, unsigned int sampling_freq, unsigned int sample_size, int data_input) { int err = 0, i = 0; if(connection->have_ipv4) err = bind_udp(&connection->bind_sk4, AF_INET); else connection->bind_sk4 = 0; if(err) { goto exit_err; } if(connection->have_ipv6) err = bind_udp(&connection->bind_sk6, AF_INET6); else connection->bind_sk6 = 0; if(err) { goto exit_err; } connection->send_interval.tv_sec = send_interval_sec; connection->send_interval.tv_usec = send_interval_usec; connection->sampling_freq = sampling_freq; connection->sample_size = sample_size; /* A magical constant at the moment... */ connection->header_size = get_rtp_header_size(); connection->seq_no = 0; connection->ssrc = random32(); connection->timestamp = 0; connection->data_input = data_input; return err; exit_err: if(connection->bind_sk4) close(connection->bind_sk4); if(connection->bind_sk6) close(connection->bind_sk6); return err; }
int main(int argc, char** argv) { unsigned short int udpPort; if(argc != 3) { fprintf(stderr, "usage: %s UDP_port filename_to_write\n\n", argv[0]); exit(1); } init_variable(); udpPort = (unsigned short int)atoi(argv[1]); bind_udp(udpPort); reliablyReceive(udpPort, argv[2]); }
int main(int argc, char *argv[]) { int i, socks, maxfd, fd[NFDS], recvq_length; uint32_t pktid[NPKTIDS], count[NCOUNTERS]; char losscount; uint32_t lastdelays[LASTDELAYS], lastdelayindex, averagedelay; packet_t *pkt[NPKTS], *recvq; config_t cfg[NCFGS]; fd_set infds, allsetinfds; averagedelay = 0; losscount = 0; lastdelayindex = 0; recvq = NULL; recvq_length = 0; pktid[expected] = FIRST_PACKET_ID; for (i = 1; i < NPKTIDS; i++) pktid[i] = FIRST_PACKET_ID - 1; for (i = 0; i < NCOUNTERS; i++) count[i] = 0; for (i = 0; i < NPKTS; i++) pkt[i] = NULL; for (i = 0; i < NCFGS; i++) { cfg[i].type = 0; cfg[i].n = 0; } FD_ZERO(&allsetinfds); fd[mon] = connect_mon(HOST, MON_PORT); FD_SET(fd[mon], &allsetinfds); fd[app] = accept_app(listen_app(HOST, APP_PORT)); FD_SET(fd[app], &allsetinfds); fd[peer] = bind_udp(HOST, PEER_PORT); FD_SET(fd[peer], &allsetinfds); maxfd = fd[peer]; for (;;) { infds = allsetinfds; socks = select(maxfd + 1, &infds, NULL, NULL, NULL); if (socks <= 0) err(ERR_SELECT); if (count[delivered] + count[discarded] > 0) losscount = ((double) count[discarded] / (double) (count[delivered] + count[discarded])) * 100; if (count[delivered] >= LASTDELAYS) { averagedelay = 0; for (i = 0; i < LASTDELAYS; i++) averagedelay += lastdelays[i]; averagedelay /= LASTDELAYS; } if (recvq_length >= MAX_RECVQ_LENGTH) { while (recvq_length > 0) { packet_t *a_pack = q_extract_first(&recvq); recvq_length--; if (a_pack->id >= pktid[expected]) { send_voice_pkts(fd[app], a_pack, SOCK_STREAM, NULL); lastdelays[lastdelayindex] = timeval_age(&a_pack->time); if (lastdelays[lastdelayindex] < MAX_DELAY) { lastdelayindex = (lastdelayindex + 1) % LASTDELAYS; count[delivered]++; count[discarded] += a_pack->id - pktid[sentapp] - 1; pktid[sentapp] = a_pack->id; } pktid[expected] = a_pack->id + 1; } else count[discarded]++; free(a_pack); } } while (socks > 0) { socks--; if (FD_ISSET(fd[mon], &infds)) { char answ = recv_mon(fd[mon], &cfg[tmp]); switch (answ) { case 'A': if (pkt[app]->id == pktid[sentpeer] && pkt[app]->id != pktid[freed]) { free(pkt[app]); pktid[freed] = pkt[app]->id; } break; case 'N': if (pkt[app]->id == pktid[sentpeer]) { struct sockaddr_in to; send_voice_pkts(fd[peer], pkt[app], SOCK_DGRAM, select_path(&cfg[new], &to, averagedelay)); } break; case 'C': cfg[new] = cfg[tmp]; print_routes(&cfg[new]); break; default: errx(ERR_MONPACK); } FD_CLR(fd[mon], &infds); } else if (FD_ISSET(fd[app], &infds)) { struct sockaddr_in to; pkt[app] = (packet_t *) malloc(sizeof(packet_t)); if (pkt[app] == NULL) err(ERR_MALLOC); recv_voice_pkts(fd[app], pkt[app], SOCK_STREAM, NULL); pkt[app]->ploss = losscount; send_voice_pkts(fd[peer], pkt[app], SOCK_DGRAM, select_path(&cfg[new], &to, averagedelay)); pktid[sentpeer] = pkt[app]->id; FD_CLR(fd[app], &infds); } else if (FD_ISSET(fd[peer], &infds)) {
void bind_call_handler(unsigned long long uniqueSockID, int threads, unsigned char *buf, ssize_t len) { int index; socklen_t addrlen; struct sockaddr_in *addr; u_char *pt; int reuseaddr; pt = buf; addrlen = *(int *) pt; pt += sizeof(int); if (addrlen <= 0) { PRINT_DEBUG("READING ERROR! CRASH, addrlen=%d", addrlen); nack_send(uniqueSockID, bind_call); return; } else { PRINT_DEBUG("addrlen=%d", addrlen); } addr = (struct sockaddr_in *) malloc(addrlen); memcpy(addr, pt, addrlen); pt += addrlen; reuseaddr = *(int *) pt; pt += sizeof(int); if (pt - buf != len) { PRINT_DEBUG("READING ERROR! CRASH, diff=%d len=%d", pt - buf, len); nack_send(uniqueSockID, bind_call); return; } PRINT_DEBUG("%d,%d,%d", (addr->sin_addr).s_addr, ntohs(addr->sin_port), addr->sin_family); index = findjinniSocket(uniqueSockID); /** if that requested socket does not exist !! * this means we can not even talk to the requester FINS crash as a response!! */ if (index == -1) { PRINT_DEBUG( " CRASH !socket descriptor not found into jinni sockets! Bind failed on Jinni Side "); nack_send(uniqueSockID, bind_call); return; } jinniSockets[index].sockopts.FSO_REUSEADDR |= reuseaddr; //TODO: when sockopts fully impelmented just set to '=' if (jinniSockets[index].type == SOCK_DGRAM) bind_udp(uniqueSockID, addr); else if (jinniSockets[index].type == SOCK_STREAM) bind_tcp(uniqueSockID, addr); else PRINT_DEBUG("unknown socket type has been read !!!"); return; } //end of bind_call_handler()