Skip to content
Permalink
Browse files

Fixed receive segmentation fault because of multiple messages from da…

…emon at once and fixed some memory leaks.
  • Loading branch information...
sweetpi committed Aug 22, 2013
1 parent f677b18 commit b61ff9fc51befbb8a9722defdd17f3ca3af83f4e
Showing with 35 additions and 12 deletions.
  1. +35 −12 receive.c
@@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with pilight. If not, see <http://www.gnu.org/licenses/>
*/

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -44,13 +44,13 @@ int main(int argc, char **argv) {
progname = strdup("pilight-receive");
struct options_t *options = NULL;

JsonNode *json = json_mkobject();
JsonNode *json = NULL;

char *server = strdup("127.0.0.1");
unsigned short port = PORT;

int sockfd = 0;
char *recvBuff;
char *recvBuff = NULL;
char *message = NULL;
char *args = NULL;
steps_t steps = WELCOME;
@@ -103,40 +103,63 @@ int main(int argc, char **argv) {
while(1) {
if(steps > WELCOME) {
/* Clear the receive buffer again and read the welcome message */
if((recvBuff = socket_read(sockfd)) != NULL) {

json = json_decode(recvBuff);
json_find_string(json, "message", &message);
} else {
recvBuff = socket_read(sockfd);
if(recvBuff == NULL) {
goto close;
}
}

switch(steps) {
case WELCOME:
socket_write(sockfd, "{\"message\":\"client receiver\"}");
steps=IDENTIFY;
break;
case IDENTIFY:
{
//extract the message
json = json_decode(recvBuff);
json_find_string(json, "message", &message);
assert(message != NULL);
if(strcmp(message, "accept client") == 0) {
steps=RECEIVE;
}
if(strcmp(message, "reject client") == 0) {
} else if(strcmp(message, "reject client") == 0) {
steps=REJECT;
} else {
assert(false);
}
//cleanup
json_delete(json);
json = NULL;
message = NULL;
}
break;
case RECEIVE:
printf("%s\n", json_stringify(json, "\t"));
{
char* line = strtok (recvBuff,"\n");
//for each line
while(line != NULL) {
json = json_decode(recvBuff);
assert(json != NULL);
char* output = json_stringify(json, "\t");
printf("%s\n", output);
free(output);
json_delete(json);
line = strtok (NULL,"\n");
}
}
break;
case REJECT:
default:
goto close;
break;
}
free(recvBuff);
recvBuff = NULL;
}
close:
socket_close(sockfd);
free(progname);
free(server);
free(message);

return EXIT_SUCCESS;
}

0 comments on commit b61ff9f

Please sign in to comment.
You can’t perform that action at this time.