X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=ctdlsh%2Fsrc%2Fsockets.c;h=3aaf7714064666dd55c79edf46462ffd2a5029e2;hb=9c2ebc50efa9c120b1969e78cea2f5256416b5b9;hp=00f0d7e350f3e1e36d1c38e028eec5c0c1397cdb;hpb=63608587a5e6dcd717ef56afd93badc78836346e;p=citadel.git diff --git a/ctdlsh/src/sockets.c b/ctdlsh/src/sockets.c index 00f0d7e35..3aaf77140 100644 --- a/ctdlsh/src/sockets.c +++ b/ctdlsh/src/sockets.c @@ -1,89 +1,40 @@ /* - * + * This file contains functions which handle ctdlsh's connection + * to the Citadel server's admin socket. + * + * Copyright (c) 2009-2012 by the Citadel.org team. + * This program is open source software, cheerfully made available to you under + * the terms of the GNU General Public License version 3. */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef INADDR_NONE -#define INADDR_NONE 0xffffffff -#endif - -int sock_connect(char *host, char *service, char *protocol) +#include "ctdlsh.h" + + +int uds_connectsock(char *sockpath) { - struct hostent *phe; - struct servent *pse; - struct protoent *ppe; - struct sockaddr_in sin; - struct sockaddr_in egress_sin; - int s, type; - - if (host == NULL) return(-1); - if (service == NULL) return(-1); - if (protocol == NULL) return(-1); - - memset(&sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; - - pse = getservbyname(service, protocol); - if (pse) { - sin.sin_port = pse->s_port; - } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) { - fprintf(stderr, "Can't get %s service entry: %s\n", - service, strerror(errno)); - return(-1); - } - phe = gethostbyname(host); - if (phe) { - 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", - host, strerror(errno)); - return(-1); - } - if ((ppe = getprotobyname(protocol)) == 0) { - fprintf(stderr, "Can't get %s protocol entry: %s\n", - protocol, strerror(errno)); - return(-1); - } - if (!strcmp(protocol, "udp")) { - type = SOCK_DGRAM; - } else { - type = SOCK_STREAM; - } + struct sockaddr_un addr; + int s; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sockpath, sizeof addr.sun_path); - s = socket(PF_INET, type, ppe->p_proto); + s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { - fprintf(stderr, "Can't create socket: %s\n", strerror(errno)); + fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno)); return(-1); } - /* Now try to connect to the remote host. */ - if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { - fprintf(stderr, "Can't connect to %s:%s: %s\n", - host, service, strerror(errno)); + if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno)); close(s); return(-1); } - return (s); + return s; } - /* * sock_read_to() - input binary data from socket, with a settable timeout. * Returns the number of bytes read, or -1 for error. @@ -153,7 +104,6 @@ int sock_write(int sock, char *buf, int nbytes) } - /* * Input string from socket - implemented in terms of sock_read() * @@ -189,9 +139,8 @@ int sock_getln(int sock, char *buf, int bufsize) } - /* - * sock_puts() - send line to server - implemented in terms of serv_write() + * sock_puts() - send line to server - implemented in terms of sock_write() * Returns the number of bytes written, or -1 for error. */ int sock_puts(int sock, char *buf) @@ -204,3 +153,20 @@ int sock_puts(int sock, char *buf) if (j<0) return(j); return(i+j); } + + +/* + * Write a formatted string to the server - implemented in terms of sock_write() + */ +void sock_printf(int sock, const char *format,...) +{ + va_list arg_ptr; + char buf[4096]; + size_t len; + + va_start(arg_ptr, format); + vsnprintf(buf, sizeof buf, format, arg_ptr); + va_end(arg_ptr); + + sock_write(sock, buf, strlen(buf)); +}