diff options
Diffstat (limited to 'mnc.c')
-rw-r--r-- | mnc.c | 79 |
1 files changed, 32 insertions, 47 deletions
@@ -3,9 +3,11 @@ | |||
3 | 3 | ||
4 | 0.0.1 6K It works. | 4 | 0.0.1 6K It works. |
5 | 0.0.2 5K Smaller and you can also check the exit condition if you wish. | 5 | 0.0.2 5K Smaller and you can also check the exit condition if you wish. |
6 | 6 | 0.0.3 Uses select() | |
7 | 7 | ||
8 | 19980918 Busy Boxed! Dave Cinege | 8 | 19980918 Busy Boxed! Dave Cinege |
9 | 19990512 Uses Select. Charles P. Wright | ||
10 | 19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright | ||
9 | 11 | ||
10 | This program is free software; you can redistribute it and/or modify | 12 | This program is free software; you can redistribute it and/or modify |
11 | it under the terms of the GNU General Public License as published by | 13 | it under the terms of the GNU General Public License as published by |
@@ -35,26 +37,24 @@ | |||
35 | #include <sys/time.h> | 37 | #include <sys/time.h> |
36 | #include <sys/ioctl.h> | 38 | #include <sys/ioctl.h> |
37 | 39 | ||
40 | #define BUFSIZE 100 | ||
41 | |||
38 | const char mnc_usage[] = | 42 | const char mnc_usage[] = |
39 | "mini-netcat 0.0.1 -- Open pipe to IP:port\n" | 43 | "mini-netcat 0.0.3 -- Open pipe to IP:port\n" |
40 | "\tmnc [IP] [port]\n"; | 44 | "\tmnc [IP] [port]\n"; |
41 | 45 | ||
42 | int | 46 | int |
43 | mnc_main(struct FileInfo * i, int argc, char **argv) | 47 | mnc_main(int argc, char **argv) |
44 | { | 48 | { |
45 | |||
46 | int sfd; | 49 | int sfd; |
47 | int result; | 50 | int result; |
48 | int len; | 51 | int len; |
49 | int pid; | 52 | char ch[BUFSIZE]; |
50 | char ch; | ||
51 | 53 | ||
52 | struct sockaddr_in address; | 54 | struct sockaddr_in address; |
53 | struct hostent *hostinfo; | 55 | struct hostent *hostinfo; |
54 | 56 | ||
55 | #ifdef SELECT | ||
56 | fd_set readfds, testfds; | 57 | fd_set readfds, testfds; |
57 | #endif | ||
58 | 58 | ||
59 | sfd = socket(AF_INET, SOCK_STREAM, 0); | 59 | sfd = socket(AF_INET, SOCK_STREAM, 0); |
60 | 60 | ||
@@ -78,7 +78,6 @@ mnc_main(struct FileInfo * i, int argc, char **argv) | |||
78 | exit(2); | 78 | exit(2); |
79 | } | 79 | } |
80 | 80 | ||
81 | #ifdef SELECT | ||
82 | FD_ZERO(&readfds); | 81 | FD_ZERO(&readfds); |
83 | FD_SET(sfd, &readfds); | 82 | FD_SET(sfd, &readfds); |
84 | FD_SET(fileno(stdin), &readfds); | 83 | FD_SET(fileno(stdin), &readfds); |
@@ -86,6 +85,7 @@ mnc_main(struct FileInfo * i, int argc, char **argv) | |||
86 | while(1) | 85 | while(1) |
87 | { | 86 | { |
88 | int fd; | 87 | int fd; |
88 | int ofd; | ||
89 | int nread; | 89 | int nread; |
90 | 90 | ||
91 | testfds = readfds; | 91 | testfds = readfds; |
@@ -101,48 +101,33 @@ mnc_main(struct FileInfo * i, int argc, char **argv) | |||
101 | { | 101 | { |
102 | if(FD_ISSET(fd,&testfds)) | 102 | if(FD_ISSET(fd,&testfds)) |
103 | { | 103 | { |
104 | ioctl(fd, FIONREAD, &nread); | 104 | int trn = 0; |
105 | int rn; | ||
105 | 106 | ||
106 | if (nread == 0) | 107 | ioctl(fd, FIONREAD, &nread); |
107 | exit(0); | ||
108 | 108 | ||
109 | if(fd == sfd) | 109 | if(fd == sfd) |
110 | { | 110 | { |
111 | read(sfd, &ch, 1); | 111 | if (nread == 0) |
112 | write(fileno(stdout), &ch, 1); | 112 | exit(0); |
113 | } | 113 | ofd = fileno(stdout); |
114 | else | 114 | } |
115 | { | 115 | else |
116 | read(fileno(stdin), &ch, 1); | 116 | { |
117 | write(sfd, &ch, 1); | 117 | ofd = sfd; |
118 | } | 118 | } |
119 | } | 119 | |
120 | |||
121 | |||
122 | do | ||
123 | { | ||
124 | rn = (BUFSIZE < nread - trn) ? BUFSIZE : nread - trn; | ||
125 | trn += rn; | ||
126 | read(fd, ch, rn); | ||
127 | write(ofd, ch, rn); | ||
128 | } | ||
129 | while (trn < nread); | ||
130 | } | ||
120 | } | 131 | } |
121 | } | 132 | } |
122 | #else | ||
123 | pid = fork(); | ||
124 | |||
125 | if (!pid) | ||
126 | { | ||
127 | int retval; | ||
128 | retval = 1; | ||
129 | while(retval == 1) | ||
130 | { | ||
131 | retval = read(fileno(stdin), &ch, 1); | ||
132 | write(sfd, &ch, 1); | ||
133 | } | ||
134 | } | ||
135 | else | ||
136 | { | ||
137 | int retval; | ||
138 | retval = 1; | ||
139 | while(retval == 1) | ||
140 | { | ||
141 | retval = read(sfd, &ch, 1); | ||
142 | write(fileno(stdout), &ch, 1); | ||
143 | } | ||
144 | } | ||
145 | |||
146 | exit(0); | ||
147 | #endif | ||
148 | } | 133 | } |