blob: 91a15230d7d903d3d4f1cd7ec8de2f82969be021 [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{
akmhoqueb77b95f2013-02-08 12:28:47 -060024 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 option -- description\n\n\
28 add|del -- specify whether you want to add or delete.\n\
29 name|neighbor -- specify whether you are adding a name or a neighbor.\n\
30 name_prefix -- name of the prefix for the name|neighbor.\n\
31 faceX -- face ID for neighbor if the third argument is neighbor.\n\n\
32 Examples:\n\
33 1) nlsrc -s 127.0.0.1 -p 9696 add name /ndn/memphis.edu/test \n", progname);
akmhoque1771c412012-11-09 13:06:08 -060034
akmhoqueb77b95f2013-02-08 12:28:47 -060035 exit(1);
akmhoque1771c412012-11-09 13:06:08 -060036}
37
akmhoque1712a392012-11-08 12:00:30 -060038int main(int argc, char *argv[])
39{
40 int sockfd;
41 int len;
akmhoque95041802012-11-16 09:18:02 -060042 struct sockaddr_in address;
akmhoque1712a392012-11-08 12:00:30 -060043 int result;
akmhoqueb77b95f2013-02-08 12:28:47 -060044 int bytesSent;
akmhoque95041802012-11-16 09:18:02 -060045 char *server_address, *server_port;
46
akmhoqueb77b95f2013-02-08 12:28:47 -060047 int command_len=0;
akmhoque1712a392012-11-08 12:00:30 -060048 int i;
akmhoque1771c412012-11-09 13:06:08 -060049
Adam Alyyanb5fff372013-01-09 14:32:52 -060050 if (argc < 8)
akmhoqueb77b95f2013-02-08 12:28:47 -060051 usage(argv[0]);
Adam Alyyanb5fff372013-01-09 14:32:52 -060052
akmhoqueb77b95f2013-02-08 12:28:47 -060053 if (strcmp(argv[6], "neighbor") == 0 && argc < 9)
54 usage(argv[0]);
Adam Alyyanb5fff372013-01-09 14:32:52 -060055
akmhoqueb77b95f2013-02-08 12:28:47 -060056 if (strcmp(argv[6], "name") != 0 && strcmp(argv[6], "neighbor") != 0)
57 usage(argv[0]);
akmhoque1771c412012-11-09 13:06:08 -060058
akmhoque95041802012-11-16 09:18:02 -060059 while ((result = getopt_long(argc, argv, "s:p:", longopts, 0)) != -1)
60 {
61 switch (result)
62 {
63 case 's':
64 server_address = optarg;
65 break;
66 case 'p':
67 server_port = optarg;
68 break;
69 }
70 }
71
72
akmhoque562caef2012-11-09 13:29:06 -060073 char recv_buffer[1024];
74 bzero(recv_buffer,1024);
75
akmhoqueb77b95f2013-02-08 12:28:47 -060076 for(i=5;i<argc;i++)
77 command_len+=(strlen(argv[i])+1);
78 char *command=malloc(command_len);
79 memset(command, 0, command_len);
80 for(i=5;i<argc;i++)
akmhoque1712a392012-11-08 12:00:30 -060081 {
akmhoqueb77b95f2013-02-08 12:28:47 -060082 memcpy(command+strlen(command),argv[i],strlen(argv[i]));
83 if ( i < argc-1 )
akmhoque1712a392012-11-08 12:00:30 -060084 memcpy(command+strlen(command)," ",1);
85 }
86
akmhoque95041802012-11-16 09:18:02 -060087 sockfd = socket(AF_INET, SOCK_STREAM, 0);
88 //address.sun_family = AF_UNIX;
89 //strcpy(address.sun_path, "/tmp/nlsr_api_server_socket");
90 address.sin_family = AF_INET;
91 address.sin_addr.s_addr = inet_addr(server_address);
Adam Alyyanb5fff372013-01-09 14:32:52 -060092 address.sin_port = htons(atoi(server_port));
akmhoque95041802012-11-16 09:18:02 -060093
akmhoque1712a392012-11-08 12:00:30 -060094 len = sizeof(address);
95 result = connect(sockfd, (struct sockaddr *)&address, len);
akmhoque1771c412012-11-09 13:06:08 -060096 if(result == -1)
97 {
akmhoque1712a392012-11-08 12:00:30 -060098 perror("oops nlsrc ");
99 exit(1);
100 }
akmhoque562caef2012-11-09 13:29:06 -0600101 printf("Command to send: %s \n",command);
akmhoqueb77b95f2013-02-08 12:28:47 -0600102 bytesSent=send(sockfd, command, strlen(command),0);
103 printf("Command len: %d, Bytes sent: %d \n",(int)strlen(command), bytesSent);
akmhoque562caef2012-11-09 13:29:06 -0600104 recv(sockfd, recv_buffer, 1024, 0);
105 printf("%s\n",recv_buffer);
akmhoque1771c412012-11-09 13:06:08 -0600106 free(command);
akmhoque1712a392012-11-08 12:00:30 -0600107 close(sockfd);
akmhoqueb77b95f2013-02-08 12:28:47 -0600108 exit(0);
akmhoque1712a392012-11-08 12:00:30 -0600109}