File: sockstd.c

package info (click to toggle)
euslisp 9.31%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,448 kB
  • sloc: ansic: 41,610; lisp: 3,339; makefile: 286; sh: 238; asm: 138; python: 53
file content (137 lines) | stat: -rw-r--r-- 3,926 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/****************************************************************/
/* sockstd.c
/*   TCP socket <---> stdio bridge
/*   % sockstd port <debug>
/*   Sockstd waits for a TCP connection at 'port`.
/*   After the connection is established, incoming data at the port
/*   are copied to sockstd's stdout.
/*   Input to sockstd's stdin (usually key inputs) are transferred to
/*   the port.
/*   Thus, pipe oriented programs, such as 'wc', 'cat', etc., will be
/*   able to work on socket connection using the sockstd.
/*
/*   (c) 1995, Toshihiro Matsui, Electrotechnical Laboraotory
/****************************************************************

#include <stdio.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <time.h>

#define bitset(v,i) ((v) | (1<<(i)))
#define bitclr(v,i) ((v) & ~(1<<(i)))
#define bittest(v,i) (v & (1<<(i)))


extern int make_socket_port();
extern void exit();
extern int errno;

long transaction_count=0;
long debug=0;

struct sockaddr_in *make_socket_address(port_no)
int port_no;
{ int stat;
  struct sockaddr_in  *serverAddr;     /* server's address */

  serverAddr=(struct sockaddr_in *)malloc(sizeof(struct sockaddr_in));
  /*bzero (serverAddr, sizeof (*serverAddr)); */
  memset(serverAddr, 0, sizeof (*serverAddr));
  serverAddr->sin_family = AF_INET;
  serverAddr->sin_port   = htons(port_no);
  serverAddr->sin_addr.s_addr = htonl(INADDR_ANY);
  return(serverAddr);
  }
  
int make_socket_port(port)
int port;
{ struct sockaddr_in *serverAddr;
  int serverSock, stat;

  serverAddr= make_socket_address(port);
  serverSock = socket (AF_INET, SOCK_STREAM, 0);
  if (serverSock < 0) {
      fprintf(stderr, "can't create socket\n"); exit (1);}
  stat=bind (serverSock, serverAddr, sizeof(struct sockaddr_in));
  if (stat<0){
      printf ("bind failed, errno = %d\n", errno);
      exit(1);}
  if (listen (serverSock, 2) <0 ) {
      printf ("listen failed\n");
      close (serverSock);
      exit (1);}
  printf("listening on port= %d sock=%d\n", port, serverSock);
  return(serverSock);}


main (argc,argv)
int argc;
char *argv[];
{ struct sigaction	sigabort;
  struct sockaddr_in  *server_addr, client_addr;
  int 	server_port;
  int   port_no;
  long  ports, exceptions, sockbits;
  struct timeval time_out;
  char buf[65536];
  int snew=0;
  int i;
  int stat;
  long client_len;

  if (argc>1) sscanf(argv[1],"%d",&port_no);
  else port_no=5000;
  if (argc>2)  sscanf(argv[2],"%d",&debug);
    
  signal(SIGINT, exit);
  signal(SIGPIPE, SIG_IGN);

  /* Listen, for clients to connect to us. */
  server_port=make_socket_port(port_no);


  sockbits=bitset(1, server_port);
  time_out.tv_sec=10;
  time_out.tv_usec=0;

  while (1) {
      select_again:
      ports=sockbits; exceptions=sockbits;
      stat=select(32, &ports,0, &exceptions, &time_out);
      if (stat>0) {
	transaction_count++;
	if (debug>2) 
	  fprintf(stderr, "select: %d count=%d ready=%x excpt=%x \n",
		 transaction_count, stat, ports, exceptions);
	if (exceptions) {
	  /*assumes exception is delivered because of pipe_broken */
	  i=0;
	  while (i<32) {
	    if (bittest(exceptions,i)) {
	      fprintf(stderr, "exception from %d\n", i);
	      sockbits=bitclr(sockbits,i);}
	    i++; }
	  goto select_again;} 
	if (bittest(ports,server_port)) { /*new connection request*/
	  client_len = sizeof (struct sockaddr_in);
	  snew = accept (server_port, &client_addr, &client_len);
	  if (snew>0) sockbits=bitset(sockbits, snew);
	  ports=bitclr(ports,server_port);
	  fprintf(stderr, "new connection %d is accepted \n", snew);
	  /* if (connection_request) (*connection_request)(snew); */
	  stat--;}
	if (bittest(ports, 0)) { /* stdin*/
	    int size;
	    size=read(0,buf,65536);
	    if (snew) write(snew,buf,size);}
	if (bittest(ports, snew)) {
	    int size;
	    size=read(snew,buf,65536);
	    if (snew) write(1,buf,size);}
	}
      }
  }