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/>.
28 #include <arpa/inet.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
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
41 unsigned key_pressed (int keycode
)
43 int scancode
= getkey ();
45 if (scancode
== keycode
)
48 if (scancode
== keycode
+128)
54 static int send_to_socket (char *data
, unsigned len
)
57 if ((ret
= send (ncsock
, data
, len
, 0)) == -1) {
58 printf ("ERROR -> send () = -1\n");
65 static int handle_input ()
78 send_to_socket (msg
, size
);
83 if (key_pressed (ESC
))
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");
99 if ((ncsock
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) == -1) {
100 printf ("Cant create socket\n");
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
);
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");
125 printf ("netcat -> connected to %s:%d\n", addr
, port
);
127 buf
= (char *) malloc (sizeof (char) * 500); // P�ij�mac� buffer
132 msg
= (char *) malloc (sizeof (char) * 64);
142 static int nc_loop ()
144 int ret
= recv (ncsock
, buf
, 499, 0);
148 if (handle_input () == -1)
156 printf ("HOST -> Connection refused\n");
164 printf ("nc: <address> <port>\nGNU/GPL3 - Coded by ZeXx86\n");
167 int main (int argc
, char **argv
)
174 const int port
= atoi (argv
[2]);
181 int ret
= nc_init ((char *) argv
[1], port
);
198 printf ("netcat -> Bye !\n");