|
@@ -9,9 +9,10 @@
|
9
|
9
|
|
10
|
10
|
/* Constants used for the console bindings. */
|
11
|
11
|
|
12
|
|
-#define SERVER_PATH "/tmp/ntk-console"
|
|
12
|
+#define SERVER_PATH "/tmp/ntk-console"
|
13
|
13
|
#define REQUEST_LENGTH 250
|
14
|
|
-#define FALSE 0
|
|
14
|
+#define FALSE 0
|
|
15
|
+#define TRUE 1
|
15
|
16
|
|
16
|
17
|
/* Variable and structure defintions, sockfd refers to socket file descriptor
|
17
|
18
|
* length refers to the required length of the requests that will be sent.
|
|
@@ -28,181 +29,167 @@ int rc, length;
|
28
|
29
|
* unlinks the server path, etc. */
|
29
|
30
|
|
30
|
31
|
void clean_up(void) {
|
31
|
|
-
|
32
|
|
- const int optVal = 1;
|
33
|
|
- const socklen_t optLen = sizeof(optVal);
|
34
|
|
-
|
35
|
|
- setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
36
|
|
- setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
37
|
|
-
|
38
|
|
- if (sockfd_1 != -1)
|
39
|
|
- close(sockfd_1);
|
40
|
|
-
|
41
|
|
- if (sockfd_2 != -1)
|
42
|
|
- close(sockfd_2);
|
|
32
|
+
|
|
33
|
+ const int optVal = 1;
|
|
34
|
+ const socklen_t optLen = sizeof(optVal);
|
|
35
|
+
|
|
36
|
+ setsockopt(sockfd_1, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
|
37
|
+ setsockopt(sockfd_2, SOL_SOCKET, SO_REUSEADDR, (void*) &optVal, optLen);
|
|
38
|
+
|
|
39
|
+ if (sockfd_1 != -1)
|
|
40
|
+ close(sockfd_1);
|
|
41
|
+
|
|
42
|
+ if (sockfd_2 != -1)
|
|
43
|
+ close(sockfd_2);
|
43
|
44
|
|
44
|
45
|
unlink(SERVER_PATH);
|
45
|
|
-
|
|
46
|
+
|
46
|
47
|
}
|
47
|
48
|
|
48
|
49
|
/* Creates an AF_UNIX socket and binds it to a local address. */
|
49
|
50
|
|
50
|
51
|
void opensocket(void) {
|
51
|
|
-
|
52
|
|
- int stop_trying;
|
53
|
|
-
|
54
|
|
- sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
|
55
|
|
- if (sockfd_1 < 0) {
|
56
|
|
- perror("socket creation failed");
|
57
|
|
- exit(-1);
|
58
|
|
- }
|
59
|
|
-
|
60
|
|
- memset(&serveraddr, 0, sizeof(serveraddr));
|
61
|
|
- serveraddr.sun_family = AF_UNIX;
|
62
|
|
- strcpy(serveraddr.sun_path, SERVER_PATH);
|
63
|
|
-
|
64
|
|
- rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
|
65
|
|
- if (rc < 0) {
|
66
|
|
- perror("bind() failed");
|
67
|
|
- clean_up();
|
68
|
|
- if(stop_trying >= 2) {
|
69
|
|
- perror("bind() failed");
|
70
|
|
- clean_up();
|
71
|
|
- opensocket();
|
72
|
|
- exit(-1);
|
73
|
|
- }
|
74
|
|
- stop_trying++;
|
75
|
|
- opensocket();
|
76
|
|
- }
|
|
52
|
+
|
|
53
|
+ int stop_trying;
|
|
54
|
+
|
|
55
|
+ sockfd_1 = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
56
|
+ if (sockfd_1 < 0) {
|
|
57
|
+ perror("socket creation failed");
|
|
58
|
+ exit(-1);
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ memset(&serveraddr, 0, sizeof(serveraddr));
|
|
62
|
+ serveraddr.sun_family = AF_UNIX;
|
|
63
|
+ strcpy(serveraddr.sun_path, SERVER_PATH);
|
|
64
|
+
|
|
65
|
+ rc = bind(sockfd_1, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
|
|
66
|
+ if (rc < 0) {
|
|
67
|
+ perror("bind() failed");
|
|
68
|
+ clean_up();
|
|
69
|
+ if(stop_trying >= 2) {
|
|
70
|
+ perror("bind() failed");
|
|
71
|
+ clean_up();
|
|
72
|
+ opensocket();
|
|
73
|
+ exit(-1);
|
|
74
|
+ }
|
|
75
|
+ stop_trying++;
|
|
76
|
+ opensocket();
|
|
77
|
+ }
|
77
|
78
|
}
|
78
|
79
|
|
79
|
80
|
/* Sends a parsed response to the ntk console client. */
|
80
|
81
|
|
81
|
|
-void send_response(char response[REQUEST_LENGTH], ...) {
|
82
|
|
- int response_length;
|
83
|
|
-
|
84
|
|
- response_length = (int)strlen(response);
|
85
|
|
- memset(response, 'a', REQUEST_LENGTH - response_length);
|
86
|
|
- rc = send(sockfd_2, response, sizeof(response), 0);
|
87
|
|
- if (rc < 0){
|
88
|
|
- perror("send() failed");
|
89
|
|
- exit(-1);
|
90
|
|
- }
|
91
|
|
-
|
|
82
|
+void
|
|
83
|
+send_response(char response[REQUEST_LENGTH], ...)
|
|
84
|
+{
|
|
85
|
+ int response_length = (int)strlen(response);
|
|
86
|
+ rc = send(sockfd_2, response, sizeof(response), 0);
|
|
87
|
+ if (rc < 0){
|
|
88
|
+ perror("send() failed");
|
|
89
|
+ exit(-1);
|
|
90
|
+ }
|
92
|
91
|
}
|
93
|
92
|
|
94
|
|
-void request_cleanup(char unprocessed_request[REQUEST_LENGTH]) {
|
95
|
|
-
|
96
|
|
- char remove = 'a';
|
97
|
|
-
|
98
|
|
- char* c;
|
99
|
|
- int x;
|
100
|
|
- char* pPosition;
|
101
|
|
- while(pPosition = strchr(unprocessed_request, 'a') != NULL) {
|
102
|
|
- if ((c = index(unprocessed_request, remove)) != NULL) {
|
103
|
|
- size_t len_left = sizeof(unprocessed_request) - (c+1-unprocessed_request);
|
104
|
|
- memmove(c, c+1, len_left);
|
105
|
|
- }
|
106
|
|
- }
|
107
|
|
- printf("Cleaned Request is: %s", unprocessed_request);
|
108
|
|
-
|
109
|
|
- request_processing(unprocessed_request);
|
110
|
|
-
|
111
|
|
-}
|
112
|
93
|
|
113
|
94
|
/* Parses the received request from the ntk console client
|
114
|
95
|
* to data from ntkd structures such as: me
|
115
|
96
|
* into a response for the ntk console client. */
|
116
|
97
|
|
117
|
|
-int request_processing(char unprocessed_request[REQUEST_LENGTH]) {
|
118
|
|
-
|
119
|
|
- if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0)
|
120
|
|
- send_response((char)time(0)-me.uptime);
|
121
|
|
-
|
122
|
|
- else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0)
|
123
|
|
- send_response(VERSION_STR);
|
124
|
|
-
|
125
|
|
- else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0)
|
126
|
|
- send_response((char)me.inet_connected);
|
127
|
|
-
|
128
|
|
- else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0)
|
129
|
|
- send_response((char)me.cur_ifs);
|
130
|
|
-
|
131
|
|
- else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request)) == 0)
|
132
|
|
- send_response((char)me.cur_ifs_n);
|
133
|
|
-
|
134
|
|
- else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request)) == 0)
|
135
|
|
- send_response((char)me.cur_qspn_id);
|
136
|
|
-
|
137
|
|
- else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request)) == 0)
|
138
|
|
- send_response((char)me.cur_ip.data);
|
139
|
|
-
|
140
|
|
- /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0)
|
141
|
|
- send_response(me.cur_node);
|
142
|
|
-
|
143
|
|
- else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request)) == 0)
|
144
|
|
- return 0;
|
145
|
|
-
|
146
|
|
- else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request)) == 0)
|
147
|
|
- return 0;*/
|
148
|
|
- send_response(unprocessed_request, " Is invalid or yet to be implemented.");
|
149
|
|
- return -1;
|
|
98
|
+int
|
|
99
|
+request_processing(char unprocessed_request[REQUEST_LENGTH])
|
|
100
|
+{
|
|
101
|
+ if(strncmp(unprocessed_request,"uptime", (int)strlen(unprocessed_request)) == 0)
|
|
102
|
+ send_response((char)time(0)-me.uptime);
|
|
103
|
+
|
|
104
|
+ else if(strncmp(unprocessed_request,"version", (int)strlen(unprocessed_request)) == 0)
|
|
105
|
+ send_response(VERSION_STR);
|
|
106
|
+
|
|
107
|
+ else if(strncmp(unprocessed_request,"inet_connected", (int)strlen(unprocessed_request)) == 0)
|
|
108
|
+ send_response((char)me.inet_connected);
|
|
109
|
+
|
|
110
|
+ else if(strncmp(unprocessed_request,"cur_ifs", (int)strlen(unprocessed_request)) == 0)
|
|
111
|
+ send_response((char)me.cur_ifs);
|
|
112
|
+
|
|
113
|
+ else if(strncmp(unprocessed_request,"cur_ifs_n", (int)strlen(unprocessed_request)) == 0)
|
|
114
|
+ send_response((char)me.cur_ifs_n);
|
|
115
|
+
|
|
116
|
+ else if(strncmp(unprocessed_request,"cur_qspn_id", (int)strlen(unprocessed_request)) == 0)
|
|
117
|
+ send_response((char)me.cur_qspn_id);
|
|
118
|
+
|
|
119
|
+ else if(strncmp(unprocessed_request,"cur_ip", (int)strlen(unprocessed_request)) == 0)
|
|
120
|
+ send_response((char)me.cur_ip.data);
|
|
121
|
+
|
|
122
|
+ /*else if(strncmp(unprocessed_request,"cur_node", (int)strlen(unprocessed_request)) == 0)
|
|
123
|
+ send_response(me.cur_node);
|
|
124
|
+
|
|
125
|
+ else if(strncmp(unprocessed_request,"ifs", (int)strlen(unprocessed_request)) == 0)
|
|
126
|
+ return 0;
|
|
127
|
+
|
|
128
|
+ else if(strncmp(unprocessed_request,"ifs_n", (int)strlen(unprocessed_request)) == 0)
|
|
129
|
+ return 0;*/
|
|
130
|
+ send_response(unprocessed_request, " Is invalid or yet to be implemented.");
|
|
131
|
+ return -1;
|
|
132
|
+}
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+static int
|
|
136
|
+request_receive(int sock, char message[], int max)
|
|
137
|
+{
|
|
138
|
+ int total = 0;
|
|
139
|
+ const int bsize = 1024;
|
|
140
|
+ char buffer[bsize+1];
|
|
141
|
+ int read = bsize;
|
|
142
|
+
|
|
143
|
+ message[0] = 0; // initialize for strcat()
|
|
144
|
+
|
|
145
|
+ while(read == bsize) {
|
|
146
|
+ read = recv(sock, buffer, bsize, 0);
|
|
147
|
+ if(read < 0)
|
|
148
|
+ return -1; // error, bail out
|
|
149
|
+ total += read;
|
|
150
|
+ if(total > max)
|
|
151
|
+ return -2; // overflow
|
|
152
|
+ buffer[read] = 0;
|
|
153
|
+ strcat(message, buffer);
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ return total;
|
150
|
157
|
}
|
151
|
158
|
|
152
|
|
-/* Receives a request of 250 bytes from the ntk console client.
|
153
|
|
- * listen and accept are also in a while loop to allow for different
|
154
|
|
- * start times between the ntk console bindings and the ntk console client,
|
155
|
|
- * As well as restarting of the ntk console client. */
|
|
159
|
+
|
156
|
160
|
void ntkd_request(void) {
|
157
|
|
-
|
158
|
|
- char request[REQUEST_LENGTH];
|
159
|
|
-
|
160
|
|
- do {
|
161
|
|
-
|
162
|
|
- rc = listen(sockfd_1, 10);
|
163
|
|
- if (rc< 0) {
|
164
|
|
- perror("listen() failed");
|
165
|
|
- exit(-1);
|
166
|
|
- }
|
167
|
|
-
|
168
|
|
- printf("Ready for client connect().\n");
|
169
|
|
-
|
170
|
|
- sockfd_2 = accept(sockfd_1, NULL, NULL);
|
171
|
|
- if (sockfd_2 < 0) {
|
172
|
|
- perror("accept() failed");
|
173
|
|
- exit(-1);
|
174
|
|
- }
|
175
|
|
-
|
176
|
|
- length = REQUEST_LENGTH;
|
177
|
|
- rc = setsockopt(sockfd_2, SOL_SOCKET, SO_RCVLOWAT,
|
178
|
|
- (char *)&length, sizeof(length));
|
179
|
|
- if (rc < 0) {
|
180
|
|
- perror("setsockopt(SO_RCVLOWAT) failed");
|
181
|
|
- exit(-1);
|
182
|
|
- }
|
183
|
|
-
|
184
|
|
- rc = recv(sockfd_2, request, sizeof(request), 0);
|
185
|
|
- if (rc < 0) {
|
186
|
|
- perror("recv() failed");
|
187
|
|
- exit(-1);
|
188
|
|
- }
|
189
|
|
-
|
190
|
|
- printf("%d bytes of data were received\n", rc);
|
191
|
|
-
|
192
|
|
- if (rc == 0 || rc < sizeof(request)) {
|
193
|
|
- printf("The console client closed the connection before all of the\n");
|
194
|
|
- printf("data was sent\n");
|
195
|
|
- exit(-1);
|
196
|
|
- }
|
197
|
|
-
|
198
|
|
- request_cleanup(request);
|
199
|
|
- } while(FALSE);
|
200
|
|
-
|
201
|
|
- clean_up();
|
202
|
|
-
|
|
161
|
+ char request[REQUEST_LENGTH];
|
|
162
|
+
|
|
163
|
+ rc = listen(sockfd_1, 10);
|
|
164
|
+ if (rc< 0) {
|
|
165
|
+ perror("listen() failed");
|
|
166
|
+ exit(-1);
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ printf("Ready for client connect().\n");
|
|
170
|
+
|
|
171
|
+ do {
|
|
172
|
+ sockfd_2 = accept(sockfd_1, NULL, NULL);
|
|
173
|
+ if (sockfd_2 < 0) {
|
|
174
|
+ perror("accept() failed");
|
|
175
|
+ exit(-1);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ rc = request_receive(sockfd_2, request, REQUEST_LENGTH);
|
|
179
|
+ if (rc < 0) {
|
|
180
|
+ perror("recv() failed");
|
|
181
|
+ exit(-1);
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ printf("%d bytes of data were received\n", rc);
|
|
185
|
+
|
|
186
|
+ request_processing(request);
|
|
187
|
+ } while(TRUE);
|
|
188
|
+
|
|
189
|
+ clean_up();
|
203
|
190
|
}
|
204
|
191
|
|
205
|
192
|
void console_recv_send(void) {
|
206
|
|
- opensocket();
|
207
|
|
- ntkd_request();
|
208
|
|
-}
|
|
193
|
+ opensocket();
|
|
194
|
+ ntkd_request();
|
|
195
|
+}
|