자료2007. 3. 10. 12:38

방화벽같은데서 특정포트 접속이 막힌 경우 사용할 수 있는 소스


 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <map>

std::map<int, bool> Sessions;

void * clnt_up(void *);
void * clnt_down(void *);

struct Address
{
 int server;
 int client;
};

void error_handling(char *message)
{
 fputs(message, stderr);
 fputc('\n', stderr);
 exit(1);
}

int nonblock(int fd, int nblockFlag) {
 int flags; flags = fcntl( fd, F_GETFL, 0);
 if ( nblockFlag == 1 )
  return fcntl( fd, F_SETFL, flags | O_NONBLOCK);
 else
  return fcntl( fd, F_SETFL, flags & (~O_NONBLOCK));
}


int main(int argc, char **argv)
{
 pid_t  pid;
 if (( pid = fork()) < 0) exit(0);
 else if(pid != 0)  exit(0);
 chdir("/");
 setsid();  

 int serv_sock=socket(PF_INET, SOCK_STREAM, 0);

 struct sockaddr_in serv_addr;
 memset((char *)&serv_addr, 0, sizeof(serv_addr));
 serv_addr.sin_family=AF_INET;
 serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
 serv_addr.sin_port=htons(~~~);

 struct sockaddr_in connect_addr;
 memset((char *)&connect_addr, 0, sizeof(connect_addr));
 connect_addr.sin_family=AF_INET;
 connect_addr.sin_addr.s_addr=htonl(~~~~);
 connect_addr.sin_port=htons(~~~);


 int option=1;
 setsockopt(serv_sock,SOL_SOCKET,SO_REUSEADDR,&option,sizeof(option));

 if(bind(serv_sock, (struct sockaddr*) &serv_addr, sizeof(serv_addr))==-1) {
  error_handling("bind() error");
  return 0;
 }

 if(listen(serv_sock, 5)==-1) {
  error_handling("listen() error");
  return 0;
 }

 struct sockaddr_in clnt_addr;
 int clnt_sock;
 int clnt_addr_size;
 pthread_t thread;
 Address address;

 int connection;

 while(1){
  clnt_addr_size=sizeof(clnt_addr);
  clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr,(socklen_t *)&clnt_addr_size);

  connection=socket(PF_INET, SOCK_STREAM, 0);
 
  if(connect(connection, (struct sockaddr*)&connect_addr, sizeof(connect_addr))!=0)  {
   close(clnt_sock);
   continue;
  }

  address.server=connection;
  address.client=clnt_sock;

  Sessions[connection]=1;
  Sessions[clnt_sock]=1;

  pthread_create(&thread, NULL, clnt_up, (void*)&address);
  pthread_create(&thread, NULL, clnt_down, (void *)&address);


  sleep(1);
 }

 return 0;
}

void * clnt_up(void * args) {
 Address *address=(Address *)args;
 int server=address->server;
 int client=address->client;

 char * buf[256];
 int recvd=-1;

 nonblock(client,1);
 
 while(true) {
  while(recvd==-1) {
   recvd=read(client,buf,256);
   usleep(10000);
  }
  write(server,buf,recvd);
  if(recvd==0) break;
  recvd=-1;
 }
 
 Sessions.erase(client);
 Sessions[server]=0;
 close(client);
}

void * clnt_down(void * args) {
 Address *address=(Address *)args;
 int server=address->server;
 int client=address->client;

 char * buf[256];
 int recvd=-1;
 
 nonblock(server,1);

 while(Sessions[server]) {
  while(recvd==-1 && Sessions[server]) {
   recvd=read(server,buf,256);
   usleep(10000);
  }
  write(client,buf,recvd);
  if(recvd==0) break;
  recvd=-1;
 }
 Sessions.erase(server);
 close(server);
}



~~~로 된 부분에 알맞은 포트와 서버주소를 넣으면 된다- 리눅스용.
Posted by jongwook