root/lang/c/friendsd/testfriendsd.cc @ 32953

Revision 15618, 2.8 kB (checked in by kazuho, 5 years ago)

initial import

Line 
1#include "../friends++/friends++.h"
2#include "tcpcalls.h"
3
4#define DAEMON_PASSWORD "KOISHIKAWA"
5#define PASSCMD "/home/g540002/friends/bin/authenticate -env /home/g540002/friends/bin/secret/Password"
6
7void usage();
8int main( int argc, char **argv );
9int secret( void );
10int sendCommand( ushort cmd, char *message );
11int receiveMessage( void );
12void dump( int len, char *str );
13
14static int sock;
15
16void usage() {
17  fputs( "TESTFRIENDSD:  an utility for friendsd.\n", stdout );
18  fputs( "         written by OKU Kazuho(g540002)\n", stdout );
19  fputs( "         usage - testfriendsd [command]\n", stdout );
20  fputs( "\n", stdout );
21}
22
23int main( int argc, char **argv ) {
24  ushort cmd;
25  char sendMessage[ 256 ];
26 
27  if( argc != 2 ) {
28    usage();
29    exit( 1 );
30  }
31  cmd = ( ushort )atoi( argv[ 1 ] );
32  if( cmd >= 0x1000 ) {
33    if( secret() ) {
34      fprintf( stderr, "Wrong password.\n" );
35      exit( 1 );
36    } else
37      strcpy( sendMessage, DAEMON_PASSWORD );
38  } else
39    strcpy( sendMessage, "\0" );
40 
41  if( 0 >= ( sock = openService() ) ) {
42    fprintf( stderr, "Couldn't connect to daemon via TCP.\n" );
43    exit( 1 );
44  }
45  if( sendCommand( cmd, sendMessage ) ) {
46    fprintf( stderr, "Couldn't send message via TCP.\n" );
47    closeService( sock );
48    exit( 1 );
49  }
50  if( receiveMessage() ) {
51    fprintf( stderr, "Couldn't receive message via TCP.\n" );
52    closeService( sock );
53    exit( 1 );
54  }
55  closeService( sock );
56  return( 0 );
57}
58
59int secret() {
60  FILE *fp;
61  int c, x;
62 
63  if( NULL == ( fp = popen( PASSCMD, "r" ) ) ) {
64    fprintf( stderr, "Couldn't create pipe.\n" );
65    exit( 1 );
66  }
67  for( x = 0; ; x++ ) {
68    if( EOF == ( c = fgetc( fp ) ) )
69      break;
70    else
71      putchar( c );
72  }
73  pclose( fp );
74  return( x );
75}
76
77int sendCommand( ushort cmd, char *message ) {
78  if( 0 >= write_short( sock, cmd ) )
79    goto ON_ERROR;
80  if( message[ 0 ] )
81    if( 0 >= write_blk( sock, ACK, strlen( message ) + 1, message ) )
82      goto ON_ERROR;
83  return( 0 );
84 ON_ERROR:
85  return( 1 );
86}
87
88int receiveMessage() {
89  ushort opcode;
90  int max, x;
91  char str[ 256 ];
92 
93  if( 0 >= read_short( sock, &opcode ) )
94    goto ON_ERROR;
95  max = ( int )opcode;
96  for( x = 0; x < max; x++ ) {
97    if( 0 >= read_blk( sock, &opcode, str ) )
98      goto ON_ERROR;
99    dump( 84, str );
100  }
101  return( 0 );
102 ON_ERROR:
103  return( 1 );
104}
105
106
107
108
109void dump( int len, char *str ) {
110  int x, y;
111 
112  for( x = 0; ; x += 16 ) {
113    printf( "%08X  ", x );
114    for( y = 0; y < 16; y++ )
115      if( x + y < len )
116        printf( "%02X ", ( unsigned char )str[ x + y ] );
117      else
118        printf( "   " );
119    printf( "  " );
120    for( y = 0; y < 16; y++ )
121      if( x + y < len ) {
122        if( ' ' <= str[ x + y ] && str[ x + y ] < 0x7F )
123          printf( "%c", str[ x + y ] );
124        else
125        printf( "." );
126      } else
127        printf( " " );
128    printf( "\n" );
129    if( x + y >= len )
130      return;
131  }
132}
Note: See TracBrowser for help on using the browser.