/*
- * ipc_c_std.c
+ * ipc_c_tcp.c
*
* Citadel/UX client/server IPC - client module using TCP/IP
*
- * version 1.3
+ * version 1.3 $Id$
*
*/
#include <string.h>
#include <pwd.h>
#include <errno.h>
+#include <stdarg.h>
+#include "citadel.h"
+#include "citadel_decls.h"
#include "ipc.h"
-
-void logoff(int code);
+#ifndef HAVE_SNPRINTF
+#include "snprintf.h"
+#endif
/*
* If server_is_local is set to nonzero, the client assumes that it is running
int serv_sock;
-void CtdlInternalTCPtimeout(int signum) {
+void timeout(int signum) {
printf("\rConnection timed out.\n");
logoff(3);
}
-int CtdlInternalTCPConnectSock(char *host, char *service, char *protocol)
+int connectsock(char *host, char *service, char *protocol)
{
struct hostent *phe;
struct servent *pse;
struct sockaddr_in sin;
int s,type;
- bzero((char *)&sin,sizeof(sin));
+ memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
pse=getservbyname(service,protocol);
phe=gethostbyname(host);
if (phe) {
- memcpy((char *)&sin.sin_addr,phe->h_addr,phe->h_length);
+ memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
}
else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
fprintf(stderr,"Can't get %s host entry: %s\n",
}
- signal(SIGALRM,CtdlInternalTCPtimeout);
+ signal(SIGALRM,timeout);
alarm(30);
if (connect(s,(struct sockaddr *)&sin,sizeof(sin))<0) {
* convert service and host entries into a six-byte numeric in the format
* expected by a SOCKS v4 server
*/
-void CtdlInternalTCPnumericize(unsigned char *buf, char *host, char *service, char *protocol)
+void numericize(char *buf, char *host, char *service, char *protocol)
{
struct hostent *phe;
struct servent *pse;
struct sockaddr_in sin;
- bzero((char *)&sin,sizeof(sin));
+ memset(&sin,0,sizeof(sin));
sin.sin_family = AF_INET;
pse=getservbyname(service,protocol);
phe=gethostbyname(host);
if (phe) {
- bcopy(phe->h_addr,(char *)&sin.sin_addr,phe->h_length);
+ memcpy(&sin.sin_addr,phe->h_addr,phe->h_length);
}
else if ((sin.sin_addr.s_addr = inet_addr(host))==INADDR_NONE) {
fprintf(stderr,"Can't get %s host entry: %s\n",
while(len<bytes) {
rlen = read(serv_sock,&buf[len],bytes-len);
if (rlen<1) {
- printf("\rNetwork error - connection terminated.\n");
- printf("%s\n", strerror(errno));
+ /* printf("\rNetwork error - connection terminated.\n");
+ printf("%s\n", strerror(errno)); */
logoff(3);
}
len = len + rlen;
retval = write(serv_sock, &buf[bytes_written],
nbytes - bytes_written);
if (retval < 1) {
+ /*
printf("\rNetwork error - connection terminated.\n");
- printf("%s\n", strerror(errno));
+ printf("%s\n", strerror(errno)); */
logoff(3);
}
bytes_written = bytes_written + retval;
*/
void serv_gets(char *buf)
{
- buf[0] = 0;
- do {
- buf[strlen(buf) + 1] = 0;
- if (strlen(buf) < 255) serv_read(&buf[strlen(buf)], 1);
- } while (buf[strlen(buf)-1] != 10);
- if (strlen(buf) > 0) buf[strlen(buf)-1] = 0;
- /* printf("> %s\n", buf); */
+ int i;
+
+ /* Read one character at a time.
+ */
+ for (i = 0;;i++) {
+ serv_read(&buf[i], 1);
+ if (buf[i] == '\n' || i == 255)
+ break;
+ }
+
+ /* If we got a long line, discard characters until the newline.
+ */
+ if (i == 255)
+ while (buf[i] != '\n')
+ serv_read(&buf[i], 1);
+
+ /* Strip the trailing newline.
+ */
+ buf[i] = 0;
}
char cithost[256]; int host_copied = 0;
char citport[256]; int port_copied = 0;
char socks4[256];
- unsigned char buf[256];
+ char buf[256];
struct passwd *p;
strcpy(cithost,DEFAULT_HOST); /* default host */
/* if not using a SOCKS proxy server, make the connection directly */
if (strlen(socks4)==0) {
- serv_sock = CtdlInternalTCPConnectSock(cithost,citport,"tcp");
+ serv_sock = connectsock(cithost,citport,"tcp");
return;
}
/* if using SOCKS, connect first to the proxy... */
- serv_sock = CtdlInternalTCPConnectSock(socks4,"1080","tcp");
+ serv_sock = connectsock(socks4,"1080","tcp");
printf("Connected to SOCKS proxy at %s.\n",socks4);
printf("Attaching to server...\r");
fflush(stdout);
- sprintf(buf,"%c%c",
+ snprintf(buf,sizeof buf,"%c%c",
4, /* version 4 */
1); /* method = connect */
serv_write(buf,2);
- CtdlInternalTCPnumericize(buf,cithost,citport,"tcp");
+ numericize(buf,cithost,citport,"tcp");
serv_write(buf,6); /* port and address */
p = (struct passwd *) getpwuid(getuid());