blob: 44ce2adc7deb02d54bc4afbcc12ce4a10eac2793 [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>
akmhoque95041802012-11-16 09:18:02 -06009#include <netinet/in.h>
10#include <netdb.h>
11#include <arpa/inet.h>
12#include <getopt.h>
13
14struct option longopts[] =
15{
16 { "server_ip", required_argument, NULL, 's'},
17 { "server_port", required_argument, NULL, 'p'},
18 { 0 }
19};
akmhoque1712a392012-11-08 12:00:30 -060020
akmhoque1771c412012-11-09 13:06:08 -060021static int
22usage(char *progname)
23{
24 printf("Usage: %s [OPTIONS...]\n\
25 NLSR Api client....\n\
akmhoque95041802012-11-16 09:18:02 -060026 nlsrc -s server_ip -p server_port add|del name|neighbor name_prefix [faceX] \n\
akmhoque1771c412012-11-09 13:06:08 -060027 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
akmhoque1712a392012-11-08 12:00:30 -060035int main(int argc, char *argv[])
36{
37 int sockfd;
38 int len;
akmhoque95041802012-11-16 09:18:02 -060039 struct sockaddr_in address;
akmhoque1712a392012-11-08 12:00:30 -060040 int result;
41 int byteSend;
akmhoque95041802012-11-16 09:18:02 -060042 char *server_address, *server_port;
43
akmhoque1712a392012-11-08 12:00:30 -060044 int command_len=0;
45 int i;
akmhoque1771c412012-11-09 13:06:08 -060046
akmhoque95041802012-11-16 09:18:02 -060047 if (argc < 8 )
akmhoque1771c412012-11-09 13:06:08 -060048 usage(argv[0]);
akmhoque95041802012-11-16 09:18:02 -060049 if ( strcmp(argv[6],"neighbor") == 0 && argc <9 )
akmhoque1771c412012-11-09 13:06:08 -060050 usage(argv[0]);
51
akmhoque95041802012-11-16 09:18:02 -060052 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
akmhoque562caef2012-11-09 13:29:06 -060066 char recv_buffer[1024];
67 bzero(recv_buffer,1024);
68
akmhoque95041802012-11-16 09:18:02 -060069 for(i=5;i<argc;i++)
akmhoque1712a392012-11-08 12:00:30 -060070 command_len+=(strlen(argv[i])+1);
71 char *command=malloc(command_len);
72 memset(command,command_len+1,0);
akmhoque95041802012-11-16 09:18:02 -060073 for(i=5;i<argc;i++)
akmhoque1712a392012-11-08 12:00:30 -060074 {
75 memcpy(command+strlen(command),argv[i],strlen(argv[i]));
76 if ( i < argc-1 )
77 memcpy(command+strlen(command)," ",1);
78 }
79
akmhoque95041802012-11-16 09:18:02 -060080 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
akmhoque1712a392012-11-08 12:00:30 -060087 len = sizeof(address);
88 result = connect(sockfd, (struct sockaddr *)&address, len);
akmhoque1771c412012-11-09 13:06:08 -060089 if(result == -1)
90 {
akmhoque1712a392012-11-08 12:00:30 -060091 perror("oops nlsrc ");
92 exit(1);
93 }
akmhoque562caef2012-11-09 13:29:06 -060094 printf("Command to send: %s \n",command);
akmhoque1712a392012-11-08 12:00:30 -060095 byteSend=send(sockfd, command, strlen(command),0);
akmhoque562caef2012-11-09 13:29:06 -060096 recv(sockfd, recv_buffer, 1024, 0);
97 printf("%s\n",recv_buffer);
akmhoque1771c412012-11-09 13:06:08 -060098 free(command);
akmhoque1712a392012-11-08 12:00:30 -060099 close(sockfd);
100 exit(0);
101}