/** * @brief Connects the client to the server. * @param QString host - the host name * @param int port - the port used by the server * @param QString pseudo - the client's name */ void MainWindow::serverConnection(QString host, int port, QString pseudo) { strcpy(_player.name, pseudo.toStdString().c_str()); init_host(_ptr_host, (char*)host.toStdString().c_str(), &_local_addr); assign_port(&_local_addr, port); _player.socket = create_socket(); // Have to test if the client can be connect to the server if(server_connection(_player.socket, _local_addr) >= 0) { qDebug() << "[Server_connection] : Client is connected"; startListeners(); // Have to start listeners threads // Ask to the server to add the new client into its array of players qDebug() << "[Server_connection] : Sending information to the server" << _player.name; frame f = make_frame(_local_addr.sin_addr, _local_addr.sin_addr, CONNECT, _player.name); write_to_server(_player.socket, &f); // When the client is connected, display the mainPage of the application ui->stackedWidget->slideInIdx(1, SlidingStackedWidget::BOTTOM2TOP); } else { //Connection is impossible QMessageBox errorBox(QMessageBox::Question,tr("error"),tr("Can't establish the connection with the server."),QMessageBox::Cancel); errorBox.exec(); } }
const char* ts3plugin_description() { if( description_cache == 0 ) { init_host( &pluginHost, &hostPluginID ); description_cache = pluginHost->desc( hostPluginID ); } return description_cache; }
const char* ts3plugin_name() { if( name_cache == 0 ) { init_host( &pluginHost, &hostPluginID ); name_cache = pluginHost->name( hostPluginID ); } return name_cache; }
const char* ts3plugin_author() { if( author_cache == 0 ) { init_host( &pluginHost, &hostPluginID ); author_cache = pluginHost->author( hostPluginID ); } return author_cache; }
const char* ts3plugin_version() { if( version_cache == 0 ) { init_host( &pluginHost, &hostPluginID ); version_cache = pluginHost->version( hostPluginID ); } return version_cache; }
int main(void) { /* typical in 10000 service scenario: 150..1000 concurrently scheduled async jobs. */ const int num_checks_at_once = MAX_CONCURRENT_CHECKS; void* neb_handle; int i; char ok = 1; char * test_nebargs[] = { "config=extras/shared.conf", }; struct nebstruct_process_struct data_process_events; data_process_events.type = NEBTYPE_PROCESS_EVENTLOOPSTART; init_externals(); init_host(); init_svc(); init_svc_check(); neb_handle = load_neb("./mod_gearman2.o", test_nebargs[0]); neb_make_callbacks(NEBCALLBACK_PROCESS_DATA, &data_process_events); while (ok) { printf("Sending %i service checks.\n", num_checks_at_once); for (i = 0; i < num_checks_at_once; i++) { nebstruct_service_check_data* data_service_check; char host_name[16]; char service_name[16]; sprintf(host_name, "%i", i / 10); sprintf(service_name, "%i", i); data_service_check = setup_service(host_name, service_name); neb_make_callbacks(NEBCALLBACK_SERVICE_CHECK_DATA, data_service_check); } ok = (reap_cr_from_neb(num_checks_at_once) >= num_checks_at_once); } unload_neb(neb_handle); return 0; }
static int apache_read_host (user_data_t *user_data) /* {{{ */ { int i; char *ptr; char *saveptr; char *lines[16]; int lines_num = 0; char *fields[4]; int fields_num; apache_t *st; st = user_data->data; assert (st->url != NULL); /* (Assured by `config_add') */ if (st->curl == NULL) { int status; status = init_host (st); if (status != 0) return (-1); } assert (st->curl != NULL); st->apache_buffer_fill = 0; if (curl_easy_perform (st->curl) != CURLE_OK) { ERROR ("apache: curl_easy_perform failed: %s", st->apache_curl_error); return (-1); } /* fallback - server_type to apache if not set at this time */ if (st->server_type == -1) { WARNING ("apache plugin: Unable to determine server software " "automatically. Will assume Apache."); st->server_type = APACHE; } ptr = st->apache_buffer; saveptr = NULL; while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL) { ptr = NULL; lines_num++; if (lines_num >= 16) break; } for (i = 0; i < lines_num; i++) { fields_num = strsplit (lines[i], fields, 4); if (fields_num == 3) { if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0)) submit_derive ("apache_requests", "", atoll (fields[2]), st); else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0)) submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st); } else if (fields_num == 2) { if (strcmp (fields[0], "Scoreboard:") == 0) submit_scoreboard (fields[1], st); else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */ || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */) submit_gauge ("apache_connections", NULL, atol (fields[1]), st); else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */ || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */) submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st); } } st->apache_buffer_fill = 0; return (0); } /* }}} int apache_read_host */
xymongen_page_t *load_layout(char *pgset) { char pagetag[100], subpagetag[100], subparenttag[100], vpagetag[100], vsubpagetag[100], vsubparenttag[100], grouptag[100], summarytag[100], titletag[100], hosttag[100]; char *name, *link, *onlycols, *exceptcols; char hostname[MAX_LINE_LEN]; xymongen_page_t *toppage, *curpage, *cursubpage, *cursubparent; group_t *curgroup; host_t *curhost; char *curtitle; int ip1, ip2, ip3, ip4; char *p; int fqdn = get_fqdn(); char *cfgdata, *inbol, *ineol, insavchar = '\0'; if (loadhostsfromxymond) { if (load_hostnames("@", NULL, fqdn) != 0) { errprintf("Cannot load host configuration from xymond\n"); return NULL; } } else { if (load_hostnames(xgetenv("HOSTSCFG"), "dispinclude", fqdn) != 0) { errprintf("Cannot load host configuration from %s\n", xgetenv("HOSTSCFG")); return NULL; } } if (first_host() == NULL) { errprintf("Empty configuration from %s\n", (loadhostsfromxymond ? "xymond" : xgetenv("HOSTSCFG"))); return NULL; } dbgprintf("load_layout(pgset=%s)\n", textornull(pgset)); /* * load_hostnames() picks up the hostname definitions, but not the page * layout. So we will scan the file again, this time doing the layout. */ if (pgset == NULL) pgset = ""; sprintf(pagetag, "%spage", pgset); sprintf(subpagetag, "%ssubpage", pgset); sprintf(subparenttag, "%ssubparent", pgset); sprintf(vpagetag, "v%spage", pgset); sprintf(vsubpagetag, "v%ssubpage", pgset); sprintf(vsubparenttag, "v%ssubparent", pgset); sprintf(grouptag, "%sgroup", pgset); sprintf(summarytag, "%ssummary", pgset); sprintf(titletag, "%stitle", pgset); sprintf(hosttag, "%s:", pgset); for (p=hosttag; (*p); p++) *p = toupper((int)*p); toppage = init_page("", "", 0); addtopagelist(toppage); curpage = NULL; cursubpage = NULL; curgroup = NULL; curhost = NULL; cursubparent = NULL; curtitle = NULL; inbol = cfgdata = hostscfg_content(); while (inbol && *inbol) { inbol += strspn(inbol, " \t"); ineol = strchr(inbol, '\n'); if (ineol) { while ((ineol > inbol) && (isspace(*ineol) || (*ineol == '\n'))) ineol--; if (*ineol != '\n') ineol++; insavchar = *ineol; *ineol = '\0'; } dbgprintf("load_layout: -- got line '%s'\n", inbol); if ((strncmp(inbol, pagetag, strlen(pagetag)) == 0) || (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)) { getnamelink(inbol, &name, &link); if (curpage == NULL) { /* First page - hook it on toppage as a subpage from there */ curpage = toppage->subpages = init_page(name, link, (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)); } else { curpage = curpage->next = init_page(name, link, (strncmp(inbol, vpagetag, strlen(vpagetag)) == 0)); } curpage->parent = toppage; if (curtitle) { curpage->pretitle = curtitle; curtitle = NULL; } cursubpage = NULL; cursubparent = NULL; curgroup = NULL; curhost = NULL; addtopagelist(curpage); } else if ( (strncmp(inbol, subpagetag, strlen(subpagetag)) == 0) || (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0) ) { if (curpage == NULL) { errprintf("'subpage' ignored, no preceding 'page' tag : %s\n", inbol); goto nextline; } getnamelink(inbol, &name, &link); if (cursubpage == NULL) { cursubpage = curpage->subpages = init_page(name, link, (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0)); } else { cursubpage = cursubpage->next = init_page(name, link, (strncmp(inbol, vsubpagetag, strlen(vsubpagetag)) == 0)); } cursubpage->parent = curpage; if (curtitle) { cursubpage->pretitle = curtitle; curtitle = NULL; } cursubparent = NULL; curgroup = NULL; curhost = NULL; addtopagelist(cursubpage); } else if ( (strncmp(inbol, subparenttag, strlen(subparenttag)) == 0) || (strncmp(inbol, vsubparenttag, strlen(vsubparenttag)) == 0) ) { xymongen_page_t *parentpage, *walk; getparentnamelink(inbol, toppage, &parentpage, &name, &link); if (parentpage == NULL) { errprintf("'subparent' ignored, unknown parent page: %s\n", inbol); goto nextline; } cursubparent = init_page(name, link, (strncmp(inbol, vsubparenttag, strlen(vsubparenttag)) == 0)); if (parentpage->subpages == NULL) { parentpage->subpages = cursubparent; } else { for (walk = parentpage->subpages; (walk->next); (walk = walk->next)) ; walk->next = cursubparent; } if (curtitle) { cursubparent->pretitle = curtitle; curtitle = NULL; } cursubparent->parent = parentpage; curgroup = NULL; curhost = NULL; addtopagelist(cursubparent); } else if (strncmp(inbol, grouptag, strlen(grouptag)) == 0) { int sorthosts = (strstr(inbol, "group-sorted") != NULL); getgrouptitle(inbol, pgset, &link, &onlycols, &exceptcols); if (curgroup == NULL) { curgroup = init_group(link, onlycols, exceptcols, sorthosts); if (cursubparent != NULL) { cursubparent->groups = curgroup; } else if (cursubpage != NULL) { /* We're in a subpage */ cursubpage->groups = curgroup; } else if (curpage != NULL) { /* We're on a main page */ curpage->groups = curgroup; } else { /* We're on the top page */ toppage->groups = curgroup; } } else { curgroup->next = init_group(link, onlycols, exceptcols, sorthosts); curgroup = curgroup->next; } if (curtitle) { curgroup->pretitle = curtitle; curtitle = NULL; } curhost = NULL; } else if (sscanf(inbol, "%3d.%3d.%3d.%3d %s", &ip1, &ip2, &ip3, &ip4, hostname) == 5) { void *xymonhost = NULL; int dialup, nonongreen, crittime = 1; double warnpct = reportwarnlevel; int warnstops = reportwarnstops; char *displayname, *clientalias, *comment, *description; char *alertlist, *onwaplist, *reporttime; char *nopropyellowlist, *nopropredlist, *noproppurplelist, *nopropacklist; char *targetpagelist[MAX_TARGETPAGES_PER_HOST]; int targetpagecount; char *hval; /* Check for ".default." hosts - they are ignored. */ if (*hostname == '.') goto nextline; if (!fqdn) { /* Strip any domain from the hostname */ char *p = strchr(hostname, '.'); if (p) *p = '\0'; } /* Get the info */ xymonhost = hostinfo(hostname); if (xymonhost == NULL) { errprintf("Confused - hostname '%s' cannot be found. Ignored\n", hostname); goto nextline; } /* Check for no-display hosts - they are ignored. */ /* But only when we're building the default pageset */ if ((strlen(pgset) == 0) && (xmh_item(xymonhost, XMH_FLAG_NODISP) != NULL)) goto nextline; for (targetpagecount=0; (targetpagecount < MAX_TARGETPAGES_PER_HOST); targetpagecount++) targetpagelist[targetpagecount] = NULL; targetpagecount = 0; dialup = (xmh_item(xymonhost, XMH_FLAG_DIALUP) != NULL); nonongreen = (xmh_item(xymonhost, XMH_FLAG_NONONGREEN) != NULL); alertlist = xmh_item(xymonhost, XMH_NK); hval = xmh_item(xymonhost, XMH_NKTIME); if (hval) crittime = within_sla(xmh_item(xymonhost, XMH_HOLIDAYS), hval, 0); onwaplist = xmh_item(xymonhost, XMH_WML); nopropyellowlist = xmh_item(xymonhost, XMH_NOPROPYELLOW); if (nopropyellowlist == NULL) nopropyellowlist = xmh_item(xymonhost, XMH_NOPROP); nopropredlist = xmh_item(xymonhost, XMH_NOPROPRED); noproppurplelist = xmh_item(xymonhost, XMH_NOPROPPURPLE); nopropacklist = xmh_item(xymonhost, XMH_NOPROPACK); displayname = xmh_item(xymonhost, XMH_DISPLAYNAME); comment = xmh_item(xymonhost, XMH_COMMENT); description = xmh_item(xymonhost, XMH_DESCRIPTION); hval = xmh_item(xymonhost, XMH_WARNPCT); if (hval) warnpct = atof(hval); hval = xmh_item(xymonhost, XMH_WARNSTOPS); if (hval) warnstops = atof(hval); reporttime = xmh_item(xymonhost, XMH_REPORTTIME); clientalias = xmh_item(xymonhost, XMH_CLIENTALIAS); if (xymonhost && (strcmp(xmh_item(xymonhost, XMH_HOSTNAME), clientalias) == 0)) clientalias = NULL; if (xymonhost && (strlen(pgset) > 0)) { /* Walk the clone-list and pick up the target pages for this host */ void *cwalk = xymonhost; do { hval = xmh_item_walk(cwalk); while (hval) { if (strncasecmp(hval, hosttag, strlen(hosttag)) == 0) targetpagelist[targetpagecount++] = strdup(hval+strlen(hosttag)); hval = xmh_item_walk(NULL); } cwalk = next_host(cwalk, 1); } while (cwalk && (strcmp(xmh_item(cwalk, XMH_HOSTNAME), xmh_item(xymonhost, XMH_HOSTNAME)) == 0) && (targetpagecount < MAX_TARGETPAGES_PER_HOST) ); /* * HACK: Check if the pageset tag is present at all in the host * entry. If it isn't, then drop this incarnation of the host. * * Without this, the following hosts.cfg file will have the * www.hswn.dk host listed twice on the alternate pageset: * * adminpage nyc NYC * * 127.0.0.1 localhost # bbd http://localhost/ CLIENT:osiris * 172.16.10.2 www.xymon.com # http://www.xymon.com/ ADMIN:nyc ssh noinfo * * page superdome Superdome * 172.16.10.2 www.xymon.com # noconn * */ if (strstr(inbol, hosttag) == NULL) targetpagecount = 0; } if (strlen(pgset) == 0) { /* * Default pageset generated. Put the host into * whatever group or page is current. */ if (curhost == NULL) { curhost = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist, nopropredlist, noproppurplelist, nopropacklist); if (curgroup != NULL) { curgroup->hosts = curhost; } else if (cursubparent != NULL) { cursubparent->hosts = curhost; } else if (cursubpage != NULL) { cursubpage->hosts = curhost; } else if (curpage != NULL) { curpage->hosts = curhost; } else { toppage->hosts = curhost; } } else { curhost = curhost->next = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist,nopropredlist, noproppurplelist, nopropacklist); } curhost->parent = (cursubparent ? cursubparent : (cursubpage ? cursubpage : curpage)); if (curtitle) { curhost->pretitle = curtitle; curtitle = NULL; } curhost->nonongreen = nonongreen; } else if (targetpagecount) { int pgnum; for (pgnum=0; (pgnum < targetpagecount); pgnum++) { char *targetpagename = targetpagelist[pgnum]; char savechar; int wantedgroup = 0; xymonpagelist_t *targetpage = NULL; /* Put the host into the page specified by the PGSET: tag */ p = strchr(targetpagename, ','); if (p) { savechar = *p; *p = '\0'; wantedgroup = atoi(p+1); } else { savechar = '\0'; p = targetpagename + strlen(targetpagename); } /* Find the page */ if (strcmp(targetpagename, "*") == 0) { *targetpagename = '\0'; } for (targetpage = pagelisthead; (targetpage && (strcmp(targetpagename, targetpage->pageentry->name) != 0)); targetpage = targetpage->next) ; *p = savechar; if (targetpage == NULL) { errprintf("Warning: Cannot find any target page named '%s' in set '%s' - dropping host '%s'\n", targetpagename, pgset, hostname); } else { host_t *newhost = init_host(hostname, 0, displayname, clientalias, comment, description, ip1, ip2, ip3, ip4, dialup, warnpct, warnstops, reporttime, alertlist, crittime, onwaplist, nopropyellowlist,nopropredlist, noproppurplelist, nopropacklist); if (wantedgroup > 0) { group_t *gwalk; host_t *hwalk; int i; for (gwalk = targetpage->pageentry->groups, i=1; (gwalk && (i < wantedgroup)); i++,gwalk=gwalk->next) ; if (gwalk) { if (gwalk->hosts == NULL) gwalk->hosts = newhost; else { for (hwalk = gwalk->hosts; (hwalk->next); hwalk = hwalk->next) ; hwalk->next = newhost; } } else { errprintf("Warning: Cannot find group %d for host %s - dropping host\n", wantedgroup, hostname); } } else { /* Just put in on the page's hostlist */ host_t *walk; if (targetpage->pageentry->hosts == NULL) targetpage->pageentry->hosts = newhost; else { for (walk = targetpage->pageentry->hosts; (walk->next); walk = walk->next) ; walk->next = newhost; } } newhost->parent = targetpage->pageentry; if (curtitle) newhost->pretitle = curtitle; } curtitle = NULL; } } } else if (strncmp(inbol, summarytag, strlen(summarytag)) == 0) { /* summary row.column IP-ADDRESS-OF-PARENT http://xymon.com/ */ char sumname[MAX_LINE_LEN]; char receiver[MAX_LINE_LEN]; char url[MAX_LINE_LEN]; summary_t *newsum; if (sscanf(inbol, "summary %s %s %s", sumname, receiver, url) == 3) { newsum = init_summary(sumname, receiver, url); newsum->next = sumhead; sumhead = newsum; } } else if (strncmp(inbol, titletag, strlen(titletag)) == 0) { /* Save the title for the next entry */ curtitle = strdup(skipwhitespace(skipword(inbol))); } nextline: if (ineol) { *ineol = insavchar; if (*ineol != '\n') ineol = strchr(ineol, '\n'); inbol = (ineol ? ineol+1 : NULL); } else inbol = NULL; } xfree(cfgdata); return toppage; }
void ts3plugin_setFunctionPointers(const struct TS3Functions funcs) { init_host( &pluginHost, &hostPluginID ); }
static int apache_read_host (user_data_t *user_data) /* {{{ */ { char *ptr; char *saveptr; char *line; char *fields[4]; int fields_num; apache_t *st; st = user_data->data; int status; char *content_type; static const char *text_plain = "text/plain"; assert (st->url != NULL); /* (Assured by `config_add') */ if (st->curl == NULL) { status = init_host (st); if (status != 0) return (-1); } assert (st->curl != NULL); st->apache_buffer_fill = 0; if (curl_easy_perform (st->curl) != CURLE_OK) { ERROR ("apache: curl_easy_perform failed: %s", st->apache_curl_error); return (-1); } /* fallback - server_type to apache if not set at this time */ if (st->server_type == -1) { WARNING ("apache plugin: Unable to determine server software " "automatically. Will assume Apache."); st->server_type = APACHE; } status = curl_easy_getinfo (st->curl, CURLINFO_CONTENT_TYPE, &content_type); if ((status == CURLE_OK) && (content_type != NULL) && (strncasecmp (content_type, text_plain, strlen (text_plain)) != 0)) { WARNING ("apache plugin: `Content-Type' response header is not `%s' " "(received: `%s'). Expecting unparseable data. Please check `URL' " "parameter (missing `?auto' suffix ?)", text_plain, content_type); } ptr = st->apache_buffer; saveptr = NULL; while ((line = strtok_r (ptr, "\n\r", &saveptr)) != NULL) { ptr = NULL; fields_num = strsplit (line, fields, STATIC_ARRAY_SIZE (fields)); if (fields_num == 3) { if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "Accesses:") == 0)) submit_derive ("apache_requests", "", atoll (fields[2]), st); else if ((strcmp (fields[0], "Total") == 0) && (strcmp (fields[1], "kBytes:") == 0)) submit_derive ("apache_bytes", "", 1024LL * atoll (fields[2]), st); } else if (fields_num == 2) { if (strcmp (fields[0], "Scoreboard:") == 0) submit_scoreboard (fields[1], st); else if ((strcmp (fields[0], "BusyServers:") == 0) /* Apache 1.* */ || (strcmp (fields[0], "BusyWorkers:") == 0) /* Apache 2.* */) submit_gauge ("apache_connections", NULL, atol (fields[1]), st); else if ((strcmp (fields[0], "IdleServers:") == 0) /* Apache 1.x */ || (strcmp (fields[0], "IdleWorkers:") == 0) /* Apache 2.x */) submit_gauge ("apache_idle_workers", NULL, atol (fields[1]), st); } } st->apache_buffer_fill = 0; return (0); } /* }}} int apache_read_host */
int main(int argc, char* argv[]) { // Checks the program parameters int opt; map_options(argc, argv, &opt); argc -= optind; argv += optind; if(argc != 2) { usage(); exit(EXIT_FAILURE); } // Destination file opening if(fd == -1) { if((fd = open(filename, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG)) < 0) { die("Error opening destination file"); } } // Port and adresses string init char *addr_str = argv[0]; char *port_str = argv[1]; if(verbose) { printf("\nListenning address \'%s\' on port \'%s\'\n", addr_str, port_str); } // Resolve the address passed to the program int result; if((result = getaddrinfo(addr_str, port_str, &hints, &address)) < 0) { printf("Error resolving address %s - code %i", addr_str, result); freeaddrinfo(address); close(fd); exit(EXIT_FAILURE); } // initialize the socket used by the receiver sock_id = init_host(address, receiver); if(sock_id == -1) { die("Error creating socket"); } packetstruct tmp_packet; // stores the just received packet // last in-sequence acknowledge packet int lastack = SEQSPAN-1; // last in-sequence acknowledge packet int bufferPos = 0; // Position following lastack in the buffer // of paylod has been received. int bufferFill = 0; //nb of unwritten valid packets received int idx; //position in the buffer for the received packet ssize_t size = PAYLOADSIZE; // Size of the received payload int is_valid; memset(window,0,BUFFSIZE*sizeof(window_slot)); while( !isReceived(lastack) ) { /* blocking receive - we are waiting for a frame */ src_len = sizeof(src_host); if(recvfrom(sock_id, (void *) &tmp_packet, sizeof(tmp_packet), 0, (struct sockaddr*) &src_host, &(src_len)) != sizeof(packetstruct)) { die("Error while receiving packet"); } is_valid = packet_valid(&tmp_packet); /* only if the packet is valid */ if(is_valid == 1) { size = tmp_packet.length; if(verbose) { printf("Received a %zd-byte type %u packet (seq %u)\n", size, tmp_packet.type, tmp_packet.seqnum); } // Is the sequence number in the receive window ? if((idx = idx_in_window(tmp_packet.seqnum,lastack,bufferPos)) != -1) { if(bufferFill<BUFFSIZE && !window[idx].received){ recv_buffer[idx] = tmp_packet; // copy packet to rcv_buffer window[idx].received = true; // mark the frame as received bufferFill++; // add 1 more received packet /* If this is the last packet from the original file * (ie, payload with a size smaller than 512 Bytes */ if(size < PAYLOADSIZE) { if(verbose){ printf("Packet with smaller PAYLOADSIZE received, end of file detected\n"); } lastseq = tmp_packet.seqnum; } // Try to empty the in-sequence received packets if(flush_frames(fd, &lastack, &bufferPos, &bufferFill)) { die("Error writing packets to file"); } } else{printf("BufferFill : %d The receiving buffer is full\n",bufferFill);} // Send an acknowledgement acknowledge(lastack); } // Else, we do nothing and discard it... else if(verbose) { printf("Received an out-of-window packet (seq:%u)(lastack:%d)\n ", tmp_packet.seqnum,lastack); } } else{ if(verbose){ printf("Received a corrupted packet !\n"); } acknowledge(lastack); } } // End of transmission : resend acks if the sender keeps sending data int received; int timeDiff = EOTDELAY-1; int EOTstart = clock(); while(timeDiff<EOTDELAY){ received = 0; fcntl(sock_id, F_SETFL, O_NONBLOCK); // make receive non-blocking received = recvfrom(sock_id,(void*)(&tmp_packet),sizeof(tmp_packet),0,NULL,NULL); if(received != sizeof(packetstruct) && errno != EAGAIN) { die("Error receiving"); } if(received==sizeof(packetstruct)){ acknowledge(lastack); } timeDiff = (clock()-EOTstart)*1000/CLOCKS_PER_SEC; } if(verbose) { printf("File successfully received\n"); } freeaddrinfo(address); close(sock_id); exit(EXIT_SUCCESS); }