static int serve_file (CC_SFILE *f, char *probfname, int silent) { char request; char probbuf[1024]; int id; int rval; rval = CCutil_sread_char (f, &request); if (rval) { fprintf (stderr, "CCutil_sread_char failed\n"); return rval; } rval = CCutil_sread_string (f, probbuf, sizeof (probbuf)); if (rval) { fprintf (stderr, "CCutil_sread_string failed\n"); return rval; } rval = CCutil_sread_int (f, &id); if (rval) { fprintf (stderr, "CCutil_sread_int failed\n"); return rval; } if (strcmp (probfname, probbuf)) { fprintf (stderr, "ERROR - serving %s, request %s\n", probfname, probbuf); return rval; } switch (request) { case CCtsp_Pread: rval = serve_read (f, probfname, id, silent); if (rval) { fprintf (stderr, "serve_read failed\n"); return rval; } break; case CCtsp_Pwrite: rval = serve_write (f, probfname, id, silent); if (rval) { fprintf (stderr, "serve_write failed\n"); return rval; } break; case CCtsp_Pdelete: rval = serve_delete (probname, id); if (rval) { fprintf (stderr, "serve_delete failed\n"); return rval; } break; default: fprintf (stderr, "Invalid request %c\n", request); return 1; } return 0; }
/* * This the main loop receiving, processing, and sending packets. */ int serve(ndd_t *nds) { ndpkt_t *pkt; int rc; /* * Since we process only packet at time, we need just one * preallocated buffer. */ if ((pkt = (ndpkt_t *)malloc(ND_MAXPKT)) == NULL) { log_msg(0, "Unable to allocate buffer: %s.", strerror(errno)); return (1); } do { ssize_t pkt_len; int op, err; /* Get packet from the network */ pkt_len = recv(nds->sc_fd, (void *)pkt, ND_MAXPKT, 0); if (nds->exiting) break; if (pkt_len == -1) { log_msg(0, "Unable to receive packet: %s.", strerror(errno)); break; } /* Verify just received packet */ err = verify_pkt(pkt, pkt_len); /* ND protocol supports only 2 operations: read and write */ op = pkt->np_op & ND_OP_CODE; if (op == ND_OP_READ) { rc = serve_read(nds, pkt, err); } else if (op == ND_OP_WRITE) { rc = serve_write(nds, pkt, err); } else { log_msg(1, "Unknown operation %d.", op); rc = 1; } } while (rc == 0 && nds->exiting == 0); free(pkt); return (rc); }