akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 1 | //1. Make the necessary includes and set up the variables: |
| 2 | #include <sys/types.h> |
| 3 | #include <sys/socket.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sys/un.h> |
| 6 | #include <unistd.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 9 | #include <netinet/in.h> |
| 10 | #include <netdb.h> |
| 11 | #include <arpa/inet.h> |
| 12 | #include <getopt.h> |
| 13 | |
| 14 | struct option longopts[] = |
| 15 | { |
| 16 | { "server_ip", required_argument, NULL, 's'}, |
| 17 | { "server_port", required_argument, NULL, 'p'}, |
| 18 | { 0 } |
| 19 | }; |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 20 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 21 | static int |
| 22 | usage(char *progname) |
| 23 | { |
| 24 | printf("Usage: %s [OPTIONS...]\n\ |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 25 | NLSR API client....\n\ |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 26 | nlsrc -s server_ip -p server_port add|del name|neighbor name_prefix [faceX] \n\ |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 27 | option -- description\n\n\ |
| 28 | add|del -- specify whether you want to add or delete.\n\ |
| 29 | name|neighbor -- specify whether you are adding a name or a neighbor.\n\ |
| 30 | name_prefix -- name of the prefix for the name|neighbor.\n\ |
| 31 | faceX -- face ID for neighbor if the third argument is neighbor.\n\n\ |
| 32 | Examples:\n\ |
| 33 | 1) nlsrc -s 127.0.0.1 -p 9696 add name /ndn/memphis.edu/test \n", progname); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 34 | |
| 35 | exit(1); |
| 36 | } |
| 37 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 38 | int main(int argc, char *argv[]) |
| 39 | { |
| 40 | int sockfd; |
| 41 | int len; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 42 | struct sockaddr_in address; |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 43 | int result; |
| 44 | int byteSend; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 45 | char *server_address, *server_port; |
| 46 | |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 47 | int command_len = 0; |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 48 | int i; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 49 | |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 50 | if (argc < 8) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 51 | usage(argv[0]); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 52 | |
| 53 | if (strcmp(argv[6], "neighbor") == 0 && argc < 9) |
| 54 | usage(argv[0]); |
| 55 | |
| 56 | if (strcmp(argv[6], "name") != 0 && strcmp(argv[6], "neighbor") != 0) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 57 | usage(argv[0]); |
| 58 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 59 | while ((result = getopt_long(argc, argv, "s:p:", longopts, 0)) != -1) |
| 60 | { |
| 61 | switch (result) |
| 62 | { |
| 63 | case 's': |
| 64 | server_address = optarg; |
| 65 | break; |
| 66 | case 'p': |
| 67 | server_port = optarg; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 73 | char recv_buffer[1024]; |
| 74 | bzero(recv_buffer,1024); |
| 75 | |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 76 | for(i = 5; i < argc; i++) |
| 77 | command_len += (strlen(argv[i]) + 1); |
| 78 | |
| 79 | char *command = malloc(command_len); |
| 80 | memset(command, 0, command_len + 1); |
| 81 | |
| 82 | for(i = 5; i < argc; i++) |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 83 | { |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 84 | memcpy(command+strlen(command), argv[i], strlen(argv[i])); |
| 85 | |
| 86 | if (i < argc - 1) |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 87 | memcpy(command+strlen(command)," ",1); |
| 88 | } |
| 89 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 90 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 91 | //address.sun_family = AF_UNIX; |
| 92 | //strcpy(address.sun_path, "/tmp/nlsr_api_server_socket"); |
| 93 | address.sin_family = AF_INET; |
| 94 | address.sin_addr.s_addr = inet_addr(server_address); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 95 | address.sin_port = htons(atoi(server_port)); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 96 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 97 | len = sizeof(address); |
| 98 | result = connect(sockfd, (struct sockaddr *)&address, len); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 99 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 100 | if(result == -1) |
| 101 | { |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 102 | perror("oops nlsrc "); |
| 103 | exit(1); |
| 104 | } |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 105 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 106 | printf("Command to send: %s \n",command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 107 | byteSend=send(sockfd, command, strlen(command),0); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 108 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 109 | recv(sockfd, recv_buffer, 1024, 0); |
| 110 | printf("%s\n",recv_buffer); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 111 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 112 | free(command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 113 | close(sockfd); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame^] | 114 | |
| 115 | return 0; |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 116 | } |