Beispiel #1
0
void *socket_http_listener()
{
	prctl(PR_SET_NAME, (char *)"HTTP", 0, 0, 0);

	while (1){
		struct client_ctx *cc = socket_listen(&srv_http);
		if (!cc)
			continue;
		struct lnode *n = calloc(sizeof(*n), 1);

		queue_push(n, cc);
	}

	pthread_exit(0);
}
Beispiel #2
0
/*-------------------------------------------------------------------------*\
* Puts the sockt in listen mode
\*-------------------------------------------------------------------------*/
static int meth_listen(lua_State *L)
{
    p_tcp tcp = (p_tcp) auxiliar_checkclass(L, "tcp{master}", 1);
    int backlog = (int) luaL_optnumber(L, 2, 32);
    int err = socket_listen(&tcp->sock, backlog);
    if (err != IO_DONE) {
        lua_pushnil(L);
        lua_pushstring(L, socket_strerror(err));
        return 2;
    }
    /* turn master object into a server object */
    auxiliar_setclass(L, "tcp{server}", 1);
    lua_pushnumber(L, 1);
    return 1;
}
Beispiel #3
0
int main(){

    lib_init();

    socket_t * serverSocket = socket_new();
    socket_bind(serverSocket, 5000);
    socket_listen(serverSocket);

    film_maker_t*  film_makers[FILM_MAKERS_AMOUNT];

     for (int i = 0; i < FILM_MAKERS_AMOUNT; i++) {
        film_makers[i] = film_maker_new();

    }

       parse(film_makers,"XML_format.xml");


    while(1) {
        socket_t * clientSocket = socket_accept(serverSocket);
        char buf[102400];

        if(socket_read(clientSocket, buf, 102400) == 0) {
            socket_close(clientSocket);
            socket_free(clientSocket);
            puts("Skipping empty request");
            continue;
        }



        http_request_t req = http_request_parse(buf);

        server_reply(clientSocket, req, film_makers);
//puts("fghj");

        socket_close(clientSocket);
        socket_free(clientSocket);
    }

    for (int i = 0; i < 3; i++)
        film_maker_free(film_makers[i]);

    socket_close(serverSocket);
    socket_free(serverSocket);
          lib_free();
        return 0;
}
Beispiel #4
0
static void ot_try_bind( char ip[4], uint16 port, int is_tcp ) {
  int64 s = is_tcp ? socket_tcp4( ) : socket_udp4();

  if( socket_bind4_reuse( s, ip, port ) == -1 )
    panic( "socket_bind4_reuse" );

  if( is_tcp && ( socket_listen( s, SOMAXCONN) == -1 ) )
    panic( "socket_listen" );

  if( !io_fd( s ) )
    panic( "io_fd" );

  io_setcookie( s, is_tcp ? FLAG_TCP : FLAG_UDP );

  io_wantread( s );
}
Beispiel #5
0
static int64_t ot_try_bind( ot_ip6 ip, uint16_t port ) {
  int64 sock = socket_tcp6( );

  if( socket_bind6_reuse( sock, ip, port, 0 ) == -1 )
    panic( "socket_bind6_reuse" );

  if( socket_listen( sock, SOMAXCONN) == -1 )
    panic( "socket_listen" );

  if( !io_fd( sock ) )
    panic( "io_fd" );

  io_setcookie( sock, (void*)FLAG_SERVERSOCKET );
  io_wantread( sock );
  return sock;
}
Beispiel #6
0
/**
 * @brief sctp_server_create 
 *
 * @param sck  [in] socket impl
 * @param ip   [in] listen ip
 * @param port [in] listen port
 *
 * @return 0, if succ; -1, if failed
 */
int sctp_server_create(struct socket_impl *sck, const char *ip, unsigned short port)
{
    struct sctp_initmsg initmsg = {0};
    struct sctp_event_subscribe event = {0};
    if (sck == NULL || sck->fd > 0) return -1;

    /* init impl */
    memset(sck, 0, sizeof(*sck));
    
    /* create, bind and listen socket */
    if ((sck->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP)) < 0)
    {
        perror("sctp socket failed");
    }

    /* socket bind */
    inet_addr_init(&sck->addr.in_addr, ip, port);
    socket_bind(sck->fd, (struct sockaddr *)&sck->addr.addr);

    /* init msg */
    initmsg.sinit_num_ostreams = 5;
    initmsg.sinit_max_instreams = 5;
    initmsg.sinit_max_attempts = 4;
    if (setsockopt(sck->fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg)) < 0)
        perror("sctp setsockopt initmsg failed");

    /* sctp event */
    event.sctp_data_io_event = 1;
    if (setsockopt(sck->fd, IPPROTO_SCTP, SCTP_EVENTS, &event, sizeof(event)) < 0)
        perror("sctp setsockopt event failed");

    /* socket listen */
    socket_listen(sck->fd, 5);

    /* set socket block */
    make_socket_block(sck->fd);
   
    /* create tcp server thread runtine */
    if (pthread_create(&sck->ptd, NULL, tcp_server_backup_service, sck))

    {
        printf("sctp server create failed.\n");
        return -1;
    }

    return 0;
}
Beispiel #7
0
void server_init (unsigned port)
{
    unsigned sock;
    client_t client;

    _server_port = port;
    for (sock = 0; sock < MAX_SOCK_NUM; sock++) {
        client_init_sock (&client, sock);

        if (client_status (&client) == SnSR_CLOSED) {
            socket_init (sock, SnMR_TCP, port, 0);
            socket_listen (sock);
            _socket_port[sock] = port;
            break;
        }
    }
}
Beispiel #8
0
/*
  test that a server responds correctly to attempted name refresh requests
*/
static bool nbt_refresh_own(struct torture_context *tctx)
{
	struct nbt_name_refresh io;
	NTSTATUS status;
	struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
	const char *myaddress;
	struct socket_address *socket_address;
	struct nbt_name name;
	const char *address;

	if (!torture_nbt_get_name(tctx, &name, &address))
		return false;
	
	myaddress = iface_best_ip(address);

	socket_address = socket_address_from_strings(tctx, nbtsock->sock->backend_name,
						     myaddress, 0);
	torture_assert(tctx, socket_address != NULL, 
				   "Can't parse socket address");

	status = socket_listen(nbtsock->sock, socket_address, 0, 0);
	torture_assert_ntstatus_ok(tctx, status, 
							   "socket_listen for nbt_referesh_own failed");

	torture_comment(tctx, "Testing name defense to name refresh\n");

	io.in.name = name;
	io.in.dest_addr = address;
	io.in.address = myaddress;
	io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
	io.in.broadcast = False;
	io.in.ttl = 1234;
	io.in.timeout = 3;
	io.in.retries = 0;
	
	status = nbt_name_refresh(nbtsock, tctx, &io);

	torture_assert_ntstatus_ok(tctx, status, 
				talloc_asprintf(tctx, "Bad response from %s for name refresh", address));
	
	CHECK_STRING(tctx, io.out.name.name, name.name);
	CHECK_VALUE(tctx, io.out.name.type, name.type);
	CHECK_VALUE(tctx, io.out.rcode, NBT_RCODE_ACT);

	return true;
}
Beispiel #9
0
int nettest5(void)
{
	int err;
	sockaddr addr;
	int new_fd;

	fd = socket_create(SOCK_PROTO_TCP, 0);
	printf("created socket, fd %d\n", fd);
	if(fd < 0)
		return 0;

	memset(&addr, 0, sizeof(addr));
	addr.addr.len = 4;
	addr.addr.type = ADDR_TYPE_IP;
	addr.port = 1900;
	NETADDR_TO_IPV4(addr.addr) = IPV4_DOTADDR_TO_ADDR(0,0,0,0);

	err = socket_bind(fd, &addr);
	printf("socket_bind returns %d\n", err);
	if(err < 0)
		return 0;

	err = socket_listen(fd);
	printf("socket_listen returns %d\n", err);
	if(err < 0)
		return 0;

	new_fd = socket_accept(fd, &addr);
	printf("socket_accept returns %d\n", new_fd);
	if(new_fd < 0)
		return 0;

	err = socket_write(new_fd, "hello world!\n", strlen("hello world!\n"));
	printf("socket_write returns %d\n", err);

	printf("sleeping for 5 seconds\n");
	usleep(5000000);

	printf("closing fd %d\n", new_fd);
	socket_close(new_fd);
	printf("closing fd %d\n", fd);
	socket_close(fd);

	return 0;
}
Beispiel #10
0
void
f_socket_listen (void)
{
    int i, fd, port;
    char addr[ADDR_BUF_SIZE];

    fd = (sp - 1)->u.number;
    get_socket_address(fd, addr, &port, 0);

    if (VALID_SOCKET("listen")) {
	i = socket_listen(fd, sp);
	pop_stack();
        sp->u.number = i;
    } else {
	pop_stack();
        sp->u.number = EESECURITY;
    }
}
Beispiel #11
0
static void
listen_thread(void* arg)
{
    struct nr_mgr* mgr = (struct nr_mgr*)arg;

    sock client_fd = SOCKET_ERROR;
    struct sockaddr_in socketaddress;
    socklen_t size = sizeof(struct sockaddr);

    sock listen_fd = socket_listen(4002, 25);
    int total_num = 0;

    if(SOCKET_ERROR != listen_fd)
    {
        for(;;)
        {
            while((client_fd = accept(listen_fd, (struct sockaddr*)&socketaddress, &size)) < 0)
            {
                if(EINTR == sErrno)
                {
                    continue;
                }
            }

            if(SOCKET_ERROR != client_fd)
            {
                total_num++;
                //printf("accept %d, current total num : %d\n", client_fd, total_num);

                nrmgr_addfd(mgr, client_fd);
            }
        }

        socket_close(listen_fd);
        listen_fd = SOCKET_ERROR;
    }
    else
    {
        //printf("listen failed\n");
    }
    
    printf("exit\n");
    exit(0);
}
Beispiel #12
0
int main() {
    lib_init();

    list_t* list=list_new();

    lanser * freeLanser[10];

    for (int i = 0; i < 10; i++)
        freeLanser[i] = Freelanser_new();

  //  parse(freeLanser);

    socket_t * serverSocket = socket_new();
    socket_bind(serverSocket, 5000);
    socket_listen(serverSocket);


   while (1) {
        socket_t * clientSocket = socket_accept(serverSocket);

        char buf[10000]="";

        if (!socket_read(clientSocket, buf, sizeof(buf))) {
            puts("Skipping empty request");
            socket_close(clientSocket);
            socket_free(clientSocket);
            continue;
        }

        http_request_t req = http_request_parse(buf);

        server_answer(list,req,clientSocket,freeLanser);

        socket_close(clientSocket);
        socket_free(clientSocket);
    }

    socket_close(serverSocket);
    socket_free(serverSocket);

    lib_free();
    list_free(list);
    return 0;
}
Beispiel #13
0
/**
 * @brief start up a local server socket
 *
 * @param domain [in] This value can be AF_INET,AF_UNIX,AF_LOCAL,
 *                    PF_INET, PF_UINX and PF_LOCAL.
 * @param type   [in] can be SOCK_TREAM, SOCK_DGRAM.
 * @param path   [in] socket file path
 * @param is_ser [in] srever or client
 *
 * @return socket fd, if succ; exit, if fail
 */
int startup_local_server(int type, const char *path)
{
    int fd = -1;
    struct sockaddr_un addr = {0};
    
    /* init sockaddr_in */
    local_addr_init(&addr, path);
	
    /* create socket */
    fd = socket_create(AF_UNIX, type);
    make_listen_socket_reuseable(fd);
	unlink(addr.sun_path);
    
    /* bind and listen socket */
    socket_bind(fd, (struct sockaddr *)&addr);
    if (type == SOCK_STREAM) socket_listen(fd, 5);
	
    return fd;  
}
Beispiel #14
0
static int	init_socket(t_socket *sock, int port)
{
  if (socket_ctor(sock, AF_INET, SOCK_STREAM, 0) == RET_FAILURE)
    {
      perror("error: socket creation");
      return (RET_FAILURE);
    }
  if (socket_bind(sock, INADDR_ANY, port))
    {
      perror("error: socket bind");
      return (RET_FAILURE);
    }
  if (socket_listen(sock, 42) == RET_FAILURE)
    {
      perror("error: socket listen");
      return (RET_FAILURE);
    }
  return (RET_SUCCESS);
}
Beispiel #15
0
/**
 * Writes the received payload into payload argument and returns the tag of the message
 */
unsigned char server(unsigned char * payload)
{
    ulong msg_len = PAYLOAD_LEN + 1;
    unsigned char* msg = malloc(msg_len);

    // receive the message
    BIO * b = socket_listen();
    recv(b, msg, msg_len);
    // wait for the client to close, to avoid "Address already in use" errors
    wait_close(b);

    unsigned char * pad = otp(msg_len);         // apply the one-time pad
    xor(msg, pad, msg_len);

    // get the payload
    memcpy(payload, msg + 1, PAYLOAD_LEN);

    // return the tag
    return *msg;
}
Beispiel #16
0
static socket_t Listen(int port)
{
	int r;
	socket_t server;

	server = socket_tcp();
	if(socket_error == server)
		return socket_invalid;

	// reuse addr
	r = socket_setreuseaddr(server, 1);

	// bind
	r = socket_bind_any(server, (unsigned short)port);

	// listen
	r = socket_listen(server, 64);

	return server;
}
Beispiel #17
0
int main() {
    lib_init();
    socket_t * server = socket_new();
    socket_bind(server, 5000);
    socket_listen(server);
	
   char buf[10000];
    char pathBuf[256];
    socket_t * client = NULL;

    while(1) {
		client = socket_accept(server);
        socket_read(client, buf, sizeof(buf));
		if (strlen(buf) == 0)
			continue;
        printf(">> Got request:\n%s\n", buf);

        http_getPath(buf, pathBuf, sizeof(pathBuf));

        http_request_t request = http_request_parse(buf);

        if (strcmp(request.uri, "/") == 0) {
            server_homepage(client);
        } else if (strcmp(request.uri, "/database") == 0) {
            server_students(client, &request);
        }  
        else if (strcmp(request.uri, "/info") == 0) {
            server_info(client);
        }
        else if (strcmp(request.uri, "/filename") == 0) {
            server_file_parse(client);
        }
        else {
            server_notFound(client);
        }
		socket_free(client);
    }
    socket_free(server);
    lib_free();
    return 0;
}
Beispiel #18
0
/**
 * @brief start up a internet server socket
 *
 * @param domain [in] This value can be AF_INET,AF_UNIX,AF_LOCAL,
 *                    PF_INET, PF_UINX and PF_LOCAL.
 * @param type   [in] can be SOCK_TREAM, SOCK_DGRAM.
 * @param ip     [in] ip address, can be NULL
 * @param port   [in] socket port
 *
 * @return socket fd, if succ; exit, if fail
 */
int startup_inet_server(int type, const char *ip, \
						u_short port)
{
    int fd = -1;
    struct sockaddr_in addr = {0};
	
    /* create socket */
    fd = socket_create(AF_INET, type);
    make_listen_socket_reuseable(fd);
    
    /* init sockaddr_in */
    inet_addr_init(&addr, ip, port);
    
    /* bind socket */
    socket_bind(fd, (struct sockaddr *)&addr);
    
    /* build socket listen */
    if (type == SOCK_STREAM) socket_listen(fd, 5);

    return fd;
}
Beispiel #19
0
int qio_channel_socket_listen_sync(QIOChannelSocket *ioc,
                                   SocketAddress *addr,
                                   Error **errp)
{
    int fd;

    trace_qio_channel_socket_listen_sync(ioc, addr);
    fd = socket_listen(addr, errp);
    if (fd < 0) {
        trace_qio_channel_socket_listen_fail(ioc);
        return -1;
    }

    trace_qio_channel_socket_listen_complete(ioc, fd);
    if (qio_channel_socket_set_fd(ioc, fd, errp) < 0) {
        close(fd);
        return -1;
    }

    return 0;
}
Beispiel #20
0
void StartServer(){
    int sockstat, listsock;

    listsock = socket_create(STREAM,"read_callback","close_callback");
    if(listsock < 0){
        debug("Couldn't create socket. errorcode: "+listsock);
        return;
    }

    sockstat = socket_bind(listsock,port);
    if(sockstat < 0){
        debug("Couldn't bind socket. errorcode: "+sockstat);
        return;
    }

    sockstat = socket_listen(listsock,"listen_callback");
    if(sockstat < 0){
        debug("Couldn't listen on socket. errorcode: "+sockstat);
        return;
    }

}
Beispiel #21
0
int Dps_ftp_open_data_port( DPS_CONN *c, DPS_CONN *d){
	char buf[64];	
	unsigned char *a, *p;
	
	int code;
	
	if (!d)
		return -1;
	if (socket_getname(c, &d->sin) == -1)
		return -1;

	if (d->port) {
	  d->sin.sin_port = htons(d->port);
	}

	if (socket_open(d))
		return -1;

	if (socket_listen(d)){
		return -1;
	}
	if (socket_getname(d, &d->sin) == -1){
		return -1;
	}

        a = (unsigned char *)&d->sin.sin_addr;
        p = (unsigned char *)&d->sin.sin_port;

	dps_snprintf(buf, 64, "PORT %d,%d,%d,%d,%d,%d",
                a[0], a[1], a[2], a[3], p[0], p[1]);
	code = Dps_ftp_send_cmd(c, buf);
	if ((code < 0) || strncasecmp(c->buf, "200 ", 4)){
		return -1;
	}
	d->user = c->user;
	d->pass = c->pass;
	return 0;
}
Beispiel #22
0
void http_request_startServer(pupils_t pupils){
    lib_init();
    socket_t * serverSocket = socket_new();
    socket_bind(serverSocket, 5000);
    socket_listen(serverSocket);
    while(1){
        puts("Waiting for connections");
        socket_t * clientSocket = socket_accept(serverSocket);
        puts("New client");
        char buff[BUFFER_LENGTH];
        int readLength = socket_read(clientSocket, buff, BUFFER_LENGTH);
        if(readLength == 0){
            socket_close(clientSocket);
            socket_free(clientSocket);
            puts("Skipping empty request");
            continue;
        }

        printf("Got Request:\n---------------\n%s\n----------------\n", buff);

        http_request_t req = http_request_parse(buff);

        printf("Method: %s\nURI: %s\n", req.method, req.uri);
        puts("Data:");
        for(int i = 0; i < req.formLength; i++){
            char * kvStr = keyvalue_toString(&req.form[i]);
            printf("\t%s\n", kvStr);
            free(kvStr);
        }
        http_request_chooseMethod(req, clientSocket, pupils);
        socket_close(clientSocket);
        socket_free(clientSocket);
    }
    socket_close(serverSocket);
    socket_free(serverSocket);
    lib_free();
}
Beispiel #23
0
int main()
{
    lib_init();
    socket_t * server = socket_new();
    socket_bind(server, 5000);
    socket_listen(server);

    char buf[10000];
    char pathBuf[256];
    socket_t * client = NULL;
    while(1)
    {
        client = socket_accept(server);
        socket_read(client, buf, sizeof(buf));
        http_request_t rs;
        rs = http_request_parse(buf);
        server_reply(client,rs);

    }
    socket_free(server);
    socket_free(client);
    lib_free();
    return 0;
}
Beispiel #24
0
int main() {
    lib_init();
    socket_t * server = socket_new();
    socket_bind(server, 5000);
    socket_listen(server);

   char buf[10000];
    char pathBuf[256];
    socket_t * client = NULL;

    while(1) {
		client = socket_accept(server);
        socket_read(client, buf, sizeof(buf));
		if (strlen(buf) == 0)
			continue;
        printf(">> Got request:\n%s\n", buf);

        http_getPath(buf, pathBuf, sizeof(pathBuf));

        http_request_t request = http_request_parse(buf);

        if (strcmp(request.uri, "/") == 0) {
            server_homepage(client);
        } else if (strcmp(request.uri, "/api/admins") == 0) {
			server_database(client);
		} else if (strcmp(request.uri, "/api/admins/") > 0) {
			server_database_id(client, &request);
		} else {
            error_massage(client, "404 - NOT FOUND!");
        }
		socket_free(client);
    }
    socket_free(server);
    lib_free();
    return 0;
}
Beispiel #25
0
static int start_web_server(http_conf *g)
{
	int count ;
	int epfd;
	int fd = 0;
	epoll_extra_data_t *data;

	data = (epoll_extra_data_t *) malloc(sizeof(epoll_extra_data_t));

	data->type = SERVERFD;
	if(g->port <=0) g->port = 80;
	epfd = epoll_init(MAX_CONNECT);
	//while(count--){
	fd = socket_listen("127.0.0.1", g->port);
	//	web->fd = fd;
	epoll_add_fd(epfd, fd,EPOLL_R, data);
	//	web = web->next;
	//}
	
	g->fd = fd;
	g->epfd = epfd;

	return epfd;
}
Beispiel #26
0
int main()
{
    db_t* base = db_new("workers.db");
    lib_init();

    socket_t* server = socket_new();
    socket_bind(server, PORT);
    socket_listen(server);

    char buffer[10000];
    while(true)
    {
        puts("Waiting for client...!");
        socket_t* client = socket_accept(server);

        if(socket_read(client, buffer, sizeof(buffer)) <= 0)
        {
            socket_close(client);
            socket_free(client);
            continue;
        }

        printf("Request:\n%s\n", buffer);

        http_request_t request = http_request_parse(buffer);

        server_answerRequest(request, client, base);

        socket_free(client);
    }

    socket_free(server);
    lib_free();
    db_free(base);
    return 0;
}
Beispiel #27
0
int mesh_start_listening(Socket *mesh_listen_socket,
                         uint16_t mesh_listen_socket_port,
                         SocketCreateAllocatedFunction create_allocated) {
    int phase = 0;
    struct addrinfo *resolved_address = NULL;
    const char *address = config_get_option_value("listen.address")->string;

    log_info("Opening mesh listen socket (P: %u)", mesh_listen_socket_port);

    resolved_address = socket_hostname_to_address(address, mesh_listen_socket_port);

    // Currently no IPv6 support for mesh.
    if(resolved_address->ai_family == AF_INET6) {
        log_error("Mesh gateway does not support IPv6");

        goto CLEANUP;
    }

    if (resolved_address == NULL) {
        log_error("Could not resolve mesh listen address '%s' (P: %u): %s (%d)",
                  address,
                  mesh_listen_socket_port,
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }

    phase = 1;

    // Create socket.
    if (socket_create(mesh_listen_socket) < 0) {
        log_error("Could not create mesh listen socket: %s (%d)",
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }

    phase = 2;

    if (socket_open(mesh_listen_socket,
                    resolved_address->ai_family,
                    resolved_address->ai_socktype,
                    resolved_address->ai_protocol) < 0) {
        log_error("Could not open %s mesh listen socket: %s (%d)",
                  network_get_address_family_name(resolved_address->ai_family, false),
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }

#ifndef _WIN32
    /*
     * On Unix the SO_REUSEADDR socket option allows to rebind sockets in
     * CLOSE-WAIT state. this is a desired effect. On Windows SO_REUSEADDR
     * allows to rebind sockets in any state. This is dangerous. Therefore,
     * don't set SO_REUSEADDR on Windows. Sockets can be rebound in CLOSE-WAIT
     * state on Windows by default.
     */
    if (socket_set_address_reuse(mesh_listen_socket, true) < 0) {
        log_error("Could not enable address-reuse mode for mesh listen socket: %s (%d)",
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }
#endif

    // Bind socket and start to listen.
    if (socket_bind(mesh_listen_socket,
                    resolved_address->ai_addr,
                    resolved_address->ai_addrlen) < 0) {
        log_error("Could not bind %s mesh listen socket to (A: %s, P: %u): %s (%d)",
                  network_get_address_family_name(resolved_address->ai_family, true),
                  address,
                  mesh_listen_socket_port,
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }

    if (socket_listen(mesh_listen_socket, 10, create_allocated) < 0) {
        log_error("Could not listen to %s mesh socket (A: %s, P: %u): %s (%d)",
                  network_get_address_family_name(resolved_address->ai_family, true),
                  address,
                  mesh_listen_socket_port,
                  get_errno_name(errno),
                  errno);

        goto CLEANUP;
    }

    log_info("Mesh gateway started listening on (A: %s, P: %u, F: %s)",
             address,
             mesh_listen_socket_port,
             network_get_address_family_name(resolved_address->ai_family, true));

    if(event_add_source(mesh_listen_socket->base.handle,
                        EVENT_SOURCE_TYPE_GENERIC,
                        EVENT_READ,
                        mesh_handle_accept,
                        mesh_listen_socket) < 0) {
        goto CLEANUP;
    }

    phase = 3;

    freeaddrinfo(resolved_address);

CLEANUP:
    switch(phase) { // No breaks, all cases fall through intentionally.
    case 2:
        socket_destroy(mesh_listen_socket);

    case 1:
        freeaddrinfo(resolved_address);

    default:
        break;
    }

    return phase == 3 ? 0 : -1;
}
Beispiel #28
0
/* test UDP/138 netlogon requests */
static bool nbt_test_netlogon(struct torture_context *tctx)
{
	struct dgram_mailslot_handler *dgmslot;
	struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(tctx, tctx->ev, 
								 lp_iconv_convenience(tctx->lp_ctx));
	struct socket_address *dest;
	const char *myaddress;
	struct nbt_netlogon_packet logon;
	struct nbt_netlogon_response *response;
	struct nbt_name myname;
	NTSTATUS status;
	struct timeval tv = timeval_current();

	struct socket_address *socket_address;

	const char *address;
	struct nbt_name name;

	struct interface *ifaces;

	name.name = lp_workgroup(tctx->lp_ctx);
	name.type = NBT_NAME_LOGON;
	name.scope = NULL;

	/* do an initial name resolution to find its IP */
	torture_assert_ntstatus_ok(tctx, 
				   resolve_name(lp_resolve_context(tctx->lp_ctx), &name, tctx, &address, tctx->ev),
				   talloc_asprintf(tctx, "Failed to resolve %s", name.name));

	load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
	myaddress = talloc_strdup(dgmsock, iface_best_ip(ifaces, address));


	socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
						     myaddress, lp_dgram_port(tctx->lp_ctx));
	torture_assert(tctx, socket_address != NULL, "Error getting address");

	/* try receiving replies on port 138 first, which will only
	   work if we are root and smbd/nmbd are not running - fall
	   back to listening on any port, which means replies from
	   most windows versions won't be seen */
	status = socket_listen(dgmsock->sock, socket_address, 0, 0);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(socket_address);
		socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
							     myaddress, 0);
		torture_assert(tctx, socket_address != NULL, "Error getting address");

		socket_listen(dgmsock->sock, socket_address, 0, 0);
	}

	/* setup a temporary mailslot listener for replies */
	dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
				      netlogon_handler, NULL);

	ZERO_STRUCT(logon);
	logon.command = LOGON_PRIMARY_QUERY;
	logon.req.pdc.computer_name = TEST_NAME;
	logon.req.pdc.mailslot_name = dgmslot->mailslot_name;
	logon.req.pdc.unicode_name  = TEST_NAME;
	logon.req.pdc.nt_version    = 1;
	logon.req.pdc.lmnt_token    = 0xFFFF;
	logon.req.pdc.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));
	torture_assert(tctx, dest != NULL, "Error getting address");

	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");

	while (timeval_elapsed(&tv) < 5 && !dgmslot->private_data) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert(tctx, response->response_type == NETLOGON_GET_PDC, "Got incorrect type of netlogon response");
	torture_assert(tctx, response->data.get_pdc.command == NETLOGON_RESPONSE_FROM_PDC, "Got incorrect netlogon response command");

	return true;
}
Beispiel #29
0
/* test UDP/138 netlogon requests */
static bool nbt_test_netlogon2(struct torture_context *tctx)
{
	struct dgram_mailslot_handler *dgmslot;
	struct nbt_dgram_socket *dgmsock = nbt_dgram_socket_init(tctx, tctx->ev,
								 lp_iconv_convenience(tctx->lp_ctx));
	struct socket_address *dest;
	const char *myaddress;
	struct nbt_netlogon_packet logon;
	struct nbt_netlogon_response *response;
	struct nbt_name myname;
	NTSTATUS status;
	struct timeval tv = timeval_current();

	struct socket_address *socket_address;

	const char *address;
	struct nbt_name name;

	struct interface *ifaces;
	struct test_join *join_ctx;
	struct cli_credentials *machine_credentials;
	const struct dom_sid *dom_sid;
	
	name.name = lp_workgroup(tctx->lp_ctx);
	name.type = NBT_NAME_LOGON;
	name.scope = NULL;

	/* do an initial name resolution to find its IP */
	torture_assert_ntstatus_ok(tctx, 
				   resolve_name(lp_resolve_context(tctx->lp_ctx), &name, tctx, &address, tctx->ev),
				   talloc_asprintf(tctx, "Failed to resolve %s", name.name));

	load_interfaces(tctx, lp_interfaces(tctx->lp_ctx), &ifaces);
	myaddress = talloc_strdup(dgmsock, iface_best_ip(ifaces, address));

	socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
						     myaddress, lp_dgram_port(tctx->lp_ctx));
	torture_assert(tctx, socket_address != NULL, "Error getting address");

	/* try receiving replies on port 138 first, which will only
	   work if we are root and smbd/nmbd are not running - fall
	   back to listening on any port, which means replies from
	   some windows versions won't be seen */
	status = socket_listen(dgmsock->sock, socket_address, 0, 0);
	if (!NT_STATUS_IS_OK(status)) {
		talloc_free(socket_address);
		socket_address = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name,
							     myaddress, 0);
		torture_assert(tctx, socket_address != NULL, "Error getting address");

		socket_listen(dgmsock->sock, socket_address, 0, 0);
	}

	/* setup a temporary mailslot listener for replies */
	dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
				      netlogon_handler, NULL);
	

	ZERO_STRUCT(logon);
	logon.command = LOGON_SAM_LOGON_REQUEST;
	logon.req.logon.request_count = 0;
	logon.req.logon.computer_name = TEST_NAME;
	logon.req.logon.user_name     = "";
	logon.req.logon.mailslot_name = dgmslot->mailslot_name;
	logon.req.logon.nt_version    = NETLOGON_NT_VERSION_5EX_WITH_IP|NETLOGON_NT_VERSION_5|NETLOGON_NT_VERSION_1;
	logon.req.logon.lmnt_token    = 0xFFFF;
	logon.req.logon.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));

	torture_assert(tctx, dest != NULL, "Error getting address");
	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");

	while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
	map_netlogon_samlogon_response(&response->data.samlogon);

	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE_EX, "Got incorrect netlogon response command");
	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.nt_version, NETLOGON_NT_VERSION_5EX_WITH_IP|NETLOGON_NT_VERSION_5EX|NETLOGON_NT_VERSION_1, "Got incorrect netlogon response command");

	/* setup (another) temporary mailslot listener for replies */
	dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
				      netlogon_handler, NULL);
	
	ZERO_STRUCT(logon);
	logon.command = LOGON_SAM_LOGON_REQUEST;
	logon.req.logon.request_count = 0;
	logon.req.logon.computer_name = TEST_NAME;
	logon.req.logon.user_name     = TEST_NAME"$";
	logon.req.logon.mailslot_name = dgmslot->mailslot_name;
	logon.req.logon.nt_version    = 1;
	logon.req.logon.lmnt_token    = 0xFFFF;
	logon.req.logon.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));

	torture_assert(tctx, dest != NULL, "Error getting address");
	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");

	while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
	map_netlogon_samlogon_response(&response->data.samlogon);

	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");

	torture_assert_str_equal(tctx, response->data.samlogon.data.nt5_ex.user_name, TEST_NAME"$", "Got incorrect user in netlogon response");

	join_ctx = torture_join_domain(tctx, TEST_NAME, 
				       ACB_WSTRUST, &machine_credentials);

	torture_assert(tctx, join_ctx != NULL,
		       talloc_asprintf(tctx, "Failed to join domain %s as %s\n",
		       		       lp_workgroup(tctx->lp_ctx), TEST_NAME));

	dom_sid = torture_join_sid(join_ctx);

	/* setup (another) temporary mailslot listener for replies */
	dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
				      netlogon_handler, NULL);
	
	ZERO_STRUCT(logon);
	logon.command = LOGON_SAM_LOGON_REQUEST;
	logon.req.logon.request_count = 0;
	logon.req.logon.computer_name = TEST_NAME;
	logon.req.logon.user_name     = TEST_NAME"$";
	logon.req.logon.mailslot_name = dgmslot->mailslot_name;
	logon.req.logon.sid           = *dom_sid;
	logon.req.logon.nt_version    = 1;
	logon.req.logon.lmnt_token    = 0xFFFF;
	logon.req.logon.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));

	torture_assert(tctx, dest != NULL, "Error getting address");
	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");


	while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
	map_netlogon_samlogon_response(&response->data.samlogon);

	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");

	/* setup (another) temporary mailslot listener for replies */
	dgmslot = dgram_mailslot_temp(dgmsock, NBT_MAILSLOT_GETDC,
				      netlogon_handler, NULL);
	
	torture_assert(tctx, dgmslot != NULL, "Error getting a Mailslot for GetDC reply");

	ZERO_STRUCT(logon);
	logon.command = LOGON_SAM_LOGON_REQUEST;
	logon.req.logon.request_count = 0;
	logon.req.logon.computer_name = TEST_NAME;
	logon.req.logon.user_name     = TEST_NAME"$";
	logon.req.logon.mailslot_name = dgmslot->mailslot_name;
	logon.req.logon.sid           = *dom_sid;
	logon.req.logon.acct_control  = ACB_WSTRUST;
	logon.req.logon.nt_version    = 1;
	logon.req.logon.lmnt_token    = 0xFFFF;
	logon.req.logon.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));

	torture_assert(tctx, dest != NULL, "Error getting address");
	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");


	while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
	map_netlogon_samlogon_response(&response->data.samlogon);

	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_RESPONSE, "Got incorrect netlogon response command");

	dgmslot->private_data = NULL;

	ZERO_STRUCT(logon);
	logon.command = LOGON_SAM_LOGON_REQUEST;
	logon.req.logon.request_count = 0;
	logon.req.logon.computer_name = TEST_NAME;
	logon.req.logon.user_name     = TEST_NAME"$";
	logon.req.logon.mailslot_name = dgmslot->mailslot_name;
	logon.req.logon.sid           = *dom_sid;
	logon.req.logon.acct_control  = ACB_NORMAL;
	logon.req.logon.nt_version    = 1;
	logon.req.logon.lmnt_token    = 0xFFFF;
	logon.req.logon.lm20_token    = 0xFFFF;

	make_nbt_name_client(&myname, TEST_NAME);

	dest = socket_address_from_strings(dgmsock, dgmsock->sock->backend_name, 
					   address, lp_dgram_port(tctx->lp_ctx));

	torture_assert(tctx, dest != NULL, "Error getting address");
	status = dgram_mailslot_netlogon_send(dgmsock, &name, dest,
					      NBT_MAILSLOT_NETLOGON, 
					      &myname, &logon);
	torture_assert_ntstatus_ok(tctx, status, "Failed to send netlogon request");


	while (timeval_elapsed(&tv) < 5 && dgmslot->private_data == NULL) {
		event_loop_once(dgmsock->event_ctx);
	}

	response = talloc_get_type(dgmslot->private_data, struct nbt_netlogon_response);

	torture_assert(tctx, response != NULL, "Failed to receive a netlogon reply packet");

	torture_assert_int_equal(tctx, response->response_type, NETLOGON_SAMLOGON, "Got incorrect type of netlogon response");
	map_netlogon_samlogon_response(&response->data.samlogon);

	torture_assert_int_equal(tctx, response->data.samlogon.data.nt5_ex.command, LOGON_SAM_LOGON_USER_UNKNOWN, "Got incorrect netlogon response command");

	torture_leave_domain(tctx, join_ctx);
	return true;
}
Beispiel #30
0
int
main(int argc, char *argv[])
{
    struct addrinfo hints;
    int ch, ls[FD_SETSIZE], nls, as, cs, optval;
    const char *listenaddr, *listenport, *hostname, *port;
    const char *relayname;
    void (*relayfunc)(int, int);

    relayname = strrchr(argv[0], '/');
    relayname = relayname ? relayname + 1 : argv[0];
    if (strcmp(relayname, "copy") == 0)
        relayfunc = relay_copy;
    else if (strcmp(relayname, "splice") == 0)
        relayfunc = relay_splice;
    else {
        argc--;
        argv++;
        if (argv[0] == NULL)
            usage();
        relayname = argv[0];
        if (strcmp(relayname, "copy") == 0)
            relayfunc = relay_copy;
        else if (strcmp(relayname, "splice") == 0)
            relayfunc = relay_splice;
        else
            usage();
    }

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = PF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    listenaddr = NULL;
    while ((ch = getopt(argc, argv, "46b:tu")) != -1) {
        switch (ch) {
        case '4':
            hints.ai_family = PF_INET;
            break;
        case '6':
            hints.ai_family = PF_INET6;
            break;
        case 'b':
            listenaddr = optarg;
            break;
        case 't':
            hints.ai_socktype = SOCK_STREAM;
            break;
        case 'u':
            hints.ai_socktype = SOCK_DGRAM;
            break;
        default:
            usage();
        }
    }
    argc -= optind;
    argv += optind;
    if (argc != 3)
        usage();
    listenport = argv[0];
    hostname = argv[1];
    port = argv[2];

    nls = socket_listen(ls, &hints, listenaddr, listenport);

    while (1) {
        if (hints.ai_socktype == SOCK_STREAM) {
            as = socket_accept(listen_select(ls, nls));
            cs = socket_connect(&hints, hostname, port);
            optval = 1;
            if (setsockopt(cs, IPPROTO_TCP, TCP_NODELAY,
                           &optval, sizeof(optval)) == -1)
                err(1, "setsockopt nodelay");
        } else {
            cs = socket_connect(&hints, hostname, port);
            as = listen_select(ls, nls);
        }

        relayfunc(as, cs);

        if (close(cs) == -1)
            err(1, "connect close");
        if (hints.ai_socktype == SOCK_STREAM) {
            if (close(as) == -1)
                err(1, "accept close");
        }
        printf("close\n");
    }
}