diff --git a/manycast/manycast_server.c b/manycast/manycast_server.c index f6f9b32b4b45b2f84c7acd5247bc66a72f42ff1f..6e4ca7ded405fe9156e349f9d2a91ef2416c3acf 100644 --- a/manycast/manycast_server.c +++ b/manycast/manycast_server.c @@ -1,3 +1,23 @@ +/********************************************************** +SNTP SERVER FILE + +Written by Simon Llewellyn & Callum Martin +Last update 6/12/2016 +Working Unicast server +Manycast client appears to work + - will accept a message from the address 224.0.0.1 + - wireshark shows packet recieved + - wireshark shows server responds + +Allows to create and recieve a sntp packet on the port of their chooseing +Can configure using command line +'-m' enables manycast +'-ip' allows you to add new ip address for multicast group +'-p' allows you to change the port number to listen on +e.g. ./manycast_client -m -p 1234 -ip 127.0.0.1 +ebables manycast, on port 1234 and sends to 127.0.0.1 +**********************************************************/ + #include "headers.h" #include "timestamp.h" #include "ntp_functions.h" @@ -14,21 +34,24 @@ int main(int argc, char * argv[]) { struct sntp_packet recPacket, sendPacket; struct timeval current_time; struct hostent *he; - char default_server[] = "localhost"; + char default_server[] = "224.0.0.1"; int LISTEN_PORT = 1234; /* The port the server listens on. */ bool manycast = false, argument_port = false, argument_server = false; int yes = 1; uint8_t flags = 0x24; - //setup the reply packet - create_packet(&sendPacket); - printf("test\n" ); + + //*********************************************************** + // START OF CLIENT CONFIG + // Loops through command line arguments to configure the client + // + char *argument_ip; int i; - for (i = 1; i < argc; i++) { /* Skip argv[0]*/ + for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-m") == 0) { - manycast == true; + manycast = true; printf("Manycast\n"); } else if (strcmp(argv[i], "-p") == 0) {/* Allows the user to input a specific port. */ @@ -43,24 +66,40 @@ int main(int argc, char * argv[]) { strcpy(argument_ip, argv[i]); } } + // + // END OF CLIENT CONFIG + // + //************************************************************* + + + -printf("Argc complete\n" ); + //*********************************************************** + // IMPLEMENT AND DISPLAY THE CONFIG + // Loops through command line arguments to configure the client + // if (manycast == true ) { if (argument_server == 0) { argument_ip = malloc(sizeof (default_server)); strcpy(argument_ip, default_server); } - printf("Mulicast IP %s: ", argument_ip); + printf("IP %s: ", argument_ip); /* resolve server host name or IP address */ if ((he = gethostbyname(argument_ip)) == NULL) { perror("Server gethostbyname"); exit(1); } }else{ - printf("Unicast server\n" ); + printf("Unicast\n" ); } + + + //*********************************************************** + // START OF CLIENT CONFIG + // Loops through command line arguments to configure the client + // printf("start loop\n"); //Forever loop to recive and send replies while(1){ @@ -93,7 +132,7 @@ printf("Argc complete\n" ); } // request to join multicast grou - printf("Joining the multicast group: "); + printf("Joining the multicast group: \n"); mreq.imr_multiaddr.s_addr = inet_addr(argument_ip); mreq.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq)) < 0) { @@ -124,10 +163,16 @@ printf("Argc complete\n" ); network_to_host_conversion(&recPacket); - + //initialise the reply packet + create_packet(&sendPacket); + //populate reply with sendPacket = populateReply(&recPacket, current_time, flags); host_to_network_conversion(&sendPacket); + //*********************************************************** + // START OF CLIENT CONFIG + // Loops through command line arguments to configure the client + // numbytes = send( sockfd, ( char* ) &sendPacket, sizeof(sendPacket),0); if (numbytes < 0){ perror("ERROR writing to socket"); @@ -164,7 +209,16 @@ struct sntp_packet populateReply(struct sntp_packet* recPacket, struct timeval c sendPacket.originate = recPacket->transmit; -// NEED TO ADD REF LOCL + // Sets to LOCL is stratum = 1 as it is unsynced + if (sendPacket.stratum == 0x01){ + char referenceChar[] = "LOCL"; + int i; + for (i = 0; i < 4; i++) { + //add the hex code for each letter then shift 8 for next letter + sendPacket.refId = sendPacket.refId << 8; + sendPacket.refId += referenceChar[i] & 0xFF; + } + }