Exemplo n.º 1
0
/* send data via UDP */
int udpif_send(uint16_t dst_addr, uint16_t port, char *data, int length)
{
    ipv6_addr_t dst;
    int bytes_send;

    if (socket < 0) {
        init_send_socket();
        if (socket < 0) {           // UGLY, something better here?!
            puts("Error sending data - unable to open sending socket");
        }
    }

    // set receiver address
    udpif_get_ipv6_address(&dst, dst_addr);
    // write address and port to socket address
    memcpy(&socket_addr.sin6_addr, &dst, sizeof(ipv6_addr_t));
    socket_addr.sin6_port = HTONS(port);

    // send data
    bytes_send = tl_socket_sendto(socket, data, length, 0, &socket_addr, sizeof(sockaddr6_t));

    if (bytes_send < 0) {
        printf("Error: Sending data to %i failed\n", dst_addr);
    } else {
        printf("Successful delivered %i bytes over UDP to %i, port %i\n", bytes_send, dst_addr, port);
    }
    return bytes_send;
}
Exemplo n.º 2
0
int main(int argc, char *argv[]){
    nodes_data_index = 0; // Keep this index for nodes
    printf("Starting RIP Protocol....\n");

    init_send_socket();
    ///                                Router#   Node_count     Node2
    insert_my_nodes(argv[1], argv[2],       argv);
    router_id = atoi(argv[1]);
    own_nodes_count = atoi(argv[2]);
    // Now we have two nodes in our hashmap, we can get them by calling 

    // Initialize Receive socket to listen for other routers
    int i;
    for (i=0; i<atoi(argv[3]); i++){
        init_recv_sockets(argv[4+atoi(argv[2])+i], i); // Parameter are offseted behind nodes interface
    }
    // Router count will be the total number of ports
    vars.numports = atoi(argv[3]);
    // Now the non-blocking promiscous receving sockets are stored at vars.sockets[index], index is 0 or 1

    //Prepare sockaddr
    sockaddrll.sll_family=AF_PACKET;
    sockaddrll.sll_protocol=htons(ETH_P_ALL);
    sockaddrll.sll_halen=6;
    sockaddrll.sll_addr[0]=0x00;
    sockaddrll.sll_addr[1]=0x00;
    sockaddrll.sll_addr[2]=0x00;
    sockaddrll.sll_addr[3]=0x00;
    sockaddrll.sll_addr[4]=0x00;
    sockaddrll.sll_addr[5]=0x00;
    sockaddrll.sll_addr[6]=0x00;
    // Start threading
    pthread_t sending_thread,refreshing_thread;
    int rc;
    rc = pthread_create(&refreshing_thread, NULL, loop_refresh, NULL);
    if (rc){
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        exit(-1);
    }
    sleep(1);
    rc = pthread_create(&sending_thread, NULL, loop_send, NULL);
    if (rc){
        printf("ERROR; return code from pthread_create() is %d\n", rc);
        exit(-1);
    }
    sleep(1);
    unsigned char recvbuf[1024];
    memset(recvbuf,'\0',1024);
    int recv_size;
    struct sockaddr_ll newsockaddr;
    int sock_size= sizeof(newsockaddr);
    while(1){
        // Recevies route update packet from other routers
        for(i=0;i<vars.numports;i++){
            recv_size = recvfrom(vars.sockets[i],recvbuf,1024,0,(struct sockaddr*)&newsockaddr,(socklen_t*)&sock_size);
            if(recv_size>0 && newsockaddr.sll_pkttype!=PACKET_OUTGOING){
                // printf("Recevied something\n");
                struct gyn_header *gyn;
                gyn = (struct gyn_header*)(recvbuf);
                int receive_router_id;
                int receive_node_count;
                memcpy(&receive_router_id, &recvbuf[sizeof(struct gyn_header)], sizeof(int));
                // Check if the ether_type is for RIP 
                if (gyn->ether_type == 0x1234) {
                    memcpy(&receive_node_count, &recvbuf[sizeof(struct gyn_header) + sizeof(int)], sizeof(int));
                    memcpy(&received_my_nodes, &recvbuf[sizeof(struct gyn_header) + (2*sizeof(int))], sizeof(received_my_nodes));
                    memcpy(&received_my_nodes_distance, &recvbuf[sizeof(received_my_nodes) +sizeof(struct gyn_header) + (2 * sizeof(int))], sizeof(received_my_nodes_distance));
                    printf("\n#######################################################\n");
                    printf("Received packet from Router ID: %d\n", receive_router_id);
                    // printf("Interface:%s and Destination has %d nodes\n", routing_data[i].iface, receive_node_count);
                    // Print out the received stuff
                    int j;
                    printf("\t Received Destinations:");
                    for (j=0;j<receive_node_count;j++){
                        printf(" %d", received_my_nodes[j]);
                    }
                    printf("\n");
                    // Add the nodes behind the routers to the node map
                    int n;
                    for (n=0; n < receive_node_count;n++){
                        // Check if the node is already stored, compared their distance and pick the closer one
                        int stored_index;
                        char found_match = 0;
                        for(stored_index=0; stored_index <= nodes_data_index; stored_index++){
                            if (received_my_nodes[n] == 0 || nodes_data[stored_index].address == 0){
                                continue;
                            }
                            if (received_my_nodes[n] == nodes_data[stored_index].address){
                                // The nodes is stored, compared their distance, only add when we have a closer distance
                                found_match = 1;
                                if (received_my_nodes_distance[n] + 1 < nodes_data[stored_index].dist) {
                                    strncpy((char*)&nodes_data[stored_index].iface, routing_data[i].iface, 10);
                                    nodes_data[stored_index].send_index = routing_data[i].send_index;
                                    nodes_data[stored_index].nexthop = receive_router_id;
                                    nodes_data[stored_index].address = received_my_nodes[n];
                                    nodes_data[stored_index].dist = received_my_nodes_distance[n] + 1;
                                    //printf("Closer node found at [%d] iface:%s add:%d nexthop:%d distance:%d\n", stored_index, nodes_data[stored_index].iface
                                    //    , nodes_data[stored_index].address, nodes_data[stored_index].nexthop, nodes_data[stored_index].dist);
                                    break;
                                }
                            } 
                        }
                        if (found_match == 0){ // When we didnt found it in our list
                            // If we dont have the node already in our list, add it
                            strncpy((char*)&nodes_data[nodes_data_index].iface, routing_data[i].iface, 10);
                            nodes_data[nodes_data_index].send_index = routing_data[i].send_index;
                            nodes_data[nodes_data_index].nexthop = receive_router_id;
                            nodes_data[nodes_data_index].address = received_my_nodes[n];
                            nodes_data[nodes_data_index].dist = received_my_nodes_distance[n]+1;
                            //printf("Added new data at [%d] iface:%s add:%d nexthop:%d distance:%d\n", nodes_data_index, nodes_data[nodes_data_index].iface
                            //    , nodes_data[nodes_data_index].address, nodes_data[nodes_data_index].nexthop, nodes_data[nodes_data_index].dist);
                            nodes_data_index++;
                        }
                            


                    }     

                    // Write into file
                    FILE *f = fopen("/tmp/rtr.config", "w");
                    if (f == NULL)
                    {
                        printf("Error opening file!\n");
                        exit(1);
                    }
                    for (j=0; j<nodes_data_index; j++){
                        fprintf(f,"%s %d %d\n", nodes_data[j].iface, nodes_data[j].address, nodes_data[j].nexthop);
                    }
                    fclose(f);
                    // Update our current node list
                    printf("#######################################################\n");
                } else if (gyn->ether_type == 0x1234){
                    // Refreesh packet received


                }
            }
        
        }
    }


}