Changeset 78 for SH_SHM/trunk


Ignore:
Timestamp:
11/10/2008 03:58:38 PM (15 years ago)
Author:
marcus
Message:

r61 | walther | 2008-11-10 16:11:15 +0100 (Mo, 10 Nov 2008) | 3 lines

  • 64bit compatibility for net I/O
  • sample configuration file for shd process
Location:
SH_SHM/trunk/source
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • SH_SHM/trunk/source/port_io.c

    r16 r78  
    22 *      ========= 
    33 * 
    4  * version 2, 9-Jul-2006 
     4 * version 3, 9-Jul-2006 
    55 *  
    66 * socket server test program 
    77 * 
    88 * K. Stammler, 28-Jun-2006 
    9  */ 
    10  
    11  
     9 * M. Walther, 22-Oct-2008 
     10 */ 
     11 
     12 
     13#include <arpa/inet.h> 
    1214#include <stdio.h> 
     15#include <string.h> 
    1316#include <errno.h> 
    1417#include <stdlib.h> 
     
    8689        int      i;                          /* counter */ 
    8790        struct sockaddr_in clientname;       /* name of client */ 
    88         size_t   size;                       /* size of clientname structure */ 
     91        socklen_t   size;                       /* size of clientname structure */ 
    8992 
    9093        /* executable code */ 
  • SH_SHM/trunk/source/shd.c

    r16 r78  
    1  
    21/* file shd.c 
    32 *      ===== 
    43 * 
    54 * version 3, 11-Jul-2006 
    6  *  
     5 * version 4, 6-Dec-2006 (upload and timeout changes) 
     6 * version 5, 27-Feb-2007 (delete uploaded files) 
     7 * version 6, 22-Oct-2008 (compatibility to 64-bit machines) 
     8 * 
    79 * SH daemon program 
    810 * 
     
    1012 */ 
    1113 
    12  
     14#include <ctype.h> 
     15#include <time.h> 
    1316#include <stdio.h> 
     17#include <string.h> 
    1418#include <errno.h> 
    1519#include <stdlib.h> 
     
    2024#include <netdb.h> 
    2125#include "port_io.h" 
    22  
    2326 
    2427#define cShd_MAXBUF  512 
     
    3740void shd_send_command( char hostname[], int portno, char cmd[], int respsock, 
    3841        int do_upload ); 
    39 void shd_upload( int src_sock, int dst_sock ); 
     42void shd_upload( int src_sock, int dst_sock, char filename[] ); 
    4043 
    4144/* global variables */ 
    4245static int *shdv_port;                     /* port numbers used */ 
    4346static int *shdv_hash;                     /* hash numbers */ 
     47 
    4448/* global parameters */ 
    4549static int shdv_portno=18005;              /* port number for daemon */ 
     
    4852static int shdv_debuglevel=1;              /* debug level */ 
    4953static FILE *shdv_log=NULL;                /* log file */ 
    50  
     54static char shdv_uploaddir[]={"/tmp/"};         /* directory for upload */ 
     55static int shdv_timeout=5;                 /* timeout for reading from server */ 
     56 
     57/* more parameters plus another variable */ 
     58static int shdv_maxfiles=50000;              /* maximum number of uploaded files */ 
     59static int shdv_namelth=200;               /* length of file name */ 
     60static char shdv_file[50000][200];           /* files uploaded in session 
     61                                            * size: shdv_maxfiles * shdv_namelth */ 
    5162 
    5263 
     
    7081        int      do_upload;           /* upload requested */ 
    7182 
    72         /* executable code */ 
     83 
     84        /* executable code */ 
    7385 
    7486        if  (argc != 2)  { 
     
    116128                                        if  (hash > 0)  { 
    117129                                                shport = shd_hash2port( hash ); 
     130                                                // debugging: 
     131                                                printf( "\n==================\nUsing port No %d\n==================\n", shport); 
     132                                                if (shport > 500+shdv_baseport) { 
     133                                                        printf("\n mark port no %d for free\n", shport-480); 
     134                                                        shd_closeport( shport-480 ); 
     135                                                } 
     136                                                if (shport > 900+shdv_baseport) { 
     137                                                        printf("\n mark port no %d for free\n", shport-780); 
     138                                                        shd_closeport( shport-780 ); 
     139                                                } 
    118140                                                sprintf( msg, "$SH_UTIL/start_sh_at_port.csh %d %d &", 
    119141                                                        shport, hash ); 
     
    141163                                                shd_send_command( "localhost", shdv_port[i], "quit y", 0, do_upload ); 
    142164                                        } /*endif*/ 
     165 
     166                                /* remove uploaded files */ 
     167                                for (i=0; i<shdv_maxfiles; i++) 
     168                                        if (strcmp(shdv_file[i], "") != 0 ) { 
     169                                                if  (shdv_debuglevel > 5 ) 
     170                                                        fprintf( shdv_log, "shd-6: remove file %s\n", shdv_file[i] ); 
     171                                                        /* get file name and dir --> misuse variable "cmd" */ 
     172                                                        sprintf( cmd, "%s%s", shdv_uploaddir, shdv_file[i] ); 
     173                                                        if (remove( cmd ) < 0 ) 
     174                                                                fprintf( shdv_log, "shd: cannot remove %s!\n", shdv_file[i] ); 
     175                                        } /*endif*/ 
     176                                /*endfor*/ 
     177 
    143178                                strcpy( msg, "shd: terminated all active SH sessions\n" ); 
    144179                                write( sock, msg, strlen(msg)+1 ); 
    145180                                write( sock, cPio_TERM, cPio_TERMLTH ); 
    146181                                break; 
     182                        } else if  (strcmp(cmd,"list-files") == 0)  { 
     183                                *msg = '\0'; 
     184                                for (i=0; i<shdv_maxfiles; i++) 
     185                                        if (strcmp(shdv_file[i], "") != 0 ) 
     186                                                sprintf( msg+strlen(msg), "%s ", shdv_file[i] ); 
     187 
     188                                write( sock, msg, strlen(msg)+1 ); 
     189                                write( sock, cPio_TERM, cPio_TERMLTH ); 
    147190                        } else if  (sscanf(cmd,"%d",&hash) == 1)  { 
    148191                                shport = shd_hash2port( hash ); 
     
    150193                                while  (buffer[i] == ' ') i++; 
    151194                                if  (strncasecmp(buffer+i,"quit",4) == 0 && buffer[i+4] <= ' ')  { 
    152                                         shd_closeport( shport ); 
     195                                        /* rude hack to prevent double use of ports */ 
     196                                        //shd_closeport( shport ); 
    153197                                        shd_send_command( "localhost", shport, "quit y", 0, do_upload ); 
    154198                                        sprintf( msg, "SH at port %d terminated\n", shport ); 
     199 
     200                                        /* remove former uploaded files 
     201                                         * should be per session, but not implemented 
     202                                         * xxx - todo */ 
     203 
    155204                                        write( sock, msg, strlen(msg)+1 ); 
    156205                                        write( sock, cPio_TERM, cPio_TERMLTH ); 
     206                                } else if (strncasecmp(buffer+i,"delete-file",11) == 0 && buffer[i+11] <= ' ') { 
     207                                        printf("DELETE\n"); 
     208                                        i += 11; 
     209                                        while  (buffer[i] == ' ') i++; 
     210                                        /* read file name */ 
     211                                        //sprintf(cmd, "%s", strtok(buffer[i], "")); 
     212                                        strcpy(cmd, buffer+i); 
     213                                        printf("delete: %s\n", cmd); 
     214                                        /* remove from file array */ 
     215                                        for (i=0; i<shdv_maxfiles; i++) { 
     216                                                if (strcmp(shdv_file[i], cmd) == 0 ) { 
     217                                                        strcpy(shdv_file[i], ""); 
     218                                                        if (shdv_debuglevel > 8) 
     219                                                                fprintf( shdv_log, "shd-9: file removed from array position %d\n", i ); 
     220                                                                break; 
     221                                                } /*endif*/ 
     222                                        } /*endfor*/ 
     223                                        /* remove file */ 
     224                                        if (remove( cmd ) < 0 ) 
     225                                                fprintf( shdv_log, "shd: cannot remove %s!\n", cmd ); 
     226 
    157227                                } else { 
    158228                                        shd_send_command( "localhost", shport, buffer+i, sock, do_upload ); 
    159                                         if  (shdv_debuglevel > 5) 
     229                                        if  (shdv_debuglevel > 5 && !do_upload) 
    160230                                                fprintf( shdv_log, "shd-6: send command >%s< to port %d\n", 
    161231                                                        buffer+i, shport ); 
     
    254324                                        exit( 1 ); 
    255325                                } /*endif*/ 
     326                        } else if  (strcmp(vname,"uploaddir") == 0)  { 
     327                                if  (sscanf(vvalue,"%s",&shdv_uploaddir) != 1) { 
     328                                        fprintf( stderr, "shd: error reading upload directory. Abort.\n" ); 
     329                                        fclose( fp ); 
     330                                        exit( 1 ); 
     331                                } /*endif*/ 
     332                        } else if  (strcmp(vname,"timeout") == 0)  { 
     333                                if  (sscanf(vvalue,"%s",&shdv_timeout) != 1) { 
     334                                        fprintf( stderr, "shd: error reading timeout value. Abort.\n" ); 
     335                                        fclose( fp ); 
     336                                        exit( 1 ); 
     337                                } /*endif*/ 
    256338                        } else { 
    257339                                fprintf( stderr, "shd: illegal variable %s in parfile %s\n", 
     
    283365        } /*endfor*/ 
    284366 
     367        /* initialize list of files */ 
     368        for  (i=0; i<shdv_maxfiles; i++)  { 
     369                strcpy( shdv_file[i], "" ); 
     370        } /*endfor*/ 
     371 
    285372} /* end of shd_init_vars */ 
    286373 
     
    304391        /* executable code */ 
    305392 
    306         for  (i=0; i<shdv_maxsessions; i++) 
     393        for  (i=0; i<shdv_maxsessions; i++) { 
     394 
    307395                if  (shdv_hash[i] == 0)  { 
    308396                        for  (;;)  { 
     
    317405                        return shdv_hash[i]; 
    318406                } /*endif*/ 
     407        } 
    319408 
    320409        return 0; 
     
    403492        int      timeout;                /* timeout at reading */ 
    404493        int      i;                      /* counter */ 
     494        char     *p;                     /* pointer for blank replacing and upper cas"ing" in filename */ 
    405495 
    406496        /* executable code */ 
     
    437527        } /*endif*/ 
    438528 
    439         /* Send data to the server. */ 
    440         shd_write_to_server( sock, cmd, &ok ); 
    441         if  (!ok)  { 
    442                 strcpy( resp, "shd: error writing to SH session\n" ); 
    443                 perror( "error writing to SH session" ); 
    444                 if  (respsock > 0)  write( respsock, resp, strlen(resp)+1 ); 
    445                 return; 
    446         } /*endif*/ 
    447  
     529        /* upload: save data to file */ 
    448530        if  (do_upload)  { 
    449                 shd_upload( respsock, sock ); 
    450         } /*endif*/ 
    451  
    452         if  (shdv_debuglevel > 5) 
    453                 fprintf( shdv_log, "shd-6: waiting for response ...\n" ); 
    454         for  (;;)  { 
    455                 if  (shdv_debuglevel > 8) 
    456                         fprintf( shdv_log, 
    457                                 "shd-9: awaiting SH response with timeout on socket %d\n", 
    458                                 sock ); 
    459                 shd_read_from_server_timeout( sock, cShd_MAXBUF, resp, &resplth, 
    460                         &timeout, &ok ); 
    461                 if  (shdv_debuglevel > 8 && timeout) 
    462                         fprintf( shdv_log, "shd-9: got timeout on SH read\n" ); 
    463                 if  (timeout || !ok)  break; 
    464                 if  (ok && !timeout)  { 
    465                         if  (shdv_debuglevel > 8)  { 
    466                                 for  (i=0; i<resplth; i++) 
    467                                         if  (resp[i] < ' ')  resp2[i] = '@'; 
    468                                         else  resp2[i] = resp[i]; 
    469                                 resp2[resplth] = '\0'; 
    470                                 fprintf( shdv_log, "shd-9: (lth:%d) %s\n", resplth, resp2 ); 
     531                /* file name expected in "cmd" var 
     532                 * replace of blanks to _ */ 
     533                while ((p = strchr(cmd, ' ')) != NULL) 
     534                        *p='_'; 
     535 
     536                /* make file name upper case */ 
     537                for (p = cmd; *p; ++p) 
     538                      *p = toupper(*p); 
     539 
     540                if (shdv_debuglevel > 5) 
     541                        fprintf( shdv_log, "shd-6: file upload: %s\n", cmd ); 
     542 
     543                shd_upload( respsock, sock, cmd ); 
     544 
     545                /* submit (new) file name to client */ 
     546                sprintf( resp, "%s uploaded.\n", cmd ); 
     547                write( respsock, resp, strlen(resp)+1 ); 
     548                write( respsock, cPio_TERM, cPio_TERMLTH ); 
     549 
     550                /* save file name in array */ 
     551                for (i=0; i<shdv_maxfiles; i++) { 
     552                        if (strcmp(shdv_file[i], "") == 0 || strcmp( shdv_file[i], cmd) == 0) { 
     553                                if (shdv_debuglevel > 8) 
     554                                        fprintf( shdv_log, "shd-9: file name saved in array position %d\n", i ); 
     555 
     556                                strcpy( shdv_file[i], cmd ); 
     557                                break; 
    471558                        } /*endif*/ 
    472                         if  (respsock > 0)  { 
    473                                 if  (shdv_debuglevel> 8) 
    474                                         fprintf( shdv_log, "shd-9: forwarding response %s (length %d)\n", 
    475                                                 resp, resplth ); 
    476                                 write( respsock, resp, resplth ); 
    477                                 if  (shdv_debuglevel > 8) 
    478                                         fprintf( shdv_log, "shd-9: done\n" ); 
     559                } /*endfor*/ 
     560        } else { 
     561                /* OR: Send command to the server. */ 
     562                shd_write_to_server( sock, cmd, &ok ); 
     563                if  (!ok)  { 
     564                        strcpy( resp, "shd: error writing to SH session\n" ); 
     565                        perror( "error writing to SH session" ); 
     566                        if  (respsock > 0)  write( respsock, resp, strlen(resp)+1 ); 
     567                        return; 
     568                } /*endif*/ 
     569 
     570                if  (shdv_debuglevel > 5) 
     571                        fprintf( shdv_log, "shd-6: waiting for response ...\n" ); 
     572                for  (;;)  { 
     573                        if  (shdv_debuglevel > 8) 
     574                                fprintf( shdv_log, 
     575                                        "shd-9: awaiting SH response with timeout on socket %d\n", 
     576                                        sock ); 
     577                        shd_read_from_server_timeout( sock, cShd_MAXBUF, resp, &resplth, 
     578                                &timeout, &ok ); 
     579                        if  (shdv_debuglevel > 8 && timeout) 
     580                                fprintf( shdv_log, "shd-9: got timeout on SH read\n" ); 
     581                        if  (timeout || !ok)  break; 
     582                        if  (ok && !timeout)  { 
     583                                if  (shdv_debuglevel > 8)  { 
     584                                        for  (i=0; i<resplth; i++) 
     585                                                if  (resp[i] < ' ')  resp2[i] = '@'; 
     586                                                else  resp2[i] = resp[i]; 
     587                                        resp2[resplth] = '\0'; 
     588                                        fprintf( shdv_log, "shd-9: (lth:%d) %s\n", resplth, resp2 ); 
     589                                } /*endif*/ 
     590                                if  (respsock > 0)  { 
     591                                        if  (shdv_debuglevel> 8) 
     592                                                fprintf( shdv_log, "shd-9: forwarding response %s (length %d)\n", 
     593                                                        resp, resplth ); 
     594                                        write( respsock, resp, resplth ); 
     595                                        if  (shdv_debuglevel > 8) 
     596                                                fprintf( shdv_log, "shd-9: done\n" ); 
     597                                } /*endif*/ 
     598                                if  (resplth >= cPio_TERMLTH 
     599                                        && strcmp(resp+resplth-cPio_TERMLTH,cPio_TERM) == 0)  break; 
    479600                        } /*endif*/ 
    480                         if  (resplth >= cPio_TERMLTH 
    481                                 && strcmp(resp+resplth-cPio_TERMLTH,cPio_TERM) == 0)  break; 
    482                 } /*endif*/ 
    483         } /*endfor*/ 
     601                } /*endfor*/ 
     602        } /*endif*/ 
    484603 
    485604        close( sock ); 
     
    604723        FD_ZERO( &set ); 
    605724        FD_SET( fd, &set ); 
    606         stimeout.tv_sec = 3; 
     725        stimeout.tv_sec = shdv_timeout; 
    607726        stimeout.tv_usec = 0; 
    608727 
     
    625744 
    626745 
    627 void shd_upload( int src_sock, int dst_sock ) 
     746void shd_upload( int src_sock, int dst_sock, char filename[] ) 
    628747 
    629748/* read data from src_sock and writes to dst_sock until termination string 
     
    633752 * int        src_sock;   input; source socket 
    634753 * int        dst_sock;   input; destination socket 
     754 * char       filename;   input; name of file 
    635755 */ 
    636756{ 
     
    642762        int      terminate;            /* terminate upload */ 
    643763        int      written;              /* bytes written */ 
     764        FILE     *fp;                   /* temporary data file pointer */ 
     765        char     tmpfile[cShd_MAXBUF]; /* name of temp file */ 
    644766 
    645767        /* executable code */ 
     768 
     769        /* open file */ 
     770        sprintf( tmpfile, "%s%s", shdv_uploaddir, filename ); 
     771 
     772        fp = fopen( tmpfile, "w" ); 
     773        if ( fp == NULL ) { 
     774                fprintf( stderr, "shd: error opening upload file %s. Upload failed.\n", tmpfile ); 
     775                return; 
     776        } 
    646777 
    647778        for  (;;)  { 
     
    656787                        return; 
    657788                } /*endif*/ 
     789 
     790                /* direct piping over network */ 
     791                /*terminate = pio_term_found( buf, &buflth ); 
     792                written = write( dst_sock, buf, buflth ); */ 
     793 
     794                /* save data into file */ 
    658795                terminate = pio_term_found( buf, &buflth ); 
    659                 written = write( dst_sock, buf, buflth ); 
     796                written = fwrite( buf, 1, buflth, fp ); 
     797 
    660798                if  (written < buflth)  { 
    661799                        fprintf( shdv_log, "shd: write error on upload\n" ); 
    662800                        perror( "write error on upload" ); 
    663801                } /*endif*/ 
     802 
    664803                if  (terminate)  break; 
     804 
    665805        } /*endfor*/ 
    666806 
     807        /* close temporary upload file */ 
     808        fclose( fp ); 
     809 
    667810} /* end of shd_upload */ 
    668811 
  • SH_SHM/trunk/source/usrinpw.c

    r16 r78  
    3636 
    3737#include <stdio.h> 
     38#include <stdlib.h> 
    3839#include <string.h> 
    3940#include <ctype.h> 
Note: See TracChangeset for help on using the changeset viewer.