blob: 89439730cf7aed397abf1d4eb31fcb6ba15d98a3 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -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#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};
20
21static int
22usage(char *progname)
23{
24 printf("Usage: %s [OPTIONS...]\n\
25 NLSR Api client....\n\
26 nlsrc -s server_ip -p server_port add|del name|neighbor name_prefix [faceX] \n\
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
35int main(int argc, char *argv[])
36{
37 int sockfd;
38 int len;
39 struct sockaddr_in address;
40 int result;
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -060041 int bytesSent;
akmhoque8fdd6412012-12-04 15:05:33 -060042 char *server_address, *server_port;
43
44 int command_len=0;
45 int i;
46
47 if (argc < 8 )
48 usage(argv[0]);
49 if ( strcmp(argv[6],"neighbor") == 0 && argc <9 )
50 usage(argv[0]);
51
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
66 char recv_buffer[1024];
67 bzero(recv_buffer,1024);
68
69 for(i=5;i<argc;i++)
70 command_len+=(strlen(argv[i])+1);
71 char *command=malloc(command_len);
72 memset(command,command_len+1,0);
73 for(i=5;i<argc;i++)
74 {
75 memcpy(command+strlen(command),argv[i],strlen(argv[i]));
76 if ( i < argc-1 )
77 memcpy(command+strlen(command)," ",1);
78 }
79
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
87 len = sizeof(address);
88 result = connect(sockfd, (struct sockaddr *)&address, len);
89 if(result == -1)
90 {
91 perror("oops nlsrc ");
92 exit(1);
93 }
94 printf("Command to send: %s \n",command);
Syed Obaid Amin78e9aa92012-12-21 03:57:13 -060095 bytesSent=send(sockfd, command, strlen(command),0);
96 printf("Command len: %d, Bytes sent: %d \n",strlen(command), bytesSent);
akmhoque8fdd6412012-12-04 15:05:33 -060097 recv(sockfd, recv_buffer, 1024, 0);
98 printf("%s\n",recv_buffer);
99 free(command);
100 close(sockfd);
101 exit(0);
102}