annotate utils/tcpserv-dump.c @ 267:81958b35f74d

NANP validation: allow made-up area codes of N9X form
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 13 Nov 2023 15:28:09 -0800
parents 9f6a148ceb25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
2 * This debug utility binds to a TCP port specified on the command line,
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
3 * accepts incoming TCP connections and reads any bytes sent by a client
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
4 * on an opened connection - but never sends anything back. A log file
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
5 * is written, recording all received connections and bytes.
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 */
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/types.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/socket.h>
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
10 #include <sys/time.h>
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
11 #include <sys/errno.h>
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <netinet/in.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <arpa/inet.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdio.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include <stdlib.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 #include <string.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 #include <strings.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 #include <time.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 #include <unistd.h>
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
21 #define MAXCONN 10
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
22
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
23 static struct in_addr bind_ip;
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
24 static int tcp_port;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
25 static int listener, nconn, conns[MAXCONN], max_fd;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 static FILE *logf;
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
27 static struct timeval curtime;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
28 static char fmt_time[32];
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 static void
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
31 format_time()
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 {
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
33 struct tm *tm;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
34
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
35 tm = gmtime(&curtime.tv_sec);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
36 sprintf(fmt_time, "%d-%02d-%02dT%02d:%02d:%02dZ",
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
37 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
38 tm->tm_hour, tm->tm_min, tm->tm_sec);
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 static void
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
42 handle_accept(newfd, sin)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
43 struct sockaddr_in *sin;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 {
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
45 fprintf(logf, "\n%s Accept conn from %s:%u fd %d\n", fmt_time,
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
46 inet_ntoa(sin->sin_addr), ntohs(sin->sin_port), newfd);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
47 if (nconn < MAXCONN) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
48 conns[nconn] = newfd;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
49 nconn++;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
50 if (newfd > max_fd)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
51 max_fd = newfd;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
52 } else {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
53 fprintf(logf, "MAXCONN exceeded, closing new fd\n");
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
54 close(newfd);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
55 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
56 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
57
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
58 static int
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
59 handle_conn_fd(fd)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
60 {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
61 u_char buf[512];
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
62 int cc, off, chunk, i, c;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
64 cc = read(fd, buf, sizeof buf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
65 fprintf(logf, "\n%s fd %d read %d\n", fmt_time, fd, cc);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
66 if (cc <= 0) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
67 fprintf(logf, "closing fd\n");
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
68 return(1);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
69 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
70 for (off = 0; off < cc; off += chunk) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
71 fprintf(logf, "%04X: ", off);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
72 chunk = cc - off;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
73 if (chunk > 16)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
74 chunk = 16;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
75 for (i = 0; i < 16; i++) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
76 if (i < chunk)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
77 fprintf(logf, "%02X ", buf[off + i]);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
78 else
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
79 fputs(" ", logf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
80 if (i == 7 || i == 15)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
81 putc(' ', logf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
82 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
83 for (i = 0; i < chunk; i++) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
84 c = buf[off + i];
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
85 if (c < ' ' || c > '~')
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
86 c = '.';
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
87 putc(c, logf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
88 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
89 putc('\n', logf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
90 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
91 return(0);
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 main(argc, argv)
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 char **argv;
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 {
217
9f6a148ceb25 tcpserv-dump: brown paper bag
Mychaela Falconia <falcon@freecalypso.org>
parents: 216
diff changeset
97 int rc, i;
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
98 struct sockaddr_in sin;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 socklen_t addrlen;
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
100 fd_set fds;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
102 if (argc != 4) {
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
103 fprintf(stderr, "usage: %s bind-ip port logfile\n", argv[0]);
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 exit(1);
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 }
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
106 bind_ip.s_addr = inet_addr(argv[1]);
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
107 if (bind_ip.s_addr == INADDR_NONE) {
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
108 fprintf(stderr, "error: invalid IP address argument \"%s\"\n",
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
109 argv[1]);
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
110 exit(1);
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
111 }
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
112 tcp_port = atoi(argv[2]);
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
113 listener = socket(AF_INET, SOCK_STREAM, 0);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
114 if (listener < 0) {
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 perror("socket");
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 exit(1);
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 }
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 sin.sin_family = AF_INET;
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
119 sin.sin_addr = bind_ip;
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
120 sin.sin_port = htons(tcp_port);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
121 rc = bind(listener, (struct sockaddr *) &sin, sizeof sin);
43
5995660dcbac sip-rx-test, sip-udp-dump: fix bind error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 18
diff changeset
122 if (rc < 0) {
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 perror("bind");
43
5995660dcbac sip-rx-test, sip-udp-dump: fix bind error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 18
diff changeset
124 exit(1);
5995660dcbac sip-rx-test, sip-udp-dump: fix bind error handling
Mychaela Falconia <falcon@freecalypso.org>
parents: 18
diff changeset
125 }
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
126 logf = fopen(argv[3], "a");
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
127 if (!logf) {
216
6aa2cd650943 tcpserv-dump: specify bind IP on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents: 215
diff changeset
128 perror(argv[3]);
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
129 exit(1);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
130 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
131 rc = listen(listener, 5);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
132 if (rc < 0) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
133 perror("listen");
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
134 exit(1);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
135 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
136 gettimeofday(&curtime, 0);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
137 format_time();
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
138 fprintf(logf, "\n%s Test server started\n", fmt_time);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
139 fflush(logf);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
140 nconn = 0;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
141 max_fd = listener;
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 for (;;) {
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
143 FD_ZERO(&fds);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
144 FD_SET(listener, &fds);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
145 for (i = 0; i < nconn; i++)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
146 FD_SET(conns[i], &fds);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
147 rc = select(max_fd+1, &fds, 0, 0, 0);
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 if (rc < 0) {
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
149 if (errno == EINTR)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
150 continue;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
151 perror("select");
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 exit(1);
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }
215
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
154 gettimeofday(&curtime, 0);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
155 format_time();
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
156 for (i = 0; i < nconn; ) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
157 if (!FD_ISSET(conns[i], &fds)) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
158 i++;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
159 continue;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
160 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
161 if (handle_conn_fd(conns[i])) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
162 close(conns[i]);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
163 nconn--;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
164 conns[i] = conns[nconn];
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
165 } else
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
166 i++;
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
167 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
168 if (FD_ISSET(listener, &fds)) {
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
169 addrlen = sizeof(struct sockaddr_in);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
170 rc = accept(listener, (struct sockaddr *) &sin,
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
171 &addrlen);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
172 if (rc >= 0)
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
173 handle_accept(rc, &sin);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
174 else
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
175 fprintf(logf, "\n%s accept syscall error!\n",
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
176 fmt_time);
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
177 }
67289fac8a44 utils: new program tcpserv-dump
Mychaela Falconia <falcon@freecalypso.org>
parents: 43
diff changeset
178 fflush(logf);
17
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 }
4e0a73be9e37 sip-udp-dump utility written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 }