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\ |
| 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\ |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 27 | add/del, -- adding/deleting operation\n\ |
| 28 | name/neighbor, -- Operation for name/neighbor\n\ |
| 29 | name_name, -- Name prefix for name/neighbor\n\ |
| 30 | faceX, -- Face Id for neighbor if third argument is neighbor\n", progname); |
| 31 | |
| 32 | exit(1); |
| 33 | } |
| 34 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 35 | int main(int argc, char *argv[]) |
| 36 | { |
| 37 | int sockfd; |
| 38 | int len; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 39 | struct sockaddr_in address; |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 40 | int result; |
| 41 | int byteSend; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 42 | char *server_address, *server_port; |
| 43 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 44 | int command_len=0; |
| 45 | int i; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 46 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 47 | if (argc < 8 ) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 48 | usage(argv[0]); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 49 | if ( strcmp(argv[6],"neighbor") == 0 && argc <9 ) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 50 | usage(argv[0]); |
| 51 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 52 | while ((result = getopt_long(argc, argv, "s:p:", longopts, 0)) != -1) |
| 53 | { |
| 54 | switch (result) |
| 55 | { |
| 56 | case 's': |
| 57 | server_address = optarg; |
| 58 | break; |
| 59 | case 'p': |
| 60 | server_port = optarg; |
| 61 | break; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 66 | char recv_buffer[1024]; |
| 67 | bzero(recv_buffer,1024); |
| 68 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 69 | for(i=5;i<argc;i++) |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 70 | command_len+=(strlen(argv[i])+1); |
| 71 | char *command=malloc(command_len); |
| 72 | memset(command,command_len+1,0); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 73 | for(i=5;i<argc;i++) |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 74 | { |
| 75 | memcpy(command+strlen(command),argv[i],strlen(argv[i])); |
| 76 | if ( i < argc-1 ) |
| 77 | memcpy(command+strlen(command)," ",1); |
| 78 | } |
| 79 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame^] | 80 | sockfd = socket(AF_INET, SOCK_STREAM, 0); |
| 81 | //address.sun_family = AF_UNIX; |
| 82 | //strcpy(address.sun_path, "/tmp/nlsr_api_server_socket"); |
| 83 | address.sin_family = AF_INET; |
| 84 | address.sin_addr.s_addr = inet_addr(server_address); |
| 85 | address.sin_port = atoi(server_port); |
| 86 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 87 | len = sizeof(address); |
| 88 | result = connect(sockfd, (struct sockaddr *)&address, len); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 89 | if(result == -1) |
| 90 | { |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 91 | perror("oops nlsrc "); |
| 92 | exit(1); |
| 93 | } |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 94 | printf("Command to send: %s \n",command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 95 | byteSend=send(sockfd, command, strlen(command),0); |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 96 | recv(sockfd, recv_buffer, 1024, 0); |
| 97 | printf("%s\n",recv_buffer); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 98 | free(command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 99 | close(sockfd); |
| 100 | exit(0); |
| 101 | } |