Fixed ZDE build - missing header file
[ZeXOS.git] / apps / nc / main.c
blobf01a772ce28632d8d5e0e562bf4367aeff104824
1 /*
2 * ZeX/OS
3 * Copyright (C) 2007 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
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 if (size < 64) {
75 msg[size] = i;
76 size ++;
79 if (i == '\n') {
80 send_to_socket (msg, size);
81 size = 0;
85 if (key_pressed (ESC))
86 return -1;
88 return 1;
91 static int nc_init (const char *addr, int port)
93 // Let's get info about remote computer
94 if ((host = gethostbyname ((char *) addr)) == NULL) {
95 printf ("Wrong address -> %s:%d\n", addr, port);
96 return 0;
99 // Create socket
100 if ((ncsock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
101 printf ("Cant create socket\n");
102 return 0;
105 // Fill structure sockaddr_in
106 // 1) Family of protocols
107 serverSock.sin_family = AF_INET;
108 // 2) Number of server port
109 serverSock.sin_port = htons (port);
110 // 3) Setup ip address of server, where we want to connect
111 memcpy (&(serverSock.sin_addr), host->h_addr, host->h_length);
113 // Now we are able to connect to remote server
114 if (connect (ncsock, (struct sockaddr *) &serverSock, sizeof (serverSock)) == -1) {
115 printf ("Connection cant be estabilished -> %s:%d\n", addr, port);
116 return -1;
119 // Set socket to non-blocking mode
120 int oldFlag = fcntl (ncsock, F_GETFL, 0);
121 if (fcntl (ncsock, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
122 printf ("Cant set socket to nonblocking mode\n");
123 return 0;
126 printf ("netcat -> connected to %s:%d\n", addr, port);
128 buf = (char *) malloc (sizeof (char) * 500); // receive buffer
130 if (!buf)
131 return 0;
133 msg = (char *) malloc (sizeof (char) * 64);
135 if (!msg)
136 return 0;
138 size = 0;
140 return 1;
143 static int nc_loop ()
145 int ret = recv (ncsock, buf, 499, 0);
147 if (handle_input () == -1)
148 return -1;
150 if (ret > 0) {
151 setcolor (14, 0);
152 buf[ret] = '\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);
196 end:
197 printf ("netcat -> Bye !\n");
199 close (ncsock);
201 return 0;