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/>.
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 ()
80 send_to_socket (msg
, size
);
85 if (key_pressed (ESC
))
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
);
100 if ((ncsock
= socket (AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) == -1) {
101 printf ("Cant create socket\n");
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
);
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");
126 printf ("netcat -> connected to %s:%d\n", addr
, port
);
128 buf
= (char *) malloc (sizeof (char) * 500); // receive buffer
133 msg
= (char *) malloc (sizeof (char) * 64);
143 static int nc_loop ()
145 int ret
= recv (ncsock
, buf
, 499, 0);
147 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
);
197 printf ("netcat -> Bye !\n");