void loop () { if (Serial.available()) handleInput(Serial.read()); if (tempTimer.poll()) { sendTempPacket(); tempTimer.set(TEMP_INTERVAL); } unsigned long now = millis(); cli(); bool needSendPulse = false; if (pulseActivity && (lastPulsePacket + PULSE_INTERVAL < now)) { pulseActivity = false; lastPulsePacket = now; needSendPulse = true; } sei(); if (needSendPulse) { sendPulsePacket(); } if (!rf12_recvDone() || rf12_crc != 0) return; // nothing to do digitalWrite(PIN_LED, HIGH); forwardPacket(); digitalWrite(PIN_LED, LOW); }
/*----------------------------------------------------------------------------- * Method: void checkCachedPackets(struct sr_instance* sr, int cachedArp) * * searches our packet cache for a matching ip in arpCache[cachedArp]. if we * find a match, we forward the packet. if we do not find a match we need an * arp cache entry for it. make sure we have been waiting at least 3 seconds * before we send the src icmp unreachable packets. this does not drop the * packet, it just looks every 3rd second to see if we have a response. every * time we look, we try to increment the arp counter *---------------------------------------------------------------------------*/ void checkCachedPackets(struct sr_instance* sr, int cachedArp) { int i, arpMatch; for (i = 0; i < PACKET_CACHE_SIZE; i++) { if (packetCache[i].len > 0) { // if we have a packet waiting if (packetCache[i].arps <= 5) { // and we have not sent 5 arps for this packet yet if ((arpMatch = arpSearchCache(packetCache[i].tip)) > -1) { // and we have an arp match for our packet's next hop forwardPacket(sr, (uint8_t*)&packetCache[i].packet, packetCache[i].len, // send it along packetCache[i].nexthop->interface, arpReturnEntryMac(arpMatch)); packetCache[i].len = 0; } else { /* wait three seconds between each arp request */ if ((int)(difftime(time(NULL), packetCache[i].timeCached))%3 < 1) { arpSendRequest(sr, sr_get_interface(sr, packetCache[i].nexthop->interface), packetCache[i].nexthop->gw.s_addr); packetCache[i].arps++; } } } else { /* then */ icmpSendUnreachable(sr, (uint8_t*)&packetCache[i].packet, packetCache[i].len, packetCache[i].nexthop->interface, ICMP_HOST_UNREACHABLE); packetCache[i].len = 0; } } } }
// // bool forwardPackets() // Last modified: 28Aug2006 // // Attempts to forward all packets to their destinations, // returning true if successful, false otherwise. // // Returns: true if successful, false otherwise // Parameters: <none> // bool Environment::forwardPackets() { Packet p; while (msgQueue.dequeue(p)) if (!forwardPacket(p)) return false; return true; } // forwardPackets()
// // bool sendPacket(p) // Last modified: 07Sep2006 // // Attempts to send a packet to its destination, // returning true if successful, false otherwise. // // Returns: true if successful, false otherwise // Parameters: // p in/out the packet being sent // bool Environment::sendPacket(const Packet &p) { // discrete message passing //if (msgQueue.enqueue(p)) return true; // continuous message passing if (forwardPacket(p)) return true; delete p.msg; return false; } // sendPacket(const Packet &)
/*----------------------------------------------------------------------------- * Method: void handleForward * * determines what interface to send a packet out of *---------------------------------------------------------------------------*/ void handleForward( struct sr_instance* sr, uint8_t* packet, unsigned int len, char* interface ) { struct ip* ipHdr = (struct ip*)(packet+14); struct sr_rt* rtptr = sr->routing_table; int cachedEntry; /* loop through rtable for the next hop with the packet's destination ip */ while (rtptr) { if (rtptr->dest.s_addr == ipHdr->ip_dst.s_addr) break; else rtptr = rtptr->next; } /* if not in our rtable, send it out eth0 to the intarwebz */ if (!rtptr) { rtptr = sr->routing_table; // eth0 if ((cachedEntry = arpSearchCache(rtptr->gw.s_addr)) > -1) { forwardPacket(sr, packet, len, rtptr->interface, arpReturnEntryMac(cachedEntry)); } else { cachePacket(sr, packet, len, rtptr); } } /* look through arp cache for mac matching the ip destination. if we have it, * forward our packet. otherwise, cache the packet and wait for an arp reply * to tell us the correct mac address */ if ((cachedEntry = arpSearchCache(rtptr->gw.s_addr)) > -1) { forwardPacket(sr, packet, len, rtptr->interface, arpReturnEntryMac(cachedEntry)); } else { cachePacket(sr, packet, len, rtptr); } }
void sendTempPacket() { digitalWrite(PIN_LED, HIGH); #define N 512 #define PIN_TEMP1 2 #define PIN_TEMP2 3 long t1 = 0; long t2 = 0; Serial.print("["); for (int i = 0; i < N; i++) { if (rf12_recvDone() && rf12_crc == 0) { forwardPacket(); } t1 += analogRead(PIN_TEMP1); t2 += analogRead(PIN_TEMP2); } Serial.print(t1 / N * 3300 / 1024); Serial.print(" "); Serial.print(t2 / N * 3300 / 1024); Serial.print(" "); //long t = ((l2 - l1) * 3300) / 4096; long t = (t2 - t1) * 3300 * 10 / N / 1024; Serial.print(t); rf12_initialize(config.out_node, code2type(config.freq), config.out_group); Serial.print(" *"); payload[4] = 1; *((int*)(payload + 5)) = (int) t; // send our packet, once possible while (!rf12_canSend()) rf12_recvDone(); Serial.print(" *"); rf12_sendStart(0, payload, sizeof payload, 1); Serial.println("]"); digitalWrite(PIN_LED, LOW); }
int main(int argc, char *argv[]) { int sockfd[NUMROUTERS]; /* socket */ int clientlen; /* byte size of client's address */ struct sockaddr_in serveraddr[NUMROUTERS]; /* server's address */ struct sockaddr_in clientaddr; /* client's address */ struct hostent *hostp; /* client host info */ int buf[BUFSIZE]; /* message buffer */ char *hostaddrp; /* dotted decimal host address string */ int optval; /* flag value for setsockopt */ int n; /* message byte size */ int count; fd_set socks; bool stableState = false; int nKills = 0; char * filepath = "sample.txt"; struct timeval tv; tv.tv_sec = 0; tv.tv_usec = 50000; int killedRouters[NUMROUTERS] = {0}; /* for testing */ struct router tableA, tableB, tableC, tableD, tableE, tableF; /* struct router tableA = { INDEXA, { 'A', NULL, NULL, NULL, NULL, NULL }, { 0, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX }, { ROUTERA, NULL, NULL, NULL, NULL, NULL }, { ROUTERA, NULL, NULL, NULL, NULL, NULL } }; */ /* struct router tableA = { INDEXA, { 'A', NULL, NULL, NULL, NULL, NULL }, { 0, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX }, { ROUTERA, NULL, NULL, NULL, NULL, NULL }, { ROUTERA, NULL, NULL, NULL, NULL, NULL } }; struct router tableB = { INDEXB, { NULL, 'B', NULL, NULL, NULL, NULL }, { INT_MAX, 0, INT_MAX, INT_MAX, INT_MAX, INT_MAX }, { NULL, ROUTERB, NULL, NULL, NULL, NULL }, { NULL, ROUTERB, NULL, NULL, NULL, NULL } }; struct router tableC = { INDEXC, { NULL, NULL, 'C', NULL, NULL, NULL }, { INT_MAX, INT_MAX, 0, INT_MAX, INT_MAX, INT_MAX }, { NULL, NULL, ROUTERC, NULL, NULL, NULL }, { NULL, NULL, ROUTERC, NULL, NULL, NULL } }; struct router tableD = { INDEXD, { NULL, NULL, NULL, 'D', NULL, NULL }, { INT_MAX, INT_MAX, INT_MAX, 0, INT_MAX, INT_MAX }, { NULL, NULL, NULL, ROUTERD, NULL, NULL }, { NULL, NULL, NULL, ROUTERD, NULL, NULL } }; struct router tableE = { INDEXE, { NULL, NULL, NULL, NULL, 'E', NULL }, { INT_MAX, INT_MAX, INT_MAX, INT_MAX, 0, INT_MAX }, { NULL, NULL, NULL, NULL, ROUTERE, NULL }, { NULL, NULL, NULL, NULL, ROUTERE, NULL } }; struct router tableF = { INDEXF, { NULL, NULL, NULL, NULL, NULL, 'F' }, { INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, 0 }, { NULL, NULL, NULL, NULL, NULL, ROUTERF }, { NULL, NULL, NULL, NULL, NULL, ROUTERF } }; */ struct router **network = malloc(NUMROUTERS * sizeof(struct router*)); int i; for (i=0; i<NUMROUTERS; i++) { network[i] = malloc(NUMROUTERS * sizeof(struct router)); } network[0] = &tableA; network[1] = &tableB; network[2] = &tableC; network[3] = &tableD; network[4] = &tableE; network[5] = &tableF; /* for(i=0;i < NUMROUTERS; i++) { printRouter(network[i]); } */ re_initialize: /* if (nKills > 0) { printf("\nenter\n"); } */ reinitializeTables(&tableA, &tableB, &tableC, &tableD, &tableE, &tableF); /* printf("***\n***AFTER REINIT:***\n***"); for(i=0;i < NUMROUTERS; i++) { printRouter(network[i]); } */ struct matrix neighborMatrix = initializeFromFile(&tableA, &tableB, &tableC, &tableD, &tableE, &tableF, killedRouters); /* end testing */ /* printf("***\n***SECOND PRINT:***\n***"); for(i=0;i < NUMROUTERS; i++) { printRouter(network[i]); } int g; for (i=0; i < NUMROUTERS; i++) { for (g=0; g < NUMROUTERS; g++) printf("%d\t", neighborMatrix.r[i][g]); printf("\n"); } */ initializeOutputFiles(network); for (i=0; i<NUMROUTERS; i++) { /* create parent socket */ if ( (sockfd[i] = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) error("Error opening socket"); /* server can be rerun immediately after killed */ optval = 1; setsockopt(sockfd[i], SOL_SOCKET, SO_REUSEADDR, (const void *)&optval, sizeof(int)); setsockopt(sockfd[i], SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); /* build server's Internet address */ bzero((char *) &serveraddr[i], sizeof(serveraddr[i])); serveraddr[i].sin_family = AF_INET; serveraddr[i].sin_addr.s_addr = htonl(INADDR_ANY); switch(i+10000) { case ROUTERA: serveraddr[i].sin_port = htons(ROUTERA); break; case ROUTERB: serveraddr[i].sin_port = htons(ROUTERB); break; case ROUTERC: serveraddr[i].sin_port = htons(ROUTERC); break; case ROUTERD: serveraddr[i].sin_port = htons(ROUTERD); break; case ROUTERE: serveraddr[i].sin_port = htons(ROUTERE); break; case ROUTERF: serveraddr[i].sin_port = htons(ROUTERF); break; } /* bind: associate parent socket with port */ if (bind(sockfd[i], (struct sockaddr *) &serveraddr[i], sizeof(serveraddr[i])) < 0) error("Error on binding"); } clientlen = sizeof(clientaddr); int serverlen = sizeof(serveraddr[0]); int start; /* begin by having ROUTERA send DV to one neighbor */ /* for this implementation, ROUTERA will send to ROUTERB */ struct router* starter; switch (argv[1][0]) { case 'A': starter = &tableA; start = INDEXA; break; case 'B': starter = &tableB; start = INDEXB; break; case 'C': starter = &tableC; start = INDEXC; break; case 'D': starter = &tableD; start = INDEXD; break; case 'E': starter = &tableE; start = INDEXE; break; case 'F': starter = &tableF; start = INDEXF; break; } //exit(0); /* for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[0][i] != -1) { start = neighborMatrix.r[0][i]; break; } } */ // start is 0 thru 5 //in this case, start=1 //printRouter(starter); memset(buf, 0, BUFSIZE); tableToBuffer(starter, &buf); // tableToBuffer(&tableA, &buf); // printf("Starting router: %d %c\n", start - 'A', start); // n = sendto(sockfd[start - 'A'], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[start], clientlen); n = sendto(sockfd[start], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[start], clientlen); int nsocks = max(sockfd[0], sockfd[1]); for (i=2; i<NUMROUTERS; i++) { nsocks = max(nsocks, sockfd[i]); } int counterdd = 0; count = 0; printf("Stabilizing network..."); /* loop: wait for datagram, then echo it */ while (1) { FD_ZERO(&socks); for (i=0; i<NUMROUTERS; i++) { FD_SET(sockfd[i], &socks); } if (select(nsocks+1, &socks, NULL, NULL, NULL) < 0) { printf("Error selecting socket\n"); } else { /* receives UDP datagram from client */ memset(buf, 0, BUFSIZE); if (FD_ISSET(sockfd[0], &socks)) { if ( (n = recvfrom(sockfd[0], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) error("Error receiving datagram from client\n"); struct router compTable; bufferToTable(buf, &compTable); if (updateTable(&tableA, compTable) == false) count++; else count = 0; tableToBuffer(&tableA, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[0][i] != -1) { n = sendto(sockfd[0], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[0][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (FD_ISSET(sockfd[1], &socks)) { if ( (n = recvfrom(sockfd[1], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) { error("Error receiving datagram from client\n"); printf("error\n"); } struct router compTable; bufferToTable(&buf, &compTable); if (updateTable(&tableB, compTable) == false) count++; else count = 0; tableToBuffer(&tableB, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[1][i] != -1) { n = sendto(sockfd[1], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[1][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (FD_ISSET(sockfd[2], &socks)) { if ( (n = recvfrom(sockfd[2], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) error("Error receiving datagram from client\n"); struct router compTable; bufferToTable(&buf, &compTable); if(updateTable(&tableC, compTable) == false) count++; else count = 0; tableToBuffer(&tableC, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[2][i] != -1) { n = sendto(sockfd[2], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[2][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (FD_ISSET(sockfd[3], &socks)) { if ( (n = recvfrom(sockfd[3], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) error("Error receiving datagram from client\n"); struct router compTable; bufferToTable(&buf, &compTable); if(updateTable(&tableD, compTable) == false) count++; else count = 0; tableToBuffer(&tableD, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[3][i] != -1) { n = sendto(sockfd[3], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[3][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (FD_ISSET(sockfd[4], &socks)) { if ( (n = recvfrom(sockfd[4], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) error("Error receiving datagram from client\n"); struct router compTable; bufferToTable(&buf, &compTable); if(updateTable(&tableE, compTable) == false) count++; else count = 0; tableToBuffer(&tableE, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[4][i] != -1) { n = sendto(sockfd[4], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[4][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (FD_ISSET(sockfd[5], &socks)) { if ( (n = recvfrom(sockfd[5], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) < 0 ) error("Error receiving datagram from client\n"); struct router compTable; bufferToTable(&buf, &compTable); if(updateTable(&tableF, compTable) == false) count++; else count = 0; tableToBuffer(&tableF, &buf); for (i=0; i<NUMROUTERS; i++) { if (neighborMatrix.r[5][i] != -1) { n = sendto(sockfd[5], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&serveraddr[neighborMatrix.r[5][i]], clientlen); if (n < 0) error("Error sending to client"); } } } if (count >= NUMROUTERS * 2) { for (i=0; i<NUMROUTERS; i++) { outputTable(network[i], true); } stableState = true; // printf("\n\nFINAL ROUTER INFO:\n\n"); // printf("ROUTER A:\n\n"); // printRouter(&tableA); // printf("\n\nROUTER B:\n\n"); // printRouter(&tableB); // printf("\n\nROUTER C:\n\n"); // printRouter(&tableC); // printf("\n\nROUTER D:\n\n"); // printRouter(&tableD); // printf("\n\nROUTER E:\n\n"); // printRouter(&tableE); // printf("\n\nROUTER F:\n\n"); // printRouter(&tableF); printf("[OK]\n\n"); choose_action: printf("Press 1<ENTER> to kill a router\nPress 2<ENTER> to send packet across the network\nPress 3<ENTER> to profile the network\nPress 4<ENTER> to exit\n-> "); // steady state // scan for input to send a packe int option, k; // kill router, or send packet from x to y char toKill, srcRouter, dstRouter; scanf("%d", &option); switch (option) { case 1: printf("Label of router to kill (A-F):\n-> "); scanf("\n%c", &toKill); printf("Killing router %c\n", toupper(toKill)); int n; /* struct timeval tv; tv.tv_usec = 500000; */ for (k = 0; k < NUMROUTERS; k++) { printf("Clearing Router %c's input buffers...", (char) k + 'A'); FD_ZERO(&socks); FD_SET(sockfd[k], &socks); while ( (n = recvfrom(sockfd[k], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) > 0) { // printf("%d %d\n", k, n); } printf("[OK]\n"); } reinitializeTopologyFile(toupper(toKill), nKills); killedRouters[toupper(toKill) - 'A'] = 1; nKills++; goto re_initialize; // will never reach this point break; case 2: printf("Label of source router (A-F):\n-> "); scanf("%c", &srcRouter); srcRouter = getchar(); srcRouter = toupper(srcRouter); printf("Label of destination router (A-F):\n-> "); scanf("%c", &dstRouter); dstRouter = getchar(); dstRouter = toupper(dstRouter); printf("Routing a packet from Router %c to Router %c...", srcRouter, dstRouter); struct packet p = { 'd', "message", srcRouter, dstRouter, 0, 0 }; forwardPacket(&p, network); printf("[OK]\n\n"); goto choose_action; break; case 3: printf("Routing tables:\n\n"); for (k = 0; k < NUMROUTERS; k++) { printf("\nRouter %c:\n\n", 'A' + k); printRouter(network[k]); } goto choose_action; break; case 4: printf("Killing all routers.\n"); /* struct timeval tv; tv.tv_usec = 500000; */ for (k = 0; k < NUMROUTERS; k++) { printf("Clearing Router %c's input buffers...", (char) k + 'A'); FD_ZERO(&socks); FD_SET(sockfd[k], &socks); while ( (n = recvfrom(sockfd[k], buf, BUFSIZE*sizeof(int), 0, (struct sockaddr *)&clientaddr, &clientlen)) > 0) { // printf("%d %d\n", k, n); } printf("[OK]\n"); } break; } break; } } } }
void DataBus::Server::processPackets(Client *client) { // Check client if (client == 0) { // Error, null pointer return; } // Find client in the list int index = m_clients.indexOf(client); if (index < 0) { // Error, client was not found return; } // Process all packets while (client->packetsAvailable()) { // Process packet Packet packet = client->takePacket(); switch (packet.getPacketType()) { case PacketType_RegisterClientRequest: { // Try to register the client quint8 returnStatus = registerClient(client, packet); // Send Register Client Response Packet responsePacket; if (RegisterClientResponsePacket::create(packet.getSource(), packet.getPacketId(), returnStatus, &responsePacket) == false) { // Error, failed to create packet break; } client->sendPacket(responsePacket); break; } case PacketType_GetClientListRequest: { // Create response Packet responsePacket; QList<quint8> clientIdList = getRegisteredClientIdList(); if (GetClientListResponsePacket::create(packet.getSource(), packet.getPacketId(), clientIdList, &responsePacket) == false) { // Error, failed to create packet break; } // Send Get Client List Response client->sendPacket(responsePacket); break; } case PacketType_GetClientStatusRequest: { // Parse packet quint8 clientId; if (DataBus::GetClientStatusRequestPacket::parse(packet, &clientId) == false) { // Error, failed to parse packet break; } // Create response ClientStatus clientStatus = getRegisteredClientStatus(clientId); Packet responsePacket; if (GetClientStatusResponsePacket::create(packet.getSource(), packet.getPacketId(), clientId, clientStatus, &responsePacket) == false) { // Error, failed to create packet break; } // Send Get Client Status Response client->sendPacket(responsePacket); break; } default: { // Forward packet to a client forwardPacket(packet); break; } } } }