--- cfrun.c Thu Dec 21 21:54:44 2000 +++ cfrun_bas.c Fri Feb 8 14:52:06 2002 @@ -68,6 +68,12 @@ void CheckAccess(char *users); void cfrunSyntax(); +/* + * Bas +*/ +int ParseHostname(char *hostname, char *new_hostname); + + /*******************************************************************/ /* Level 0 : Main */ /*******************************************************************/ @@ -99,11 +105,11 @@ Debug("pid = %d i = %d\n", getpid(), i); if (ConnectToServer(ip->name,ip->classes, FileFlag)) { - Verbose("Connection done...\n"); + printf("Connection %s done...\n", ip->name); } else { - Verbose("Connection refused...\n"); + printf("Connection %s refused...\n", ip->name); } exit(0); } @@ -349,6 +355,18 @@ /********************************************************************/ +int ParseHostname(hostname, new_hostname) + +char *hostname; +char *new_hostname; + +{ int port=0; + +sscanf(hostname,"%[^:]:%d", new_hostname, &port); + +return(port); +} + int ConnectToServer(host,options, StoreInFile) char *host, *options; @@ -359,9 +377,10 @@ char sendbuffer[bufsize]; char recvbuffer[bufsize]; char filebuffer[bufsize]; + char parsed_host[bufsize]; struct servent *server; - int sd,err,n_read; + int sd,err,n_read, port; char *sp; FILE *fp; @@ -375,33 +394,49 @@ { fp = stdout; } - -FileVerbose(fp, "Connecting to server %s with options %s %s\n", - host,options,CFRUNOPTIONS); -if ((hp = gethostbyname(host)) == NULL) +port = ParseHostname(host, parsed_host); +FileVerbose(fp, "Connecting to server %s to port %d with options %s %s\n", + parsed_host,port,options,CFRUNOPTIONS); + +if ((hp = gethostbyname(parsed_host)) == NULL) { - printf("Unknown host: %s\n", host); + printf("Unknown host: %s\n", parsed_host); printf("Make sure that fully qualified names can be looked up at your site!\ n"); printf("i.e. prep.ai.mit.edu, not just prep. If you use NIS or /etc/hosts\n" ); printf("make sure that the full form is registered too as an alias!\n"); exit(1); } -if ((server = getservbyname(CFENGINE_SERVICE,"tcp")) == NULL) +bzero(&raddr,sizeof(raddr)); + +/* + * Bas: If port is set then use it else default one +*/ +if (port) { - perror("getservbyname"); - exit (1); + /* Thanks to Walter de Jong: walter@sara.nl */ + raddr.sin_port = htons(port); + } +else + { + if ((server = getservbyname(CFENGINE_SERVICE,"tcp")) == NULL) + { + perror("getservbyname"); + exit (1); + } + else + { + raddr.sin_port = (unsigned int) server->s_port; + } } -bzero(&raddr,sizeof(raddr)); - -raddr.sin_port = (unsigned int) server->s_port; raddr.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr; -raddr.sin_family = AF_INET; +raddr.sin_family = AF_INET; -Debug("Trying to connect to %s = %s, port h=%d n=%d\n", - host,inet_ntoa(raddr.sin_addr),ntohs(server->s_port),server->s_port); +Debug("Trying to connect to %s = %s, port h=%d n=%d\n", + parsed_host, inet_ntoa(raddr.sin_addr), + ntohs(raddr.sin_port), raddr.sin_port); if ((sd = socket(AF_INET,SOCK_STREAM,0)) == -1) { @@ -416,7 +451,7 @@ { alarm(0); signal(SIGALRM,SIG_DFL); - printf("Host %s isn't talking to anyone\n\n",host); + printf("Host %s isn't talking to anyone\n\n",parsed_host); return false; } @@ -441,13 +476,16 @@ SendClassData(sd,sendbuffer); -FileVerbose(fp, "\n%s replies..\n\n",host); +FileVerbose(fp, "\n%s replies..\n\n",parsed_host); while (true) { bzero(recvbuffer,bufsize); - if ((n_read = recv(sd, recvbuffer, bufsize,0)) == -1) + /* + * Bas: without MSG_WAITALL we did not got all the output, was 0 + */ + if ((n_read = recv(sd, recvbuffer, bufsize, MSG_WAITALL)) == -1) { if (errno == EINTR) {