int main(void)
{
	int sfd, cfd, bytes, ret;
	char buf[16];

	buf[15] = 0;

	ret = sfd = create_unix_server_socket("/tmp/echosock",STREAM,0);

	if ( ret < 0 )
	{
		perror(0);
		exit(1);
	}

	for ( ;; )
	{
		ret = cfd = accept_unix_stream_socket(sfd,0);

		if ( ret < 0 )
		{
			perror(0);
			exit(1);
		}

		while ( 0 < ( bytes = read(cfd,buf,15) ) )
		{
			write(cfd,buf,bytes);
			write(1,buf,bytes);
		}

		ret = destroy_unix_socket(cfd);

		if ( ret < 0 )
		{
			perror(0);
			exit(1);
		}

	}

	ret = destroy_unix_socket(sfd);

	if ( ret < 0 )
	{
		perror(0);
		exit(1);
	}

	return 0;
}
Exemple #2
0
/* main auth program. Listen on unix socket and send back response */
int authproxy (void) {
    char buf[BUFSIZE];
    int fd,cl;
    PacketType  pkt_type = UNKNOWN;
    uint32_t len, readlen;
    AuthFun authfun;

    fd = create_unix_server_socket(socket_path,LIBSOCKET_STREAM, 0);
    if (fd == -1) {
        perror("socket error");
        exit(-1);
    }

    while (1) {
        cl = accept_unix_stream_socket(fd, 0);
        if(cl == -1) {
            perror("accept error");
            break;
        }

        readn(cl, &pkt_type, 1);
        readn(cl, &len, 4);
        //len = ntohl(len);
        //fprintf(stdout, "%d\n",len);
        readlen = (uint32_t)readn(cl, buf, (size_t)len);
        if(readlen < len) {
            // incomplete packet
            fprintf(stderr, "incomplete packet, skip");
            close(cl);
            continue;
        }

        authfun = GetAuthFunbyType(pkt_type);
        if(authfun)
            authfun(buf, cl);  // make fd close_on_exec?
        else {
            // fail
            fprintf(stderr, "unsupported auth tyep");
        }
        close(cl);

    }
    close(fd);
    return 0;
}