hacking contest

hacking exploits security forum
hacking
compliance articles
upgrade backup exec
information security consultant

Grinler
Anyone have any example non-blocking c winsock programs? I am trying, for no particular application, make a program that I can connect to a port, and be able to receive banners and issue commands back if i like. Something like a telnet app but without the negotiation and emulations.

My problem is that I do not know how to do a non-blocking program, and that is making my program freeze.

I have looked everywhere for an example, but they assume I know more than I do.
GAN_GR33N
i'm not sure i get exactly what you mean jbut this could probably be done much easier with a system comand
clip
A "nc" clone by me.

CODE

#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#define SOCKET int
#endif
#ifdef WIN32
#include <winsock2.h>
#include <windows.h>
#include <winsock2.h>
#include <io.h>
#include <ws2tcpip.h>
#include <conio.h>
//#include <sockets.h>
#pragma comment (lib,"ws2_32")
#endif


void shell(SOCKET s) {
struct timeval timev;

char buff[512];



fd_set rfds1;
fd_set rfds2;
timev.tv_sec=1;
   FD_ZERO (&rfds1);
FD_ZERO (&rfds2);
timev.tv_usec=0;
int l = 0;
while(1) {
 FD_ZERO (&rfds1);
 FD_ZERO (&rfds2);
 FD_SET (s, &rfds1);
 FD_SET (0, &rfds2);  
 if(select ((int)s, &rfds1, NULL, NULL, &timev)) {
 // printf("WOOT\n");
  l = recv(s,buff,sizeof(buff),0);
  write(1,buff,l);
 }
 if (_kbhit()!=0) {
 // printf("HEPP\n");
  l = read(fileno(stdin),buff,sizeof(buff));  
  send(s,buff,l,0);
 }
 Sleep(1);
// if (select ((int)stdin, &rfds2, NULL, NULL, &timev)) {
//;
// }
 //printf("(filtered)");
}
}

int main (int argc, char *argv[]) {
WSAData wsa;
WSAStartup(MAKEWORD(1,1),&wsa);
struct sockaddr_in target_ip;
SOCKET sock;

   target_ip.sin_family = AF_INET;
   target_ip.sin_addr.s_addr = inet_addr(argv[1]);
   target_ip.sin_port = htons(atoi(argv[2]));
 if ((sock=socket(AF_INET,SOCK_STREAM,0)) == -1)
   {
       perror("- Socket");
       return(0);
   }
 if(connect(sock,(struct sockaddr *)&target_ip, sizeof(target_ip)) != 0)
   {
       perror("- Connect");
       return(0);
   }
 shell(sock);
 WSACleanup();
}

Grinler
Clip,

Thanks bro. THat was exactly what I needed. I have found so many articles on google about how you are supposed to make a blocking socket with certain calls like recv, but nothing showing the proper usage of select or the FD_ functions.

Thanks again.

Grinler
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.

 
Invision Power Board © 2001-2005 Invision Power Services, Inc.