├── Makefile ├── README.md ├── src ├── mping.c └── uthash.h └── COPYING /Makefile: -------------------------------------------------------------------------------- 1 | CC=gcc 2 | WFLAGS=-Wall -Werror -Wno-unused 3 | LOCAL_CFLAGS=-O2 -Iinclude -fno-strict-aliasing 4 | PRGNAME=mping 5 | 6 | UNAME_S := $(shell uname -s) 7 | ifeq ($(UNAME_S),Linux) 8 | CCFLAGS += -D LINUX 9 | LINKFLAGS=-Wl,--no-undefined 10 | else ifeq ($(UNAME_S),Darwin) 11 | CCFLAGS += -D OSX 12 | LINKFLAGS=-Wl,-undefined,error 13 | endif 14 | 15 | all: $(PRGNAME) 16 | 17 | clean: 18 | rm -f $(PRGNAME) $(OBJ) 19 | %.o: %.c 20 | $(CC) $(WFLAGS) -c -o $@ $(LOCAL_CFLAGS) $(CPPFLAGS) $(CFLAGS) $< 21 | 22 | OBJ=src/mping.o 23 | 24 | $(PRGNAME): $(OBJ) 25 | $(CC) $(LDFLAGS) $(LINKFLAGS) -o $@ $^ 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Mping - README 2 | 3 | Description 4 | ----------- 5 | A network testing tool to verify multicast connectivity. 6 | 7 | This tool is used for checking if multicast packets can 8 | cross multiple routers. 9 | 10 | Application has to be executed on all of the network leaves 11 | used in connectivity testing - tests require a single sender 12 | and multiple receivers. 13 | 14 | Compiling 15 | --------- 16 | make 17 | 18 | Usage 19 | ----- 20 | 21 | mping -l -I [-p ] [-g ] [-q] 22 | mping -s -I [-p ] [-g ] [-i ] [-c ] [-T TTL ] [-q] [-S payload size] 23 | Defaults: interval - 1 second, multicast group - 226.1.1.1 , udp port - 4322 24 | 25 | Any-source multicast mode (ASM) used by default. If you want to use Source-specific multicast (SSM) mode, specify the source IP address of the multicast sender with the group option. 26 | 27 | Usage Example 28 | ------------- 29 | Simple network topology: 30 | 31 | Host A ---- Cisco ---- Cisco ---- Host B 32 | | 33 | Cisco 34 | | 35 | -------- Host C 36 | 37 | Host A IP - 10.0.101.2/24 38 | Host B IP - 10.0.102.2/24 39 | Host C IP - 10.0.103.2/24 40 | 41 | Verifying that multicast packets sent from Host A can be 42 | heard on Host B and Host C. 43 | 44 | Host B: 45 | 46 | user@host_b:/tmp$ mping -l -I 10.0.102.2 47 | 48 | Host C: 49 | 50 | user@host_c:/tmp$ mping -l -I 10.0.103.2 51 | 52 | Host A: 53 | 54 | user@host_a:/tmp$ mping -s -I 10.0.101.2 -c 10 55 | MPING 226.1.1.1 1024(1052) bytes of data. 56 | 1024 bytes from 10.0.102.2: seq=0 ttl=62 time=0.889 ms 57 | 1024 bytes from 10.0.103.2: seq=0 ttl=61 time=1.884 ms 58 | 1024 bytes from 10.0.102.2: seq=1 ttl=62 time=0.660 ms 59 | 1024 bytes from 10.0.103.2: seq=1 ttl=61 time=1.516 ms 60 | 1024 bytes from 10.0.102.2: seq=2 ttl=62 time=0.447 ms 61 | 1024 bytes from 10.0.103.2: seq=2 ttl=61 time=1.543 ms 62 | 1024 bytes from 10.0.102.2: seq=3 ttl=62 time=0.921 ms 63 | 1024 bytes from 10.0.103.2: seq=3 ttl=61 time=1.591 ms 64 | 1024 bytes from 10.0.102.2: seq=4 ttl=62 time=0.677 ms 65 | 1024 bytes from 10.0.103.2: seq=4 ttl=61 time=1.766 ms 66 | 1024 bytes from 10.0.102.2: seq=5 ttl=62 time=0.778 ms 67 | 1024 bytes from 10.0.103.2: seq=5 ttl=61 time=1.494 ms 68 | 1024 bytes from 10.0.102.2: seq=6 ttl=62 time=0.818 ms 69 | 1024 bytes from 10.0.103.2: seq=6 ttl=61 time=1.640 ms 70 | 1024 bytes from 10.0.102.2: seq=7 ttl=62 time=0.896 ms 71 | 1024 bytes from 10.0.103.2: seq=7 ttl=61 time=1.433 ms 72 | 1024 bytes from 10.0.102.2: seq=8 ttl=62 time=0.536 ms 73 | 1024 bytes from 10.0.103.2: seq=8 ttl=61 time=1.666 ms 74 | 1024 bytes from 10.0.102.2: seq=9 ttl=62 time=0.647 ms 75 | 1024 bytes from 10.0.103.2: seq=9 ttl=61 time=1.348 ms 76 | 77 | 10.0.102.2: rtt min/avg/max: 0.447/0.726/0.921 ms, loss: 0% (0 packets) 78 | 10.0.103.2: rtt min/avg/max: 1.348/1.588/1.884 ms, loss: 0% (0 packets) 79 | 80 | 81 | -------------------------------------------------------------------------------- /src/mping.c: -------------------------------------------------------------------------------- 1 | /* 2 | Multicast Ping (mping) is a network administration utility used to test the 3 | reachability of multiple hosts via multicast traffic. 4 | 5 | Copyright (C) 2015 Alexis Green 6 | 7 | This program is free software: you can redistribute it and/or modify it under 8 | the terms of the GNU General Public License as published by the Free Software 9 | Foundation, either version 3 of the License, or (at your option) any later 10 | version. 11 | 12 | This program is distributed in the hope that it will be useful, but WITHOUT ANY 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 14 | PARTICULAR PURPOSE. See the GNU General Public License for more details. 15 | 16 | You should have received a copy of the GNU General Public License along with 17 | this program. If not, see . 18 | */ 19 | 20 | #include 21 | #ifdef LINUX 22 | #include 23 | #else 24 | #include 25 | #endif 26 | #include 27 | #include 28 | #include 29 | #ifdef ANDROID 30 | #include 31 | #else 32 | #include 33 | #endif 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include "uthash.h" 41 | 42 | #define DEFAULT_SIZE 1024 43 | #define MAX_SIZE 65507 44 | #define UDP_HEADER 28 45 | #define DEFAULT_PORT 4322 46 | #define DEFAULT_TTL 64 47 | #define DEFAULT_MADDR "226.1.1.1" 48 | #define MAX_ADDR_LEN 100 49 | 50 | #ifndef SOL_IP 51 | #define SOL_IP IPPROTO_IP 52 | #endif 53 | 54 | static struct in_addr local_iface; 55 | static struct sockaddr_in group_sock, server_addr, client_addr; 56 | static int sock_desc, addr_len, recv_bytes; 57 | static int stop_count; 58 | static char recv_buf[MAX_SIZE]; 59 | static char mcast_addr[MAX_ADDR_LEN]; 60 | static char local_addr[MAX_ADDR_LEN]; 61 | static char src_addr[MAX_ADDR_LEN]; 62 | static struct timeval delay; 63 | static int send_sequence; 64 | static int udp_port; 65 | static int verbose; 66 | static int send_ttl; 67 | static int data_size; 68 | static int sender; 69 | 70 | static int recv_len = sizeof(recv_buf); 71 | 72 | struct client_store { 73 | UT_hash_handle hh; 74 | unsigned long s_addr; 75 | int last_seq; 76 | int num_pongs; 77 | struct timeval min_rtt; 78 | struct timeval max_rtt; 79 | struct timeval total_time; 80 | }; 81 | 82 | #pragma pack(push, 1) 83 | 84 | struct ping_header { 85 | uint32_t sequence; 86 | uint32_t send_time_sec; 87 | uint32_t send_time_usec; 88 | uint32_t ttl; 89 | }; 90 | 91 | struct ping_msg { 92 | struct ping_header hdr; 93 | char msg[65535]; 94 | }; 95 | 96 | #pragma pack(pop) 97 | 98 | static struct ping_msg payload; 99 | static struct ping_header* recv_ping_hdr; 100 | 101 | static struct client_store* clients = NULL; 102 | 103 | static 104 | void finish(int ignore) 105 | { 106 | 107 | if(sender) 108 | { 109 | signal(SIGINT, SIG_IGN); 110 | signal(SIGTERM, SIG_IGN); 111 | printf("\n"); 112 | struct client_store * client; 113 | for (client=clients; client!=NULL; client=client->hh.next) 114 | { 115 | struct in_addr blah={.s_addr=client->s_addr}; 116 | printf("%s: rtt min/avg/max: %lu.%03lu/%lu.%03lu/%lu.%03lu ms, loss: %d%% (%d packets)\n", inet_ntoa(blah), 117 | (client->min_rtt.tv_sec*1000000+client->min_rtt.tv_usec)/1000, 118 | (client->min_rtt.tv_sec*1000000+client->min_rtt.tv_usec)%1000, 119 | ((client->total_time.tv_sec*1000000+client->total_time.tv_usec)/client->num_pongs)/1000, 120 | ((client->total_time.tv_sec*1000000+client->total_time.tv_usec)/client->num_pongs)%1000, 121 | (client->max_rtt.tv_sec*1000000+client->max_rtt.tv_usec)/1000, 122 | (client->max_rtt.tv_sec*1000000+client->max_rtt.tv_usec)%1000, 123 | ((send_sequence-client->num_pongs)*100/send_sequence), 124 | send_sequence-client->num_pongs); 125 | } 126 | } 127 | 128 | fflush(stdout); 129 | exit(0); 130 | } 131 | 132 | static 133 | int run_client() 134 | { 135 | struct ip_mreq group; 136 | struct ip_mreq_source group_source; 137 | struct msghdr msgh; 138 | struct cmsghdr *cmsg; 139 | struct iovec iov; 140 | char cbuf[1000]; 141 | int err; 142 | int flags = 0; 143 | memset((char *) &client_addr, 0, sizeof(struct sockaddr_in)); 144 | 145 | sock_desc = socket(AF_INET, SOCK_DGRAM, 0); 146 | if(sock_desc < 0) 147 | { 148 | perror("Opening datagram socket for reception error"); 149 | return 1; 150 | } 151 | 152 | int reuse = 1; 153 | if(setsockopt(sock_desc, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) 154 | { 155 | perror("Setting SO_REUSEADDR error"); 156 | close(sock_desc); 157 | return 1; 158 | } 159 | 160 | int rec_ttl = 1; 161 | if (setsockopt(sock_desc, SOL_IP, IP_RECVTTL, (char *)&rec_ttl, sizeof(rec_ttl)) < 0 ) 162 | { 163 | perror("Setting IP_RECVTTL error"); 164 | close(sock_desc); 165 | return 1; 166 | } 167 | 168 | memset((char *) &group_sock, 0, sizeof(group_sock)); 169 | group_sock.sin_family = AF_INET; 170 | group_sock.sin_port = htons(udp_port); 171 | group_sock.sin_addr.s_addr = INADDR_ANY; 172 | if(bind(sock_desc, (struct sockaddr*)&group_sock, sizeof(group_sock))) 173 | { 174 | perror("Binding datagram socket error"); 175 | close(sock_desc); 176 | return 1; 177 | } 178 | 179 | if (strlen(src_addr) != 0) { 180 | group_source.imr_multiaddr.s_addr = inet_addr(mcast_addr); 181 | group_source.imr_sourceaddr.s_addr = inet_addr(src_addr); 182 | group_source.imr_interface.s_addr = inet_addr(local_addr); 183 | if(setsockopt(sock_desc, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, (char *)&group_source, sizeof(group_source)) < 0) 184 | { 185 | perror("Adding multicast group error"); 186 | close(sock_desc); 187 | return 1; 188 | } 189 | } else { 190 | group.imr_multiaddr.s_addr = inet_addr(mcast_addr); 191 | group.imr_interface.s_addr = inet_addr(local_addr); 192 | if(setsockopt(sock_desc, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) 193 | { 194 | perror("Adding multicast group error"); 195 | close(sock_desc); 196 | return 1; 197 | } 198 | } 199 | 200 | signal(SIGINT, finish); 201 | signal(SIGTERM, finish); 202 | 203 | while(1) 204 | { 205 | recv_len = sizeof(recv_buf); 206 | addr_len=sizeof(struct sockaddr); 207 | memset(&payload, 0, sizeof(payload)); 208 | memset(&msgh, 0, sizeof(msgh)); 209 | iov.iov_base = recv_buf; 210 | iov.iov_len = recv_len; 211 | msgh.msg_control = cbuf; 212 | msgh.msg_controllen = sizeof(cbuf); 213 | msgh.msg_name = &client_addr; 214 | msgh.msg_namelen = (socklen_t)addr_len; 215 | msgh.msg_iov = &iov; 216 | msgh.msg_iovlen = 1; 217 | msgh.msg_flags = 0; 218 | 219 | recv_bytes = recvmsg(sock_desc, &msgh, flags); 220 | if ( recv_bytes <= 0 ) 221 | { 222 | perror("Failed to receive packet from the socket"); 223 | continue; 224 | } 225 | for (cmsg = CMSG_FIRSTHDR(&msgh); cmsg != NULL; cmsg = CMSG_NXTHDR(&msgh,cmsg)) { 226 | if (cmsg->cmsg_level == IPPROTO_IP && cmsg->cmsg_type == IP_TTL) { 227 | payload.hdr.ttl=htonl(*((int *) CMSG_DATA(cmsg))); 228 | break; 229 | } 230 | } 231 | if (cmsg == NULL) 232 | { 233 | perror("Failed to get TTL for packet"); 234 | payload.hdr.ttl=htonl(DEFAULT_TTL); 235 | } 236 | 237 | // don't want to reply to 0.0.0.0 or myself 238 | if ( client_addr.sin_addr.s_addr != INADDR_ANY && client_addr.sin_addr.s_addr!=inet_addr(local_addr) ) 239 | { 240 | recv_ping_hdr = (struct ping_header*) recv_buf; 241 | 242 | memset((char *) &server_addr, 0, sizeof(server_addr)); 243 | server_addr.sin_family = AF_INET; 244 | server_addr.sin_port = htons(udp_port); 245 | server_addr.sin_addr.s_addr = client_addr.sin_addr.s_addr; 246 | 247 | // not doing ntoh and back since stuff gets sent back to network anyway 248 | payload.hdr.sequence=recv_ping_hdr->sequence; 249 | payload.hdr.send_time_sec=recv_ping_hdr->send_time_sec; 250 | payload.hdr.send_time_usec=recv_ping_hdr->send_time_usec; 251 | 252 | if (verbose == 1) 253 | printf("%d bytes from %s: seq=%d ttl=%d\n", 254 | recv_bytes, 255 | inet_ntoa(server_addr.sin_addr), 256 | ntohl(recv_ping_hdr->sequence), 257 | ntohl(payload.hdr.ttl)); 258 | sendto(sock_desc, &payload, recv_bytes, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)); 259 | } 260 | } 261 | } 262 | 263 | static 264 | int run_server () 265 | { 266 | fd_set master; 267 | fd_set read_fds; 268 | FD_ZERO(&master); 269 | FD_ZERO(&read_fds); 270 | struct timeval send_time, cur_time, timeout, elapsed, recv_time, payload_timeval, rtt; 271 | struct client_store* client; 272 | 273 | sock_desc = socket(AF_INET, SOCK_DGRAM, 0); 274 | if(sock_desc < 0) 275 | { 276 | perror("Opening datagram socket server error"); 277 | return 1; 278 | } 279 | 280 | int reuse = 1; 281 | if(setsockopt(sock_desc, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0) 282 | { 283 | perror("Setting SO_REUSEADDR error"); 284 | close(sock_desc); 285 | return 1; 286 | } 287 | 288 | memset((char *) &group_sock, 0, sizeof(group_sock)); 289 | group_sock.sin_family = AF_INET; 290 | group_sock.sin_addr.s_addr = inet_addr(mcast_addr); 291 | group_sock.sin_port = htons(udp_port); 292 | 293 | local_iface.s_addr = inet_addr(local_addr); 294 | if(setsockopt(sock_desc, IPPROTO_IP, IP_MULTICAST_IF, (char *)&local_iface, sizeof(local_iface)) < 0) 295 | { 296 | perror("Setting local interface error"); 297 | return 1; 298 | } 299 | 300 | 301 | if(setsockopt(sock_desc, IPPROTO_IP, IP_MULTICAST_TTL, &send_ttl, sizeof(send_ttl) ) < 0) 302 | { 303 | perror("Setting ttl error"); 304 | return 1; 305 | } 306 | 307 | memset((char *) &server_addr, 0, sizeof(struct sockaddr_in)); 308 | server_addr.sin_port = htons(udp_port); 309 | server_addr.sin_family = AF_INET; 310 | server_addr.sin_addr.s_addr= htonl(INADDR_ANY); 311 | 312 | if ( bind(sock_desc, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0 ) 313 | { 314 | perror("Failed to bind server socket"); 315 | close(sock_desc); 316 | return 1; 317 | } 318 | 319 | signal(SIGINT, finish); 320 | signal(SIGTERM, finish); 321 | 322 | send_sequence=0; 323 | FD_SET(sock_desc, &master); 324 | addr_len=sizeof(struct sockaddr); 325 | 326 | gettimeofday(&send_time, NULL); 327 | 328 | timeout.tv_sec=0; 329 | timeout.tv_usec=0; 330 | 331 | printf("MPING %s %d(%d) bytes of data.\n", mcast_addr, data_size, data_size+UDP_HEADER); 332 | 333 | int ret; 334 | while(1) 335 | { 336 | read_fds=master; 337 | 338 | if ( (ret=select(sock_desc+1, &read_fds, NULL, NULL, &timeout)) > 0 ) 339 | { 340 | if ((recv_bytes=recvfrom(sock_desc, recv_buf, recv_len, 0, (struct sockaddr *)&client_addr, (socklen_t *)&addr_len)) !=0) 341 | { 342 | gettimeofday(&recv_time, NULL); 343 | recv_ping_hdr = (struct ping_header *)recv_buf; 344 | unsigned long s_addr = client_addr.sin_addr.s_addr; 345 | HASH_FIND(hh, clients, &(s_addr) ,sizeof(s_addr) , client); 346 | if (!client) 347 | { 348 | signal(SIGINT, SIG_IGN); 349 | signal(SIGTERM, SIG_IGN); 350 | client = (struct client_store*)calloc(1, sizeof(*client)); 351 | client->s_addr=s_addr; 352 | client->last_seq=0; 353 | client->last_seq=0; 354 | client->min_rtt.tv_sec=10000; 355 | client->min_rtt.tv_usec=0; 356 | client->total_time.tv_sec=0; 357 | client->total_time.tv_usec=0; 358 | client->max_rtt.tv_sec=0; 359 | client->max_rtt.tv_usec=0; 360 | HASH_ADD(hh, clients, s_addr, sizeof(s_addr), client); 361 | signal(SIGINT, finish); 362 | signal(SIGTERM, finish); 363 | } 364 | 365 | payload_timeval.tv_sec=ntohl(recv_ping_hdr->send_time_sec); 366 | payload_timeval.tv_usec=ntohl(recv_ping_hdr->send_time_usec); 367 | 368 | timersub(&recv_time, &payload_timeval, &rtt); 369 | 370 | if (timercmp(&rtt, &(client->min_rtt), <)) 371 | client->min_rtt=rtt; 372 | if (timercmp(&rtt, &(client->max_rtt), >)) 373 | client->max_rtt=rtt; 374 | client->num_pongs=client->num_pongs+1; 375 | timeradd(&(client->total_time),&rtt, &(client->total_time)); 376 | client->last_seq=(int)(ntohl(recv_ping_hdr->sequence)); 377 | 378 | if (verbose == 1) 379 | printf("%d bytes from %s: seq=%d ttl=%d time=%ld.%03ld ms\n", 380 | recv_bytes, 381 | inet_ntoa(client_addr.sin_addr), 382 | ntohl(recv_ping_hdr->sequence), 383 | ntohl(recv_ping_hdr->ttl), 384 | ((rtt.tv_sec*1000000+rtt.tv_usec)/1000), 385 | ((rtt.tv_sec*1000000+rtt.tv_usec)%1000)); 386 | } 387 | } 388 | else if (ret == -1) 389 | { 390 | perror("Select failure"); 391 | close(sock_desc); 392 | return 1; 393 | } 394 | 395 | gettimeofday(&cur_time, NULL); 396 | 397 | timersub(&cur_time, &send_time, &elapsed); 398 | if( timercmp(&elapsed, &timeout, >=) ) 399 | { 400 | if (stop_count !=0 && send_sequence >= stop_count) 401 | finish(0); 402 | 403 | memset((char *) &payload, 0, sizeof(payload)); 404 | payload.hdr.sequence=htonl(send_sequence); 405 | payload.hdr.send_time_sec=htonl(cur_time.tv_sec); 406 | payload.hdr.send_time_usec=htonl(cur_time.tv_usec); 407 | payload.hdr.ttl=0; 408 | 409 | if(sendto(sock_desc, &payload, data_size, 0, (struct sockaddr*)&group_sock, sizeof(group_sock)) < 0) 410 | perror("Sending datagram message error"); 411 | 412 | send_sequence+=1; 413 | gettimeofday(&send_time, NULL); 414 | timeout=delay; 415 | } 416 | else 417 | timersub(&timeout, &elapsed, &timeout); 418 | } 419 | } 420 | 421 | static 422 | void usage(int return_code) 423 | { 424 | // TODO: 425 | // would be good to have -w 426 | printf("Usage:\tmping -l -I [-p ] [-g ] [-q]\n"); 427 | printf("\tmping -s -I [-p ] [-g ] "); 428 | printf("[-i ] [-c ] [-T TTL ] [-q] [-S payload size]\n"); 429 | printf("\tDefaults: interval - 1 second, multicast group - %s , udp port - %d\n", DEFAULT_MADDR, DEFAULT_PORT ); 430 | exit(return_code); 431 | } 432 | 433 | int main (int argc, char *argv[ ]) 434 | { 435 | int c; 436 | 437 | char *address=NULL; 438 | char *group=NULL; 439 | char *port=NULL; 440 | char *interval=NULL; 441 | char *count=NULL; 442 | char *ttl_string=NULL; 443 | char *size_string=NULL; 444 | int listener=0; 445 | 446 | extern char *optarg; 447 | extern int optind, optopt; 448 | 449 | 450 | if (argc==1) 451 | usage(0); 452 | 453 | sender=0; 454 | verbose=1; 455 | 456 | while (( c = getopt(argc, argv, "c:g:hi:lp:qsI:S:T:V")) !=-1) 457 | { 458 | switch(c) 459 | { 460 | case 'c': 461 | count=optarg; 462 | break; 463 | case 'I': 464 | address=optarg; 465 | break; 466 | case 'i': 467 | interval=optarg; 468 | break; 469 | case 'g': 470 | group=optarg; 471 | break; 472 | case 'h': 473 | usage(0); 474 | case 'p': 475 | port=optarg; 476 | break; 477 | case 'q': 478 | verbose=0; 479 | break; 480 | case 'l': 481 | listener=1; 482 | break; 483 | case 's': 484 | sender=1; 485 | break; 486 | case 'T': 487 | ttl_string=optarg; 488 | break; 489 | case 'S': 490 | size_string=optarg; 491 | break; 492 | case 'V': 493 | printf("mping version 0.1\n"); 494 | exit(0); 495 | case '?': 496 | usage(0); 497 | default: 498 | abort(); 499 | } 500 | } 501 | 502 | if (!address) 503 | { 504 | fprintf(stderr, "Must specify local interface address.\n"); 505 | usage(1); 506 | } 507 | else 508 | { 509 | in_addr_t addr; 510 | snprintf(local_addr, MAX_ADDR_LEN, "%s",address); 511 | addr=inet_addr(local_addr); 512 | if (addr == ( in_addr_t)(-1)) 513 | { 514 | fprintf(stderr, "Invalid address specified for local interface\n"); 515 | usage(1); 516 | } 517 | 518 | } 519 | 520 | if (!group) 521 | snprintf(mcast_addr, MAX_ADDR_LEN, "%s", DEFAULT_MADDR); 522 | else 523 | { 524 | in_addr_t group_addr; 525 | char *source=NULL; 526 | char *groupp = group; 527 | char *grp; 528 | 529 | grp = strsep(&groupp, "/"); 530 | if (groupp != NULL) { 531 | source = strsep(&groupp, "/"); 532 | snprintf(src_addr, MAX_ADDR_LEN, "%s",source); 533 | } 534 | group = grp; 535 | 536 | snprintf(mcast_addr, MAX_ADDR_LEN, "%s",group); 537 | 538 | group_addr=inet_addr(mcast_addr); 539 | if ( group_addr == (in_addr_t)(-1) || !IN_MULTICAST(ntohl(group_addr))) 540 | { 541 | fprintf(stderr, "Invalid address specified for multicast group - address must be multicast.\n"); 542 | usage(1); 543 | } 544 | } 545 | 546 | if (!port) 547 | udp_port=DEFAULT_PORT; 548 | else 549 | { 550 | udp_port=atoi(port); 551 | if (udp_port < 1024 || udp_port > 65535) 552 | { 553 | fprintf(stderr, "Invalide port set - must be between 1024 and 65535.\n"); 554 | usage(1); 555 | } 556 | 557 | } 558 | 559 | 560 | if (listener) 561 | { 562 | if (sender) 563 | { 564 | fprintf(stderr, "Unable to be sender and receiver at the same time.\n"); 565 | usage(1); 566 | } 567 | 568 | if (interval != NULL) 569 | { 570 | fprintf(stderr, "Unable to set interval when in receive mode.\n"); 571 | usage(1); 572 | } 573 | if (count != NULL) 574 | { 575 | fprintf(stderr, "Unable to set ping count in receiver mode.\n"); 576 | usage(1); 577 | } 578 | if (ttl_string != NULL) 579 | { 580 | fprintf(stderr, "Unable to set TTL in receiver mode.\n"); 581 | usage(1); 582 | } 583 | if (size_string != NULL) 584 | { 585 | fprintf(stderr, "Unable to set payload size in receiver mode.\n"); 586 | usage(1); 587 | } 588 | return run_client(); 589 | } 590 | 591 | if (sender) 592 | { 593 | if (!interval) 594 | { 595 | delay.tv_sec=1; 596 | delay.tv_usec=0; 597 | } 598 | else 599 | { 600 | double secs = strtod(interval, NULL)/1000; // returns zero on errors 601 | if (secs > 0.0) 602 | { 603 | delay.tv_sec = (long) secs; 604 | delay.tv_usec = (long) (1e6*(secs - delay.tv_sec)); 605 | } 606 | else 607 | { 608 | fprintf(stderr, "Invalid interval set - must be greater than 0.\n"); 609 | usage(1); 610 | } 611 | } 612 | 613 | if(!count) 614 | stop_count=0; 615 | else 616 | { 617 | stop_count=atoi(count); 618 | if (stop_count <= 0 || stop_count > 65535) 619 | { 620 | fprintf(stderr, "Invalid stop count.\n"); 621 | usage(1); 622 | } 623 | } 624 | 625 | if(!ttl_string) 626 | send_ttl=DEFAULT_TTL; 627 | else 628 | { 629 | send_ttl=atoi(ttl_string); 630 | if (send_ttl <= 0 || send_ttl > 255) 631 | { 632 | fprintf(stderr, "Invalid TTL value - must be between 1 and 255.\n"); 633 | usage(1); 634 | } 635 | } 636 | 637 | if(!size_string) 638 | data_size=DEFAULT_SIZE; 639 | else 640 | { 641 | data_size=atoi(size_string); 642 | if (data_size < sizeof(struct ping_header) || data_size > MAX_SIZE) 643 | { 644 | fprintf(stderr, "Invalid payload size value - must be between %d and %d.\n", (int)(sizeof(struct ping_header)), DEFAULT_SIZE); 645 | usage(1); 646 | } 647 | } 648 | 649 | return run_server(); 650 | 651 | } 652 | 653 | fprintf(stderr, "Must select sender or receiver mode.\n"); 654 | usage(1); 655 | return 0; 656 | } 657 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/uthash.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2011, Troy D. Hanson http://uthash.sourceforge.net 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without 6 | modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 12 | IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 13 | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 14 | PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 15 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 16 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 17 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 18 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 19 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 20 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 21 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 | */ 23 | 24 | #ifndef UTHASH_H 25 | #define UTHASH_H 26 | 27 | #include /* memcmp,strlen */ 28 | #include /* ptrdiff_t */ 29 | #include /* exit() */ 30 | 31 | /* These macros use decltype or the earlier __typeof GNU extension. 32 | As decltype is only available in newer compilers (VS2010 or gcc 4.3+ 33 | when compiling c++ source) this code uses whatever method is needed 34 | or, for VS2008 where neither is available, uses casting workarounds. */ 35 | #ifdef _MSC_VER /* MS compiler */ 36 | #if _MSC_VER >= 1600 && defined(__cplusplus) /* VS2010 or newer in C++ mode */ 37 | #define DECLTYPE(x) (decltype(x)) 38 | #else /* VS2008 or older (or VS2010 in C mode) */ 39 | #define NO_DECLTYPE 40 | #define DECLTYPE(x) 41 | #endif 42 | #else /* GNU, Sun and other compilers */ 43 | #define DECLTYPE(x) (__typeof(x)) 44 | #endif 45 | 46 | #ifdef NO_DECLTYPE 47 | #define DECLTYPE_ASSIGN(dst,src) \ 48 | do { \ 49 | char **_da_dst = (char**)(&(dst)); \ 50 | *_da_dst = (char*)(src); \ 51 | } while(0) 52 | #else 53 | #define DECLTYPE_ASSIGN(dst,src) \ 54 | do { \ 55 | (dst) = DECLTYPE(dst)(src); \ 56 | } while(0) 57 | #endif 58 | 59 | /* a number of the hash function use uint32_t which isn't defined on win32 */ 60 | #ifdef _MSC_VER 61 | typedef unsigned int uint32_t; 62 | typedef unsigned char uint8_t; 63 | #else 64 | #include /* uint32_t */ 65 | #endif 66 | 67 | #define UTHASH_VERSION 1.9.4 68 | 69 | #define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */ 70 | #define uthash_malloc(sz) malloc(sz) /* malloc fcn */ 71 | #define uthash_free(ptr,sz) free(ptr) /* free fcn */ 72 | 73 | #define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */ 74 | #define uthash_expand_fyi(tbl) /* can be defined to log expands */ 75 | 76 | /* initial number of buckets */ 77 | #define HASH_INITIAL_NUM_BUCKETS 32 /* initial number of buckets */ 78 | #define HASH_INITIAL_NUM_BUCKETS_LOG2 5 /* lg2 of initial number of buckets */ 79 | #define HASH_BKT_CAPACITY_THRESH 10 /* expand when bucket count reaches */ 80 | 81 | /* calculate the element whose hash handle address is hhe */ 82 | #define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho))) 83 | 84 | #define HASH_FIND(hh,head,keyptr,keylen,out) \ 85 | do { \ 86 | unsigned _hf_bkt,_hf_hashv; \ 87 | out=NULL; \ 88 | if (head) { \ 89 | HASH_FCN(keyptr,keylen, (head)->hh.tbl->num_buckets, _hf_hashv, _hf_bkt); \ 90 | if (HASH_BLOOM_TEST((head)->hh.tbl, _hf_hashv)) { \ 91 | HASH_FIND_IN_BKT((head)->hh.tbl, hh, (head)->hh.tbl->buckets[ _hf_bkt ], \ 92 | keyptr,keylen,out); \ 93 | } \ 94 | } \ 95 | } while (0) 96 | 97 | #ifdef HASH_BLOOM 98 | #define HASH_BLOOM_BITLEN (1ULL << HASH_BLOOM) 99 | #define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8) + ((HASH_BLOOM_BITLEN%8) ? 1:0) 100 | #define HASH_BLOOM_MAKE(tbl) \ 101 | do { \ 102 | (tbl)->bloom_nbits = HASH_BLOOM; \ 103 | (tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \ 104 | if (!((tbl)->bloom_bv)) { uthash_fatal( "out of memory"); } \ 105 | memset((tbl)->bloom_bv, 0, HASH_BLOOM_BYTELEN); \ 106 | (tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \ 107 | } while (0); 108 | 109 | #define HASH_BLOOM_FREE(tbl) \ 110 | do { \ 111 | uthash_free((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \ 112 | } while (0); 113 | 114 | #define HASH_BLOOM_BITSET(bv,idx) (bv[(idx)/8] |= (1U << ((idx)%8))) 115 | #define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8] & (1U << ((idx)%8))) 116 | 117 | #define HASH_BLOOM_ADD(tbl,hashv) \ 118 | HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) 119 | 120 | #define HASH_BLOOM_TEST(tbl,hashv) \ 121 | HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1ULL << (tbl)->bloom_nbits) - 1))) 122 | 123 | #else 124 | #define HASH_BLOOM_MAKE(tbl) 125 | #define HASH_BLOOM_FREE(tbl) 126 | #define HASH_BLOOM_ADD(tbl,hashv) 127 | #define HASH_BLOOM_TEST(tbl,hashv) (1) 128 | #endif 129 | 130 | #define HASH_MAKE_TABLE(hh,head) \ 131 | do { \ 132 | (head)->hh.tbl = (UT_hash_table*)uthash_malloc( \ 133 | sizeof(UT_hash_table)); \ 134 | if (!((head)->hh.tbl)) { uthash_fatal( "out of memory"); } \ 135 | memset((head)->hh.tbl, 0, sizeof(UT_hash_table)); \ 136 | (head)->hh.tbl->tail = &((head)->hh); \ 137 | (head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \ 138 | (head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \ 139 | (head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \ 140 | (head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \ 141 | HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ 142 | if (! (head)->hh.tbl->buckets) { uthash_fatal( "out of memory"); } \ 143 | memset((head)->hh.tbl->buckets, 0, \ 144 | HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \ 145 | HASH_BLOOM_MAKE((head)->hh.tbl); \ 146 | (head)->hh.tbl->signature = HASH_SIGNATURE; \ 147 | } while(0) 148 | 149 | #define HASH_ADD(hh,head,fieldname,keylen_in,add) \ 150 | HASH_ADD_KEYPTR(hh,head,&add->fieldname,keylen_in,add) 151 | 152 | #define HASH_ADD_KEYPTR(hh,head,keyptr,keylen_in,add) \ 153 | do { \ 154 | unsigned _ha_bkt; \ 155 | (add)->hh.next = NULL; \ 156 | (add)->hh.key = (char*)keyptr; \ 157 | (add)->hh.keylen = keylen_in; \ 158 | if (!(head)) { \ 159 | head = (add); \ 160 | (head)->hh.prev = NULL; \ 161 | HASH_MAKE_TABLE(hh,head); \ 162 | } else { \ 163 | (head)->hh.tbl->tail->next = (add); \ 164 | (add)->hh.prev = ELMT_FROM_HH((head)->hh.tbl, (head)->hh.tbl->tail); \ 165 | (head)->hh.tbl->tail = &((add)->hh); \ 166 | } \ 167 | (head)->hh.tbl->num_items++; \ 168 | (add)->hh.tbl = (head)->hh.tbl; \ 169 | HASH_FCN(keyptr,keylen_in, (head)->hh.tbl->num_buckets, \ 170 | (add)->hh.hashv, _ha_bkt); \ 171 | HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt],&(add)->hh); \ 172 | HASH_BLOOM_ADD((head)->hh.tbl,(add)->hh.hashv); \ 173 | HASH_EMIT_KEY(hh,head,keyptr,keylen_in); \ 174 | HASH_FSCK(hh,head); \ 175 | } while(0) 176 | 177 | #define HASH_TO_BKT( hashv, num_bkts, bkt ) \ 178 | do { \ 179 | bkt = ((hashv) & ((num_bkts) - 1)); \ 180 | } while(0) 181 | 182 | /* delete "delptr" from the hash table. 183 | * "the usual" patch-up process for the app-order doubly-linked-list. 184 | * The use of _hd_hh_del below deserves special explanation. 185 | * These used to be expressed using (delptr) but that led to a bug 186 | * if someone used the same symbol for the head and deletee, like 187 | * HASH_DELETE(hh,users,users); 188 | * We want that to work, but by changing the head (users) below 189 | * we were forfeiting our ability to further refer to the deletee (users) 190 | * in the patch-up process. Solution: use scratch space to 191 | * copy the deletee pointer, then the latter references are via that 192 | * scratch pointer rather than through the repointed (users) symbol. 193 | */ 194 | #define HASH_DELETE(hh,head,delptr) \ 195 | do { \ 196 | unsigned _hd_bkt; \ 197 | struct UT_hash_handle *_hd_hh_del; \ 198 | if ( ((delptr)->hh.prev == NULL) && ((delptr)->hh.next == NULL) ) { \ 199 | uthash_free((head)->hh.tbl->buckets, \ 200 | (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ 201 | HASH_BLOOM_FREE((head)->hh.tbl); \ 202 | uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ 203 | head = NULL; \ 204 | } else { \ 205 | _hd_hh_del = &((delptr)->hh); \ 206 | if ((delptr) == ELMT_FROM_HH((head)->hh.tbl,(head)->hh.tbl->tail)) { \ 207 | (head)->hh.tbl->tail = \ 208 | (UT_hash_handle*)((char*)((delptr)->hh.prev) + \ 209 | (head)->hh.tbl->hho); \ 210 | } \ 211 | if ((delptr)->hh.prev) { \ 212 | ((UT_hash_handle*)((char*)((delptr)->hh.prev) + \ 213 | (head)->hh.tbl->hho))->next = (delptr)->hh.next; \ 214 | } else { \ 215 | DECLTYPE_ASSIGN(head,(delptr)->hh.next); \ 216 | } \ 217 | if (_hd_hh_del->next) { \ 218 | ((UT_hash_handle*)((char*)_hd_hh_del->next + \ 219 | (head)->hh.tbl->hho))->prev = \ 220 | _hd_hh_del->prev; \ 221 | } \ 222 | HASH_TO_BKT( _hd_hh_del->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \ 223 | HASH_DEL_IN_BKT(hh,(head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \ 224 | (head)->hh.tbl->num_items--; \ 225 | } \ 226 | HASH_FSCK(hh,head); \ 227 | } while (0) 228 | 229 | 230 | /* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */ 231 | #define HASH_FIND_STR(head,findstr,out) \ 232 | HASH_FIND(hh,head,findstr,strlen(findstr),out) 233 | #define HASH_ADD_STR(head,strfield,add) \ 234 | HASH_ADD(hh,head,strfield,strlen(add->strfield),add) 235 | #define HASH_FIND_INT(head,findint,out) \ 236 | HASH_FIND(hh,head,findint,sizeof(int),out) 237 | #define HASH_ADD_INT(head,intfield,add) \ 238 | HASH_ADD(hh,head,intfield,sizeof(int),add) 239 | #define HASH_FIND_PTR(head,findptr,out) \ 240 | HASH_FIND(hh,head,findptr,sizeof(void *),out) 241 | #define HASH_ADD_PTR(head,ptrfield,add) \ 242 | HASH_ADD(hh,head,ptrfield,sizeof(void *),add) 243 | #define HASH_DEL(head,delptr) \ 244 | HASH_DELETE(hh,head,delptr) 245 | 246 | /* HASH_FSCK checks hash integrity on every add/delete when HASH_DEBUG is defined. 247 | * This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined. 248 | */ 249 | #ifdef HASH_DEBUG 250 | #define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0) 251 | #define HASH_FSCK(hh,head) \ 252 | do { \ 253 | unsigned _bkt_i; \ 254 | unsigned _count, _bkt_count; \ 255 | char *_prev; \ 256 | struct UT_hash_handle *_thh; \ 257 | if (head) { \ 258 | _count = 0; \ 259 | for( _bkt_i = 0; _bkt_i < (head)->hh.tbl->num_buckets; _bkt_i++) { \ 260 | _bkt_count = 0; \ 261 | _thh = (head)->hh.tbl->buckets[_bkt_i].hh_head; \ 262 | _prev = NULL; \ 263 | while (_thh) { \ 264 | if (_prev != (char*)(_thh->hh_prev)) { \ 265 | HASH_OOPS("invalid hh_prev %p, actual %p\n", \ 266 | _thh->hh_prev, _prev ); \ 267 | } \ 268 | _bkt_count++; \ 269 | _prev = (char*)(_thh); \ 270 | _thh = _thh->hh_next; \ 271 | } \ 272 | _count += _bkt_count; \ 273 | if ((head)->hh.tbl->buckets[_bkt_i].count != _bkt_count) { \ 274 | HASH_OOPS("invalid bucket count %d, actual %d\n", \ 275 | (head)->hh.tbl->buckets[_bkt_i].count, _bkt_count); \ 276 | } \ 277 | } \ 278 | if (_count != (head)->hh.tbl->num_items) { \ 279 | HASH_OOPS("invalid hh item count %d, actual %d\n", \ 280 | (head)->hh.tbl->num_items, _count ); \ 281 | } \ 282 | /* traverse hh in app order; check next/prev integrity, count */ \ 283 | _count = 0; \ 284 | _prev = NULL; \ 285 | _thh = &(head)->hh; \ 286 | while (_thh) { \ 287 | _count++; \ 288 | if (_prev !=(char*)(_thh->prev)) { \ 289 | HASH_OOPS("invalid prev %p, actual %p\n", \ 290 | _thh->prev, _prev ); \ 291 | } \ 292 | _prev = (char*)ELMT_FROM_HH((head)->hh.tbl, _thh); \ 293 | _thh = ( _thh->next ? (UT_hash_handle*)((char*)(_thh->next) + \ 294 | (head)->hh.tbl->hho) : NULL ); \ 295 | } \ 296 | if (_count != (head)->hh.tbl->num_items) { \ 297 | HASH_OOPS("invalid app item count %d, actual %d\n", \ 298 | (head)->hh.tbl->num_items, _count ); \ 299 | } \ 300 | } \ 301 | } while (0) 302 | #else 303 | #define HASH_FSCK(hh,head) 304 | #endif 305 | 306 | /* When compiled with -DHASH_EMIT_KEYS, length-prefixed keys are emitted to 307 | * the descriptor to which this macro is defined for tuning the hash function. 308 | * The app can #include to get the prototype for write(2). */ 309 | #ifdef HASH_EMIT_KEYS 310 | #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) \ 311 | do { \ 312 | unsigned _klen = fieldlen; \ 313 | write(HASH_EMIT_KEYS, &_klen, sizeof(_klen)); \ 314 | write(HASH_EMIT_KEYS, keyptr, fieldlen); \ 315 | } while (0) 316 | #else 317 | #define HASH_EMIT_KEY(hh,head,keyptr,fieldlen) 318 | #endif 319 | 320 | /* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */ 321 | #ifdef HASH_FUNCTION 322 | #define HASH_FCN HASH_FUNCTION 323 | #else 324 | #define HASH_FCN HASH_JEN 325 | #endif 326 | 327 | /* The Bernstein hash function, used in Perl prior to v5.6 */ 328 | #define HASH_BER(key,keylen,num_bkts,hashv,bkt) \ 329 | do { \ 330 | unsigned _hb_keylen=keylen; \ 331 | char *_hb_key=(char*)(key); \ 332 | (hashv) = 0; \ 333 | while (_hb_keylen--) { (hashv) = ((hashv) * 33) + *_hb_key++; } \ 334 | bkt = (hashv) & (num_bkts-1); \ 335 | } while (0) 336 | 337 | 338 | /* SAX/FNV/OAT/JEN hash functions are macro variants of those listed at 339 | * http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx */ 340 | #define HASH_SAX(key,keylen,num_bkts,hashv,bkt) \ 341 | do { \ 342 | unsigned _sx_i; \ 343 | char *_hs_key=(char*)(key); \ 344 | hashv = 0; \ 345 | for(_sx_i=0; _sx_i < keylen; _sx_i++) \ 346 | hashv ^= (hashv << 5) + (hashv >> 2) + _hs_key[_sx_i]; \ 347 | bkt = hashv & (num_bkts-1); \ 348 | } while (0) 349 | 350 | #define HASH_FNV(key,keylen,num_bkts,hashv,bkt) \ 351 | do { \ 352 | unsigned _fn_i; \ 353 | char *_hf_key=(char*)(key); \ 354 | hashv = 2166136261UL; \ 355 | for(_fn_i=0; _fn_i < keylen; _fn_i++) \ 356 | hashv = (hashv * 16777619) ^ _hf_key[_fn_i]; \ 357 | bkt = hashv & (num_bkts-1); \ 358 | } while(0); 359 | 360 | #define HASH_OAT(key,keylen,num_bkts,hashv,bkt) \ 361 | do { \ 362 | unsigned _ho_i; \ 363 | char *_ho_key=(char*)(key); \ 364 | hashv = 0; \ 365 | for(_ho_i=0; _ho_i < keylen; _ho_i++) { \ 366 | hashv += _ho_key[_ho_i]; \ 367 | hashv += (hashv << 10); \ 368 | hashv ^= (hashv >> 6); \ 369 | } \ 370 | hashv += (hashv << 3); \ 371 | hashv ^= (hashv >> 11); \ 372 | hashv += (hashv << 15); \ 373 | bkt = hashv & (num_bkts-1); \ 374 | } while(0) 375 | 376 | #define HASH_JEN_MIX(a,b,c) \ 377 | do { \ 378 | a -= b; a -= c; a ^= ( c >> 13 ); \ 379 | b -= c; b -= a; b ^= ( a << 8 ); \ 380 | c -= a; c -= b; c ^= ( b >> 13 ); \ 381 | a -= b; a -= c; a ^= ( c >> 12 ); \ 382 | b -= c; b -= a; b ^= ( a << 16 ); \ 383 | c -= a; c -= b; c ^= ( b >> 5 ); \ 384 | a -= b; a -= c; a ^= ( c >> 3 ); \ 385 | b -= c; b -= a; b ^= ( a << 10 ); \ 386 | c -= a; c -= b; c ^= ( b >> 15 ); \ 387 | } while (0) 388 | 389 | #define HASH_JEN(key,keylen,num_bkts,hashv,bkt) \ 390 | do { \ 391 | unsigned _hj_i,_hj_j,_hj_k; \ 392 | char *_hj_key=(char*)(key); \ 393 | hashv = 0xfeedbeef; \ 394 | _hj_i = _hj_j = 0x9e3779b9; \ 395 | _hj_k = keylen; \ 396 | while (_hj_k >= 12) { \ 397 | _hj_i += (_hj_key[0] + ( (unsigned)_hj_key[1] << 8 ) \ 398 | + ( (unsigned)_hj_key[2] << 16 ) \ 399 | + ( (unsigned)_hj_key[3] << 24 ) ); \ 400 | _hj_j += (_hj_key[4] + ( (unsigned)_hj_key[5] << 8 ) \ 401 | + ( (unsigned)_hj_key[6] << 16 ) \ 402 | + ( (unsigned)_hj_key[7] << 24 ) ); \ 403 | hashv += (_hj_key[8] + ( (unsigned)_hj_key[9] << 8 ) \ 404 | + ( (unsigned)_hj_key[10] << 16 ) \ 405 | + ( (unsigned)_hj_key[11] << 24 ) ); \ 406 | \ 407 | HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ 408 | \ 409 | _hj_key += 12; \ 410 | _hj_k -= 12; \ 411 | } \ 412 | hashv += keylen; \ 413 | switch ( _hj_k ) { \ 414 | case 11: hashv += ( (unsigned)_hj_key[10] << 24 ); \ 415 | case 10: hashv += ( (unsigned)_hj_key[9] << 16 ); \ 416 | case 9: hashv += ( (unsigned)_hj_key[8] << 8 ); \ 417 | case 8: _hj_j += ( (unsigned)_hj_key[7] << 24 ); \ 418 | case 7: _hj_j += ( (unsigned)_hj_key[6] << 16 ); \ 419 | case 6: _hj_j += ( (unsigned)_hj_key[5] << 8 ); \ 420 | case 5: _hj_j += _hj_key[4]; \ 421 | case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); \ 422 | case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); \ 423 | case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); \ 424 | case 1: _hj_i += _hj_key[0]; \ 425 | } \ 426 | HASH_JEN_MIX(_hj_i, _hj_j, hashv); \ 427 | bkt = hashv & (num_bkts-1); \ 428 | } while(0) 429 | 430 | /* The Paul Hsieh hash function */ 431 | #undef get16bits 432 | #if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ 433 | || defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) 434 | #define get16bits(d) (*((const uint16_t *) (d))) 435 | #endif 436 | 437 | #if !defined (get16bits) 438 | #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8) \ 439 | +(uint32_t)(((const uint8_t *)(d))[0]) ) 440 | #endif 441 | #define HASH_SFH(key,keylen,num_bkts,hashv,bkt) \ 442 | do { \ 443 | char *_sfh_key=(char*)(key); \ 444 | uint32_t _sfh_tmp, _sfh_len = keylen; \ 445 | \ 446 | int _sfh_rem = _sfh_len & 3; \ 447 | _sfh_len >>= 2; \ 448 | hashv = 0xcafebabe; \ 449 | \ 450 | /* Main loop */ \ 451 | for (;_sfh_len > 0; _sfh_len--) { \ 452 | hashv += get16bits (_sfh_key); \ 453 | _sfh_tmp = (get16bits (_sfh_key+2) << 11) ^ hashv; \ 454 | hashv = (hashv << 16) ^ _sfh_tmp; \ 455 | _sfh_key += 2*sizeof (uint16_t); \ 456 | hashv += hashv >> 11; \ 457 | } \ 458 | \ 459 | /* Handle end cases */ \ 460 | switch (_sfh_rem) { \ 461 | case 3: hashv += get16bits (_sfh_key); \ 462 | hashv ^= hashv << 16; \ 463 | hashv ^= _sfh_key[sizeof (uint16_t)] << 18; \ 464 | hashv += hashv >> 11; \ 465 | break; \ 466 | case 2: hashv += get16bits (_sfh_key); \ 467 | hashv ^= hashv << 11; \ 468 | hashv += hashv >> 17; \ 469 | break; \ 470 | case 1: hashv += *_sfh_key; \ 471 | hashv ^= hashv << 10; \ 472 | hashv += hashv >> 1; \ 473 | } \ 474 | \ 475 | /* Force "avalanching" of final 127 bits */ \ 476 | hashv ^= hashv << 3; \ 477 | hashv += hashv >> 5; \ 478 | hashv ^= hashv << 4; \ 479 | hashv += hashv >> 17; \ 480 | hashv ^= hashv << 25; \ 481 | hashv += hashv >> 6; \ 482 | bkt = hashv & (num_bkts-1); \ 483 | } while(0); 484 | 485 | #ifdef HASH_USING_NO_STRICT_ALIASING 486 | /* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads. 487 | * For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error. 488 | * MurmurHash uses the faster approach only on CPU's where we know it's safe. 489 | * 490 | * Note the preprocessor built-in defines can be emitted using: 491 | * 492 | * gcc -m64 -dM -E - < /dev/null (on gcc) 493 | * cc -## a.c (where a.c is a simple test file) (Sun Studio) 494 | */ 495 | #if (defined(__i386__) || defined(__x86_64__)) 496 | #define MUR_GETBLOCK(p,i) p[i] 497 | #else /* non intel */ 498 | #define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 0x3) == 0) 499 | #define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 0x3) == 1) 500 | #define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 0x3) == 2) 501 | #define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 0x3) == 3) 502 | #define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL)) 503 | #if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__)) 504 | #define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24)) 505 | #define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16)) 506 | #define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8)) 507 | #else /* assume little endian non-intel */ 508 | #define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24)) 509 | #define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16)) 510 | #define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8)) 511 | #endif 512 | #define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \ 513 | (MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \ 514 | (MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \ 515 | MUR_ONE_THREE(p)))) 516 | #endif 517 | #define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r)))) 518 | #define MUR_FMIX(_h) \ 519 | do { \ 520 | _h ^= _h >> 16; \ 521 | _h *= 0x85ebca6b; \ 522 | _h ^= _h >> 13; \ 523 | _h *= 0xc2b2ae35l; \ 524 | _h ^= _h >> 16; \ 525 | } while(0) 526 | 527 | #define HASH_MUR(key,keylen,num_bkts,hashv,bkt) \ 528 | do { \ 529 | const uint8_t *_mur_data = (const uint8_t*)(key); \ 530 | const int _mur_nblocks = (keylen) / 4; \ 531 | uint32_t _mur_h1 = 0xf88D5353; \ 532 | uint32_t _mur_c1 = 0xcc9e2d51; \ 533 | uint32_t _mur_c2 = 0x1b873593; \ 534 | const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+_mur_nblocks*4); \ 535 | int _mur_i; \ 536 | for(_mur_i = -_mur_nblocks; _mur_i; _mur_i++) { \ 537 | uint32_t _mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \ 538 | _mur_k1 *= _mur_c1; \ 539 | _mur_k1 = MUR_ROTL32(_mur_k1,15); \ 540 | _mur_k1 *= _mur_c2; \ 541 | \ 542 | _mur_h1 ^= _mur_k1; \ 543 | _mur_h1 = MUR_ROTL32(_mur_h1,13); \ 544 | _mur_h1 = _mur_h1*5+0xe6546b64; \ 545 | } \ 546 | const uint8_t *_mur_tail = (const uint8_t*)(_mur_data + _mur_nblocks*4); \ 547 | uint32_t _mur_k1=0; \ 548 | switch((keylen) & 3) { \ 549 | case 3: _mur_k1 ^= _mur_tail[2] << 16; \ 550 | case 2: _mur_k1 ^= _mur_tail[1] << 8; \ 551 | case 1: _mur_k1 ^= _mur_tail[0]; \ 552 | _mur_k1 *= _mur_c1; \ 553 | _mur_k1 = MUR_ROTL32(_mur_k1,15); \ 554 | _mur_k1 *= _mur_c2; \ 555 | _mur_h1 ^= _mur_k1; \ 556 | } \ 557 | _mur_h1 ^= (keylen); \ 558 | MUR_FMIX(_mur_h1); \ 559 | hashv = _mur_h1; \ 560 | bkt = hashv & (num_bkts-1); \ 561 | } while(0) 562 | #endif /* HASH_USING_NO_STRICT_ALIASING */ 563 | 564 | /* key comparison function; return 0 if keys equal */ 565 | #define HASH_KEYCMP(a,b,len) memcmp(a,b,len) 566 | 567 | /* iterate over items in a known bucket to find desired item */ 568 | #define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,out) \ 569 | do { \ 570 | if (head.hh_head) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,head.hh_head)); \ 571 | else out=NULL; \ 572 | while (out) { \ 573 | if (out->hh.keylen == keylen_in) { \ 574 | if ((HASH_KEYCMP(out->hh.key,keyptr,keylen_in)) == 0) break; \ 575 | } \ 576 | if (out->hh.hh_next) DECLTYPE_ASSIGN(out,ELMT_FROM_HH(tbl,out->hh.hh_next)); \ 577 | else out = NULL; \ 578 | } \ 579 | } while(0) 580 | 581 | /* add an item to a bucket */ 582 | #define HASH_ADD_TO_BKT(head,addhh) \ 583 | do { \ 584 | head.count++; \ 585 | (addhh)->hh_next = head.hh_head; \ 586 | (addhh)->hh_prev = NULL; \ 587 | if (head.hh_head) { (head).hh_head->hh_prev = (addhh); } \ 588 | (head).hh_head=addhh; \ 589 | if (head.count >= ((head.expand_mult+1) * HASH_BKT_CAPACITY_THRESH) \ 590 | && (addhh)->tbl->noexpand != 1) { \ 591 | HASH_EXPAND_BUCKETS((addhh)->tbl); \ 592 | } \ 593 | } while(0) 594 | 595 | /* remove an item from a given bucket */ 596 | #define HASH_DEL_IN_BKT(hh,head,hh_del) \ 597 | (head).count--; \ 598 | if ((head).hh_head == hh_del) { \ 599 | (head).hh_head = hh_del->hh_next; \ 600 | } \ 601 | if (hh_del->hh_prev) { \ 602 | hh_del->hh_prev->hh_next = hh_del->hh_next; \ 603 | } \ 604 | if (hh_del->hh_next) { \ 605 | hh_del->hh_next->hh_prev = hh_del->hh_prev; \ 606 | } 607 | 608 | /* Bucket expansion has the effect of doubling the number of buckets 609 | * and redistributing the items into the new buckets. Ideally the 610 | * items will distribute more or less evenly into the new buckets 611 | * (the extent to which this is true is a measure of the quality of 612 | * the hash function as it applies to the key domain). 613 | * 614 | * With the items distributed into more buckets, the chain length 615 | * (item count) in each bucket is reduced. Thus by expanding buckets 616 | * the hash keeps a bound on the chain length. This bounded chain 617 | * length is the essence of how a hash provides constant time lookup. 618 | * 619 | * The calculation of tbl->ideal_chain_maxlen below deserves some 620 | * explanation. First, keep in mind that we're calculating the ideal 621 | * maximum chain length based on the *new* (doubled) bucket count. 622 | * In fractions this is just n/b (n=number of items,b=new num buckets). 623 | * Since the ideal chain length is an integer, we want to calculate 624 | * ceil(n/b). We don't depend on floating point arithmetic in this 625 | * hash, so to calculate ceil(n/b) with integers we could write 626 | * 627 | * ceil(n/b) = (n/b) + ((n%b)?1:0) 628 | * 629 | * and in fact a previous version of this hash did just that. 630 | * But now we have improved things a bit by recognizing that b is 631 | * always a power of two. We keep its base 2 log handy (call it lb), 632 | * so now we can write this with a bit shift and logical AND: 633 | * 634 | * ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0) 635 | * 636 | */ 637 | #define HASH_EXPAND_BUCKETS(tbl) \ 638 | do { \ 639 | unsigned _he_bkt; \ 640 | unsigned _he_bkt_i; \ 641 | struct UT_hash_handle *_he_thh, *_he_hh_nxt; \ 642 | UT_hash_bucket *_he_new_buckets, *_he_newbkt; \ 643 | _he_new_buckets = (UT_hash_bucket*)uthash_malloc( \ 644 | 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ 645 | if (!_he_new_buckets) { uthash_fatal( "out of memory"); } \ 646 | memset(_he_new_buckets, 0, \ 647 | 2 * tbl->num_buckets * sizeof(struct UT_hash_bucket)); \ 648 | tbl->ideal_chain_maxlen = \ 649 | (tbl->num_items >> (tbl->log2_num_buckets+1)) + \ 650 | ((tbl->num_items & ((tbl->num_buckets*2)-1)) ? 1 : 0); \ 651 | tbl->nonideal_items = 0; \ 652 | for(_he_bkt_i = 0; _he_bkt_i < tbl->num_buckets; _he_bkt_i++) \ 653 | { \ 654 | _he_thh = tbl->buckets[ _he_bkt_i ].hh_head; \ 655 | while (_he_thh) { \ 656 | _he_hh_nxt = _he_thh->hh_next; \ 657 | HASH_TO_BKT( _he_thh->hashv, tbl->num_buckets*2, _he_bkt); \ 658 | _he_newbkt = &(_he_new_buckets[ _he_bkt ]); \ 659 | if (++(_he_newbkt->count) > tbl->ideal_chain_maxlen) { \ 660 | tbl->nonideal_items++; \ 661 | _he_newbkt->expand_mult = _he_newbkt->count / \ 662 | tbl->ideal_chain_maxlen; \ 663 | } \ 664 | _he_thh->hh_prev = NULL; \ 665 | _he_thh->hh_next = _he_newbkt->hh_head; \ 666 | if (_he_newbkt->hh_head) _he_newbkt->hh_head->hh_prev = \ 667 | _he_thh; \ 668 | _he_newbkt->hh_head = _he_thh; \ 669 | _he_thh = _he_hh_nxt; \ 670 | } \ 671 | } \ 672 | uthash_free( tbl->buckets, tbl->num_buckets*sizeof(struct UT_hash_bucket) ); \ 673 | tbl->num_buckets *= 2; \ 674 | tbl->log2_num_buckets++; \ 675 | tbl->buckets = _he_new_buckets; \ 676 | tbl->ineff_expands = (tbl->nonideal_items > (tbl->num_items >> 1)) ? \ 677 | (tbl->ineff_expands+1) : 0; \ 678 | if (tbl->ineff_expands > 1) { \ 679 | tbl->noexpand=1; \ 680 | uthash_noexpand_fyi(tbl); \ 681 | } \ 682 | uthash_expand_fyi(tbl); \ 683 | } while(0) 684 | 685 | 686 | /* This is an adaptation of Simon Tatham's O(n log(n)) mergesort */ 687 | /* Note that HASH_SORT assumes the hash handle name to be hh. 688 | * HASH_SRT was added to allow the hash handle name to be passed in. */ 689 | #define HASH_SORT(head,cmpfcn) HASH_SRT(hh,head,cmpfcn) 690 | #define HASH_SRT(hh,head,cmpfcn) \ 691 | do { \ 692 | unsigned _hs_i; \ 693 | unsigned _hs_looping,_hs_nmerges,_hs_insize,_hs_psize,_hs_qsize; \ 694 | struct UT_hash_handle *_hs_p, *_hs_q, *_hs_e, *_hs_list, *_hs_tail; \ 695 | if (head) { \ 696 | _hs_insize = 1; \ 697 | _hs_looping = 1; \ 698 | _hs_list = &((head)->hh); \ 699 | while (_hs_looping) { \ 700 | _hs_p = _hs_list; \ 701 | _hs_list = NULL; \ 702 | _hs_tail = NULL; \ 703 | _hs_nmerges = 0; \ 704 | while (_hs_p) { \ 705 | _hs_nmerges++; \ 706 | _hs_q = _hs_p; \ 707 | _hs_psize = 0; \ 708 | for ( _hs_i = 0; _hs_i < _hs_insize; _hs_i++ ) { \ 709 | _hs_psize++; \ 710 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ 711 | ((void*)((char*)(_hs_q->next) + \ 712 | (head)->hh.tbl->hho)) : NULL); \ 713 | if (! (_hs_q) ) break; \ 714 | } \ 715 | _hs_qsize = _hs_insize; \ 716 | while ((_hs_psize > 0) || ((_hs_qsize > 0) && _hs_q )) { \ 717 | if (_hs_psize == 0) { \ 718 | _hs_e = _hs_q; \ 719 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ 720 | ((void*)((char*)(_hs_q->next) + \ 721 | (head)->hh.tbl->hho)) : NULL); \ 722 | _hs_qsize--; \ 723 | } else if ( (_hs_qsize == 0) || !(_hs_q) ) { \ 724 | _hs_e = _hs_p; \ 725 | _hs_p = (UT_hash_handle*)((_hs_p->next) ? \ 726 | ((void*)((char*)(_hs_p->next) + \ 727 | (head)->hh.tbl->hho)) : NULL); \ 728 | _hs_psize--; \ 729 | } else if (( \ 730 | cmpfcn(DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_p)), \ 731 | DECLTYPE(head)(ELMT_FROM_HH((head)->hh.tbl,_hs_q))) \ 732 | ) <= 0) { \ 733 | _hs_e = _hs_p; \ 734 | _hs_p = (UT_hash_handle*)((_hs_p->next) ? \ 735 | ((void*)((char*)(_hs_p->next) + \ 736 | (head)->hh.tbl->hho)) : NULL); \ 737 | _hs_psize--; \ 738 | } else { \ 739 | _hs_e = _hs_q; \ 740 | _hs_q = (UT_hash_handle*)((_hs_q->next) ? \ 741 | ((void*)((char*)(_hs_q->next) + \ 742 | (head)->hh.tbl->hho)) : NULL); \ 743 | _hs_qsize--; \ 744 | } \ 745 | if ( _hs_tail ) { \ 746 | _hs_tail->next = ((_hs_e) ? \ 747 | ELMT_FROM_HH((head)->hh.tbl,_hs_e) : NULL); \ 748 | } else { \ 749 | _hs_list = _hs_e; \ 750 | } \ 751 | _hs_e->prev = ((_hs_tail) ? \ 752 | ELMT_FROM_HH((head)->hh.tbl,_hs_tail) : NULL); \ 753 | _hs_tail = _hs_e; \ 754 | } \ 755 | _hs_p = _hs_q; \ 756 | } \ 757 | _hs_tail->next = NULL; \ 758 | if ( _hs_nmerges <= 1 ) { \ 759 | _hs_looping=0; \ 760 | (head)->hh.tbl->tail = _hs_tail; \ 761 | DECLTYPE_ASSIGN(head,ELMT_FROM_HH((head)->hh.tbl, _hs_list)); \ 762 | } \ 763 | _hs_insize *= 2; \ 764 | } \ 765 | HASH_FSCK(hh,head); \ 766 | } \ 767 | } while (0) 768 | 769 | /* This function selects items from one hash into another hash. 770 | * The end result is that the selected items have dual presence 771 | * in both hashes. There is no copy of the items made; rather 772 | * they are added into the new hash through a secondary hash 773 | * hash handle that must be present in the structure. */ 774 | #define HASH_SELECT(hh_dst, dst, hh_src, src, cond) \ 775 | do { \ 776 | unsigned _src_bkt, _dst_bkt; \ 777 | void *_last_elt=NULL, *_elt; \ 778 | UT_hash_handle *_src_hh, *_dst_hh, *_last_elt_hh=NULL; \ 779 | ptrdiff_t _dst_hho = ((char*)(&(dst)->hh_dst) - (char*)(dst)); \ 780 | if (src) { \ 781 | for(_src_bkt=0; _src_bkt < (src)->hh_src.tbl->num_buckets; _src_bkt++) { \ 782 | for(_src_hh = (src)->hh_src.tbl->buckets[_src_bkt].hh_head; \ 783 | _src_hh; \ 784 | _src_hh = _src_hh->hh_next) { \ 785 | _elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \ 786 | if (cond(_elt)) { \ 787 | _dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \ 788 | _dst_hh->key = _src_hh->key; \ 789 | _dst_hh->keylen = _src_hh->keylen; \ 790 | _dst_hh->hashv = _src_hh->hashv; \ 791 | _dst_hh->prev = _last_elt; \ 792 | _dst_hh->next = NULL; \ 793 | if (_last_elt_hh) { _last_elt_hh->next = _elt; } \ 794 | if (!dst) { \ 795 | DECLTYPE_ASSIGN(dst,_elt); \ 796 | HASH_MAKE_TABLE(hh_dst,dst); \ 797 | } else { \ 798 | _dst_hh->tbl = (dst)->hh_dst.tbl; \ 799 | } \ 800 | HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \ 801 | HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt],_dst_hh); \ 802 | (dst)->hh_dst.tbl->num_items++; \ 803 | _last_elt = _elt; \ 804 | _last_elt_hh = _dst_hh; \ 805 | } \ 806 | } \ 807 | } \ 808 | } \ 809 | HASH_FSCK(hh_dst,dst); \ 810 | } while (0) 811 | 812 | #define HASH_CLEAR(hh,head) \ 813 | do { \ 814 | if (head) { \ 815 | uthash_free((head)->hh.tbl->buckets, \ 816 | (head)->hh.tbl->num_buckets*sizeof(struct UT_hash_bucket)); \ 817 | uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \ 818 | (head)=NULL; \ 819 | } \ 820 | } while(0) 821 | 822 | #ifdef NO_DECLTYPE 823 | #define HASH_ITER(hh,head,el,tmp) \ 824 | for((el)=(head), (*(char**)(&(tmp)))=(char*)((head)?(head)->hh.next:NULL); \ 825 | el; (el)=(tmp),(*(char**)(&(tmp)))=(char*)((tmp)?(tmp)->hh.next:NULL)) 826 | #else 827 | #define HASH_ITER(hh,head,el,tmp) \ 828 | for((el)=(head),(tmp)=DECLTYPE(el)((head)?(head)->hh.next:NULL); \ 829 | el; (el)=(tmp),(tmp)=DECLTYPE(el)((tmp)?(tmp)->hh.next:NULL)) 830 | #endif 831 | 832 | /* obtain a count of items in the hash */ 833 | #define HASH_COUNT(head) HASH_CNT(hh,head) 834 | #define HASH_CNT(hh,head) ((head)?((head)->hh.tbl->num_items):0) 835 | 836 | typedef struct UT_hash_bucket { 837 | struct UT_hash_handle *hh_head; 838 | unsigned count; 839 | 840 | /* expand_mult is normally set to 0. In this situation, the max chain length 841 | * threshold is enforced at its default value, HASH_BKT_CAPACITY_THRESH. (If 842 | * the bucket's chain exceeds this length, bucket expansion is triggered). 843 | * However, setting expand_mult to a non-zero value delays bucket expansion 844 | * (that would be triggered by additions to this particular bucket) 845 | * until its chain length reaches a *multiple* of HASH_BKT_CAPACITY_THRESH. 846 | * (The multiplier is simply expand_mult+1). The whole idea of this 847 | * multiplier is to reduce bucket expansions, since they are expensive, in 848 | * situations where we know that a particular bucket tends to be overused. 849 | * It is better to let its chain length grow to a longer yet-still-bounded 850 | * value, than to do an O(n) bucket expansion too often. 851 | */ 852 | unsigned expand_mult; 853 | 854 | } UT_hash_bucket; 855 | 856 | /* random signature used only to find hash tables in external analysis */ 857 | #define HASH_SIGNATURE 0xa0111fe1 858 | #define HASH_BLOOM_SIGNATURE 0xb12220f2 859 | 860 | typedef struct UT_hash_table { 861 | UT_hash_bucket *buckets; 862 | unsigned num_buckets, log2_num_buckets; 863 | unsigned num_items; 864 | struct UT_hash_handle *tail; /* tail hh in app order, for fast append */ 865 | ptrdiff_t hho; /* hash handle offset (byte pos of hash handle in element */ 866 | 867 | /* in an ideal situation (all buckets used equally), no bucket would have 868 | * more than ceil(#items/#buckets) items. that's the ideal chain length. */ 869 | unsigned ideal_chain_maxlen; 870 | 871 | /* nonideal_items is the number of items in the hash whose chain position 872 | * exceeds the ideal chain maxlen. these items pay the penalty for an uneven 873 | * hash distribution; reaching them in a chain traversal takes >ideal steps */ 874 | unsigned nonideal_items; 875 | 876 | /* ineffective expands occur when a bucket doubling was performed, but 877 | * afterward, more than half the items in the hash had nonideal chain 878 | * positions. If this happens on two consecutive expansions we inhibit any 879 | * further expansion, as it's not helping; this happens when the hash 880 | * function isn't a good fit for the key domain. When expansion is inhibited 881 | * the hash will still work, albeit no longer in constant time. */ 882 | unsigned ineff_expands, noexpand; 883 | 884 | uint32_t signature; /* used only to find hash tables in external analysis */ 885 | #ifdef HASH_BLOOM 886 | uint32_t bloom_sig; /* used only to test bloom exists in external analysis */ 887 | uint8_t *bloom_bv; 888 | char bloom_nbits; 889 | #endif 890 | 891 | } UT_hash_table; 892 | 893 | typedef struct UT_hash_handle { 894 | struct UT_hash_table *tbl; 895 | void *prev; /* prev element in app order */ 896 | void *next; /* next element in app order */ 897 | struct UT_hash_handle *hh_prev; /* previous hh in bucket order */ 898 | struct UT_hash_handle *hh_next; /* next hh in bucket order */ 899 | void *key; /* ptr to enclosing struct's key */ 900 | unsigned keylen; /* enclosing struct's key len */ 901 | unsigned hashv; /* result of hash-fcn(key) */ 902 | } UT_hash_handle; 903 | 904 | #endif /* UTHASH_H */ 905 | --------------------------------------------------------------------------------