1 /* cvm/client_xfer_local.c - CVM client local transmission library
2  * Copyright (C) 2010  Bruce Guenter <bruce@untroubled.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18 #include <bglibs/sysdeps.h>
19 #include <unistd.h>
20 
21 #include <bglibs/socket.h>
22 
23 #include "v1client.h"
24 #include "protocol.h"
25 
26 /* UNIX local-domain socket module invocation ********************************/
cvm_xfer_local_packets(const char * path,const struct cvm_packet * request,struct cvm_packet * response)27 unsigned cvm_xfer_local_packets(const char* path,
28 				const struct cvm_packet* request,
29 				struct cvm_packet* response)
30 {
31   int sock;
32   int result;
33   unsigned io;
34   unsigned done;
35   unsigned len;
36   result = CVME_IO;
37   response->length = 0;
38   if ((sock = socket_unixstr()) != -1 &&
39       socket_connectu(sock, path)) {
40     for (done = 0, len = request->length; done < len; done += io) {
41       if ((io = write(sock, request->data+done, len-done)) == 0) break;
42       if (io == (unsigned)-1) break;
43     }
44     socket_shutdown(sock, 0, 1);
45     if (done >= len) {
46       for (done = 0; done < CVM_BUFSIZE; done += io) {
47 	if ((io = read(sock, response->data+done, CVM_BUFSIZE-done)) == 0)
48 	  break;
49 	if (io == (unsigned)-1) done = CVM_BUFSIZE+1;
50       }
51       if (done <= CVM_BUFSIZE) {
52 	response->length = done;
53 	result = 0;
54       }
55     }
56   }
57   close(sock);
58   return result;
59 }
60