ACQ2XX_API
connectto.cpp
Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* connectto.c - dt100 network attached satellite DAQ                        */
00003 /* ------------------------------------------------------------------------- */
00004 /*   Copyright (C) 2003 Peter Milne, D-TACQ Solutions Ltd
00005  *                      <Peter dot Milne at D hyphen TACQ dot com>
00006  
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of Version 2 of the GNU General Public License
00009     as published by the Free Software Foundation;
00010  
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015  
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00019 /* ------------------------------------------------------------------------- */
00020 
00021 /** @file connectto.cpp socket client utils. */
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <stdarg.h>
00026 #include <string.h>
00027 #include <errno.h>
00028 #include <netdb.h>
00029 #include <sys/socket.h>
00030 #include <netinet/in.h>
00031 #include <arpa/inet.h>
00032 #include <signal.h>
00033 #include "iclient3.h"
00034 
00035 char *program_name;
00036 
00037 #define EXIT _exit
00038 
00039 /* error - print a diagnostic and optionally exit */
00040 void error( int status, int err, const char *fmt, ... )
00041 {
00042         va_list ap;
00043 
00044         va_start( ap, fmt );
00045         fprintf( stderr, "%s: ", program_name );
00046         vfprintf( stderr, fmt, ap );
00047         va_end( ap );
00048         if ( err )
00049                 fprintf( stderr, ": %s (%d)\n", strerror( err ), err );
00050         if ( status )
00051                 EXIT( status );
00052 }
00053 
00054 /* set_address - fill in a sockaddr_in structure */
00055 void set_address(const char *hname, const char *sname,
00056                   struct sockaddr_in *sap, const char *protocol )
00057 {
00058         struct servent *sp;
00059         struct hostent *hp;
00060         char *endptr;
00061         short port;
00062 
00063         bzero( sap, sizeof( *sap ) );
00064         sap->sin_family = AF_INET;
00065         if ( hname != NULL ){
00066                 if ( !inet_aton( hname, &sap->sin_addr ) ){
00067                         hp = gethostbyname( hname );
00068                         if ( !hp ){
00069                                 error( 1, 0, "unknown host: %s\n", hname );
00070                         }
00071                         sap->sin_addr = *( struct in_addr * )hp->h_addr;
00072                 }
00073         }
00074         else{
00075                 sap->sin_addr.s_addr = htonl( INADDR_ANY );
00076         }
00077         port = strtol( sname, &endptr, 0 );
00078         if ( *endptr == '\0' ){
00079                 sap->sin_port = htons( port );
00080         }else{
00081                 sp = getservbyname( sname, protocol );
00082                 if ( !sp ){
00083                         error( 1, 0, "unknown service: %s\n", sname );
00084                 }
00085                 sap->sin_port = sp->s_port;
00086         }
00087 }
00088 
00089 /* readline - read a newline terminated record */
00090 int readline( SOCKET fd, char *bufptr, size_t len )
00091 {
00092         char *bufx = bufptr;
00093         static char *bp;
00094         static int cnt = 0;
00095         static char b[1];
00096         char c;
00097 
00098         while ( --len > 0 )
00099         {
00100                 if ( --cnt <= 0 )
00101                 {
00102                         cnt = read( fd, b, sizeof( b ));
00103                         if ( cnt < 0 )
00104                         {
00105                                 fprintf(stderr, "read err %d\n", errno);
00106 
00107                                 if ( errno == EINTR )
00108                                         continue;
00109                                 return -1;
00110                         }
00111                         if ( cnt == 0 )
00112                                 return 0;
00113                         bp = b;
00114                 }
00115                 c = *bp++;
00116                 *bufptr++ = c;
00117                 if ( c == '\n' )
00118                 {
00119                         *bufptr = '\0';
00120                         return bufptr - bufx;
00121                 }
00122         }
00123         errno = EMSGSIZE;
00124         return -1;
00125 }