• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

schweikert / fping / 20530406429

26 Dec 2025 10:24PM UTC coverage: 88.006% (+0.02%) from 87.985%
20530406429

Pull #443

github

web-flow
Merge 7e76bb57c into 15f05dd89
Pull Request #443: Replace atoi/atof/sscanf with strtoXX for stricter option parsing

42 of 46 new or added lines in 2 files covered. (91.3%)

145 existing lines in 2 files now uncovered.

1629 of 1851 relevant lines covered (88.01%)

363.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.14
/src/seqmap.c
1
/*
2
 * fping: fast-ping, file-ping, favorite-ping, funky-ping
3
 *
4
 *   Ping a list of target hosts in a round robin fashion.
5
 *   A better ping overall.
6
 *
7
 * fping website:  http://www.fping.org
8
 *
9
 * Current maintainer of fping: David Schweikert
10
 * Please send suggestions and patches to: david@schweikert.ch
11
 *
12
 *
13
 * Original author:  Roland Schemers  <schemers@stanford.edu>
14
 * IPv6 Support:     Jeroen Massar    <jeroen@unfix.org / jeroen@ipng.nl>
15
 * Improved main loop: David Schweikert <david@schweikert.ch>
16
 * Debian Merge, TOS settings: Tobi Oetiker <tobi@oetiker.ch>
17
 * Bugfixes, byte order & senseful seq.-numbers: Stephan Fuhrmann (stephan.fuhrmann AT 1und1.de)
18
 *
19
 *
20
 * Redistribution and use in source and binary forms are permitted
21
 * provided that the above copyright notice and this paragraph are
22
 * duplicated in all such forms and that any documentation,
23
 * advertising materials, and other materials related to such
24
 * distribution and use acknowledge that the software was developed
25
 * by Stanford University.  The name of the University may not be used
26
 * to endorse or promote products derived from this software without
27
 * specific prior written permission.
28
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
29
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
30
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31
 *
32
 * seqmap.c: implementation of a mapping between sequence number and (host, ping_nr)
33
 *           we can't just use ping_nr*host_count + host_nr, because it can
34
 *           overflow the 16 bit of the icmp header field. See also:
35
 *           https://github.com/schweikert/fping/issues/48
36
 */
37

38
#include "config.h"
39
#include "seqmap.h"
40
#include "limits.h"
41
#include "options.h"
42
#include "fping.h"
43

44
#include <inttypes.h>
45
#include <stdio.h>
46
#include <stdlib.h>
47

48
/* description of the data structure used:
49
 *
50
 * - we assume that no more than SEQMAP_MAXSEQ (65535) pings are sent in
51
 *   the timeout interval (seqmap_timeout_in_ns)
52
 * - we store the values in an array with SEQMAP_MAXSEQ elements
53
 * - current sequence number % SEQMAP_MAXSEQ gives the current index
54
 * - when entering a value, we check that the current entry is expired
55
 */
56

57
static SEQMAP_VALUE* seqmap_map = NULL;
58
static unsigned int seqmap_next_id = 0;
59
static int64_t seqmap_timeout_in_ns;
60

61
#define SEQMAP_UNASSIGNED_HOST_NR UINT_MAX
62

63
void seqmap_init(int64_t timeout)
385✔
64
{
65
    seqmap_timeout_in_ns = timeout;
385✔
66
    seqmap_map = calloc(SEQMAP_MAXSEQ, sizeof(SEQMAP_VALUE));
385✔
67
    if (seqmap_map == NULL) {
385✔
68
        perror("malloc error (can't allocate seqmap_map)");
×
69
    }
70
}
385✔
71

72
unsigned int seqmap_add(unsigned int host_nr, unsigned int ping_count, int64_t timestamp)
1,032✔
73
{
74
    unsigned int current_id;
75
    SEQMAP_VALUE* next_value;
76

77
    if (!seqmap_map) {
1,032✔
78
        fprintf(stderr, "fping internal error: seqmap not initialized.\n");
×
79
        exit(4);
×
80
    }
81

82
    /* check if expired (note that unused seqmap values will have fields set to
83
     * 0, so will be seen as expired */
84
    next_value = &seqmap_map[seqmap_next_id];
1,032✔
85
    if (next_value->ping_ts != 0 && timestamp - next_value->ping_ts < seqmap_timeout_in_ns) {
1,032✔
NEW
86
        return SEQMAP_MAXSEQ + 1;
×
87
    }
88

89
    /* store the value */
90
    next_value->host_nr = host_nr;
1,032✔
91
    next_value->ping_count = ping_count;
1,032✔
92
    next_value->ping_ts = timestamp;
1,032✔
93

94
    /* increase next id */
95
    current_id = seqmap_next_id;
1,032✔
96
    seqmap_next_id = (seqmap_next_id + 1) % SEQMAP_MAXSEQ;
1,032✔
97

98
    dbg_printf("seqmap_add(host: %d, index: %d) -> %d\n", host_nr, ping_count, current_id);
99

100
    return current_id;
1,032✔
101
}
270✔
102

103
SEQMAP_VALUE* seqmap_fetch(unsigned int id, int64_t now)
903✔
104
{
105
    SEQMAP_VALUE* value;
106

107
    if (id >= SEQMAP_MAXSEQ) {
903✔
UNCOV
108
        return NULL;
×
109
    }
110

111
    value = &seqmap_map[id];
903✔
112

113
    /* verify that value is not expired */
114
    if (now - value->ping_ts >= seqmap_timeout_in_ns) {
903✔
115
        dbg_printf("seqmap_fetch(%d) -> host: %d, index: %d -> DISCARDED %ld\n", id, value->host_nr, value->ping_count,
116
                now - value->ping_ts);
117
        return NULL;
12✔
118
    }
119

120
    dbg_printf("seqmap_fetch(%d) -> host: %d, index: %d\n", id, value->host_nr, value->ping_count);
121

122
    return value;
891✔
123
}
249✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc