blob: 20e199fcb45cb93fcd33c6b525040deaec42c37e [file] [log] [blame]
akmhoque1712a392012-11-08 12:00:30 -06001//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
akmhoque1771c412012-11-09 13:06:08 -060010static int
11usage(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
akmhoque1712a392012-11-08 12:00:30 -060024int main(int argc, char *argv[])
25{
26 int sockfd;
27 int len;
28 struct sockaddr_un address;
29 int result;
30 int byteSend;
akmhoque562caef2012-11-09 13:29:06 -060031
akmhoque1712a392012-11-08 12:00:30 -060032 int command_len=0;
33 int i;
akmhoque1771c412012-11-09 13:06:08 -060034
35 if (argc < 4 )
36 usage(argv[0]);
37 if ( strcmp(argv[2],"neighbor") == 0 && argc <5 )
38 usage(argv[0]);
39
akmhoque562caef2012-11-09 13:29:06 -060040 char recv_buffer[1024];
41 bzero(recv_buffer,1024);
42
akmhoque1712a392012-11-08 12:00:30 -060043 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);
akmhoque1771c412012-11-09 13:06:08 -060059 if(result == -1)
60 {
akmhoque1712a392012-11-08 12:00:30 -060061 perror("oops nlsrc ");
62 exit(1);
63 }
akmhoque562caef2012-11-09 13:29:06 -060064 printf("Command to send: %s \n",command);
akmhoque1712a392012-11-08 12:00:30 -060065 byteSend=send(sockfd, command, strlen(command),0);
akmhoque562caef2012-11-09 13:29:06 -060066 recv(sockfd, recv_buffer, 1024, 0);
67 printf("%s\n",recv_buffer);
akmhoque1771c412012-11-09 13:06:08 -060068 free(command);
akmhoque1712a392012-11-08 12:00:30 -060069 close(sockfd);
70 exit(0);
71}