TCP-IP

       

UNIX


etcp.h

etcp. h ( 1.1). , skel. h ( 2.1), , -.

1.1. etcp, h

1 #ifndef _ETCP_H_

2 #define ___

3 /* . */

4 #include <errno.h>

5 #include <stdlib.h>

6 #include <unistd.h>

7 #include <stdio.h>

8 #include <stdarg.h>

9 #include <string.h>

10 #include <netdb.h>



11 #include <signal.h>

12 #include <fcntl.h>

13 #include <sys/socket.h>

14 #include <sys/wait.h>

15 #include <sys/time.h>

16 #include <sys/resource.h>

17 #include <sys/stat.h>

18 #include <netinet/in.h>

19 #include <arpa/inet.h>

20 #include "skel.h"

21 #define TRUE 1

22 #define FALSE 0

23 #define NLISTEN 5 /* . */

24 #define NSMB 5 /* . */

25 #tdefine SMBUFSZ256/* . */

26 extern char *program_name; /* . */

27 #ifdef _SVR4

28 #define bzero(b,n) memset( ( b ), 0, ( n ) )

29 #endif

30 typedef void ( *tofunc_t ) ( void * ) ;

31 void error( int, int, char*, ... );

32 int readn( SOCKET, char *, size_t );

33 int readvrect SOCKET, char *, size_t ) ;

34 int readcrlf( SOCKET, char *, size_t ) ;

35 int readline( SOCKET, char *, size_t ) ;

36 int tcp_server( char *, char * };

37 int tcp_client ( char *, char * ) ;

38 int udp_server ( char *, char * } ;

39 int udp_client( char *, char *, struct sockaddr_in * );


40 int tselect( int, fd_set *, fd_set *, fd_set *);

41 unsigned int timeout( tofunc_t, void *, int );

42 void untimeout( unsigned int );

43 void init_smb( int ) ;

44 void *smballoc( void ) ;

45 void smbfree( void * ) ;

46 void smbsendf SOCKET, void * );

47 void *smbrecv( SOCKET ) ;

48 void set_address ( char *, char *, struct sockaddr_in *', char * ) ;

49 #endif /* _ETCP_H_ */

㭪 daemon

㭪 daemon, ᯮ짮 ணࠬ tcpmux, 室 ⠭ ⥪, ⠢塞 ⥬ BSD. ⥬ SVR4 ਢ , ⥪ ன ⨭ 1.2.

⨭ 1.2. 㭪 daemon

daemon.

1 int daemon( int nocd, int noclose )

2 (

3 struct rlimit rlim;

4 pid_t pid;

5 int i;

6 mask( 0 ); /* ᮧ 䠩. */

7 /* ᨬ쭮 ᫮ 䠩. */

8 if ( getrlimit( RLIMIT_NOFILE, &rlim ) < 0 )

9 error( 1, errno, "getrlimit failed" );

10 /* ஬ ᨨ, ⮬ ࠢ騩 ନ... */

11 pid = fork();

12 if ( pid < 0 )

13 return -1;

14 if ( pid != 0 )

15 exit( 0 ) ;

16 setsid();

17 /* ... ࠭஢, 㤥. */

18 signal( SIGHUP, SIG_IGN );

19 pid = fork(};

20 if ( pid < 0 )

21 return -1;

22 if ( pid != 0 )

23 exit( 0 );

24 * ⥪騬 ୥ ⠫, ॡ ⭮ */

25 if ( !nocd )

26 chdir( "/" ) ;

27 /*

28 * ᫨ ᨫ ⮣ , 䠩.

29 * ⥬ ७ࠢ stdin, stdout stderr

30 * /dev/null.

31 */

32 if (!noclose }

33 {

34 #if 0 /* 1 䠩. */

35 if ( rlim.rlim_max == RLIM_INFINITY )

3 rlim.rlim_max = 1024;



37 for ( i = 0; i < rlim.rlim_max; i++ )

38 close( i );

39 endif

40 i = open( "/dev/null", 0_RDWR );

41 f ( i < 0 )

42 return -1;

43 up2( i, 0 ) ;

44 up2( i, 1 };

45 up2( i, 2 );

46 f ( i > 2 )

47 close( i ) ;

48 }

49 return 0;

50 }

㭪 signal

⮩ 㦥 㯮, UNIX 㭪 s ignal ॠ ᭮ ᥬ⨪ ᨣ. ⠪ 砥 ­祭 ᥬ⨪ ᨣ ᫥ ᯮ짮 㭪 sigaction. ⮡ ७ᨬ, 室 ॠ signal sigaction (⨭ 1.3)

⨭ 1.3. 㭪 signal

signal. c

/* signal - SVR4 㣨 ⥬. */

1 typedef void sighndlr_t( int );

2 sighndlr_t *signal( int sig, sighndlr_t *hndlr )

3 {

4 struct sigaction act;

5 struct sigaction xact;

6 act.sa_handler = hndlr;

7 act.sa_flags =0;

8 sigemptyset( &act.sa_mask );

9 if ( sigaction( sig, &act, &xact ) < 0 )

10 return SIG_ERR;

11 return xact.sa_handler;

12 }

| | |