Browse Source

ntk-console: Fix retransmit on console

Alexander von Gluck IV 10 years ago
parent
commit
98e77ded15
2 changed files with 41 additions and 42 deletions
  1. 40
    41
      src/ntk-console.c
  2. 1
    1
      src/ntk-console.h

+ 40
- 41
src/ntk-console.c View File

@@ -53,7 +53,7 @@ const struct supported_commands {
53 53
 
54 54
 
55 55
 command_t
56
-command_parse(char *request)
56
+command_parse(char* request)
57 57
 {
58 58
 	if (strlen(request) > CONSOLE_BUFFER_LENGTH) {
59 59
 		printf("Error: Command longer than 250 bytes.\n");
@@ -98,42 +98,13 @@ request_receive(int sock, char message[], int max)
98 98
 }
99 99
 
100 100
 
101
-/* Sends and receives to ntkd */
102
-void
103
-ntkd_request(command_t command)
104
-{
105
-	if (sockfd <= 0) {
106
-		perror("ntkd connection closed unexpectedly!\n");
107
-		exit(-1);
108
-	}
109
-
110
-	cmd_packet_t packetOut;
111
-	packetOut.command = command;
112
-
113
-	rc = send(sockfd, &packetOut, sizeof(packetOut), 0);
114
-	if (rc < sizeof(packetOut)) {
115
-		perror("send() failed");
116
-		exit(-1);
117
-	}
118
-
119
-	char response[CONSOLE_BUFFER_LENGTH];
120
-	request_receive(sockfd, response, CONSOLE_BUFFER_LENGTH);
121
-	if (rc < 0) {
122
-		perror("recv() failed");
123
-		exit(-1);
124
-	}
125
-
126
-	printf("Response: %s\n", response);
127
-}
128
-
129
-
130
-void
101
+int
131 102
 opensocket(void)
132 103
 {
133 104
 	sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
134 105
 	if (sockfd < 0) {
135
-		 perror("socket creation failed");
136
-		 exit(-1);
106
+		perror("socket creation failed");
107
+		return -1;
137 108
 	}
138 109
 	
139 110
 	memset(&serveraddr, 0, sizeof(serveraddr));
@@ -143,10 +114,9 @@ opensocket(void)
143 114
 	rc = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
144 115
 	if (rc < 0) {
145 116
 		perror("connect() failed");
146
-		printf("Unable to connect to ntk daemon console.\n");
147
-		exit(-1);
117
+		return -1;
148 118
 	}
149
-	printf("ntkd console connection opened successfully.\n");
119
+	return 0;
150 120
 }
151 121
 
152 122
 
@@ -160,8 +130,37 @@ closesocket(void)
160 130
 
161 131
 	if (sockfd >= 0)
162 132
 		close(sockfd);
133
+}
134
+
135
+
136
+/* Sends and receives to ntkd */
137
+void
138
+ntkd_request(command_t command)
139
+{
140
+	if (opensocket() < 0) {
141
+		printf("Unable to connect to ntk daemon console.\n");
142
+		return;
143
+	}
144
+
145
+	cmd_packet_t packetOut;
146
+	packetOut.command = command;
147
+
148
+	rc = send(sockfd, &packetOut, sizeof(packetOut), 0);
149
+	if (rc < sizeof(packetOut)) {
150
+		perror("send() failed");
151
+		exit(-1);
152
+	}
163 153
 
164
-	printf("ntkd console connection closed.\n");
154
+	char* response = (char*)malloc(CONSOLE_BUFFER_LENGTH);
155
+	request_receive(sockfd, response, CONSOLE_BUFFER_LENGTH);
156
+	if (rc < 0) {
157
+		perror("recv() failed");
158
+		exit(-1);
159
+	}
160
+
161
+	printf("Response: '%s'\n", response);
162
+	free(response);
163
+	closesocket();
165 164
 }
166 165
 
167 166
 
@@ -263,8 +262,6 @@ main(void)
263 262
 	uptime_month = timeinfo->tm_mon;
264 263
 	uptime_year = timeinfo->tm_year;
265 264
 	
266
-	opensocket();
267
-
268 265
 	printf("This is the Netsukuku Console. Please type 'help' for more information.\n");
269 266
 	for(;;) {
270 267
 		char* request = (char*)malloc(CONSOLE_BUFFER_LENGTH);
@@ -273,13 +270,15 @@ main(void)
273 270
 		fflush(stdin);
274 271
 		console(request);
275 272
 		free(request);
273
+		closesocket();
276 274
 	}
277
-	closesocket();
278 275
 
279 276
 	return 0;
280 277
 }
281 278
 
282
-void usage(void) {
279
+void
280
+usage(void)
281
+{
283 282
 	printf("Usage:\n");
284 283
 	for (int i = 0; i < sizeof(kSupportedCommands)
285 284
 		/ sizeof(kSupportedCommands[0]); i++) {

+ 1
- 1
src/ntk-console.h View File

@@ -43,7 +43,7 @@ int i;
43 43
 void usage();
44 44
 void clean_up();
45 45
 
46
-void opensocket();
46
+int opensocket();
47 47
 void closesocket();
48 48
 
49 49
 

Loading…
Cancel
Save