Changeset 78 for SH_SHM/trunk
- Timestamp:
- 11/10/2008 03:58:38 PM (15 years ago)
- Location:
- SH_SHM/trunk/source
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
SH_SHM/trunk/source/port_io.c
r16 r78 2 2 * ========= 3 3 * 4 * version 2, 9-Jul-20064 * version 3, 9-Jul-2006 5 5 * 6 6 * socket server test program 7 7 * 8 8 * K. Stammler, 28-Jun-2006 9 */ 10 11 9 * M. Walther, 22-Oct-2008 10 */ 11 12 13 #include <arpa/inet.h> 12 14 #include <stdio.h> 15 #include <string.h> 13 16 #include <errno.h> 14 17 #include <stdlib.h> … … 86 89 int i; /* counter */ 87 90 struct sockaddr_in clientname; /* name of client */ 88 s ize_t size; /* size of clientname structure */91 socklen_t size; /* size of clientname structure */ 89 92 90 93 /* executable code */ -
SH_SHM/trunk/source/shd.c
r16 r78 1 2 1 /* file shd.c 3 2 * ===== 4 3 * 5 4 * 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 * 7 9 * SH daemon program 8 10 * … … 10 12 */ 11 13 12 14 #include <ctype.h> 15 #include <time.h> 13 16 #include <stdio.h> 17 #include <string.h> 14 18 #include <errno.h> 15 19 #include <stdlib.h> … … 20 24 #include <netdb.h> 21 25 #include "port_io.h" 22 23 26 24 27 #define cShd_MAXBUF 512 … … 37 40 void shd_send_command( char hostname[], int portno, char cmd[], int respsock, 38 41 int do_upload ); 39 void shd_upload( int src_sock, int dst_sock );42 void shd_upload( int src_sock, int dst_sock, char filename[] ); 40 43 41 44 /* global variables */ 42 45 static int *shdv_port; /* port numbers used */ 43 46 static int *shdv_hash; /* hash numbers */ 47 44 48 /* global parameters */ 45 49 static int shdv_portno=18005; /* port number for daemon */ … … 48 52 static int shdv_debuglevel=1; /* debug level */ 49 53 static FILE *shdv_log=NULL; /* log file */ 50 54 static char shdv_uploaddir[]={"/tmp/"}; /* directory for upload */ 55 static int shdv_timeout=5; /* timeout for reading from server */ 56 57 /* more parameters plus another variable */ 58 static int shdv_maxfiles=50000; /* maximum number of uploaded files */ 59 static int shdv_namelth=200; /* length of file name */ 60 static char shdv_file[50000][200]; /* files uploaded in session 61 * size: shdv_maxfiles * shdv_namelth */ 51 62 52 63 … … 70 81 int do_upload; /* upload requested */ 71 82 72 /* executable code */ 83 84 /* executable code */ 73 85 74 86 if (argc != 2) { … … 116 128 if (hash > 0) { 117 129 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 } 118 140 sprintf( msg, "$SH_UTIL/start_sh_at_port.csh %d %d &", 119 141 shport, hash ); … … 141 163 shd_send_command( "localhost", shdv_port[i], "quit y", 0, do_upload ); 142 164 } /*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 143 178 strcpy( msg, "shd: terminated all active SH sessions\n" ); 144 179 write( sock, msg, strlen(msg)+1 ); 145 180 write( sock, cPio_TERM, cPio_TERMLTH ); 146 181 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 ); 147 190 } else if (sscanf(cmd,"%d",&hash) == 1) { 148 191 shport = shd_hash2port( hash ); … … 150 193 while (buffer[i] == ' ') i++; 151 194 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 ); 153 197 shd_send_command( "localhost", shport, "quit y", 0, do_upload ); 154 198 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 155 204 write( sock, msg, strlen(msg)+1 ); 156 205 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 157 227 } else { 158 228 shd_send_command( "localhost", shport, buffer+i, sock, do_upload ); 159 if (shdv_debuglevel > 5 )229 if (shdv_debuglevel > 5 && !do_upload) 160 230 fprintf( shdv_log, "shd-6: send command >%s< to port %d\n", 161 231 buffer+i, shport ); … … 254 324 exit( 1 ); 255 325 } /*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*/ 256 338 } else { 257 339 fprintf( stderr, "shd: illegal variable %s in parfile %s\n", … … 283 365 } /*endfor*/ 284 366 367 /* initialize list of files */ 368 for (i=0; i<shdv_maxfiles; i++) { 369 strcpy( shdv_file[i], "" ); 370 } /*endfor*/ 371 285 372 } /* end of shd_init_vars */ 286 373 … … 304 391 /* executable code */ 305 392 306 for (i=0; i<shdv_maxsessions; i++) 393 for (i=0; i<shdv_maxsessions; i++) { 394 307 395 if (shdv_hash[i] == 0) { 308 396 for (;;) { … … 317 405 return shdv_hash[i]; 318 406 } /*endif*/ 407 } 319 408 320 409 return 0; … … 403 492 int timeout; /* timeout at reading */ 404 493 int i; /* counter */ 494 char *p; /* pointer for blank replacing and upper cas"ing" in filename */ 405 495 406 496 /* executable code */ … … 437 527 } /*endif*/ 438 528 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 */ 448 530 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; 471 558 } /*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; 479 600 } /*endif*/ 480 if (resplth >= cPio_TERMLTH 481 && strcmp(resp+resplth-cPio_TERMLTH,cPio_TERM) == 0) break; 482 } /*endif*/ 483 } /*endfor*/ 601 } /*endfor*/ 602 } /*endif*/ 484 603 485 604 close( sock ); … … 604 723 FD_ZERO( &set ); 605 724 FD_SET( fd, &set ); 606 stimeout.tv_sec = 3;725 stimeout.tv_sec = shdv_timeout; 607 726 stimeout.tv_usec = 0; 608 727 … … 625 744 626 745 627 void shd_upload( int src_sock, int dst_sock )746 void shd_upload( int src_sock, int dst_sock, char filename[] ) 628 747 629 748 /* read data from src_sock and writes to dst_sock until termination string … … 633 752 * int src_sock; input; source socket 634 753 * int dst_sock; input; destination socket 754 * char filename; input; name of file 635 755 */ 636 756 { … … 642 762 int terminate; /* terminate upload */ 643 763 int written; /* bytes written */ 764 FILE *fp; /* temporary data file pointer */ 765 char tmpfile[cShd_MAXBUF]; /* name of temp file */ 644 766 645 767 /* 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 } 646 777 647 778 for (;;) { … … 656 787 return; 657 788 } /*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 */ 658 795 terminate = pio_term_found( buf, &buflth ); 659 written = write( dst_sock, buf, buflth ); 796 written = fwrite( buf, 1, buflth, fp ); 797 660 798 if (written < buflth) { 661 799 fprintf( shdv_log, "shd: write error on upload\n" ); 662 800 perror( "write error on upload" ); 663 801 } /*endif*/ 802 664 803 if (terminate) break; 804 665 805 } /*endfor*/ 666 806 807 /* close temporary upload file */ 808 fclose( fp ); 809 667 810 } /* end of shd_upload */ 668 811 -
SH_SHM/trunk/source/usrinpw.c
r16 r78 36 36 37 37 #include <stdio.h> 38 #include <stdlib.h> 38 39 #include <string.h> 39 40 #include <ctype.h>
Note: See TracChangeset
for help on using the changeset viewer.