System call sys_getchar () was improved; stdin is correspond with 0. fd and stdout...
[ZeXOS.git] / apps / nc / main.c
blob48cffa9f777dd52e1f35029cb07e8de12e769f94
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <fcntl.h>
23 #include <netdb.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <arpa/inet.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
32 #define ESC 1
34 static struct sockaddr_in serverSock; // Remote connection
35 static struct hostent *host; // Remote host
36 static int ncsock; // Socket
37 static int size; // Count of input/output bytes
38 static char *buf; // Buffer
39 static char *msg;
41 unsigned key_pressed (int keycode)
43 int scancode = getkey ();
45 if (scancode == keycode)
46 return 1;
48 if (scancode == keycode+128)
49 return 2;
50 else
51 return 0;
54 static int send_to_socket (char *data, unsigned len)
56 int ret;
57 if ((ret = send (ncsock, data, len, 0)) == -1) {
58 printf ("ERROR -> send () = -1\n");
59 return -1;
62 return ret;
65 static int handle_input ()
67 char i = getch ();
69 if (i) {
70 setcolor (7, 0);
72 putch (i);
74 msg[size] = i;
75 size ++;
77 if (i == '\n') {
78 send_to_socket (msg, size);
79 size = 0;
83 if (key_pressed (ESC))
84 return -1;
86 return 1;
89 static int nc_init (const char *addr, int port)
91 // Let's get info about remote computer
92 if ((host = gethostbyname ((char *) addr)) == NULL) {
93 //printf ("Wrong address -> %s:%d\n", addr, port);
94 printf ("Wrong address\n");
95 return 0;
98 // Create socket
99 if ((ncsock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
100 printf ("Cant create socket\n");
101 return 0;
104 // Fill structure sockaddr_in
105 // 1) Family of protocols
106 serverSock.sin_family = AF_INET;
107 // 2) Number of server port
108 serverSock.sin_port = htons (port);
109 // 3) Setup ip address of server, where we want to connect
110 memcpy (&(serverSock.sin_addr), host->h_addr, host->h_length);
112 // Now we are able to connect to remote server
113 if (connect (ncsock, (struct sockaddr *) &serverSock, sizeof (serverSock)) == -1) {
114 printf ("Connection cant be estabilished -> %s:%d\n", addr, port);
115 return -1;
118 // Set socket to non-blocking mode
119 int oldFlag = fcntl (ncsock, F_GETFL, 0);
120 if (fcntl (ncsock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
121 printf ("Cant set socket to nonblocking mode\n");
122 return 0;
125 printf ("netcat -> connected to %s:%d\n", addr, port);
127 buf = (char *) malloc (sizeof (char) * 500); // P�ij�mac� buffer
129 if (!buf)
130 return 0;
132 msg = (char *) malloc (sizeof (char) * 64);
134 if (!msg)
135 return 0;
137 size = 0;
139 return 1;
142 static int nc_loop ()
144 int ret = recv (ncsock, buf, 499, 0);
146 buf[ret] = '\0';
148 if (handle_input () == -1)
149 return -1;
151 if (ret > 0) {
152 setcolor (14, 0);
153 printf (buf);
154 } else {
155 if (ret == -1)
156 printf ("HOST -> Connection refused\n");
159 return ret;
162 void syntax ()
164 printf ("nc: <address> <port>\nGNU/GPL3 - Coded by ZeXx86\n");
167 int main (int argc, char **argv)
169 if (argc < 3) {
170 syntax ();
171 return 0;
174 const int port = atoi (argv[2]);
176 if (!port) {
177 syntax ();
178 return 0;
181 int ret = nc_init ((char *) argv[1], port);
183 if (ret < 1)
184 goto end;
186 while (1) {
187 ret = nc_loop ();
188 if (ret == -1)
189 break;
191 schedule ();
194 free (msg);
195 free (buf);
197 end:
198 printf ("netcat -> Bye !\n");
200 close (ncsock);
202 return 0;