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; |
| 31 | |
| 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 | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 40 | for(i=1;i<argc;i++) |
| 41 | command_len+=(strlen(argv[i])+1); |
| 42 | char *command=malloc(command_len); |
| 43 | memset(command,command_len+1,0); |
| 44 | for(i=1;i<argc;i++) |
| 45 | { |
| 46 | memcpy(command+strlen(command),argv[i],strlen(argv[i])); |
| 47 | if ( i < argc-1 ) |
| 48 | memcpy(command+strlen(command)," ",1); |
| 49 | } |
| 50 | |
| 51 | sockfd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 52 | address.sun_family = AF_UNIX; |
| 53 | strcpy(address.sun_path, "/tmp/nlsr_api_server_socket"); |
| 54 | len = sizeof(address); |
| 55 | result = connect(sockfd, (struct sockaddr *)&address, len); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 56 | if(result == -1) |
| 57 | { |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 58 | perror("oops nlsrc "); |
| 59 | exit(1); |
| 60 | } |
| 61 | printf("Data to send: %s \n",command); |
| 62 | byteSend=send(sockfd, command, strlen(command),0); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 63 | free(command); |
akmhoque | 1712a39 | 2012-11-08 12:00:30 -0600 | [diff] [blame] | 64 | close(sockfd); |
| 65 | exit(0); |
| 66 | } |