static int phase34(struct tile_info *info, struct zip_info *zip_info, FILE **in, FILE **reference, int in_count, int with_range) { int i; processed_nodes=processed_nodes_out=processed_ways=processed_relations=processed_tiles=0; bytes_read=0; sig_alrm(0); if (! info->write) tile_hash=g_hash_table_new(g_str_hash, g_str_equal); for (i = 0 ; i < in_count ; i++) { if (in[i]) { if (with_range) phase34_process_file_range(info, in[i], reference ? reference[i]:NULL); else phase34_process_file(info, in[i], reference ? reference[i]:NULL); } } if (! info->write) merge_tiles(info); sig_alrm(0); sig_alrm_end(); write_tilesdir(info, zip_info, info->tilesdir_out); return 0; }
int map_collect_data_osm(FILE *in, struct maptool_osm *osm) { int size=BUFFER_SIZE; char buffer[BUFFER_SIZE]; char *p; sig_alrm(0); if (!fgets(buffer, size, in) || !xml_declaration_in_line(buffer)){ fprintf(stderr,"FATAL: First line does not start with XML declaration;\n" "this does not look like a valid OSM file.\n"); exit(EXIT_FAILURE); } while (fgets(buffer, size, in)) { p=strchr(buffer,'<'); if (! p) { fprintf(stderr,"FATAL: wrong line in input data (does not start with '<'): %s\n", buffer); fprintf(stderr,"This does not look like a valid OSM file.\n" "Note that maptool can only process OSM files without wrapped or empty lines.\n"); exit(EXIT_FAILURE); } if (!strncmp(p, "<osm ",5)) { } else if (!strncmp(p, "<bound ",7)) { } else if (!strncmp(p, "<node ",6)) { if (!parse_node(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); processed_nodes++; } else if (!strncmp(p, "<tag ",5)) { if (!parse_tag(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); } else if (!strncmp(p, "<way ",5)) { if (!parse_way(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); processed_ways++; } else if (!strncmp(p, "<nd ",4)) { if (!parse_nd(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); } else if (!strncmp(p, "<relation ",10)) { if (!parse_relation(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); processed_relations++; } else if (!strncmp(p, "<member ",8)) { if (!parse_member(p)) fprintf(stderr,"WARNING: failed to parse %s\n", buffer); } else if (!strncmp(p, "</node>",7)) { osm_end_node(osm); } else if (!strncmp(p, "</way>",6)) { osm_end_way(osm); } else if (!strncmp(p, "</relation>",11)) { osm_end_relation(osm); } else if (!strncmp(p, "</osm>",6)) { } else { fprintf(stderr,"WARNING: unknown tag in %s\n", buffer); } } sig_alrm(0); sig_alrm_end(); return 1; }
int zip_write_directory(struct zip_info *info) { struct zip_eoc eoc = { 0x06054b50, 0x0000, 0x0000, 0x0000, 0x0000, 0x0, 0x0, 0x0, }; fseek(info->dir, 0, SEEK_SET); cat(info->dir, info->res); eoc.zipenum=info->zipnum; eoc.zipecenn=info->zipnum; eoc.zipecsz=info->dir_size; eoc.zipeofst=info->offset; fwrite(&eoc, sizeof(eoc), 1, info->res); sig_alrm(0); #ifndef _WIN32 alarm(0); #endif return 0; }
void readloop(void) { int size; char recvbuf[BUFSIZE]; socklen_t len; ssize_t n; struct timeval tval; sockfd = Socket(pr->sasend->sa_family, SOCK_RAW, pr->icmpproto); setuid(getuid()); /* don't need special permissions any more */ size = 60 * 1024; /* OK if setsockopt fails */ setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)); sig_alrm(SIGALRM); /* send first packet */ for ( ; ; ) { len = pr->salen; n = recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, pr->sarecv, &len); if (n < 0) { if (errno == EINTR) continue; else err_sys("recvfrom error"); } Gettimeofday(&tval, NULL); (*pr->fproc)(recvbuf, n, &tval); } }
int zip_write_directory(struct zip_info *info) { struct zip_eoc eoc = { 0x06054b50, 0x0000, 0x0000, 0x0000, 0x0000, 0x0, 0x0, 0x0, }; struct zip64_eoc eoc64 = { 0x06064b50, 0x0, 0x0, 0x0403, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, }; struct zip64_eocl eocl = { 0x07064b50, 0x0, 0x0, 0x0, }; fseek(info->dir, 0, SEEK_SET); zip_write_file_data(info, info->dir); if (info->zip64) { eoc64.zip64esize=sizeof(eoc64)-12; eoc64.zip64enum=info->zipnum; eoc64.zip64ecenn=info->zipnum; eoc64.zip64ecsz=info->dir_size; eoc64.zip64eofst=info->offset; zip_write(info, &eoc64, sizeof(eoc64)); eocl.zip64lofst=info->offset+info->dir_size; zip_write(info, &eocl, sizeof(eocl)); } eoc.zipenum=info->zipnum; eoc.zipecenn=info->zipnum; eoc.zipecsz=info->dir_size; eoc.zipeofst=info->offset; zip_write(info, &eoc, sizeof(eoc)); sig_alrm(0); #ifndef _WIN32 alarm(0); #endif return 0; }
int map_collect_data_osm_db(char *dbstr, struct maptool_osm *osm) { PGconn *conn; PGresult *res,*node,*way,*tag; int count,tagged,i,j,k; long min, max, id, tag_id, node_id; char query[256]; sig_alrm(0); conn=PQconnectdb(dbstr); if (! conn) { fprintf(stderr,"Failed to connect to database with '%s'\n",dbstr); exit(1); } res=PQexec(conn, "begin"); if (! res) { fprintf(stderr, "Cannot begin transaction: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "set transaction isolation level serializable"); if (! res) { fprintf(stderr, "Cannot set isolation level: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "declare node cursor for select id,x(coordinate),y(coordinate) from node order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "declare way cursor for select id from way order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } res=PQexec(conn, "declare relation cursor for select id from relation order by id"); if (! res) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } for (;;) { node=PQexec(conn, "fetch 100000 from node"); if (! node) { fprintf(stderr, "Cannot setup cursor for nodes: %s\n", PQerrorMessage(conn)); PQclear(node); exit(1); } count=PQntuples(node); if (! count) break; min=atol(PQgetvalue(node, 0, 0)); max=atol(PQgetvalue(node, count-1, 0)); sprintf(query,"select node_id,name,value from node_tag where node_id >= %ld and node_id <= %ld order by node_id", min, max); tag=PQexec(conn, query); if (! tag) { fprintf(stderr, "Cannot query node_tag: %s\n", PQerrorMessage(conn)); exit(1); } j=0; for (i = 0 ; i < count ; i++) { id=atol(PQgetvalue(node, i, 0)); osm_add_node(id, atof(PQgetvalue(node, i, 1)), atof(PQgetvalue(node, i, 2))); tagged=0; processed_nodes++; while (j < PQntuples(tag)) { tag_id=atol(PQgetvalue(tag, j, 0)); if (tag_id == id) { osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2)); tagged=1; j++; } if (tag_id < id) j++; if (tag_id > id) break; } osm_end_node(osm); } PQclear(tag); PQclear(node); } for (;;) { way=PQexec(conn, "fetch 100000 from way"); if (! way) { fprintf(stderr, "Cannot setup cursor for ways: %s\n", PQerrorMessage(conn)); PQclear(node); exit(1); } count=PQntuples(way); if (! count) break; min=atol(PQgetvalue(way, 0, 0)); max=atol(PQgetvalue(way, count-1, 0)); sprintf(query,"select way_id,node_id from way_node where way_id >= %ld and way_id <= %ld order by way_id,sequence_id", min, max); node=PQexec(conn, query); if (! node) { fprintf(stderr, "Cannot query way_node: %s\n", PQerrorMessage(conn)); exit(1); } sprintf(query,"select way_id,name,value from way_tag where way_id >= %ld and way_id <= %ld order by way_id", min, max); tag=PQexec(conn, query); if (! tag) { fprintf(stderr, "Cannot query way_tag: %s\n", PQerrorMessage(conn)); exit(1); } j=0; k=0; for (i = 0 ; i < count ; i++) { id=atol(PQgetvalue(way, i, 0)); osm_add_way(id); tagged=0; processed_ways++; while (k < PQntuples(node)) { node_id=atol(PQgetvalue(node, k, 0)); if (node_id == id) { osm_add_nd(atoll(PQgetvalue(node, k, 1))); tagged=1; k++; } if (node_id < id) k++; if (node_id > id) break; } while (j < PQntuples(tag)) { tag_id=atol(PQgetvalue(tag, j, 0)); if (tag_id == id) { osm_add_tag(PQgetvalue(tag, j, 1), PQgetvalue(tag, j, 2)); tagged=1; j++; } if (tag_id < id) j++; if (tag_id > id) break; } if (tagged) osm_end_way(osm); } PQclear(tag); PQclear(node); PQclear(way); } res=PQexec(conn, "commit"); if (! res) { fprintf(stderr, "Cannot commit transaction: %s\n", PQerrorMessage(conn)); PQclear(res); exit(1); } sig_alrm(0); sig_alrm_end(); return 1; }
int main(int argc, char **argv){ const int on = 1; unsigned char ttl = 1; time_t rawtime; time_t lasttime; int maxfd; fd_set rset; ssize_t how_much_read; char msg[MAXLINE]; rt = Socket(AF_INET, SOCK_RAW, MY_IP_PROTO); Setsockopt(rt, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)); pg = Socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); request_sock = Socket(PF_PACKET, SOCK_RAW, htons(ETH_P_IP)); multicast_sock = Socket(AF_INET, SOCK_DGRAM, 0); multicast_recv = Socket(AF_INET, SOCK_DGRAM, 0); Mcast_set_loop(multicast_sock,0); Setsockopt(multicast_sock, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); Setsockopt(multicast_recv, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); gethostname(our_hostname, sizeof(our_hostname)); //printf("My our_hostname: %s\n", our_hostname); get_eth0_stuff(); if(argc > 1) { //DEBUG("%s is the start of the tour!\n",our_hostname); int i = 1; tour tour_pkt; // separate func bzero(&tour_pkt, sizeof(tour_pkt)); fill_buff_with_ip_of_hostname(our_hostname); //printf("fill buff ok\n"); inet_aton(ip_static_buff, &(tour_pkt.payload[0])); join_mcast(MULTICAST_ADDRESS, MULTICAST_PORT); #if 0 // tokenize by ' ' and send each char* to gethostbyname char *token, *prev_token; const char s[2] = " "; /* get the first token */ token = strtok(argv[1], s); /* walk through other tokens */ while(token != NULL) { DEBUG("token = %s\n", token ); fill_buff_with_ip_of_hostname(token); inet_aton(ip_static_buff, &tour_pkt.payload[i]); prev_token = token; token = strtok(NULL, s); if(!strcmp(prev_token, token)){ perror("The same node should not appear consequentively in the tour list – i.e., the next node on the tour cannot be the current node itself"); exit(0); } i++; } // separate func #endif while(i< argc) { //printf("argv[%d] is %s\n",i, argv[i]); fill_buff_with_ip_of_hostname(argv[i]); //printf("fill buff ok\n"); inet_aton(ip_static_buff, &(tour_pkt.payload[i])); i++; } tour_pkt.index = 0; // We're the origin tour_pkt.total = i-1; //printf("tour_pkt total %d\n", tour_pkt.total); send_raw_tour_packet(&tour_pkt); }else{ //DEBUG("We're NOT the start of the tour!"); #if 0 while(1) { int len = 0; len = Recv(rt, msg, sizeof(msg), 0); printf("received len %d\n",len); int j = 0; for(;j<len; j++) { printf("%.2x ", msg[j]); if((i-9) % 10 == 0) printf("\n"); fflush(stdout); } } #endif } #if 0 fill_buff_with_ip_of_hostname("vm2"); inet_aton(ip_static_buff, (struct in_addr *)&dest_of_echo_req); printf("destofechoreq %x\n", dest_of_echo_req.s_addr); struct sockaddr_in IPaddr; struct hwaddr HWaddr; bzero(&IPaddr, sizeof(IPaddr)); bzero(&HWaddr, sizeof(HWaddr)); IPaddr.sin_addr = dest_of_echo_req; //Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr); //printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",HWaddr.sll_addr[0],HWaddr.sll_addr[1],HWaddr.sll_addr[2], // HWaddr.sll_addr[3],HWaddr.sll_addr[4],HWaddr.sll_addr[5]); void sig_alrm(int signo); if(argc > 1){ Signal(SIGALRM, sig_alrm); sig_alrm(SIGALRM); } //send_icmp_request(HWaddr.sll_addr); int n = 0; struct timeval tval; while(1) { n = recv(pg, msg, sizeof(msg), 0); if(n<0) { if (errno == EINTR) continue; } printf("receive ping packet\n"); //int j = 0; //for (; j < n; j++) //{ // printf("%.2x ", msg[j]); // if((j-9) % 10 == 0) printf("\n"); // fflush(stdout); //} Gettimeofday(&tval, NULL); proc_v4(msg,n,NULL, &tval); } #endif #if 1 //act_open.sa_flags = 0; //sigemptyset(&act_open.sa_mask); //sigaddset(&act_open.sa_mask, SIGALRM); //act_open.sa_handler = terminate; Signal(SIGALRM, sig_alrm); FD_ZERO(&rset); maxfd = max(pg, multicast_recv); maxfd = max(rt, maxfd); while(1){ FD_SET(multicast_recv, &rset); FD_SET(rt, &rset); FD_SET(pg, &rset); if(select(maxfd+1, &rset, NULL, NULL, NULL)<0) { if(errno == EINTR) { continue; } } if(FD_ISSET(rt, &rset)){ //DEBUG("possibly totally wrong here\n"); //DEBUG("possibly totally wrong here\n"); //DEBUG("possibly totally wrong here\n"); alarm(0); bzero(msg, sizeof(msg)); how_much_read = Recv(rt, msg, sizeof(msg), 0); //assert(how_much_read == sizeof(sizeof(my_ip_header)+sizeof(tour))); //printf("rt receive pkt len %d\n", how_much_read); my_ip_header *head = (my_ip_header *)msg; tour *tour_pkt = (tour *)(msg+sizeof(my_ip_header)); if(ntohs(head->id) != MY_IP_ID){ //DEBUG("head->id != MY_IP_ID\n"); continue; } char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&head->saddr)); time(&rawtime); printf("<%s> recieved source routing packet from <%s>\n", ctime (&rawtime), name); if(!already_here){ already_here = 1; // IP_MULTICAST_TTL - If not otherwise specified, has a default value of 1. join_mcast(inet_ntoa(tour_pkt->multicast_addr), ntohs(tour_pkt->multicast_port)); stop_pinging = 0; } // start icmp echo reqs for(i = 0; i < prev_sender_list.index; i++) { if(!memcmp(&prev_sender_list.previous_senders[i], &tour_pkt->payload[(tour_pkt->index)], sizeof(struct in_addr))) we_dont_need_to_send_echo_req = 1; else{ we_dont_need_to_send_echo_req = 0; break; } } if(!we_dont_need_to_send_echo_req){ //DEBUG("We Need To Send Echo Request\n"); prev_sender_list.previous_senders[prev_sender_list.index] = tour_pkt->payload[(tour_pkt->index)]; prev_sender_list.index++; dest_of_echo_req = tour_pkt->payload[(tour_pkt->index)]; char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)])); printf("PING %s (%s): %d data bytes\n",name,inet_ntoa(dest_of_echo_req), icmpdata); sig_alrm(SIGALRM); } else { char *name = find_vm_name(inet_ntoa(*(struct in_addr *)&tour_pkt->payload[(tour_pkt->index)])); printf("We have ping %s before\n",name); } // INCREMENTING THE INDEX tour_pkt->index = (tour_pkt->index)+1; if((tour_pkt->index) == MAX_VISITS){ perror("MAX_VISITS has been reached."); exit(0); } // Now blank //bzero(&for_comparisons, sizeof(for_comparisons)); // ghiTODO this may not work ghiTODO //if(!memcmp(&tour_pkt->payload[ntohl(tour_pkt->index)], &for_comparisons, sizeof(for_comparisons))){ //printf("rt current pkt index %d, total index %d\n",tour_pkt->index, tour_pkt->total); if(tour_pkt->index == tour_pkt->total) { //printf("We're the final destination.\n"); // ping the saddr final_destination = 1; char buffer[100]; snprintf(buffer, 100, "<<<<< This is node %s. Tour has ended. Group members please identify yourselves. >>>>>",our_hostname); printf("Node %s. Sending: <%s>. ",our_hostname, buffer); send_multicast_message(buffer,strlen(buffer)+1); //printf("send mulitcast msg ok\n"); }else{ //printf("We're NOT the final destination, keep touring.\n"); // pass it along send_raw_tour_packet(tour_pkt); } }else if(FD_ISSET(multicast_recv, &rset)){ //DEBUG("Receiving multicast message.\n"); printf(""); bzero(msg, sizeof(msg)); how_much_read = Recv(multicast_recv, msg, sizeof(msg), 0); //printf("multicast msg len %d\n", how_much_read); msg[how_much_read]='\0'; if(!first_term){ printf("Node %s. Received <%s>. ",our_hostname,msg); // then should immediately stop ping //DEBUG("how_much_read is %lu\n", how_much_read); stop_pinging = 1; alarm(0); bzero(msg, sizeof(msg)); snprintf(msg, sizeof(msg), "<<<<< Node %s I am a member of the group. >>>>>", our_hostname); printf("Node %s. Sending <%s>. ", our_hostname, msg); fflush(stdout); first_term = 1; send_multicast_message(msg,strlen(msg)+1); } else { Signal(SIGALRM, terminate); alarm(5); printf(""); printf("Node %s. Received <%s>. ",our_hostname,msg); fflush(stdout); } //send_multicast_message(msg); }else if(FD_ISSET(pg, &rset)){ //DEBUG("Receiving echo reply message.\n"); struct timeval tval; bzero(msg, sizeof(msg)); how_much_read = Recv(pg, msg, sizeof(msg), 0); Gettimeofday(&tval, NULL); proc_v4(msg,how_much_read,NULL, &tval); //assert(how_much_read == sizeof(sizeof(my_eth_header)+sizeof(my_ip_header)+sizeof(my_icmp_header))); //DEBUG("how_much_read is %lu\n", how_much_read); //DEBUG("Node %s recieved %s\n", our_hostname, msg); // my_eth_header *eh = (my_eth_header *)msg; // my_ip_header *iph = (my_ip_header *)msg+sizeof(my_eth_header); // my_icmp_header *icmph = (my_icmp_header *)msg+sizeof(my_eth_header)+sizeof(my_ip_header); //my_ip_header *iph = (my_ip_header *)(msg+sizeof(my_eth_header)); //if(!memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))){ // DEBUG("ghiTODO\n"); // count_of_replies++; // DEBUG("ghiTODO\n"); //DEBUG("ghiTODO\n"); //DEBUG("ghiTODO\n"); // if(final_destination && count_of_replies >= 5){ // stop_pinging = 1; // bzero(msg, sizeof(msg)); /// snprintf(msg, sizeof(msg), "<<<<< This is node %s Tour has ended. Group members please identify yourselves.>>>>>", our_hostname); // printf("Node %s sending %s", our_hostname, msg); // send_multicast_message(msg, strlen(msg)+1); // alarm(5); // } //}else{ //DEBUG("The following is FALSE !memcmp(&iph->saddr, &dest_of_echo_req, sizeof(dest_of_echo_req))"); //} } #if 0 time(&rawtime); if(lasttime+1 <= rawtime){ if(!stop_pinging){ struct sockaddr_in IPaddr; struct hwaddr HWaddr; bzero(&IPaddr, sizeof(IPaddr)); bzero(&HWaddr, sizeof(HWaddr)); IPaddr.sin_addr = dest_of_echo_req; Areq((struct sockaddr *)&IPaddr, sizeof(struct sockaddr_in), &HWaddr); send_raw_echo_request_message(HWaddr.sll_addr); } }else{ lasttime = rawtime; } #endif } #endif }
void traceloop(void) { int seq, code, done; double rtt; struct rec *rec; struct timeval tvrecv; recvfd = Socket(pr->sasend->sa_family, SOCK_RAW, pr->icmpproto); setuid(getuid()); /* don't need special permissions any more */ sendfd = Socket(pr->sasend->sa_family, SOCK_DGRAM, 0); pr->sabind->sa_family = pr->sasend->sa_family; sport = (getpid() & 0xffff) | 0x8000; /* our source UDP port# */ sock_set_port(pr->sabind, pr->salen, htons(sport)); Bind(sendfd, pr->sabind, pr->salen); sig_alrm(SIGALRM); seq = 0; done = 0; for (ttl = 1; ttl <= max_ttl && done == 0; ttl++) { Setsockopt(sendfd, pr->ttllevel, pr->ttloptname, &ttl, sizeof(int)); bzero(pr->salast, pr->salen); printf("%2d ", ttl); fflush(stdout); for (probe = 0; probe < nprobes; probe++) { rec = (struct rec *) sendbuf; rec->rec_seq = ++seq; rec->rec_ttl = ttl; Gettimeofday(&rec->rec_tv, NULL); sock_set_port(pr->sasend, pr->salen, htons(dport + seq)); Sendto(sendfd, sendbuf, datalen, 0, pr->sasend, pr->salen); if ( (code = (*pr->recv)(seq, &tvrecv)) == -3) printf(" *"); /* timeout, no reply */ else { char str[NI_MAXHOST]; if (sock_cmp_addr(pr->sarecv, pr->salast, pr->salen) != 0) { if (getnameinfo(pr->sarecv, pr->salen, str, sizeof(str), NULL, 0, 0) == 0) printf(" %s (%s)", str, Sock_ntop_host(pr->sarecv, pr->salen)); else printf(" %s", Sock_ntop_host(pr->sarecv, pr->salen)); memcpy(pr->salast, pr->sarecv, pr->salen); } tv_sub(&tvrecv, &rec->rec_tv); rtt = tvrecv.tv_sec * 1000.0 + tvrecv.tv_usec / 1000.0; printf(" %.3f ms", rtt); if (code == -1) /* port unreachable; at destination */ done++; else if (code >= 0) printf(" (ICMP %s)", (*pr->icmpcode)(code)); } fflush(stdout); } printf("\n"); } }