├── netcat_client.h ├── .gitignore ├── LICENSE ├── netcat_core.c ├── netcat_core.h └── netcat_client.c /netcat_client.h: -------------------------------------------------------------------------------- 1 | /* *********************************************************************** * 2 | * Filename: netcat_client.h 3 | * 4 | * Description: 5 | * 6 | * Version: 1.0 7 | * Created: Thursday 07 March 2013 01:28:17 IST 8 | * Revision: none 9 | * Compiler: gcc 10 | * 11 | * Author: Prakash Gamit (09211014), 12 | * Organization: Indian Institute of Technology, Roorkee 13 | * 14 | * ********************************************************************** */ 15 | 16 | #include "netcat_core.h" 17 | 18 | 19 | /* start client program */ 20 | extern void start_client(); 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 thegreggilbert 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /netcat_core.c: -------------------------------------------------------------------------------- 1 | /* *********************************************************************** * 2 | * 3 | * Filename: netcat_core.c 4 | * 5 | * Description: 6 | * 7 | * Version: 1.0 8 | * Created: Wednesday 06 March 2013 02:06:25 IST 9 | * Revision: none 10 | * Compiler: gcc 11 | * 12 | * Author: Prakash Gamit (09211014), 13 | * Organization: Indian Institute of Technology, Roorkee 14 | * 15 | * ********************************************************************* */ 16 | 17 | #include "netcat_core.h" 18 | 19 | 20 | struct options o; 21 | 22 | 23 | void init_options(void){ 24 | o.port = 0; 25 | 26 | /* deafault is client mode */ 27 | o.listen = 0; 28 | o.keepopen = 0; 29 | 30 | /* default socket type is tcp */ 31 | o.udp = 0; 32 | 33 | /* do not execute any command */ 34 | o.execcommand = 0; 35 | 36 | /* don't be verbose */ 37 | o.verbose = 0; 38 | } 39 | 40 | 41 | void bye(const char *format, ...){ 42 | va_list ap; 43 | 44 | va_start(ap, format); 45 | vfprintf(stderr, format, ap); 46 | va_end(ap); 47 | fprintf(stderr, " QUITTING.\n"); 48 | 49 | exit(EXIT_FAILURE); 50 | } 51 | 52 | 53 | void die(char *msg){ 54 | perror(msg); 55 | exit(EXIT_FAILURE); 56 | } 57 | -------------------------------------------------------------------------------- /netcat_core.h: -------------------------------------------------------------------------------- 1 | /* *********************************************************************** * 2 | * Filename: netcat_core.h 3 | * 4 | * Description: define options struct & declare global variables 5 | * 6 | * Version: 1.0 7 | * Created: Wednesday 06 March 2013 01:31:03 IST 8 | * Revision: none 9 | * Compiler: gcc 10 | * 11 | * Author: Prakash Gamit (09211014), 12 | * Organization: Indian Institute of Technology, Roorkee 13 | * 14 | * ********************************************************************** */ 15 | 16 | #ifndef _NETCAT_CORE_H_ 17 | #define _NETCAT_CORE_H_ 18 | 19 | 20 | /* include standar header files */ 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | #include 33 | #include 34 | #include 35 | 36 | // remove warnings for inet_aton and other similar functions 37 | #include 38 | 39 | #define MAX 1024 40 | 41 | 42 | struct options{ 43 | /* host to connect to in case of client mode */ 44 | char *target; 45 | 46 | /* source port to use */ 47 | unsigned short port; 48 | 49 | /* is it listen mode or client mode */ 50 | int listen; 51 | /* accept multiple connections in listen mode */ 52 | int keepopen; 53 | 54 | /* socket is udp */ 55 | int udp; 56 | 57 | /* should be verbose */ 58 | int verbose; 59 | 60 | /* command is to be executed */ 61 | int execcommand; 62 | /* command to execute */ 63 | char *command; 64 | }; 65 | 66 | 67 | /* global options struct */ 68 | extern struct options o; 69 | 70 | 71 | /* initialize options struct to default values */ 72 | extern void init_options(void); 73 | 74 | 75 | /* print error message to @stderr and exit */ 76 | extern void bye(const char *, ...); 77 | 78 | 79 | /* writes error message using perror and then quits */ 80 | extern void die(char *); 81 | 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /netcat_client.c: -------------------------------------------------------------------------------- 1 | /* *********************************************************************** * 2 | * 3 | * Filename: netcat_client.c 4 | * 5 | * Description: 6 | * 7 | * Version: 1.0 8 | * Created: Thursday 07 March 2013 03:03:24 IST 9 | * Revision: none 10 | * Compiler: gcc 11 | * 12 | * Author: Prakash Gamit (09211014), 13 | * Organization: Indian Institute of Technology, Roorkee 14 | * 15 | * ********************************************************************* */ 16 | 17 | #include "netcat_client.h" 18 | #include // gethostbyname 19 | 20 | int sockfd; 21 | 22 | 23 | void start_client(){ 24 | 25 | if(o.udp) 26 | sockfd = socket(AF_INET, SOCK_DGRAM, 0); 27 | else 28 | sockfd = socket(AF_INET, SOCK_STREAM, 0); 29 | 30 | if(sockfd == -1) 31 | die("socket"); 32 | 33 | struct sockaddr_in servaddr; 34 | bzero(&servaddr, sizeof(servaddr)); 35 | 36 | /////////////// hostname support //////////////////////////// 37 | struct hostent *hostent1 = gethostbyname(o.target); 38 | if(!hostent1) 39 | { 40 | bye("could't resolve hostname\n"); 41 | } 42 | memcpy(&(servaddr.sin_addr), hostent1->h_addr_list[0], hostent1->h_length); 43 | ///////////////////////////////////////////////////////////// 44 | 45 | servaddr.sin_family = AF_INET; 46 | servaddr.sin_port = htons(o.port); 47 | 48 | // int status = inet_pton(AF_INET, o.target, &servaddr.sin_addr); 49 | // if(status == -1) 50 | // die("inet_pton"); 51 | // else if(status == 0){/* hostname given not IP */ 52 | // //TODO 53 | // /* get IP from hostname 54 | // * but for now print error and exit 55 | // */ 56 | // bye("hostname not supported\ngive IP-address\n"); 57 | // } 58 | 59 | if(connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) 60 | die("connect"); 61 | 62 | if(o.verbose){ 63 | printf("connected to (%s, %d)\n", o.target, o.port); 64 | } 65 | 66 | char sendline[MAX], recvline[MAX]; 67 | 68 | fd_set rset; 69 | int maxfdp1; 70 | int bytes; 71 | 72 | FD_ZERO(&rset); 73 | while(1){ 74 | FD_SET(fileno(stdin), &rset); 75 | FD_SET(sockfd, &rset); 76 | 77 | maxfdp1 = (fileno(stdin) < sockfd ? sockfd : fileno(stdin)) + 1; 78 | 79 | if(select(maxfdp1, &rset, NULL, NULL, NULL) == -1) 80 | die("select"); 81 | 82 | if(FD_ISSET(sockfd, &rset)){ 83 | bzero(recvline, MAX); 84 | bytes = read(sockfd, recvline, MAX); 85 | 86 | if(bytes == -1) 87 | die("read"); 88 | else if(bytes == 0) 89 | break; 90 | 91 | fputs(recvline, stdout); 92 | } 93 | 94 | if(FD_ISSET(fileno(stdin), &rset)){ 95 | bzero(sendline, MAX); 96 | bytes = read(fileno(stdin), sendline, MAX); 97 | 98 | if(bytes == -1) 99 | die("read"); 100 | else if(bytes == 0) 101 | break; 102 | 103 | if(write(sockfd, sendline, strlen(sendline)) == -1) 104 | die("write"); 105 | } 106 | }/* while */ 107 | 108 | close(sockfd); 109 | if(o.verbose) 110 | printf("closed connection to (%s, %d)\n", o.target, o.port); 111 | }/* start_client() */ 112 | --------------------------------------------------------------------------------