#define INADDR_NONE 0xffffffff
#endif
-int sock_connect(char *host, char *service, char *protocol)
+int sock_connect(char *host, char *service)
{
struct hostent *phe;
struct servent *pse;
return(-1);
if ((service == NULL) || IsEmptyStr(service))
return(-1);
- if ((protocol == NULL) || IsEmptyStr(protocol))
- return(-1);
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
- pse = getservbyname(service, protocol);
+ pse = getservbyname(service, "tcp");
if (pse) {
sin.sin_port = pse->s_port;
} else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
host, strerror(errno));
return(-1);
}
- if ((ppe = getprotobyname(protocol)) == 0) {
- CtdlLogPrintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
- protocol, strerror(errno));
+ if ((ppe = getprotobyname("tcp")) == 0) {
+ CtdlLogPrintf(CTDL_CRIT, "Can't get tcp protocol entry: %s\n", strerror(errno));
return(-1);
}
- if (!strcmp(protocol, "udp")) {
- type = SOCK_DGRAM;
- } else {
- type = SOCK_STREAM;
- }
+ type = SOCK_STREAM;
s = socket(PF_INET, type, ppe->p_proto);
if (s < 0) {
int bytes,
int timeout)
{
- CitContext *CCC=CC;
+ CitContext *CCC = MyContext();
const char *Error;
int retval = 0;
int sock_read_to(int *sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
{
- CitContext *CCC=CC;
+ CitContext *CCC = MyContext();
int rc;
+ FlushStrBuf(CCC->MigrateBuf);
rc = socket_read_blob(sock,
CCC->sMigrateBuf,
bytes,
int CtdlSockGetLine(int *sock, StrBuf *Target)
{
- CitContext *CCC=CC;
+ CitContext *CCC = MyContext();
const char *Error;
int rc;
+ FlushStrBuf(Target);
rc = StrBufTCP_read_buffered_line_fast(Target,
CCC->sReadBuf,
&CCC->sPos,
int sock_getln(int *sock, char *buf, int bufsize)
{
int i, retval;
- CitContext *CCC=CC;
+ CitContext *CCC = MyContext();
const char *pCh;
+ FlushStrBuf(CCC->sMigrateBuf);
retval = CtdlSockGetLine(sock, CCC->sMigrateBuf);
i = StrLength(CCC->sMigrateBuf);
FlushStrBuf(CCC->sMigrateBuf);
if (retval < 0) {
safestrncpy(&buf[i], "000", bufsize - i);
+ i += 3;
}
- return(retval >= 0);
+ return i;
}
* sock_write() - send binary to server.
* Returns the number of bytes written, or -1 for error.
*/
-int sock_write(int sock, char *buf, int nbytes)
+int sock_write(int *sock, const char *buf, int nbytes)
{
int bytes_written = 0;
int retval;
- while (bytes_written < nbytes) {
- retval = write(sock, &buf[bytes_written],
+
+ while ((*sock != -1) &&
+ (bytes_written < nbytes))
+ {
+ retval = write(*sock, &buf[bytes_written],
nbytes - bytes_written);
if (retval < 1) {
+ sock_close(*sock);
+ *sock = -1;
return (-1);
}
bytes_written = bytes_written + retval;
* sock_puts() - send line to server - implemented in terms of serv_write()
* Returns the number of bytes written, or -1 for error.
*/
-int sock_puts(int sock, char *buf)
+int sock_puts(int *sock, char *buf)
{
int i, j;