2 Copyright 2003 Red Hat, Inc.
4 The Red Hat Cluster Manager API Library is free software; you can
5 redistribute it and/or modify it under the terms of the GNU Lesser
6 General Public License as published by the Free Software Foundation;
7 either version 2.1 of the License, or (at your option) any later
10 The Red Hat Cluster Manager API Library is distributed in the hope
11 that it will be useful, but WITHOUT ANY WARRANTY; without even the
12 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * Thin TCP Socket Functions for Cluster Manager API Library
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
30 #include <sys/select.h>
37 int hex2bin(char *, int, char *, int);
40 * Connect to localhost on the specified port.
42 * @param port Port to which we intend to connect.
43 * @return fd, or -1 on failure. See socket(2) and connect(2) for
47 tcp_localconnect(int port
)
50 struct sockaddr_in addr
;
54 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
58 if (!CFG_Loaded() && (CFG_ReadFile(CLU_CONFIG_FILE
) != CFG_OK
))
59 auth_md5_init(NULL
, 0);
62 CFG_Get("cluster%key", NULL
, &key
) == CFG_OK
) {
63 j
= hex2bin(key
, strlen(key
), keybuf
, sizeof(keybuf
));
65 auth_md5_init(NULL
, 0);
67 auth_md5_init(keybuf
, j
);
69 auth_md5_init(NULL
, 0);
73 setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, &j
, sizeof(j
));
76 memset(&addr
,0,sizeof(addr
));
77 addr
.sin_family
= PF_INET
;
78 addr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
79 if (port
== 0 || port
> 65535)
81 addr
.sin_port
= htons(port
);
83 j
= connect(fd
, (struct sockaddr
*)&addr
, sizeof(addr
));
84 if ((j
== 0) && (auth_md5(fd
) == 0))
95 * Send a message to a socket.
100 * @return Number of bytes send, or -1 on failure.
103 tcp_send(int fd
, void *msg
, uint32_t len
)
118 * Send the # of bytes we intend to send as first 4 bytes.
120 truesize
= len
+ sizeof(uint32_t);
121 truemsg
= malloc(truesize
);
125 memset(truemsg
, 0, truesize
);
126 ((uint32_t *)(truemsg
))[0] = len
;
127 swab32(((uint32_t *)(truemsg
))[0]);
128 memcpy(truemsg
+ sizeof(truesize
), msg
, len
);
132 tv
.tv_sec
= TCP_TIMEOUT
;
135 ret
= (ssize_t
)select(fd
+1, NULL
, &set
, NULL
, &tv
);
143 ret
= write(fd
, truemsg
, truesize
);
149 if (ret
< truesize
) {
159 * Send a message to a socket.
164 * @return Number of bytes send, or -1 on failure.
167 tcp_receive(int fd
, void *msg
, uint32_t len
)
170 uint32_t length
, truesize
;
179 tv
.tv_sec
= TCP_TIMEOUT
;
182 ret
= (ssize_t
)select(fd
+1, NULL
, &set
, NULL
, &tv
);
189 ret
= read(fd
, (void *)&length
, sizeof(length
));
190 if (ret
!= sizeof(length
))
195 if (length
> TCP_MAXLEN
)
198 truesize
= (length
< len
) ? length
: len
;
199 ret
= read(fd
, msg
, truesize
);
203 if (ret
< truesize
) {