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> |
| 9 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 10 | static int |
| 11 | usage(char *progname) |
| 12 | { |
| 13 | printf("Usage: %s [OPTIONS...]\n\ |
| 14 | NLSR Api client....\n\ |
| 15 | nlsrc add|del name|neighbor name_prefix [faceX] \n\ |
| 16 | add/del, -- adding/deleting operation\n\ |
| 17 | name/neighbor, -- Operation for name/neighbor\n\ |
| 18 | name_name, -- Name prefix for name/neighbor\n\ |
| 19 | faceX, -- Face Id for neighbor if third argument is neighbor\n", progname); |
| 20 | |
| 21 | exit(1); |
| 22 | } |
| 23 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 24 | int main(int argc, char *argv[]) |
| 25 | { |
| 26 | int sockfd; |
| 27 | int len; |
| 28 | struct sockaddr_un address; |
| 29 | int result; |
| 30 | int byteSend; |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame^] | 31 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 32 | int command_len=0; |
| 33 | int i; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 34 | |
| 35 | if (argc < 4 ) |
| 36 | usage(argv[0]); |
| 37 | if ( strcmp(argv[2],"neighbor") == 0 && argc <5 ) |
| 38 | usage(argv[0]); |
| 39 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame^] | 40 | char recv_buffer[1024]; |
| 41 | bzero(recv_buffer,1024); |
| 42 | |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 43 | for(i=1;i<argc;i++) |
| 44 | command_len+=(strlen(argv[i])+1); |
| 45 | char *command=malloc(command_len); |
| 46 | memset(command,command_len+1,0); |
| 47 | for(i=1;i<argc;i++) |
| 48 | { |
| 49 | memcpy(command+strlen(command),argv[i],strlen(argv[i])); |
| 50 | if ( i < argc-1 ) |
| 51 | memcpy(command+strlen(command)," ",1); |
| 52 | } |
| 53 | |
| 54 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 55 | address.sun_family = AF_UNIX; |
| 56 | strcpy(address.sun_path, "/tmp/nlsr_api_server_socket"); |
| 57 | len = sizeof(address); |
| 58 | result = connect(sockfd, (struct sockaddr *)&address, len); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 59 | if(result == -1) |
| 60 | { |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 61 | perror("oops nlsrc "); |
| 62 | exit(1); |
| 63 | } |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame^] | 64 | printf("Command to send: %s \n",command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 65 | byteSend=send(sockfd, command, strlen(command),0); |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame^] | 66 | recv(sockfd, recv_buffer, 1024, 0); |
| 67 | printf("%s\n",recv_buffer); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 68 | free(command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 69 | close(sockfd); |
| 70 | exit(0); |
| 71 | } |