• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

contrib/maloc/tools/main.c

00001 /*
00002  * ***************************************************************************
00003  * MALOC = < Minimal Abstraction Layer for Object-oriented C >                                               
00004  * Copyright (C) 1994--2008 Michael Holst                                                                    
00005  *                                                                                                           
00006  * This library is free software; you can redistribute it and/or                                             
00007  * modify it under the terms of the GNU Lesser General Public                                                
00008  * License as published by the Free Software Foundation; either                                              
00009  * version 2.1 of the License, or (at your option) any later version.                                        
00010  *                                                                                                           
00011  * This library 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 GNU                                          
00014  * Lesser General Public License for more details.                                                           
00015  *                                                                                                           
00016  * You should have received a copy of the GNU Lesser General Public                                          
00017  * License along with this library; if not, write to the Free Software                                       
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  * 
00020  * rcsid="$Id: main.c,v 1.12 2008/02/22 23:06:00 fetk Exp $"
00021  * ***************************************************************************
00022  */
00023 
00024 /*
00025  * ***************************************************************************
00026  * File:     bridge.c
00027  *
00028  * Purpose:  Socket-to-socket bridge.
00029  *
00030  * Notes:    The source and destination sockets can be any combination of
00031  *           UNIX domain (AF_UNIX, AF_LOCAL) and internet (AF_INET) sockets.
00032  *
00033  * Author:   Michael Holst
00034  * ***************************************************************************
00035  */
00036 
00037 #include "maloc/maloc.h"
00038 
00039 #define VEMBED(rctag) VPRIVATE const char* rctag; \
00040     static void* use_rcsid=(0 ? &use_rcsid : (void*)&rcsid);
00041 VEMBED(rcsid="$Id: main.c,v 1.12 2008/02/22 23:06:00 fetk Exp $")
00042 
00043 /* int MAIN__(void) {}; */
00044 
00045 int main(int argc, char *argv[])
00046 {
00047     char inType[80], outType[80], hostname[64], buf[VMAX_BUFSIZE];
00048     int  ok, inputWasFile, done, firstTime, bufsize;
00049     Vio   *inSock, *outSock;
00050 
00051     inputWasFile = 0;
00052     ok = 1;
00053     if (argc == 4) {
00054         strncpy(hostname,"localhost",sizeof(hostname));
00055         fprintf(stderr,"%s: Starting up on host <%s>\n", argv[0], hostname);
00056         if (!strcmp(argv[1],"-u2i")) {
00057             strcpy(inType,"UNIX");
00058             strcpy(outType,"INET");
00059         } else if (!strcmp(argv[1],"-i2u")) {
00060             strcpy(inType,"INET");
00061             strcpy(outType,"UNIX");
00062         } else if (!strcmp(argv[1],"-u2u")) {
00063             strcpy(inType,"UNIX");
00064             strcpy(outType,"UNIX");
00065         } else if (!strcmp(argv[1],"-i2i")) {
00066             strcpy(inType,"INET");
00067             strcpy(outType,"INET");
00068         } else if (!strcmp(argv[1],"-f2u")) {
00069             strcpy(inType,"FILE");
00070             strcpy(outType,"UNIX");
00071             inputWasFile = 1;
00072         } else if (!strcmp(argv[1],"-f2i")) {
00073             strcpy(inType,"FILE");
00074             strcpy(outType,"INET");
00075             inputWasFile = 1;
00076         } else ok = 0;
00077     } else ok = 0;
00078 
00079     VJMPERR1( ok );
00080 
00081     /* start the Vio communication layer */
00082     Vio_start();
00083 
00084     /* open the input socket */
00085     if ( VNULL==(inSock=Vio_ctor(inType,"ASC",hostname,argv[2],"r")) ) {
00086         fprintf(stderr,"%s: Problem open input socket <%s>\n",
00087             argv[0],argv[2]);
00088         VJMPERR1(0);
00089     } else {
00090         firstTime = 1;
00091         done = 0;
00092         while (!done) {
00093 
00094             /* do some extra stuff the first time */
00095             if (firstTime) {
00096                 firstTime = 0;
00097 
00098                 /* if input was a file, we just do one shot */
00099                 if (inputWasFile) done = 1;
00100 
00101             /* must be non-file input; go to sleep for a bit */
00102             } else {
00103                 /* (don't need this since now do BLOCKing accept/connect) */
00104                 /* Vsig_sleep(100); */
00105             }
00106 
00107             /* call (blocking) accept on input socket */
00108             if ( 0 > Vio_accept(inSock,0) ) {
00109                 fprintf(stderr,"%s: Problem accept input socket <%s>\n",
00110                     argv[0], argv[3]);
00111                 VJMPERR1(0);
00112             } else {
00113 
00114                 /* some i/o */
00115                 fprintf(stderr,"%s: Hit on %s port <%s> from <%s>\n",
00116                     argv[0],inType,inSock->file,inSock->rhost);
00117 
00118                 /* now that we have some data, open the output socket */
00119                 if ( VNULL==(outSock=Vio_ctor(outType,"ASC",
00120                   hostname,argv[3],"w")) ) {
00121                     fprintf(stderr,"%s: Problem open output socket <%s>\n",
00122                         argv[0], argv[3]);
00123                     VJMPERR1(0);
00124                 } else {
00125 
00126                     /* call (blocking) connect on output socket */
00127                     if ( 0 > Vio_connect(outSock,0) ) {
00128                         fprintf(stderr,"%s: Problem conn output socket <%s>\n",
00129                             argv[0], argv[3]);
00130                         VJMPERR1(0);
00131                     } else {
00132 
00133                         /* some i/o */
00134                         fprintf(stderr,"%s: Pass to %s port <%s> on <%s>\n",
00135                             argv[0],outType,outSock->file,outSock->lhost);
00136 
00137                         /* grab input and write to output until done */
00138                         memset(buf, '\0', sizeof(buf));
00139                         while (0<(bufsize=Vio_read(inSock,buf,sizeof(buf)))) {
00140                             VJMPERR2(bufsize==Vio_write(outSock,buf,bufsize));
00141                             memset(buf, '\0', sizeof(buf));
00142                         }
00143 
00144                         /* release the input subsocket and the output socket */
00145                       VERROR2:
00146                         Vio_connectFree(outSock);
00147                     }
00148                     /* close output socket */
00149                     Vio_dtor(&outSock);
00150                 }
00151                 /* free the input subsocket */
00152                 Vio_acceptFree(inSock);
00153             }
00154         }
00155     }
00156     /* close input socket */
00157     Vio_dtor(&inSock);
00158 
00159     /* stop the Vio communication layer */
00160     Vio_stop();
00161 
00162     /* return with no errors */
00163     fprintf(stderr,"%s: Shutting down on host <%s>\n", argv[0], hostname);
00164     return 1;
00165 
00166   VERROR1:
00167     fprintf(stderr,"Usage: %s -u2i unixSocket inetSocket\n", argv[0]);
00168     fprintf(stderr,"   or: %s -i2u inetSocket unixSocket\n", argv[0]);
00169     fprintf(stderr,"   or: %s -u2u unixSocket unixSocket\n", argv[0]);
00170     fprintf(stderr,"   or: %s -i2i inetSocket inetSocket\n", argv[0]);
00171     fprintf(stderr,"   or: %s -f2u fileName   unixSocket\n", argv[0]);
00172     fprintf(stderr,"   or: %s -f2i fileName   inetSocket\n", argv[0]);
00173     return 0;
00174 }
00175 

Generated on Wed Oct 20 2010 11:12:14 for APBS by  doxygen 1.7.2