-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlttyd.cpp
97 lines (90 loc) · 2.47 KB
/
htmlttyd.cpp
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
#include "gsys.h"
#include "Sock2.h"
#include "ServerLog.h"
#include <stdio.h>
//#define DEBUGGING
#include <signal.h>
#include <sys/socket.h> /* shutdown() */
#include <netdb.h> /* getservent() */
#include <netinet/in.h> /* struct sockaddr_in, INADDR_NONE */
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h> /* inet_addr() */
#include <sys/ioctl.h> /* ioctl() */
#include <sys/times.h> /* times() */
#include <sys/termios.h> /* fstat() */
#include <sched.h>
#include <fcntl.h> /* fcntl() */
#include <string.h> /* bcopy() */
#include <errno.h> /* errno */
#include <stdlib.h> /* exit() */
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h> /* close(),sleep(),chdir(), select(),getopt() */
#include <sys/types.h>
#include <sys/stat.h> /* fstat() */
ServerLog *slog;
extern int HDCon(char *,Sock2 *);
int main(int argc,char *argv[])
{
Sock2 s;
Sock2 c;
int port;
int ret;
fd_set readset;
char buf[1000];
if (argc < 2) {
port=5001;
}
else {
port=atoi(argv[1]);
if (port <1024 || port > 49149) {
port=5001;
}
}
if (argc < 3) {
slog = new ServerLog(""); // send log to console
}
else {
slog = new ServerLog(argv[2]); // send log to file
}
s.open(port); // serve port
sprintf(buf,"\n\n\nServing Port:%d ",port);
slog->info(buf);
signal(SIGCHLD,SIG_IGN);
while (1) {
FD_ZERO(&readset);
FD_SET(s.fd,&readset);
// pend forever waiting for connection request
ret=select((int)(s.fd+1),&readset,NULL,NULL,NULL);
if (ret == -1 && errno == EINTR) {
continue;
}
if (ret == -1 || s.fd == INVALID_SOCKET) {
slog->error((char *)"Error Ret from select\n");
break;
}
//accept connection
c.open(s); //child of fork must call DoHandShake
if (c.fd == -2) {
continue;
}
if (c.fd == INVALID_SOCKET) {
slog->error((char *)"open failed ??\n");
continue;
}
#ifdef DEBUGGING
HDCon(argv[0],&c); // pass connected Sock2 to HDCon
return 0;
#else
if (0 == fork() ) {
s.close();
HDCon(argv[0],&c); // pass connected Sock2 to HDCon
return 0;
}
c.close(); // child server has the socket
#endif
}
s.close();
return 0;
}