예제 #1
0
파일: server.c 프로젝트: DaisyPi/sensortag
struct server *server_open_tcp(enum server_type type)
{
	struct server *server;

	server = malloc(sizeof(*server));
	if (!server)
		return server;

	memset(server, 0, sizeof(*server));
	server->type = type;
	server->id = 0x43;

	server->fd = open_tcp();
	if (server->fd < 0) {
		free(server);
		return NULL;
	}

	if (mainloop_add_fd(server->fd, EPOLLIN, server_accept_callback,
						server, server_destroy) < 0) {
		close(server->fd);
		free(server);
		return NULL;
	}

	return server;
}
FILE *fopen_http( char *server, char *path )
{
	int fd;
	FILE *stream;
	char buffer[BUFFER_SIZE];

	/* Make the connection */
	fd = open_tcp(server,HTTP_PORT);

	/* Put a buffered stream on the fd */
	stream = fdopen(fd,"r+");
	if(!stream) {
		printf("FAILURE: fdopen(): %s\n", strerror(errno));
		close(fd);
		exit(EXIT_FAILURE);
	}

	/* Send the request */
	fprintf(stream,"GET %s HTTP/1.0\n\n",path);
	fflush(stream);

	/* Eat up any header data until a blank line */
	while(fgets(buffer,BUFFER_SIZE,stream)) {
		if(strlen(buffer)==2) {
			return stream;
		}
	}

	/* If we got here, the end of the header was not found */
	fclose(stream);
	return NULL;
}
예제 #3
0
int http_connect(http_client_t *http_client, char *hostname, int port)
{
    http_request_init(&http_client->request);
    http_response_init(&http_client->response);
    sprintf(http_client->request.req_header.host, "%s", hostname);

    return open_tcp(&http_client->connection, hostname, port);
}
예제 #4
0
int http_init(http_client_t *http_client, const char *hostname)
{
	if(open_tcp(&http_client->network, hostname, 80) == -1)
		return -1;
	http_client->status = -1;
	http_client->content_length = -1;
	bzero(http_client->content_text, 400001);
	//fprintf(stderr,"http init success!\n");
	return 0;
}
예제 #5
0
파일: tcpsock.c 프로젝트: zidier215/tcpip
int
so_tcp_conn(struct usock *up)
{
	int s;
	struct tcb *tcb;
	struct socket lsock,fsock;
	struct sockaddr_in *local,*remote;

	if(up->name == NULL){
		autobind(up);
	}
	
	if(checkipaddr(up->peername,up->peernamelen) == -1){
		errno = EAFNOSUPPORT;
		return -1;
	}
	s = up->index;
	/* Construct the TCP-style ports from the sockaddr structs */
	local = (struct sockaddr_in *)up->name;
	remote = (struct sockaddr_in *)up->peername;

	if(local->sin_addr.s_addr == INADDR_ANY)
		/* Choose a local address */
		local->sin_addr.s_addr = locaddr(remote->sin_addr.s_addr);

	lsock.address = local->sin_addr.s_addr;
	lsock.port = local->sin_port;
	fsock.address = remote->sin_addr.s_addr;
	fsock.port = remote->sin_port;

	/* Open the TCB in active mode */
	up->cb.tcb = open_tcp(&lsock,&fsock,TCP_ACTIVE,0,
	 s_trcall,s_ttcall,s_tscall,up->tos,s);

	/* Wait for the connection to complete */
	while((tcb = up->cb.tcb) != NULL && tcb->state != TCP_ESTABLISHED){
		if(up->noblock){
			errno = EWOULDBLOCK;
			return -1;
		} else if((errno = kwait(up)) != 0){
			return -1;
		}
	}
	if(tcb == NULL){
		/* Probably got refused */
		FREE(up->peername);
		errno = ECONNREFUSED;
		return -1;
	}
	return 0;
}
예제 #6
0
/* Start up FTP service */
int
ftpstart(int argc,char *argv[],void *p)
{
	struct socket lsocket;

	if(ftp_tcb != NULL)
		close_tcp(ftp_tcb);
	lsocket.address = INADDR_ANY;
	if(argc < 2)
		lsocket.port = IPPORT_FTP;
	else
		lsocket.port = tcp_port_number(argv[1]);

	ftp_tcb = open_tcp(&lsocket,NULL,TCP_SERVER,0,ftpscr,NULL,ftpscs,0,0);
	return 0;
}
예제 #7
0
파일: smisc.c 프로젝트: dieterdeyke/WAMPES
/* Start TCP echo server */
int
echo1(
    int argc,
    char *argv[],
    void *p)
{
    struct socket lsocket;

    lsocket.address = INADDR_ANY;
    if(argc < 2)
        lsocket.port = IPPORT_ECHO;
    else
        lsocket.port = tcp_port_number(argv[1]);
    echo_tcb = open_tcp(&lsocket,NULL,TCP_SERVER,0,echo_server,echo_server,misc_state,0,0);
    return 0;
}
예제 #8
0
파일: tcpsock.c 프로젝트: zidier215/tcpip
int
so_tcp_listen(struct usock *up,int backlog)
{
	struct sockaddr_in *local;
	struct socket lsock;

	if(up->name == NULL)
		autobind(up);

	local = (struct sockaddr_in *)up->name;
	lsock.address = local->sin_addr.s_addr;
	lsock.port = local->sin_port;
	up->cb.tcb = open_tcp(&lsock,NULL,
	 backlog ? TCP_SERVER:TCP_PASSIVE,0,
	s_trcall,s_ttcall,s_tscall,up->tos,up->index);
	return 0;
}
예제 #9
0
파일: s2.c 프로젝트: GabberBaby/shells
int main (int argc, char *argv[])
{
    args_t args;

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

    // set default parameters
    args.address   = NULL;
    args.ai_family = AF_INET;
    args.port      = DEFAULT_PORT;
    args.port_nbr  = atoi(args.port);
    args.mode      = -1;

    printf ("\n[ simple remote shell for windows v2\n");

    parse_args(&args, argc, argv);

    if (args.mode == -1) {
        if (args.address==NULL) {
            args.mode=SERVER_MODE;
        } else {
            args.mode=CLIENT_MODE;
        }
    }

    if (open_tcp(&args))
    {
        if (args.mode == SERVER_MODE)
        {
            server(&args);
        } else {
            client(&args);
        }
        close_tcp(&args);
    }
    return 0;
}
예제 #10
0
int main(int argc, char *argv[])
{
	const char *connect_address = NULL;
	const char *server_address = NULL;
	const char *unix_path = NULL;
	unsigned short tcp_port = 0xb1ee;	/* 45550 */
	const char *str;
	sigset_t mask;

	for (;;) {
		int opt;

		opt = getopt_long(argc, argv, "c:l::u::p:i:dvh",
						main_options, NULL);
		if (opt < 0)
			break;

		switch (opt) {
		case 'c':
			connect_address = optarg;
			break;
		case 'l':
			if (optarg)
				server_address = optarg;
			else
				server_address = "0.0.0.0";
			break;
		case 'u':
			if (optarg)
				unix_path = optarg;
			else
				unix_path = "/tmp/bt-server-bredr";
			break;
		case 'p':
			tcp_port = atoi(optarg);
			break;
		case 'i':
			if (strlen(optarg) > 3 && !strncmp(optarg, "hci", 3))
				str = optarg + 3;
			else
				str = optarg;
			if (!isdigit(*str)) {
				usage();
				return EXIT_FAILURE;
			}
			hci_index = atoi(str);
			break;
		case 'd':
			debug_enabled = true;
			break;
		case 'v':
			printf("%s\n", VERSION);
			return EXIT_SUCCESS;
		case 'h':
			usage();
			return EXIT_SUCCESS;
		default:
			return EXIT_FAILURE;
		}
	}

	if (argc - optind > 0) {
		fprintf(stderr, "Invalid command line parameters\n");
		return EXIT_FAILURE;
	}

	if (unix_path && server_address) {
		fprintf(stderr, "Invalid to specify TCP and Unix servers\n");
		return EXIT_FAILURE;
	}

	if (connect_address && (unix_path || server_address)) {
		fprintf(stderr, "Invalid to specify client and server mode\n");
		return EXIT_FAILURE;
	}

	mainloop_init();

	sigemptyset(&mask);
	sigaddset(&mask, SIGINT);
	sigaddset(&mask, SIGTERM);

	mainloop_set_signal(&mask, signal_callback, NULL, NULL);

	if (connect_address) {
		int host_fd, dev_fd;

		printf("Connecting to %s:%u\n", connect_address, tcp_port);

		dev_fd = connect_tcp(connect_address, tcp_port);
		if (dev_fd < 0)
			return EXIT_FAILURE;

		printf("Opening virtual device\n");

		host_fd = open_vhci(0x00);
		if (host_fd < 0) {
			close(dev_fd);
			return EXIT_FAILURE;
		}

		if (!setup_proxy(host_fd, false, dev_fd, true)) {
			close(dev_fd);
			close(host_fd);
			return EXIT_FAILURE;
		}
	} else {
		int server_fd;

		if (unix_path) {
			printf("Listening on %s\n", unix_path);

			server_fd = open_unix(unix_path);
		} else if (server_address) {
			printf("Listening on %s:%u\n", server_address,
								tcp_port);

			server_fd = open_tcp(server_address, tcp_port);
		} else {
			fprintf(stderr, "Missing emulator device\n");
			return EXIT_FAILURE;
		}

		if (server_fd < 0)
			return EXIT_FAILURE;

		mainloop_add_fd(server_fd, EPOLLIN, server_callback,
							NULL, NULL);
	}

	return mainloop_run();
}
예제 #11
0
파일: cms.c 프로젝트: MavenRain/sslcmd
int main (int argc, char *argv[])
{
  INIT_SECURITY_INTERFACE pInitSecurityInterface;
  
  // set buffer width of console
  setw (300);
  
  puts ("\n  [ cms v0.1 - Copyleft 2015 (x) @Odzhan\n");
  
  // set up default values
  args.address   = NULL;
  args.ai_family = AF_INET;
  args.port      = DEFAULT_PORT;
  args.port_nbr  = atoi(args.port);
  
  pInitSecurityInterface = (INIT_SECURITY_INTERFACE)GetProcAddress(LoadLibrary("Secur32"), "InitSecurityInterfaceA" );
  if (pInitSecurityInterface==NULL) printf ("didn't resolve");
  sspi = pInitSecurityInterface();
  
  // process command line
  parse_args(argc, argv);

  // resolve address and open socket
  if (open_tcp ()) 
  {
    start_handler ();
      
    // create credentials
    if (create_creds()==SEC_E_OK)
    {
      // connect to server
      if (connect (s, ai_addr, ai_addrlen) != SOCKET_ERROR) {
        // perform the handshake
        if (chs () == SEC_E_OK) {
          printf ("  [ connected\n\n");
          secure_info();
          ss=sspi->QueryContextAttributes (&hContext, SECPKG_ATTR_STREAM_SIZES, &Sizes );
          cbBufferLen  = Sizes.cbHeader  +  Sizes.cbMaximumMessage  +  Sizes.cbTrailer;
          pbBufferIn        = LocalAlloc(LMEM_FIXED, cbBufferLen);
          pbBufferOut       = LocalAlloc(LMEM_FIXED, cbBufferLen);
          pbDataIn=pbBufferIn + Sizes.cbHeader;
          pbDataOut=pbBufferOut + Sizes.cbHeader;
          cbBufferLen = Sizes.cbMaximumMessage;
          
          printf ("  [ running cmd\n");
          cmd();
            
        } else {
          printf ("  [ handshake failed\n");
        }
      } else {
        printf ("  [ unable to connect\n");
      }
    } else {
      printf ("  [ error creating credentials\n");
    }
    stop_handler ();
    close_tcp();
  }
  return 0;
}
예제 #12
0
void * do_crawler(void *item)
{
	char *url_ptr;
	int clientfd ;
	static int pages = 0;
	static int error = 0;
	char *buf = NULL;
	urlq_t *url_list_head = NULL, *p, *p_pre;
	char cur_dir[256];
	hash_table *hash_in, *hash_out;
/*	int tid = pthread_self();*/
	int j = 0;
	int i= 0;
	int pos_found = 0;
	char temp[256];
	int status = 0;
	static int a = 0;
	static int b = 0;
	static int c = 0;
	static int d = 0;
	static int e = 0;
	static int f = 0;
	static int g = 0;
	while(1){	
		pthread_mutex_lock(&mutex);
		while (urlqueue.head_ptr->next == NULL){
			pthread_cond_wait(&ready, &mutex);
		}

		pthread_mutex_lock(&((threadpool_item *)item)->waitlock);
		((threadpool_item *)item)->idle = 0;
		pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
		url_ptr = queue_pop(&urlqueue);
		g++;
		pthread_mutex_unlock(&mutex);
		

		pthread_mutex_lock(&mutex);
		/*if not visited, set flag = 1*/
		if(has_visited(hash, url_ptr) == 1){
			pthread_mutex_unlock(&mutex);
			free(url_ptr); 
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);
			d++;	
			continue;	
		}
		hash_out = has_url(hash, url_ptr);
		e++;
		pthread_mutex_unlock(&mutex);
		if (hash_out == NULL){
			printf("error\n");
			getchar();
		}
		*temp = '\0';
		cur_dir[0] = '\0';
		strcpy(cur_dir, url_ptr);
		j = strlen(cur_dir);
		for (;cur_dir[j] != '/' && j != 0; j--) ;
		if(j == 0)
			cur_dir[j] = '\0';
		else
			cur_dir[j+1] = '\0';

		for (i = 0; i < 3; i++){
			if((clientfd = open_tcp("127.0.0.1", 80)) < 0){
				close_fd(clientfd);
				continue;
			}

			if( http_do_get(clientfd, rootdir, "127.0.0.1", url_ptr) < 0){
				close_fd(clientfd);
				continue;
			}

			if(recv_line(clientfd, temp) <= 0){
				close_fd(clientfd);
				continue;
			}
			if((status = http_response_status(temp))  == 4){
				printf("%s error %d\n",url_ptr, error++);
				pthread_mutex_lock(&mutex);
				set_status(hash, url_ptr, 4);
				set_webg_status(webg, hash_out, 4);
				pthread_mutex_unlock(&mutex);
			
				pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
				((threadpool_item *)item)->idle =1;
				pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
				close_fd(clientfd);
				break;	
			}
			buf = http_response_body(clientfd);
			close_fd(clientfd);
			break;
		}
		if (status == 4)
			continue;
		if(i == 3){
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
			close_fd(clientfd);
			continue;	
		}
		if (buf == NULL){
			pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
			((threadpool_item *)item)->idle =1;
			pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
			continue;
		}
		printf("%s pages %d\n", url_ptr,pages++);
		extract_link(buf, cur_dir, &url_list_head);
		free(buf);
		buf = NULL;	
		p = url_list_head->next;
		p_pre = url_list_head;
		while (p != NULL){
			
			if(strcmp(url_ptr, p->url_ptr) == 0){
				p_pre->next = p->next;
				free(p->url_ptr);
				free(p);
				a++;
				p = p_pre->next;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
				continue;	
			}			
			pthread_mutex_lock(&mutex);
			hash_in = has_url(hash, p->url_ptr);	
			if (hash_in != NULL ){
				insert_edge(webg, hash_in, hash_out);
				pthread_mutex_unlock(&mutex);
				p_pre->next = p->next;
				free(p->url_ptr);
				free(p);
				p = p_pre->next;
				b++;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
				continue;
			}
			else{
				pos_found = insert_vertex(webg, hash_out, p->url_ptr);
				insert_hash_item(hash, p->url_ptr, pos_found, 0);	
				pthread_mutex_unlock(&mutex);
				c++;
				p_pre = p;
				p = p->next;
				printf("a= %d, b= %d, c= %d, d= %d, e= %d, f= %d, g= %d\n", a,b,c,d,e,f,g);
			}
		}
		
		if(p_pre != url_list_head){
			pthread_mutex_lock(&mutex);
			queue_push(&urlqueue, url_list_head->next, p_pre);
			f++;
			pthread_mutex_unlock(&mutex);
		}
		free(url_list_head);
		p = p_pre = url_list_head = NULL;
	
		pthread_mutex_lock(&((threadpool_item *)item)->waitlock);	
		((threadpool_item *)item)->idle = 1;
		pthread_mutex_unlock(&((threadpool_item *)item)->waitlock);	
/*printf("next time!\n");*/
	}
/*printf("over!\n");*/
	return NULL;	
}
예제 #13
0
/* Implements the general case for command message send-receive
 *
 * If any of our underlying calls return an error, we will pass
 * that up to our caller immediately.
 *
 * Otherwise we'll examine the response.
 *
 * Returns:
 * - some errno from one of our calls
 * - INVALID_ID if the server does not recognize our member id
 * - COMMAND_SUCC on success
 * - COMMAND_FAIL on failure
 * - BOGUS_RESPONSE on an unexpected reply
 *
 * Takes advantage of the fact that for a request message type
 * x, the corresponding success and fail responses are x+1
 * and x+2 respectively. We encode that relationship in the macros
 * CODE_SUCC() and CODE_FAIL(). Keepalive and quit messages do not
 * wait for a response.
 */
int generic_command(int msg_type, char *arg, char *buf) {

    int ret = 0, bytes = 0;
    struct control_msghdr *cmh = (struct control_msghdr *) buf;

    memset(buf, 0, MAX_MSG_LEN);
    cmh->msg_type = msg_type;
    cmh->member_id = /*htons*/(member_id);
    cmh->msg_len = /*htons*/(sizeof(struct control_msghdr));

    if (arg) {
        strcpy((char *)(cmh->msgdata), arg);
        cmh->msg_len += strlen(arg);
    }
    
    if (msg_type == REGISTER_REQUEST) {
        struct register_msgdata *rdata;
        rdata = (struct register_msgdata *)cmh->msgdata;
        /* send registration data */
        rdata->udp_port = htons(client_udp_port);
        strcpy((char *)rdata->member_name, member_name);
        cmh->msg_len +=
            sizeof(struct register_msgdata) + strlen(member_name);
    }

    /* OPEN */
    if ((ret = open_tcp()) != 0) {
        return ret;
    }   

    /* WRITE */
    if ((bytes = write(tcp_sock, buf, /*ntohs*/(cmh->msg_len))) == -1) {
        ret = errno;
        fprintf(stderr, "%s: write: %s\n", __func__, strerror(errno));
        return ret;
    } else {
        seen_server();
    }   

    debug_sub_print(DBG_TCP, "%s: %dB written\n", __func__, bytes);

    /* Response expected? */
    if (msg_type < MEMBER_KEEP_ALIVE) {
        if ((bytes = recv(tcp_sock, buf, MAX_MSG_LEN, 0)) == -1) {
            ret = errno;
            fprintf(stderr, "%s: recv'd: %s\n", __func__, strerror(errno));
            return ret;
        }
        close_tcp();
        debug_sub_print(DBG_TCP, "%s: %dB recv'd\n", __func__, bytes);

        if ((cmh->msg_type) == CODE_SUCC(msg_type)) {
            return COMMAND_SUCC;
        } else if ((cmh->msg_type) == CODE_FAIL(msg_type)) {
            if ((strstr((char *) (cmh->msgdata), "Member id invalid!"))
                == (char *) (cmh->msgdata))
            {
                return ID_INVALID;
            }
            return COMMAND_FAIL;
        } else {
            fprintf(stderr, "Unexpected response (%d)\n", cmh->msg_type);
            return BOGUS_RESPONSE;
        }
    } else {
        close_tcp();
    }
    return ret;
}