- if(request == ADD_HOST)
- {
- if((lenin = read(cl->meta_socket, &tmp[1], sizeof(add_host_t) - 1)) <= 0)
- {
- syslog(LOG_ERR, "Receive failed for ADD_HOST: %m");
- return -1;
- }
- }
+ cl->reqlen = 0;
+
+ for(i = oldlen; i < cl->buflen; i++)
+ {
+ if(cl->buffer[i] == '\n')
+ {
+ cl->buffer[i] = 0; /* replace end-of-line by end-of-string so we can use sscanf */
+ cl->reqlen = i + 1;
+ break;
+ }
+ }
+
+ if(cl->reqlen)
+ {
+ if(sscanf(cl->buffer, "%d", &request) == 1)
+ {
+ if(request_handlers[request] == NULL)
+ {
+ syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
+ return 0;
+ }
+
+ if(debug_lvl > 3)
+ syslog(LOG_DEBUG, "Got request: %s", cl->buffer);
+
+ request_handlers[request](cl);
+ }
+ else
+ {
+ syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
+ }
+
+ cl->buflen -= cl->reqlen;
+ memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
+ oldlen = 0;
+ }