/***************************************************************** * sthp_cli.c the execute module is is implementation * the client side of String Type Handle Protocol * * Copyright (C) 2009 Andrey Y. Rjavskov(aka rjaan) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *****************************************************************/ #include #include #include #include #include #include #include #include #include "sthp.h" static int connect_to_unixserver ( const char *unix_path ); int main ( char** argv, int argc ) { while ( 1 ) { int sockfd; char* ask = NULL; char* reply = NULL; print_stderr ( 1, "=> " ); ask = sthp_puts(); if( *ask == '^' && *(ask+1) == 'q' ){ break; } sockfd = connect_to_unixserver ( STHP_UNIXSERVER_PATH ); if( sockfd == -1 ) { continue; } sthp_set_signal ( &sockfd ); print_stderr ( strlen(ask), "STRING ASC: \"%s\"\n", ask); if( sthp_send ( sockfd, ask ) == -EXIT_FAILURE ) { goto end_cycle; } if ( sthp_recv ( sockfd, &reply ) == -EXIT_FAILURE ) { close ( sockfd ); goto end_cycle; } print_stderr ( strlen(ask), "ACCEPTED REPLY: %s\n", reply ); end_cycle: if ( reply )sthp_free ( reply ); if ( ask ) sthp_free ( ask ); close ( sockfd ); } print_stderr ( 0, "programm terminated!\n"); return EXIT_SUCCESS; } static int connect_to_unixserver ( const char *unix_path ) { int sockfd; struct sockaddr_un servaddr; sockfd = socket ( PF_LOCAL, SOCK_STREAM, 0 ); if ( sockfd == -1 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp client error: can\'t create socket!\n" ); return -1; } bzero( &servaddr, sizeof ( struct sockaddr_un ) ); servaddr.sun_family = AF_LOCAL; strcpy ( servaddr.sun_path, STHP_UNIXSERVER_PATH ); if ( connect ( sockfd, (struct sockaddr*)&servaddr, sizeof ( struct sockaddr_un ) ) == -1 ) { perror(""); print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: can\'t connect to server with unix path(%s)!\n", STHP_UNIXSERVER_PATH ); close ( sockfd ); return -1; } return sockfd; } /*eof*/