예제 #1
0
파일: clnt_dg.c 프로젝트: bahamas10/openzfs
static void
clnt_dg_destroy(CLIENT *cl)
{
/* LINTED pointer alignment */
	struct cu_data *cu = (struct cu_data *)cl->cl_private;
	int cu_fd = cu->cu_fd;

	(void) rpc_fd_lock(dgtbl, cu_fd);
	if (cu->cu_closeit)
		(void) t_close(cu_fd);
	XDR_DESTROY(&(cu->cu_outxdrs));
	cu->cu_tr_data->udata.buf = NULL;
	(void) t_free((char *)cu->cu_tr_data, T_UNITDATA);
	free(cu->cu_raddr.buf);
	free(cu);
	if (cl->cl_netid && cl->cl_netid[0])
		free(cl->cl_netid);
	if (cl->cl_tp && cl->cl_tp[0])
		free(cl->cl_tp);
	free(cl);
	rpc_fd_unlock(dgtbl, cu_fd);
}
예제 #2
0
void
t_respond (int j)
{
    if (!t[j].active)
        return;

    if (debug_level)
        log_querydone (t[j].active, response, response_len);

    response_id (t[j].id);
    t[j].len = response_len + 2;
    t_free (j);
    t[j].buf = alloc (response_len + 2);
    if (!t[j].buf)
    {
        t_close (j);
        return;
    }
    uint16_pack_big (t[j].buf, response_len);
    byte_copy (t[j].buf + 2, response_len, response);
    t[j].pos = 0;
    t[j].state = -1;
}
예제 #3
0
void
destroy_tlx_info(tlx_info_t *tlx)
{
	tlx_conn_ind_t  *ci;
	void		*cookie = NULL;

	if (tlx == NULL)
		return;

	free(tlx->dev_name);

	if (tlx->conn_ind_queue != NULL) {
		/* free up conn ind queue */
		while ((ci = uu_list_teardown(tlx->conn_ind_queue, &cookie)) !=
		    NULL) {
			(void) t_free((char *)ci->call, T_CALL);
			free(ci);
		}
		uu_list_destroy(tlx->conn_ind_queue);
	}

	free(tlx->local_addr.buf);
	free(tlx);
}
예제 #4
0
파일: file.c 프로젝트: lazygunner/transfer
int handle_re_transimit_frame(file_desc *f_desc, unsigned char *data_buf)
{
    unsigned short        re_tran_count = 0, parse_count = 0;
    unsigned int        offset = 0;
    unsigned char       *data = NULL;
    unsigned short      last_block = 0, block_index_no, frame_index_no;
    file_block_desc     *b_desc;
    frame_index         *f_index;
    frame_index         *f_index_next;
    
    data = data_buf;
    re_tran_count = HTONS(*((unsigned short *)data));
    
    if(re_tran_count <= 0 || re_tran_count > MAX_FRAME_COUNT)
    {
        printf("re transmit frame count error!\n");
        return -1;
    }

    data += sizeof(unsigned short);
    /* generate the re transmit block desc */
    while(parse_count < re_tran_count)
    {
        b_desc = (file_block_desc *)t_malloc(sizeof(file_block_desc));
        b_desc->retry_flag = RETRAN_FRAME;
        b_desc->next = NULL;
        f_index = (frame_index *)t_malloc(sizeof(frame_index));
        b_desc->index = f_index;
        block_index_no = HTONS(*((unsigned short *)data));
        frame_index_no = HTONS(*((unsigned short *)(data + 2)));
        
        //printf("b:%d, f:%d\n", block_index_no, frame_index_no);
        if(block_index_no > f_desc->block_count || block_index_no <= 0 ||\
            (frame_index_no > MAX_FRAME_COUNT && frame_index_no != 0xFFFF)\
            || frame_index_no <= 0)
        {
            t_free(b_desc);
            printf("re tran index error!b:%d, f:%d\n",\
                     block_index_no, frame_index_no);
            data += 4;
            parse_count++;
            continue;
        }

        f_index->block_index = block_index_no;
        f_index->frame_index = frame_index_no;
        f_index->next = NULL;

        f_desc->frame_remain[block_index_no - 1]++;
        data += 4;
        parse_count++;

        if(frame_index_no == 0xFFFF)
        {
            f_desc->frame_remain[block_index_no - 1] = MAX_FRAME_COUNT;
            goto add_to_list;
        }
        
        while(parse_count < re_tran_count)
        {
            if(HTONS(*((unsigned short *)data)) == block_index_no)
            {
                frame_index_no = HTONS(*((unsigned short *)(data + 2)));
                //printf("b:%d, f:%d\n", block_index_no, frame_index_no);
                if(block_index_no > f_desc->block_count ||\
                    block_index_no <= 0 ||\
                    frame_index_no > MAX_FRAME_COUNT ||\
                    frame_index_no <= 0)
                {
                    printf("re tran index error!b:%d, f:%d\n",\
                            block_index_no, frame_index_no);
                    goto next_index;
                }

                f_index_next = (frame_index *)t_malloc(sizeof(frame_index));
                f_index_next->block_index = block_index_no;
                f_index_next->frame_index = frame_index_no;
                f_index_next->next = NULL;
                f_index->next = f_index_next;
                f_index = f_index_next;
                
                f_desc->frame_remain[block_index_no - 1]++;
next_index:                
                data += 4;
                parse_count++;
                continue;
            }
            else
                break;

        }
add_to_list:
    add_block_to_list_head(f_desc, b_desc);
    }

}
예제 #5
0
/*
 * Create the autofs service for amd
 */
int
create_autofs_service(void)
{
  struct t_bind *tbp = 0;
  int fd = -1, err = 1;		/* assume failed */

  plog(XLOG_INFO, "creating autofs service listener");
  autofs_ncp = getnetconfigent(autofs_conftype);
  if (autofs_ncp == NULL) {
    plog(XLOG_ERROR, "create_autofs_service: cannot getnetconfigent for %s", autofs_conftype);
    goto out;
  }

  fd = t_open(autofs_ncp->nc_device, O_RDWR, NULL);
  if (fd < 0) {
    plog(XLOG_ERROR, "create_autofs_service: t_open failed (%s)",
	 t_errlist[t_errno]);
    goto out;
  }

  tbp = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
  if (!tbp) {
    plog(XLOG_ERROR, "create_autofs_service: t_alloca failed");
    goto out;
  }

  if (get_autofs_address(autofs_ncp, tbp) != 0) {
    plog(XLOG_ERROR, "create_autofs_service: get_autofs_address failed");
    goto out;
  }

  autofs_xprt = svc_tli_create(fd, autofs_ncp, tbp, 0, 0);
  if (autofs_xprt == NULL) {
    plog(XLOG_ERROR, "cannot create autofs tli service for amd");
    goto out;
  }

  rpcb_unset(AUTOFS_PROG, AUTOFS_VERS, autofs_ncp);
  if (svc_reg(autofs_xprt, AUTOFS_PROG, AUTOFS_VERS, autofs_program_1, autofs_ncp) == FALSE) {
    plog(XLOG_ERROR, "could not register amd AUTOFS service");
    goto out;
  }
  err = 0;
  goto really_out;

out:
  if (autofs_ncp)
    freenetconfigent(autofs_ncp);
  if (autofs_xprt)
    SVC_DESTROY(autofs_xprt);
  else {
    if (fd > 0)
      t_close(fd);
  }

really_out:
  if (tbp)
    t_free((char *) tbp, T_BIND);

  dlog("create_autofs_service: returning %d\n", err);
  return err;
}
예제 #6
0
/*
 * Called to read and interpret the event on a connectionless descriptor.
 * Returns 0 if successful, or a UNIX error code if failure.
 */
static int
do_poll_clts_action(int fd, int conn_index)
{
	int error;
	int ret;
	int flags;
	struct netconfig *nconf = &conn_polled[conn_index].nc;
	static struct t_unitdata *unitdata = NULL;
	static struct t_uderr *uderr = NULL;
	static int oldfd = -1;
	struct nd_hostservlist *host = NULL;
	struct strbuf ctl[1], data[1];
	/*
	 * We just need to have some space to consume the
	 * message in the event we can't use the TLI interface to do the
	 * job.
	 *
	 * We flush the message using getmsg(). For the control part
	 * we allocate enough for any TPI header plus 32 bytes for address
	 * and options. For the data part, there is nothing magic about
	 * the size of the array, but 256 bytes is probably better than
	 * 1 byte, and we don't expect any data portion anyway.
	 *
	 * If the array sizes are too small, we handle this because getmsg()
	 * (called to consume the message) will return MOREDATA|MORECTL.
	 * Thus we just call getmsg() until it's read the message.
	 */
	char ctlbuf[sizeof (union T_primitives) + 32];
	char databuf[256];

	/*
	 * If this is the same descriptor as the last time
	 * do_poll_clts_action was called, we can save some
	 * de-allocation and allocation.
	 */
	if (oldfd != fd) {
		oldfd = fd;

		if (unitdata) {
			(void) t_free((char *)unitdata, T_UNITDATA);
			unitdata = NULL;
		}
		if (uderr) {
			(void) t_free((char *)uderr, T_UDERROR);
			uderr = NULL;
		}
	}

	/*
	 * Allocate a unitdata structure for receiving the event.
	 */
	if (unitdata == NULL) {
		/* LINTED pointer alignment */
		unitdata = (struct t_unitdata *)t_alloc(fd, T_UNITDATA, T_ALL);
		if (unitdata == NULL) {
			if (t_errno == TSYSERR) {
				/*
				 * Save the error code across
				 * syslog(), just in case
				 * syslog() gets its own error
				 * and therefore overwrites errno.
				 */
				error = errno;
				(void) syslog(LOG_ERR,
	"t_alloc(file descriptor %d/transport %s, T_UNITDATA) failed: %m",
				    fd, nconf->nc_proto);
				return (error);
			}
			(void) syslog(LOG_ERR,
"t_alloc(file descriptor %d/transport %s, T_UNITDATA) failed TLI error %d",
			    fd, nconf->nc_proto, t_errno);
			goto flush_it;
		}
	}

try_again:
	flags = 0;

	/*
	 * The idea is we wait for T_UNITDATA_IND's. Of course,
	 * we don't get any, because rpcmod filters them out.
	 * However, we need to call t_rcvudata() to let TLI
	 * tell us we have a T_UDERROR_IND.
	 *
	 * algorithm is:
	 * 	t_rcvudata(), expecting TLOOK.
	 * 	t_look(), expecting T_UDERR.
	 * 	t_rcvuderr(), expecting success (0).
	 * 	expand destination address into ASCII,
	 *	and dump it.
	 */

	ret = t_rcvudata(fd, unitdata, &flags);
	if (ret == 0 || t_errno == TBUFOVFLW) {
		(void) syslog(LOG_WARNING,
"t_rcvudata(file descriptor %d/transport %s) got unexpected data, %d bytes",
		    fd, nconf->nc_proto, unitdata->udata.len);

		/*
		 * Even though we don't expect any data, in case we do,
		 * keep reading until there is no more.
		 */
		if (flags & T_MORE)
			goto try_again;

		return (0);
	}

	switch (t_errno) {
	case TNODATA:
		return (0);
	case TSYSERR:
		/*
		 * System errors are returned to caller.
		 * Save the error code across
		 * syslog(), just in case
		 * syslog() gets its own error
		 * and therefore overwrites errno.
		 */
		error = errno;
		(void) syslog(LOG_ERR,
		    "t_rcvudata(file descriptor %d/transport %s) %m",
		    fd, nconf->nc_proto);
		return (error);
	case TLOOK:
		break;
	default:
		(void) syslog(LOG_ERR,
		"t_rcvudata(file descriptor %d/transport %s) TLI error %d",
		    fd, nconf->nc_proto, t_errno);
		goto flush_it;
	}

	ret = t_look(fd);
	switch (ret) {
	case 0:
		return (0);
	case -1:
		/*
		 * System errors are returned to caller.
		 */
		if (t_errno == TSYSERR) {
			/*
			 * Save the error code across
			 * syslog(), just in case
			 * syslog() gets its own error
			 * and therefore overwrites errno.
			 */
			error = errno;
			(void) syslog(LOG_ERR,
			    "t_look(file descriptor %d/transport %s) %m",
			    fd, nconf->nc_proto);
			return (error);
		}
		(void) syslog(LOG_ERR,
		    "t_look(file descriptor %d/transport %s) TLI error %d",
		    fd, nconf->nc_proto, t_errno);
		goto flush_it;
	case T_UDERR:
		break;
	default:
		(void) syslog(LOG_WARNING,
	"t_look(file descriptor %d/transport %s) returned %d not T_UDERR (%d)",
		    fd, nconf->nc_proto, ret, T_UDERR);
	}

	if (uderr == NULL) {
		/* LINTED pointer alignment */
		uderr = (struct t_uderr *)t_alloc(fd, T_UDERROR, T_ALL);
		if (uderr == NULL) {
			if (t_errno == TSYSERR) {
				/*
				 * Save the error code across
				 * syslog(), just in case
				 * syslog() gets its own error
				 * and therefore overwrites errno.
				 */
				error = errno;
				(void) syslog(LOG_ERR,
	"t_alloc(file descriptor %d/transport %s, T_UDERROR) failed: %m",
				    fd, nconf->nc_proto);
				return (error);
			}
			(void) syslog(LOG_ERR,
"t_alloc(file descriptor %d/transport %s, T_UDERROR) failed TLI error: %d",
			    fd, nconf->nc_proto, t_errno);
			goto flush_it;
		}
	}

	ret = t_rcvuderr(fd, uderr);
	if (ret == 0) {

		/*
		 * Save the datagram error in errno, so that the
		 * %m argument to syslog picks up the error string.
		 */
		errno = uderr->error;

		/*
		 * Log the datagram error, then log the host that
		 * probably triggerred. Cannot log both in the
		 * same transaction because of packet size limitations
		 * in /dev/log.
		 */
		(void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING,
"NFS response over <file descriptor %d/transport %s> generated error: %m",
		    fd, nconf->nc_proto);

		/*
		 * Try to map the client's address back to a
		 * name.
		 */
		ret = netdir_getbyaddr(nconf, &host, &uderr->addr);
		if (ret != -1 && host && host->h_cnt > 0 &&
		    host->h_hostservs) {
		(void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING,
"Bad NFS response was sent to client with host name: %s; service port: %s",
		    host->h_hostservs->h_host,
		    host->h_hostservs->h_serv);
		} else {
			int i, j;
			char *buf;
			char *hex = "0123456789abcdef";

			/*
			 * Mapping failed, print the whole thing
			 * in ASCII hex.
			 */
			buf = (char *)malloc(uderr->addr.len * 2 + 1);
			for (i = 0, j = 0; i < uderr->addr.len; i++, j += 2) {
				buf[j] = hex[((uderr->addr.buf[i]) >> 4) & 0xf];
				buf[j+1] = hex[uderr->addr.buf[i] & 0xf];
			}
			buf[j] = '\0';
		(void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING,
	"Bad NFS response was sent to client with transport address: 0x%s",
		    buf);
			free((void *)buf);
		}

		if (ret == 0 && host != NULL)
			netdir_free((void *)host, ND_HOSTSERVLIST);
		return (0);
	}
예제 #7
0
void
freeDeref(Deref * d)
{
  t_free(memory, d);
}
예제 #8
0
파일: rpc_soc.c 프로젝트: BjoKaSH/mac-zfs
/*
 * Bind a fd to a privileged IP port.
 * This is slightly different from the code in netdir_options
 * because it has a different interface - main thing is that it
 * needs to know its own address.  We also wanted to set the qlen.
 * t_getname() can be used for those purposes and perhaps job can be done.
 */
int
__rpc_bindresvport_ipv6(int fd, struct sockaddr *sin, int *portp, int qlen,
			char *fmly)
{
	int res;
	static in_port_t port, *sinport;
	struct sockaddr_in6 myaddr;
	int i;
	struct t_bind tbindstr, *tres;
	struct t_info tinfo;
	extern mutex_t portnum_lock;

	/* VARIABLES PROTECTED BY portnum_lock: port */

#define	STARTPORT 600
#define	ENDPORT (IPPORT_RESERVED - 1)
#define	NPORTS	(ENDPORT - STARTPORT + 1)

	if (sin == 0 && fmly == 0) {
		errno = EINVAL;
		return (-1);
	}
	if (geteuid()) {
		errno = EACCES;
		return (-1);
	}
	if ((i = t_getstate(fd)) != T_UNBND) {
		if (t_errno == TBADF)
			errno = EBADF;
		if (i != -1)
			errno = EISCONN;
		return (-1);
	}
	if (sin == 0) {
		sin = (struct sockaddr *)&myaddr;
		get_myaddress_ipv6(fmly, sin);
	}
	if (sin->sa_family == AF_INET) {
		/* LINTED pointer cast */
		sinport = &((struct sockaddr_in *)sin)->sin_port;
	} else if (sin->sa_family == AF_INET6) {
		/* LINTED pointer cast */
		sinport = &((struct sockaddr_in6 *)sin)->sin6_port;
	} else {
		errno = EPFNOSUPPORT;
		return (-1);
	}

	/* Transform sockaddr to netbuf */
	if (t_getinfo(fd, &tinfo) == -1) {
		return (-1);
	}
	/* LINTED pointer cast */
	tres = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR);
	if (tres == NULL)
		return (-1);

	tbindstr.qlen = qlen;
	tbindstr.addr.buf = (char *)sin;
	tbindstr.addr.len = tbindstr.addr.maxlen = __rpc_get_a_size(tinfo.addr);
	/* LINTED pointer cast */
	sin = (struct sockaddr *)tbindstr.addr.buf;

	res = -1;
	(void) mutex_lock(&portnum_lock);
	if (port == 0)
		port = (getpid() % NPORTS) + STARTPORT;
	for (i = 0; i < NPORTS; i++) {
		*sinport = htons(port++);
		if (port > ENDPORT)
			port = STARTPORT;
		res = t_bind(fd, &tbindstr, tres);
		if (res == 0) {
			if ((tbindstr.addr.len == tres->addr.len) &&
				(memcmp(tbindstr.addr.buf, tres->addr.buf,
					(int)tres->addr.len) == 0))
				break;
			(void) t_unbind(fd);
			res = -1;
		} else if (t_errno != TSYSERR || errno != EADDRINUSE)
			break;
	}
	(void) mutex_unlock(&portnum_lock);

	if ((portp != NULL) && (res == 0))
		*portp = *sinport;
	(void) t_free((char *)tres, T_BIND);
	return (res);
}
예제 #9
0
static int
get_tcp_socket(
	char *machine,		/* remote host */
	char *service,		/* nttp/smtp etc. */
	unsigned short port)	/* tcp port number */
{
	int s = -1;
	int save_errno = 0;
	struct sockaddr_in sock_in;
#	ifdef TLI /* Transport Level Interface */
	char device[20];
	char *env_device;
	extern int t_errno;
	extern struct hostent *gethostbyname();
	struct hostent *hp;
	struct t_call *callptr;

	/*
	 * Create a TCP transport endpoint.
	 */
	if ((env_device = getenv("DEV_TCP")) != NULL) /* SCO uses DEV_TCP, most other OS use /dev/tcp */
		STRCPY(device, env_device);
	else
		strcpy(device, "/dev/tcp");

	if ((s = t_open(device, O_RDWR, (struct t_info *) 0)) < 0){
		t_error(txt_error_topen);
		return -EPROTO;
	}
	if (t_bind(s, (struct t_bind *) 0, (struct t_bind *) 0) < 0) {
		t_error("t_bind");
		t_close(s);
		return -EPROTO;
	}
	memset((char *) &sock_in, '\0', sizeof(sock_in));
	sock_in.sin_family = AF_INET;
	sock_in.sin_port = htons(port);

	if (!isdigit((unsigned char)*machine) ||
#		ifdef HAVE_INET_ATON
	    !inet_aton(machine, &sock_in)
#		else
#			ifdef HAVE_INET_ADDR
	    (long) (sock_in.sin_addr.s_addr = inet_addr(machine)) == INADDR_NONE)
#			endif /* HAVE_INET_ADDR */
#		endif /* HAVE_INET_ATON */
	{
		if ((hp = gethostbyname(machine)) == NULL) {
			my_fprintf(stderr, _(txt_gethostbyname), "gethostbyname() ", machine);
			t_close(s);
			return -EHOSTUNREACH;
		}
		memcpy((char *) &sock_in.sin_addr, hp->h_addr, hp->h_length);
	}

	/*
	 * Allocate a t_call structure and initialize it.
	 * Let t_alloc() initialize the addr structure of the t_call structure.
	 */
	if ((callptr = (struct t_call *) t_alloc(s, T_CALL, T_ADDR)) == NULL){
		t_error("t_alloc");
		t_close(s);
		return -EPROTO;
	}

	callptr->addr.maxlen = sizeof(sock_in);
	callptr->addr.len = sizeof(sock_in);
	callptr->addr.buf = (char *) &sock_in;
	callptr->opt.len = 0;			/* no options */
	callptr->udata.len = 0;			/* no user data with connect */

	/*
	 * Connect to the server.
	 */
	if (t_connect(s, callptr, (struct t_call *) 0) < 0) {
		save_errno = t_errno;
		if (save_errno == TLOOK)
			fprintf(stderr, _(txt_error_server_unavailable));
		else
			t_error("t_connect");
		t_free((char *) callptr, T_CALL);
		t_close(s);
		return -save_errno;
	}

	/*
	 * Now replace the timod module with the tirdwr module so that
	 * standard read() and write() system calls can be used on the
	 * descriptor.
	 */

	t_free((char *) callptr, T_CALL);

	if (ioctl(s, I_POP, (char *) 0) < 0) {
		perror("I_POP(timod)");
		t_close(s);
		return -EPROTO;
	}

	if (ioctl(s, I_PUSH, "tirdwr") < 0) {
		perror("I_PUSH(tirdwr)");
		t_close(s);
		return -EPROTO;
	}

#	else
#		ifndef EXCELAN
	struct servent *sp;
	struct hostent *hp;
#			ifdef h_addr
	int x = 0;
	char **cp;
	static char *alist[2] = {0, 0};
#			endif /* h_addr */
	static struct hostent def;
	static struct in_addr defaddr;
	static char namebuf[256];

#			ifdef HAVE_GETSERVBYNAME
	if ((sp = (struct servent *) getservbyname(service, "tcp")) == NULL) {
		my_fprintf(stderr, _(txt_error_unknown_service), service);
		return -EHOSTUNREACH;
	}
#			else
	sp = my_malloc(sizeof(struct servent));
	sp->s_port = htons(IPPORT_NNTP);
#			endif /* HAVE_GETSERVBYNAME */

	/* If not a raw ip address, try nameserver */
	if (!isdigit((unsigned char) *machine) ||
#			ifdef HAVE_INET_ATON
	    !inet_aton(machine, &defaddr)
#			else
#				ifdef HAVE_INET_ADDR
	    (long) (defaddr.s_addr = (long) inet_addr(machine)) == -1
#				endif /* HAVE_INET_ADDR */
#			endif /* HAVE_INET_ATON */
	    )
	{
		hp = gethostbyname(machine);
	} else {
		/* Raw ip address, fake */
		STRCPY(namebuf, machine);
		def.h_name = (char *) namebuf;
#			ifdef h_addr
		def.h_addr_list = alist;
#			endif /* h_addr */
		def.h_addr = (char *) &defaddr;
		def.h_length = sizeof(struct in_addr);
		def.h_addrtype = AF_INET;
		def.h_aliases = 0;
		hp = &def;
	}

	if (hp == NULL) {
		my_fprintf(stderr, _(txt_gethostbyname), "\n", machine);
		return -EHOSTUNREACH;
	}

	memset((char *) &sock_in, '\0', sizeof(sock_in));
	sock_in.sin_family = hp->h_addrtype;
	sock_in.sin_port = htons(port);
/*	sock_in.sin_port = sp->s_port; */
#		else
	memset((char *) &sock_in, '\0', sizeof(sock_in));
	sock_in.sin_family = AF_INET;
#		endif /* !EXCELAN */

	/*
	 * The following is kinda gross. The name server under 4.3
	 * returns a list of addresses, each of which should be tried
	 * in turn if the previous one fails. However, 4.2 hostent
	 * structure doesn't have this list of addresses.
	 * Under 4.3, h_addr is a #define to h_addr_list[0].
	 * We use this to figure out whether to include the NS specific
	 * code...
	 */

#		ifdef h_addr
	/*
	 * Get a socket and initiate connection -- use multiple addresses
	 */
	for (cp = hp->h_addr_list; cp && *cp; cp++) {
#			if defined(__hpux) && defined(SVR4)
		unsigned long socksize, socksizelen;
#			endif /* __hpux && SVR4 */

		if ((s = socket(hp->h_addrtype, SOCK_STREAM, 0)) < 0) {
			perror("socket");
			return -errno;
		}

		memcpy((char *) &sock_in.sin_addr, *cp, hp->h_length);

#			ifdef HAVE_INET_NTOA
		if (x < 0)
			my_fprintf(stderr, _(txt_trying), (char *) inet_ntoa(sock_in.sin_addr));
#			endif /* HAVE_INET_NTOA */

#			if defined(__hpux) && defined(SVR4)	/* recommended by [email protected] */
#				define HPSOCKSIZE 0x8000
		getsockopt(s, SOL_SOCKET, SO_SNDBUF, /* (caddr_t) */ &socksize, /* (caddr_t) */ &socksizelen);
		if (socksize < HPSOCKSIZE) {
			socksize = HPSOCKSIZE;
			setsockopt(s, SOL_SOCKET, SO_SNDBUF, /* (caddr_t) */ &socksize, sizeof(socksize));
		}
		socksize = 0;
		socksizelen = sizeof(socksize);
		getsockopt(s, SOL_SOCKET, SO_RCVBUF, /* (caddr_t) */ &socksize, /* (caddr_t) */ &socksizelen);
		if (socksize < HPSOCKSIZE) {
			socksize = HPSOCKSIZE;
			setsockopt(s, SOL_SOCKET, SO_RCVBUF, /* (caddr_t) */ &socksize, sizeof(socksize));
		}
#			endif /* __hpux && SVR4 */

		if ((x = connect(s, (struct sockaddr *) &sock_in, sizeof(sock_in))) == 0)
			break;

		save_errno = errno;									/* Keep for later */
#			ifdef HAVE_INET_NTOA
		my_fprintf(stderr, _(txt_connection_to), (char *) inet_ntoa(sock_in.sin_addr));
		perror("");
#			endif /* HAVE_INET_NTOA */
		(void) s_close(s);
	}

	if (x < 0) {
		my_fprintf(stderr, _(txt_giving_up));
		return -save_errno;					/* Return the last errno we got */
	}
#		else

#			ifdef EXCELAN
	if ((s = socket(SOCK_STREAM, (struct sockproto *) NULL, &sock_in, SO_KEEPALIVE)) < 0) {
		perror("socket");
		return -errno;
	}

	/* set up addr for the connect */
	memset((char *) &sock_in, '\0', sizeof(sock_in));
	sock_in.sin_family = AF_INET;
	sock_in.sin_port = htons(IPPORT_NNTP);

	if ((sock_in.sin_addr.s_addr = rhost(&machine)) == -1) {
		my_fprintf(stderr, _(txt_gethostbyname), "\n", machine);
		return -1;
	}

	/* And connect */
	if (connect(s, (struct sockaddr *) &sock_in) < 0) {
		save_errno = errno;
		perror("connect");
		(void) s_close(s);
		return -save_errno;
	}

#			else
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		return -errno;
	}

	/* And then connect */

	memcpy((char *) &sock_in.sin_addr, hp->h_addr, hp->h_length);

	if (connect(s, (struct sockaddr *) &sock_in, sizeof(sock_in)) < 0) {
		save_errno = errno;
		perror("connect");
		(void) s_close(s);
		return -save_errno;
	}

#			endif /* !EXCELAN */
#		endif /* !h_addr */
#	endif /* !TLI */
	return s;
}
예제 #10
0
void initafwdlist ()
//*************************************************************************
//
// read afwd.bcm into memory
//
//*************************************************************************
{
  FILE *f;
  char s[LINELEN+1];
  char word[MAXPARSE];
  char dstb[HADRESSLEN+1];
  int boxindex = 0;
  int line = 0;
  int i;
  int destlen = 0;
  afwdpara_t *ff;
  char name[20];

  strcpy(name, "initafwdlist");
  if (! m.autofwdtime) return;
  if (afp) t_free(afp);
  afp = (afwdpara_t *) t_malloc(MAXFWD * sizeof(afwdpara_t), "*afp");
  if (aboxdest) t_free(aboxdest);
  aboxdest = (char *) t_malloc(MAXDESTLEN * sizeof(char), "*ads");
  if (! afp || ! aboxdest)
  {
    trace(fatal, name, "out of memory");
    return;
  }
  memset(afp, 0, MAXFWD * sizeof(afwdpara_t));
  ff = afp;
  if ((f = s_fopen(AFWDLISTNAME, "srt")) != NULL)
  {
    while (fgets(s, LINELEN, f))
    {
      line++;
      subst1(s, ';', 0);
      if (strlen(s) > 3)
      {
        i = (*s == ' ');
        strupr(s);
        parseline(s, word);
        if (i)
        {
          if (! boxindex)
            goto error; // erster Eintrag: Boxcall fehlt
          else
          {
            char *dest = afp[boxindex - 1].dest;
            i = 0;
            while (word[i])
            {
              strncpy(dstb, s + word[i++], HADRESSLEN);
              dstb[HADRESSLEN] = 0;
              //strip_hadr(dstb);
              if (*dstb && (destlen + strlen(dstb) + 1) < MAXDESTLEN)
              {
                strcat(dest, dstb);
                strcat(dest, " ");
                destlen += (strlen(dstb) + 1);
              }
              else
              {
                trace(serious, name, ms(m_fwd_toomanydest));
                if (*b->logincall) putf(ms(m_fwd_toomanydest));
                break;
              }
            }
          }
        }
        else
        {
          if (! boxindex)
            ff->dest = aboxdest;
          else
          {
            ff->dest = afp[boxindex - 1].dest
                       + strlen(afp[boxindex - 1].dest) + 1;
            destlen += 1;
          }
          *ff->dest = 0;
          *ff->call = 0;
          if (strlen(s + *word) <= 6) strcpy(ff->call, s + *word);
          else goto error;                      // Boxcall zu lang
          if (++boxindex == MAXFWD)
          {
            trace(serious, name, ms(m_fwd_toomanycalls));
            if (*b->logincall) putf(ms(m_fwd_toomanycalls));
            break;
          }
          ff = &afp[boxindex];
        }
      }
    }
    *ff->call = 0;
    s_fclose(f);
#ifdef DEBUG_AFWD
    trace(report, name, "%d partners", boxindex);
#endif
    if (*b->logincall) putf("%s: %d partners.\n", name, boxindex);
    afwdpartners = boxindex;
    if (! boxindex)
    {
      t_free(afp);
      t_free(aboxdest);
      afp = NULL;
      aboxdest = NULL;
    }
    return;
  }
  else
  {
    trace(serious, name, AFWDLISTNAME" not found");
    if (*b->logincall) putf("%s: "AFWDLISTNAME" not found.\n", name);
    return;
  }
error: //that should never happen
#ifdef __DOS16__
    sound(1000);
    wdelay(106);
    nosound();
#endif
    trace(serious, name, "line %d error", line);
    if (*b->logincall) putf("%s: line %d error.\n", name, line);
    s_fclose(f);
}
예제 #11
0
void afwdlist (char *)
//*************************************************************************
//
// afwd.bcm is generated here ..
//
//*************************************************************************
{
  char name[20];

  strcpy(name, "afwdlist");
  if (! m.hadrstore || ! m.autofwdtime) return; //afwd disabled
  // Alle boxen durchsuchen
//  trace(report, "afwdlist", "scanning HADR");
  unsigned int found = 0, i = 0;
  if (! (found = scan_hadr())) return;
  // Sort file by reversed short addr
  char linetmp[LINELEN+1];
  char *xx;
  afwd_data *adrs = (afwd_data*) t_malloc(sizeof(afwd_data) * found, "hadr");
  if (! adrs)
  {
    trace(serious, name, "out of memory");
    putf("not enough memory");
    return;
  }
#ifdef DEBUG_AFWD
  trace(report, name, "reading entries in memory");
#endif
  FILE *f = s_fopen(AFWDTMP, "srt");
  if (f)
  {
    while (fgets(linetmp, LINELEN, f))
    {
      //fwdcall, shortheader
      xx = strchr(linetmp, ' ');
      *xx++ = 0;
      strcpy(adrs[i].partner, linetmp);
      strcpy(adrs[i].shortaddr, reverse_addr(xx));
      i++;
    }
    s_fclose(f);
  }
  else
  {
    trace(serious, name, ms(m_filenotopen), AFWDTMP);
    putf(ms(m_filenotopen), AFWDTMP);
    t_free(adrs);
    return;
  }
#ifdef DEBUG_AFWD
  trace(report, name, "read %u", i);
  trace(report, name, "sorting ..");
#endif
  qsort(adrs, i, sizeof(afwd_data), data_cmp);
  found = process_afwd_data(adrs, found);
#ifdef DEBUG_AFWD
  trace(report, name, "writing "AFWDLISTNAME);
#endif
  f = s_fopen(AFWDLISTNAME, "lwt");
  if (! f)
  {
    trace(serious, name, ms(m_filenotopen), AFWDLISTNAME);
    putf(ms(m_filenotopen), AFWDLISTNAME);
    t_free(adrs);
    return;
  }
  fprintf(f, "; OpenBCM AutoFwd File !!! DO NOT EDIT !!!\n");
  fprintf(f, "; This file is generated automatically. (use %s for manual editing!)\n", FWDLISTNAME);
  write_afwd(f, adrs, found);
  s_fclose(f);
  t_free(adrs);
  initafwdlist();
}
예제 #12
0
static int scan_hadr ()
//*************************************************************************
//
// process HADR database, find entries newer than autofwdtime,
// write afwd1.tmp
//
//*************************************************************************
{
  hadr_t *ha;
  unsigned int count = 0, found = 0, i = 0;
  char boxcall[CALLEN+1];
  char *header;
  char shortheader[HADRESSLEN+1];
  char name[20];
  int num;

  strcpy(name, "afwdlist");
  FILE *f = s_fopen(HADRNAME, "lrb");
  if (! f)
  {
    trace(serious, name, ms(m_filenotopen), HADRNAME);
    putf(ms(m_filenotopen), HADRNAME);
    return 0;
  }
  FILE *g = s_fopen(AFWDTMP, "lwt");
  if (! g)
  {
    trace(serious, name, ms(m_filenotopen), AFWDTMP);
    putf(ms(m_filenotopen), AFWDTMP);
    s_fclose(f);
    return 0;
  }
  ha = (hadr_t*) t_malloc(sizeof (hadr_t), "hadr");
  setvbuf(f, NULL, _IOFBF, 4096); // buffer for increasing performance
  fseek(f, sizeof (hadr_t), SEEK_SET);
  while (fread(ha, sizeof(hadr_t), 1, f))
  {
    if (! *ha->adr) continue;
    if (! strcmp(ha->adr, m.boxadress)) continue;
    count++;
    if (! check_adr(ha->adr)) continue;
    if ((num = which_partner(NULL, ha)) == NOTFOUND) continue;
    // Forwardweg suchen
    //no hadr, only boxcall (corrupt R: lines etc.)
    //(should not pass through check_adr() anyway)
    if (! (header = strchr(ha->adr, '.'))) continue;
    i = header - ha->adr;
    strcpy(shortheader, ha->adr);
    strip_hadr(shortheader);
    if (i > CALLEN) i = CALLEN;
    strncpy(boxcall, ha->adr, i);
    boxcall[i] = 0;
    if (mbcallok(boxcall))
    {
      found++;
      // putf("%i) %s%s -> %s to %s\n", count, boxcall, header, shortheader, fwdcall);
      // Routerfile schreiben
      fprintf(g, "%s %s \n", ha->adjacent[num], shortheader);
    }
    waitfor(e_ticsfull);
    if (testabbruch()) break;
  }
  t_free(ha);
  s_fclose(f);
  s_fclose(g);
#ifdef DEBUG_AFWD
  trace(report, name, "%u searched,  %u found", count, found);
#endif
  putf("%u searched, %u found\n", count, found);
  return found;
}
예제 #13
0
int autohadr (char *call, int ausgabe)
//*************************************************************************
//
// Search for address in HADR database
//
//*************************************************************************
{
  lastfunc("autohadr");
  int pos;
  int i;
  bitfeld options = b->optplus;
  int which = -1;
  hadr_t *ha = (hadr_t*) t_malloc(sizeof(hadr_t), "hadr");

  if (m.hadrstore)
  {
    strupr(call);
    hadr_tryopen();
    pos = loadhadr(call, ha, 0);
    uclose();
    if (pos)
    {
      which = which_partner(call, ha);
      if (ausgabe && (options & o_a))
      {
        time_t mindelay = ad_time();
        time_t mtime = ad_time();
        putf("Forward data from HADR-entry:\n");
        putf("H-Addr     : %s\n", ha->adr);
        putf("R-Header   : %s\n", ha->lastheader);
        putf("Last BID   : %s\n", ha->lastbid);
        putf("Last Board : %s\n", ha->lastboard);
        putf("Last User  : %s\n", ha->lastuser);
        putf("Header Date: %s\n", datestr(ha->lasthtime, 12));
        if (*ha->sysopcall)
          putf("Sysop:       %s %s\n", ha->sysopcall,
                                       get_name(ha->sysopcall, 1));
        if (*ha->protocol && *ha->hwaddress)
          putf("HW-Address:  %s: %s\n", ha->protocol, ha->hwaddress);
        if (ha->bversion && *ha->bstatus && ha->lastwprcvd)
          putf("BVersion:    %ld %s (%s)\n", ha->bversion, ha->bstatus,
                                             datestr(ha->lastwprcvd, 12));
        putf("Bulletins  : %ld\n", ha->bulletins);
        putf("Usermails  : %ld\n", ha->usermails);
        putf("Neighbour Count Update +Delay Hops\n");
        for (i = 0; i < ADJNUM; i++)
        {
          if (ha->adjacent[i][0])
          {
            if (ha->delay[i] < mindelay) mindelay = ha->delay[i];
          }
        }
        for (i = 0; i < ADJNUM; i++)
        { // Ausgabe HADR-Eintrag
          if (ha->adjacent[i][0])
          {
            putf("%-9s%6d%7s%7s%5d", ha->adjacent[i], ha->rel_mails[i],
                  zeitspanne(mtime - ha->lastupdate[i], zs_seconds),
                  zeitspanne(ha->delay[i] - mindelay, zs_seconds),
                  ha->hops[i]);
            if (i == which) putf(" <");
            if (isforwardpartner(ha->adjacent[i]) == NOTFOUND)
              putf(" fwd terminated");
            putv(LF);
          }
        }
        putv(LF);
      }
    }
  }
  if (which != NOTFOUND) strcpy(b->destboxlist, ha->adjacent[which]);
  t_free(ha);
  if (which == NOTFOUND) return unbekannt;
  return auto_bekannt;
}
예제 #14
0
파일: file.c 프로젝트: lazygunner/transfer
void send_thread(void *args)
{
    transfer_session    *session;
    file_desc           *f_desc;
    file_block_desc     *b_desc;
    file_frame_data     *file_frame;
    frame_index         *f_index;
    frame_header        *f_header;
    block_sent_frame    bs;
    int                 qid;
    q_msg               f_msg;
    int                 err;
    int                 frame_len = 0, len = 0, blk_count = 0;
    unsigned short      b_index = 0;
    char                buf_send[2061];
#if DEBUG
    char test[10];
    FILE *fp;
    fp = fopen("receive.txt", "w");
#endif
    struct  timeval  start;
    struct  timeval  end;
    float   time_used;
    char    started = 0;
    int     send_count = 0;
    int     ret_val = 0;

    
    session = (transfer_session *)args;
    f_desc = session->f_desc;
    blk_count = f_desc->block_count;
    frame_block_sent_init(&bs);

    if((qid = session->data_qid) < 0)
    {
        printf("msg q error");
        return;
    }

    frame_header_init(FRAME_TYPE_DATA, FRAME_DATA_MONITOR, &f_header);
    for(;;)
    {
            if (session->state != STATE_TRANSFER)
            {
                /* free the packages in the queue */
                while(recv_msg_q(qid, &f_msg, sizeof(q_msg),\
                        MSG_TYPE_FILE_FRAME, IPC_NOWAIT) >= 0)
                {
                    file_frame = (file_frame_data *)(f_msg.msg_buf.data);
                    t_free(file_frame);
                }
                //t_free(f_header);
                //return;
                sleep(1);
                continue;
            }
            /* get frame from msg q */
            if ((err = recv_msg_q(qid, &f_msg, sizeof(q_msg),\
                    MSG_TYPE_FILE_FRAME, IPC_NOWAIT)) < 0)  
            {  
                /* no msg in the queue, assum sending finished */
                if(started)
                {
                    gettimeofday(&end,NULL);
                    time_used = (end.tv_sec - start.tv_sec) * 1000000\
                                + (end.tv_usec - start.tv_usec);
                    time_used /= 1000000;
                    printf("send_time_used = %f s\n", time_used);
                    printf("send_frame_count = %d \n", send_count);
                    send_count = 0;
                    started = 0;
                }
                sleep(1);
                continue;
            }
            if(0 == started)
            {
                gettimeofday(&start, NULL);
                started = 1;
            }
            
                        

            file_frame = (file_frame_data *)(f_msg.msg_buf.data);
            b_index = file_frame->block_index;
            /* encapusulate frame */
            file_frame->file_id = HTONS(file_frame->file_id);
            file_frame->block_index = HTONS(file_frame->block_index);
            file_frame->frame_index = HTONS(file_frame->frame_index);
            //printf("send data block:%d, frame:%d\n", HTONS(file_frame->block_index), HTONS(file_frame->frame_index));
            frame_len = frame_build(f_header, file_frame, sizeof(file_frame_data), buf_send);
            
            if(f_msg.msg_buf.data_len < FILE_FRAME_SIZE)
            {
                ((frame_header *)buf_send)->length = HTONS(f_msg.msg_buf.data_len + 11);
                frame_crc_gen((frame_header *)buf_send, (unsigned char *)buf_send + 6,\
                                f_msg.msg_buf.data_len + 6);
            }
            
            /* send file data frame */
            ret_val = send_file_data(session, buf_send, f_msg.msg_buf.data_len + 13);
            if(ret_val < 0)
            {
                printf("return value:%d.\n", ret_val);
                perror("send to!");
            }
            send_count++;
            
            if(!f_desc->frame_remain)
            {
                t_free(file_frame);
                continue;
            }
            f_desc->frame_remain[b_index - 1]--;
            if(f_desc->frame_remain[b_index - 1] == 0)
            {
                bs.file_id = HTONS(f_desc->file_id);
                bs.block_index = file_frame->block_index;
                frame_crc_gen(&(bs.f_header), (unsigned char*)(&bs.file_id), 4);
                if ((len = send(session->fd, (char *)(&bs), sizeof(block_sent_frame), 0))\
                            != sizeof(block_sent_frame))           
                {
                    printf("send socket finished failed.\n");
                    session->state = STATE_CONN_LOST;
                    t_free(file_frame);
                    continue;
                }
                printf("block:%d has send completed.\n", HTONS(bs.block_index));

                /* if all count have been sent, tansfer finished */
                //blk_count--;
                //if(blk_count == 0)
                //    session->state = STATE_TRANSFER_FIN;
            }
            
            /* free the data after send */
            t_free(file_frame);

    }
    t_free(f_header);

}
예제 #15
0
파일: file.c 프로젝트: lazygunner/transfer
void read_thread(void *args)
{
    transfer_session    *session;
    file_desc           *f_desc;
    file_block_desc     *b_desc;
    frame_index         *f_index, *last_index;
    FILE                *fp = NULL;
    int                 err;

    int i = 0;
    
    session = (transfer_session *)args;
    f_desc = session->f_desc;

   
    for(;;)
    {
        if(session->state != STATE_TRANSFER)
        {
            /* if state = finish, close file and exit the thread */
            if(fp)
            {
                fclose(fp);
                fp = NULL;
            }
            sleep(1);
            continue;
        }
        
        if(NULL == fp)
        {
            if(NULL == (fp = fopen(f_desc->file_name, "r")))
            {
                t_log("open file error");
                continue;
            }
        }

        b_desc = get_first_block(f_desc);
        if(b_desc == ERR_BLOCK_LIST_NULL)
            return;
        else if(b_desc == ERR_BLOCK_LIST_EMPTY)
        {
            sleep(1);
            continue;
        }
        else
        {
            if(b_desc->retry_flag == ORIGIN_FRAME)
            {
                f_index = b_desc->index;
                for(i = 1; i <= MAX_FRAME_COUNT; i++)
                {
                    f_index->frame_index = i;
                    if(read_file_to_msg_q(f_index, f_desc, fp, session->data_qid) < 0)
                        break;
                }
                t_free(f_index);
            }
            else if(b_desc->retry_flag == RETRAN_FRAME)
            {
                printf("read_thread re tran\n");
                f_index = b_desc->index;
                if(f_index->frame_index == 0XFFFF)
                {
                    for(i = 1; i <= MAX_FRAME_COUNT; i++)
                    {
                        f_index->frame_index = i;
                        read_file_to_msg_q(f_index, f_desc, fp, session->data_qid);
                    }
                    t_free(f_index);

                }
                else
                {
                    do
                    {
                        read_file_to_msg_q(f_index, f_desc, fp, session->data_qid);
                        last_index = f_index;
                        f_index = f_index->next;
                        t_free(last_index);
                    }while(f_index);

                }


            }

        }

        t_free(b_desc);
//        printf("block:%d\n", b_desc->index->block_index);
            
    }

    return;

}
예제 #16
0
파일: tlx.c 프로젝트: alhazred/onarm
/*
 * Handle a TLOOK notification received during a t_accept() call.
 * Returns -1 on failure, else 0.
 */
static int
process_tlook(const char *fmri, tlx_info_t *tlx_info)
{
	int	event;
	int	fd = tlx_info->pr_info.listen_fd;

	debug_msg("Entering process_tlook:");

	switch (event = t_look(fd)) {
	case T_LISTEN: {
		struct t_call *call;

		debug_msg("process_tlook: T_LISTEN event");
		if ((call = get_new_conind(fd)) == NULL)
			return (-1);
		if (queue_conind(tlx_info->conn_ind_queue, call) == -1) {
			error_msg(gettext("Failed to queue connection "
			    "indication for instance %s"), fmri);
			(void) t_free((char *)call, T_CALL);
			return (-1);
		}
		break;
	}
	case T_DISCONNECT: {
		/*
		 * Note: In Solaris 2.X (SunOS 5.X) bundled
		 * connection-oriented transport drivers
		 * [ e.g /dev/tcp and /dev/ticots and
		 * /dev/ticotsord (tl)] we do not send disconnect
		 * indications to listening endpoints.
		 * So this will not be seen with endpoints on Solaris
		 * bundled transport devices. However, Streams TPI
		 * allows for this (broken?) behavior and so we account
		 * for it here because of the possibility of unbundled
		 * transport drivers causing this.
		 */
		tlx_conn_ind_t	*cip;
		struct t_discon	*discon;

		debug_msg("process_tlook: T_DISCONNECT event");

		/* LINTED */
		if ((discon = (struct t_discon *)
		    t_alloc(fd, T_DIS, T_ALL)) == NULL) {
			error_msg("t_alloc: %s", t_strerror(t_errno));
			return (-1);
		}
		if (t_rcvdis(fd, discon) < 0) {
			error_msg("t_rcvdis: %s", t_strerror(t_errno));
			(void) t_free((char *)discon, T_DIS);
			return (-1);
		}

		/*
		 * Find any queued connection pending that matches this
		 * disconnect notice and remove from the pending queue.
		 */
		cip = uu_list_first(tlx_info->conn_ind_queue);
		while ((cip != NULL) &&
		    (cip->call->sequence != discon->sequence)) {
			cip = uu_list_next(tlx_info->conn_ind_queue, cip);
		}
		if (cip != NULL) {	/* match found */
			uu_list_remove(tlx_info->conn_ind_queue, cip);
			(void) t_free((char *)cip->call, T_CALL);
			free(cip);
		}

		(void) t_free((char *)discon, T_DIS);
		break;
	}
	case -1:
		error_msg("t_look: %s", t_errno);
		return (-1);
	default:
		error_msg(gettext("do_tlook: unexpected t_look event: %d"),
		    event);
		return (-1);
	}

	return (0);
}
예제 #17
0
파일: tlx.c 프로젝트: alhazred/onarm
/*
 * This call attempts to t_accept() an incoming/pending TLI connection.
 * If it is thwarted by a TLOOK, it is deferred and whatever is on the
 * file descriptor, removed after a t_look. (Incoming connect indications
 * get queued for later processing and disconnect indications remove a
 * a queued connection request if a match found).
 * Returns -1 on failure, else 0.
 */
int
tlx_accept(const char *fmri, tlx_info_t *tlx_info,
    struct sockaddr_storage *remote_addr)
{
	tlx_conn_ind_t	*conind;
	struct t_call	*call;
	int		fd;
	int		listen_fd = tlx_info->pr_info.listen_fd;

	debug_msg("Entering tlx_accept: instance: %s", fmri);

	if ((fd = t_open(tlx_info->dev_name, O_RDWR, NULL)) == -1) {
		error_msg("t_open: %s", t_strerror(t_errno));
		return (-1);
	}

	if (tlx_info->pr_info.v6only) {
		int	on = 1;

		/* restrict to IPv6 communications only */
		if (tlx_setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on,
		    sizeof (on)) == -1) {
			(void) t_close(fd);
			return (-1);
		}
	}

	if (t_bind(fd, NULL, NULL) == -1) {
		error_msg("t_bind: %s", t_strerror(t_errno));
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Get the next connection indication - first try the pending
	 * queue, then, if none there, get a new one from the file descriptor.
	 */
	if ((conind = uu_list_first(tlx_info->conn_ind_queue)) != NULL) {
		debug_msg("taking con off queue");
		call = conind->call;
	} else if ((call = get_new_conind(listen_fd)) == NULL) {
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Accept the connection indication on the newly created endpoint.
	 * If we fail, and it's the result of a tlook, queue the indication
	 * if it isn't already, and go and process the t_look.
	 */
	if (t_accept(listen_fd, fd, call) == -1) {
		if (t_errno == TLOOK) {
			if (uu_list_first(tlx_info->conn_ind_queue) == NULL) {
				/*
				 * We are first one to have to defer accepting
				 * and start the pending connections list.
				 */
				if (queue_conind(tlx_info->conn_ind_queue,
				    call) == -1) {
					error_msg(gettext(
					    "Failed to queue connection "
					    "indication for instance %s"),
					    fmri);
					(void) t_free((char *)call, T_CALL);
					return (-1);
				}
			}
			(void) process_tlook(fmri, tlx_info);
		} else {		  /* non-TLOOK accept failure */
			error_msg("%s: %s", "t_accept failed",
			    t_strerror(t_errno));
			/*
			 * If we were accepting a queued connection, dequeue
			 * it.
			 */
			if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
				(void) dequeue_conind(tlx_info->conn_ind_queue);
			(void) t_free((char *)call, T_CALL);
		}

		(void) t_close(fd);
		return (-1);
	}

	/* Copy remote address into address parameter */
	(void) memcpy(remote_addr, call->addr.buf,
	    MIN(call->addr.len, sizeof (*remote_addr)));

	/* If we were accepting a queued connection, dequeue it. */
	if (uu_list_first(tlx_info->conn_ind_queue) != NULL)
		(void) dequeue_conind(tlx_info->conn_ind_queue);
	(void) t_free((char *)call, T_CALL);

	return (fd);
}
예제 #18
0
void extract (unsigned msgnr)
//*************************************************************************
//
//  EXTRACT-Utility by DF3VI, extrahiert Binaerteile einer Mail auf
//  die Mailbox-Festplatte ins Box-temp-Verzeichnis
//
//*************************************************************************
{ //is this re-entrant? if not, pse set semaphor
  FILE *f, *g;
  char name[FNAMELEN+1], s[BUFLEN], st[FNAMELEN+1], *ptr;
  unsigned long posi, bytes;
  int err;
  unsigned int w, r;
  char *puffer;

  strlwr(b->mailpath);
  if (! (f = s_fopen(b->mailpath, "sr")))
  {
    putf(ms(m_extract_errorread), b->mailpath);
    return;
  }
  fgets(s, BUFLEN - 1, f);
  mbsend_parse(s, 0);
  do
  {
    waitfor(e_ticsfull);
    fgets(s, BUFLEN - 1, f);
  }
  while (! feof(f)
         && ! (strstr(s, " go_") == s) && ! (strstr(s, "#BIN#") == s));
  if (feof(f)) // keine Markierung gefunden
  {
    putf(ms(m_extract_nostartline), b->boardname, msgnr);
    s_fclose(f);
    return;
  }
  do
  {
    if (strstr(s, "#BIN#") == s)
    {
      s_fclose(f);
      subst1(s, LF, 0);
      if (! sscanf(s, "#BIN#%lu", &bytes)) // Laenge lesen
      {
        putf(ms(m_extract_invalidheader), b->boardname, msgnr);
        return;
      }
      unsigned long bintime;
      sscanf(strchr(s, '$'), "$%lX#", &bintime);
      ptr = s + strlen(s);
      while (*ptr != '#' && ptr > s) ptr--;    // Dateinamen suchen
      if (*(ptr - 1) != '#') ptr++;            // # im Dateinamen ?
      if (! *ptr || *ptr == '$' || *ptr == '|' )
      {
        putf(ms(m_extract_filenamemissing));
        err = 0;
        do                              // Ersatzname RUBRIK.lfn erzeugen
        {
          waitfor(e_ticsfull);
          sprintf(s, TEMPPATH "/%s.%03i", b->boardname, ++err);
          strlwr(s);
        }
        while (! access(s, 0) && err <= 999);
        if (err > 999)
        {
          putf(ms(m_extract_filecantcreate));
          return;
        }
        else
          putf(ms(m_extract_filecreate), s);
      }
      else
      {
        strcpy(name, ptr);
        ptr = name + strlen(name);
        while (ptr >= name && *ptr != ':' && *ptr != '\\' && *ptr != '/')
          ptr--; // LW/Pfad abtrennnen
//      putf("Dateiname: %s\n", ptr+1);
        sprintf(s, TEMPPATH "/%s", ptr + 1);
        strlwr(s);
      }
      if (! (g = s_fopen(s, "swb")))
      {                                  // Ziel-Datei oeffnen
        putf(ms(m_extract_errorwritebin), errno, s, strerror(errno));
//      perror("");
        return;
      }
      if (! (puffer = (char *) t_malloc(2048, "extr")))
      {                                  // 2 kb Puffer zum Daten-Kopieren
        putf(ms(m_nomem));
        s_fclose(g);
        return;
      }
      f = s_fopen(b->mailpath, "srb"); // BIN-Datei oeffnen
      fseek(f, b->binstart + BINCMDLEN, SEEK_SET); // Position auf Anfang
      posi = r = 0; // Byte-Zaehler gesamt/gelesen
      do
      {
        r = fread(puffer, 1, 2048, f);
        posi += r;
        fwrite(puffer, 1, r, g);
        waitfor(e_ticsfull);
      }
      while (r == 2048);
      t_free(puffer);
      s_fclose(g);
      s_fclose(f);
      setfiletime(s, bintime);
      putf(ms(m_extract_extractbin), s, posi, bytes, b->boardname, msgnr);
      return; // BIN ist immer Fileende
    }
    // Ansonsten DG1BBQ-FFR-Markierung
    if ((strstr(s, " go_text. ") == s) || (strstr(s, " go_info. ") == s))
    {
      sscanf(s + 10, "%s", st); // Text-Datei-Namen lesen
      sprintf(name, TEMPPATH "/%s", st);
      strlwr(name);
      if (! access(name, 0)) // Wenn vorhanden, Extension hochzaehlen
      {
        err = 1;
        switch (name + strlen(name) - strchr(name, '.'))
        {
          case 1:                              // "name."    -> "name.__"
          case 2: strcat(name, "__" ); break;  // "name.x"   -> "name.x__"
          case 3: strcat(name, "_");           // "name.xx"  -> "name.xx_"
          case 4: break;                       // "name.xxx" -> "name.xxx"
          default: strcat(name, ".__");        // "name"     -> "name.__"
        }
        *(name + strlen(name) - 2) = 0;
        do                            // ZWEI-stellige laufenden Nummer
        {
          waitfor(e_ticsfull);
          sprintf(s, "%s%02x", name, err++);
        }
        while (! access(s, 0) && err < 255);
        if (err >= 255)
        {
          putf(ms(m_extract_filecantcreate));
          *name = 0;
        }
        else
        {
          strcpy(st, s);
          strlwr(st);
        }
      }
      *s = 0; // Keine Startmarkierung schreiben
    }
    else
    if (strstr(s, " go_7+. ") == s) // 7PLUS-Datei-Kopf auswerten
    {                               // lfn, Bereich, File-Name
      if (sscanf(s + 8, "%u of %u %s", &w, &r, name) != 3)
      {
        putf(ms(m_extract_invalidstartline), b->boardname, msgnr);
        *name = 0;
      }
      else
      {
        subst1(name, '.', 0); // Extension abtrennen
        if (r == 1)
          sprintf(st, TEMPPATH "/%s.7pl", name); // Einteilig: Endung .7PL
        else
          sprintf(st, TEMPPATH "/%s.p%02x", name, w); // Mehrteilig: End. .PXX
      }
    }
    else
    {
      putf(ms(m_extract_invalidstartline), b->boardname, msgnr);
      *st = 0;
    }
    if (*st)
    {
      strlwr(st);
      if (! (g = s_fopen(st, "sw"))) // Ziel-Datei schreiben
      {
        putf(ms(m_extract_errorwrite), st);
        return;
      }
      do
      {
        fputs(s, g);
        *s = 0;
        waitfor(e_ticsfull);
        fgets(s, BUFLEN - 1, f);
      }                            // Bis File-Ende oder Stop-Markierung
      while (! feof(f) && ! (strstr(s, " stop_") == s));
      if (strstr(s, " stop_7+"))
        fputs(s, g); //nur bei 7+ Endemarkierung schreiben
      s_fclose(g);
      putf(ms(m_extract_extract7p), st, b->boardname, msgnr);
    }
    do
    {
      waitfor(e_ticsfull);
      *s = 0;
      fgets(s, BUFLEN - 1, f); // Naechste Start-Markierung suchen
    }
    while (! feof(f)
           && ! (strstr(s, " go_") == s) && ! (strstr(s, "#BIN#") == s));
  }
  while (! feof(f));                     // solange 7+ folgt
  s_fclose(f);                           // oder File-Ende
}
예제 #19
0
/*
 * The highest level interface for server creation.
 * Copied from svc_generic.c and cmd/keyserv/key_generic.c, but adapted
 * to work only for TPI_CLTS semantics, and to be called only once
 * from kwarnd.c. Returns 1 (interface created) on success and 0
 * (no interfaces created) on failure.
 */
int
svc_create_local_service(void (*dispatch) (),		/* Dispatch function */
			u_long prognum,			/* Program number */
			u_long versnum,			/* Version number */
			char *nettype,			/* Networktype token */
			char *servname)			/* name of the srvc */
{
	int num = 0;
	SVCXPRT *xprt;
	struct netconfig *nconf;
	struct t_bind *bind_addr;
	void *net;
	int fd;
	struct nd_hostserv ns;
	struct nd_addrlist *nas;

	if ((net = __rpc_setconf(nettype)) == 0) {
		(void) syslog(LOG_ERR,
		gettext("svc_create: could not read netconfig database"));
		return (0);
	}
	while (nconf = __rpc_getconf(net)) {
		if ((strcmp(nconf->nc_protofmly, NC_LOOPBACK)) ||
				(nconf->nc_semantics != NC_TPI_COTS_ORD))
			continue;

		if ((fd = t_open(nconf->nc_device, O_RDWR, NULL)) < 0) {
			(void) syslog(LOG_ERR,
			gettext("svc_create: %s: cannot open connection: %s"),
				nconf->nc_netid, t_errlist[t_errno]);
			break;
		}

		/*
		 * Negotiate for returning the uid of the caller.
		 * This should be done before enabling the endpoint for
		 * service via t_bind() (called in svc_tli_create())
		 * so that requests to kwarnd contain the uid.
		 */
		if (__rpc_negotiate_uid(fd) != 0) {
			syslog(LOG_ERR,
			gettext("Could not negotiate for"
				" uid with loopback transport %s"),
				nconf->nc_netid);
			t_close(fd);
			break;
		}

		/* LINTED pointer alignment */
		bind_addr = (struct t_bind *) t_alloc(fd, T_BIND, T_ADDR);
		if ((bind_addr == NULL)) {
			(void) t_close(fd);
			(void) syslog(LOG_ERR,
				gettext("svc_create: t_alloc failed\n"));
			break;
		}
		ns.h_host = HOST_SELF;
		ns.h_serv = servname;
		if (!netdir_getbyname(nconf, &ns, &nas)) {
			/* Copy the address */
			bind_addr->addr.len = nas->n_addrs->len;
			(void) memcpy(bind_addr->addr.buf, nas->n_addrs->buf,
				(int) nas->n_addrs->len);
			bind_addr->qlen = 8;
			netdir_free((char *) nas, ND_ADDRLIST);
		} else {
			(void) syslog(LOG_ERR,
			gettext("svc_create: no well known "
				"address for %s on %s\n"),
				servname, nconf->nc_netid);
			(void) t_free((char *) bind_addr, T_BIND);
			bind_addr = NULL;
		}

		xprt = svc_tli_create(fd, nconf, bind_addr, 0, 0);
		if (bind_addr)
			(void) t_free((char *) bind_addr, T_BIND);
		if (xprt == NULL) {
			(void) t_close(fd);
			(void) syslog(LOG_ERR,
			    gettext("svc_create: svc_tli_create failed\n"));
			break;
		} else {
			(void) rpcb_unset(prognum, versnum, nconf);
			if (svc_reg(xprt, prognum, versnum, dispatch, nconf)
					== FALSE) {
				(void) syslog(LOG_ERR,
				gettext("svc_create: cannot"
					" register %d vers %d on %s"),
					prognum, versnum, nconf->nc_netid);
				SVC_DESTROY(xprt);	/* also t_closes fd */
				break;
			}
			num = 1;
			break;
		}
	}
	__rpc_endconf(net);
	return (num);
}
예제 #20
0
int gtcm_bgn_net(omi_conn_ll *cll)
{
	extern int4	omi_nxact, omi_nerrs, omi_brecv, omi_bsent;
	omi_fd		fd;
	int		i;
	int 		save_errno;
	int		rc;
#ifdef NET_TCP
	struct servent		*se;
	unsigned short		port;
	char			port_buffer[NI_MAXSERV];
#endif /* defined(NET_TCP) */
#ifdef BSD_TCP
	struct addrinfo		*ai_ptr, hints;
	const  boolean_t	reuseaddr = TRUE;
	int			errcode;
#else /* defined(BSD_TCP) */
#ifdef SYSV_TCP
	struct t_bind		*bind;
#endif /* defined(SYSV_TCP) */
#endif /* !defined(BSD_TCP) */

	/*  The linked list of connections */
	cll->head = cll->tail = (omi_conn *)0;
	/*  The statistics */
	cll->stats.conn = cll->stats.clos = cll->stats.disc = 0;
	cll->st_cn.bytes_recv = 0;
	cll->st_cn.bytes_send = 0;
	cll->st_cn.start      = 0;
	for (i = 0; i < OMI_OP_MAX; i++)
		cll->st_cn.xact[i] = 0;
	for (i = 0; i < OMI_ER_MAX; i++)
		cll->st_cn.errs[i] = 0;
	omi_nxact = omi_nerrs = omi_brecv = omi_bsent = 0;
	/*  Fall back on a compile time constant */
	if (!omi_service)
		omi_service = SRVC_NAME;
#ifdef NET_TCP
	/* NET_TCP is defined only when BSD_TCP is defined or SYSV_TCP is defined, but SYSV_TCP is never defined (a bug?)
	 * so we move the code of obtaining port information from service down to #ifdef BSD_TCP
	 */
#ifdef SYSV_TCP
	GTMASSERT;
#endif
#endif /* defined(NET_TCP) */
#ifdef BSD_TCP
	/*  Create a socket always tries IPv6 first */
	SERVER_HINTS(hints, ((GTM_IPV6_SUPPORTED && !ipv4_only) ? AF_INET6 : AF_INET));
	if ((fd = socket(hints.ai_family, SOCK_STREAM, 0)) < 0)
	{
		if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		{
			save_errno = errno;
			return save_errno;
		}
		hints.ai_family = AF_INET;
	}
	/*  Bind an address to the socket */
	if (0 != (errcode = getaddrinfo(NULL, omi_service, &hints, &ai_ptr)))
	{
		RTS_ERROR_ADDRINFO(NULL, ERR_GETADDRINFO, errcode);
		return errcode;
	}
	if (ISDIGIT_ASCII(*omi_service))
		port = atoi(omi_service);
	else
	{
		if (0 != (errcode = getnameinfo(ai_ptr->ai_addr, ai_ptr->ai_addrlen, NULL, 0, port_buffer,
						 NI_MAXSERV, NI_NUMERICSERV)))
		{
			assert(FALSE);
			RTS_ERROR_ADDRINFO(NULL, ERR_GETNAMEINFO, errcode);
			return errcode;
		}
		port = atoi(port_buffer);
	}
	/*  Reuse a specified address */
	if (port && setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&reuseaddr, SIZEOF(reuseaddr)) < 0)
	{
		save_errno = errno;
		CLOSEFILE_RESET(fd, rc);	/* resets "fd" to FD_INVALID */
		return save_errno;
	}
	if (bind(fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen) < 0)
	{
		save_errno = errno;
		CLOSEFILE_RESET(fd, rc);	/* resets "fd" to FD_INVALID */
		return save_errno;
	}
	/*  Initialize the listen queue */
	if (listen(fd, 5) < 0)
	{
		save_errno = errno;
		CLOSEFILE_RESET(fd, rc);	/* resets "fd" to FD_INVALID */
		return save_errno;
	}
	/* set up raw socket for use with pinging option */
	if (ping_keepalive)
		psock = init_ping();
	/*  Store the file descriptor away for use later */
	cll->nve = fd;
	OMI_DBG_STMP;
	OMI_DBG((omi_debug, "%s: socket registered at port %d\n", SRVR_NAME, (int)port));
#ifdef GTCM_RC
	OMI_DBG((omi_debug, "RC server ID %d, Process ID %d\n", rc_server_id, omi_pid));
#endif
	if (authenticate)
		OMI_DBG((omi_debug, "Password verification on OMI connections enabled.\n"));
	if (!one_conn_per_inaddr)
		OMI_DBG((omi_debug, "Multiple connections from the same internet address allowed.\n"));
	if (psock > 0)
		OMI_DBG((omi_debug, "Keepalive option (-ping) enabled.\n"));
	return 0;
#else /* defined(BSD_TCP) */
#ifdef SYSV_TCP
	GTMASSERT;
	if ((fd = t_open(SYSV_TCP, O_RDWR, NULL)) < 0)
	{
		save_errno = errno;
		return save_errno;
	}
	if (!(bind = (struct t_bind *)t_alloc(fd, T_BIND, T_ALL)))
	{
		save_errno = errno;
		(void) t_close(fd);
		return save_errno;
	}
	bind->qlen      = 5;
	bind->addr.len  = 0;
	bind->addr.buf  = 0;
	if (t_bind(fd, bind, bind) < 0)
	{
		save_errno = errno;
		(void) t_free(bind, T_BIND);
		(void) t_close(fd);
		return save_errno;
	}
	/*  Store the file descriptor away for use later */
	cll->nve = fd;
	OMI_DBG_STMP;
	OMI_DBG((omi_debug, "%s: socket registered at port %d\n", SRVR_NAME, (int)port));
#ifdef GTCM_RC
	OMI_DBG((omi_debug, "RC server ID %d\n", rc_server_id));
#endif
	return 0;
#else /* defined(SYSV_TCP) */
	cll->nve = FD_INVALID;
	return -1;
#endif /* !defined(SYSV_TCP) */
#endif /* !defined(BSD_TCP) */
}
예제 #21
0
int
nfslib_bindit(struct netconfig *nconf, struct netbuf **addr,
	struct nd_hostserv *hs, int backlog)
{
	int fd;
	struct t_bind  *ntb;
	struct t_bind tb;
	struct nd_addrlist *addrlist;
	struct t_optmgmt req, resp;
	struct opthdr *opt;
	char reqbuf[128];
	bool_t use_any = FALSE;
	bool_t gzone = TRUE;

	if ((fd = nfslib_transport_open(nconf)) == -1) {
		syslog(LOG_ERR, "cannot establish transport service over %s",
		    nconf->nc_device);
		return (-1);
	}

	addrlist = (struct nd_addrlist *)NULL;

	/* nfs4_callback service does not used a fieed port number */

	if (strcmp(hs->h_serv, "nfs4_callback") == 0) {
		tb.addr.maxlen = 0;
		tb.addr.len = 0;
		tb.addr.buf = 0;
		use_any = TRUE;
		gzone = (getzoneid() == GLOBAL_ZONEID);
	} else if (netdir_getbyname(nconf, hs, &addrlist) != 0) {

		syslog(LOG_ERR,
		"Cannot get address for transport %s host %s service %s",
		    nconf->nc_netid, hs->h_host, hs->h_serv);
		(void) t_close(fd);
		return (-1);
	}

	if (strcmp(nconf->nc_proto, "tcp") == 0) {
		/*
		 * If we're running over TCP, then set the
		 * SO_REUSEADDR option so that we can bind
		 * to our preferred address even if previously
		 * left connections exist in FIN_WAIT states.
		 * This is somewhat bogus, but otherwise you have
		 * to wait 2 minutes to restart after killing it.
		 */
		if (reuseaddr(fd) == -1) {
			syslog(LOG_WARNING,
			"couldn't set SO_REUSEADDR option on transport");
		}
	} else if (strcmp(nconf->nc_proto, "udp") == 0) {
		/*
		 * In order to run MLP on UDP, we need to handle creds.
		 */
		if (recvucred(fd) == -1) {
			syslog(LOG_WARNING,
			    "couldn't set SO_RECVUCRED option on transport");
		}
	}

	/*
	 * Make non global zone nfs4_callback port MLP
	 */
	if (use_any && is_system_labeled() && !gzone) {
		if (anonmlp(fd) == -1) {
			/*
			 * failing to set this option means nfs4_callback
			 * could fail silently later. So fail it with
			 * with an error message now.
			 */
			syslog(LOG_ERR,
			    "couldn't set SO_ANON_MLP option on transport");
			(void) t_close(fd);
			return (-1);
		}
	}

	if (nconf->nc_semantics == NC_TPI_CLTS)
		tb.qlen = 0;
	else
		tb.qlen = backlog;

	/* LINTED pointer alignment */
	ntb = (struct t_bind *)t_alloc(fd, T_BIND, T_ALL);
	if (ntb == (struct t_bind *)NULL) {
		syslog(LOG_ERR, "t_alloc failed:  t_errno %d, %m", t_errno);
		(void) t_close(fd);
		netdir_free((void *)addrlist, ND_ADDRLIST);
		return (-1);
	}

	/*
	 * XXX - what about the space tb->addr.buf points to? This should
	 * be either a memcpy() to/from the buf fields, or t_alloc(fd,T_BIND,)
	 * should't be called with T_ALL.
	 */
	if (addrlist)
		tb.addr = *(addrlist->n_addrs);		/* structure copy */

	if (t_bind(fd, &tb, ntb) == -1) {
		syslog(LOG_ERR, "t_bind failed:  t_errno %d, %m", t_errno);
		(void) t_free((char *)ntb, T_BIND);
		netdir_free((void *)addrlist, ND_ADDRLIST);
		(void) t_close(fd);
		return (-1);
	}

	/* make sure we bound to the right address */
	if (use_any == FALSE &&
	    (tb.addr.len != ntb->addr.len ||
	    memcmp(tb.addr.buf, ntb->addr.buf, tb.addr.len) != 0)) {
		syslog(LOG_ERR, "t_bind to wrong address");
		(void) t_free((char *)ntb, T_BIND);
		netdir_free((void *)addrlist, ND_ADDRLIST);
		(void) t_close(fd);
		return (-1);
	}

	/*
	 * Call nfs4svc_setport so that the kernel can be
	 * informed what port number the daemon is listing
	 * for incoming connection requests.
	 */

	if ((nconf->nc_semantics == NC_TPI_COTS ||
	    nconf->nc_semantics == NC_TPI_COTS_ORD) && Mysvc4 != NULL)
		(*Mysvc4)(fd, NULL, nconf, NFS4_SETPORT, &ntb->addr);

	*addr = &ntb->addr;
	netdir_free((void *)addrlist, ND_ADDRLIST);

	if (strcmp(nconf->nc_proto, "tcp") == 0) {
		/*
		 * Disable the Nagle algorithm on TCP connections.
		 * Connections accepted from this listener will
		 * inherit the listener options.
		 */

		/* LINTED pointer alignment */
		opt = (struct opthdr *)reqbuf;
		opt->level = IPPROTO_TCP;
		opt->name = TCP_NODELAY;
		opt->len = sizeof (int);

		/* LINTED pointer alignment */
		*(int *)((char *)opt + sizeof (*opt)) = 1;

		req.flags = T_NEGOTIATE;
		req.opt.len = sizeof (*opt) + opt->len;
		req.opt.buf = (char *)opt;
		resp.flags = 0;
		resp.opt.buf = reqbuf;
		resp.opt.maxlen = sizeof (reqbuf);

		if (t_optmgmt(fd, &req, &resp) < 0 ||
		    resp.flags != T_SUCCESS) {
			syslog(LOG_ERR,
	"couldn't set NODELAY option for proto %s: t_errno = %d, %m",
			    nconf->nc_proto, t_errno);
		}

		nfslib_set_sockbuf(fd);
	}

	return (fd);
}
예제 #22
0
파일: transp_tli.c 프로젝트: AzerTyQsdF/osx
/*
 * How to bind to reserved ports.
 * (port-only) version.
 */
int
bind_resv_port2(u_short *pp)
{
  int td, rc = -1, port;
  struct t_bind *treq, *tret;
  struct sockaddr_in *sin;
  extern char *t_errlist[];
  extern int t_errno;
  struct netconfig *nc = (struct netconfig *) NULL;
  voidp nc_handle;

  if ((nc_handle = setnetconfig()) == (voidp) NULL) {
    plog(XLOG_ERROR, "Cannot rewind netconfig: %s", nc_sperror());
    return -1;
  }
  /*
   * Search the netconfig table for INET/UDP.
   * This loop will terminate if there was an error in the /etc/netconfig
   * file or if you reached the end of the file without finding the udp
   * device.  Either way your machine has probably far more problems (for
   * example, you cannot have nfs v2 w/o UDP).
   */
  while (1) {
    if ((nc = getnetconfig(nc_handle)) == (struct netconfig *) NULL) {
      plog(XLOG_ERROR, "Error accessing getnetconfig: %s", nc_sperror());
      endnetconfig(nc_handle);
      return -1;
    }
    if (STREQ(nc->nc_protofmly, NC_INET) &&
	STREQ(nc->nc_proto, NC_UDP))
      break;
  }

  /*
   * This is the primary reason for the getnetconfig code above: to get the
   * correct device name to udp, and t_open a descriptor to be used in
   * t_bind below.
   */
  td = t_open(nc->nc_device, O_RDWR, (struct t_info *) 0);
  endnetconfig(nc_handle);

  if (td < 0) {
    plog(XLOG_ERROR, "t_open failed: %d: %s", t_errno, t_errlist[t_errno]);
    return -1;
  }
  treq = (struct t_bind *) t_alloc(td, T_BIND, T_ADDR);
  if (!treq) {
    plog(XLOG_ERROR, "t_alloc req");
    return -1;
  }
  tret = (struct t_bind *) t_alloc(td, T_BIND, T_ADDR);
  if (!tret) {
    t_free((char *) treq, T_BIND);
    plog(XLOG_ERROR, "t_alloc ret");
    return -1;
  }
  memset((char *) treq->addr.buf, 0, treq->addr.len);
  sin = (struct sockaddr_in *) treq->addr.buf;
  sin->sin_family = AF_INET;
  treq->qlen = 0;
  treq->addr.len = treq->addr.maxlen;
  errno = EADDRINUSE;
  port = IPPORT_RESERVED;

  do {
    --port;
    sin->sin_port = htons(port);
    rc = t_bind(td, treq, tret);
    if (rc < 0) {
      plog(XLOG_ERROR, "t_bind for port %d: %s", port, t_errlist[t_errno]);
    } else {
      if (memcmp(treq->addr.buf, tret->addr.buf, tret->addr.len) == 0)
	break;
      else
	t_unbind(td);
    }
  } while ((rc < 0 || errno == EADDRINUSE) && (int) port > IPPORT_RESERVED / 2);

  if (pp && rc == 0)
    *pp = port;
  t_free((char *) tret, T_BIND);
  t_free((char *) treq, T_BIND);
  return rc;
}
예제 #23
0
/** Free a tag list.
 *
 * The function tl_free() frees resources associated with a tag list.
 * In other words, it calls t_free on each tag item on the list.
 *
 */
void tl_free(tagi_t list[])
{
    while (list)
        list = t_free(list);
}
예제 #24
0
static int
TRANS(TLITLIBindLocal)(int fd, int family, char *port)

{
    struct sockaddr_un	*sunaddr=NULL;
    struct t_bind	*req=NULL;

    prmsg(2, "TLITLIBindLocal(%d,%d,%s)\n", fd, family, port);

    if( family == AF_UNIX )
    {
	if( (req=(struct t_bind *)t_alloc(fd,T_BIND,0)) == NULL )
	{
	    prmsg(1,
		  "TLITLIBindLocal() failed to allocate a t_bind\n");
	    return -1;
	}

	if( (sunaddr=(struct sockaddr_un *)
	     malloc(sizeof(struct sockaddr_un))) == NULL )
	{
	    prmsg(1,
		  "TLITLIBindLocal: failed to allocate a sockaddr_un\n");
	    t_free((char *)req,T_BIND);
	    return -1;
	}

	sunaddr->sun_family=AF_UNIX;

#ifdef nuke
	if( *port == '/' ) { /* A full pathname */
	    (void) strcpy(sunaddr->sun_path, port);
	} else {
	    (void) sprintf(sunaddr->sun_path,"%s%s", TLINODENAME, port );
	}
#endif /*NUKE*/

	(void) sprintf(sunaddr->sun_path,"%s%d",
		       TLINODENAME, getpid()^time(NULL) );

	prmsg(4, "TLITLIBindLocal: binding to %s\n",
	      sunaddr->sun_path);

	req->addr.buf=(char *)sunaddr;
	req->addr.len=sizeof(*sunaddr);
	req->addr.maxlen=sizeof(*sunaddr);
    }

    if( t_bind(fd, req, NULL) < 0 )
    {
	prmsg(1,
	      "TLIBindLocal: Unable to bind TLI device to %s\n",
	      port);
	if (sunaddr)
	    free((char *) sunaddr);
	if (req)
	    t_free((char *)req,T_BIND);
	return -1;
    }
    return 0;
}
예제 #25
0
void
t_rw (int j)
{
    int r;
    char ch;
    static char *q = 0;
    char qtype[2], qclass[2];
    struct tcpclient *x = NULL;

    x = t + j;
    if (x->state == -1)
    {
        r = write (x->tcp, x->buf + x->pos, x->len - x->pos);
        if (r <= 0)
        {
            t_close (j);
            return;
        }
        x->pos += r;
        if (x->pos == x->len)
        {
            t_free (j);
            x->state = 1; /* could drop connection immediately */
        }
        return;
    }

    r = read (x->tcp, &ch, 1);
    if (r == 0)
    {
        errno = error_pipe;
        t_close (j);
        return;
    }
    if (r < 0)
    {
        t_close (j);
        return;
    }

    if (x->state == 1)
    {
        x->len = (unsigned char)ch;
        x->len <<= 8;
        x->state = 2;
        return;
    }
    if (x->state == 2)
    {
        x->len += (unsigned char)ch;
        if (!x->len)
        {
            errno = error_proto;
            t_close (j);
            return;
        }
        x->buf = alloc (x->len);
        if (!x->buf)
        {
            t_close(j);
            return;
        }
        x->pos = 0;
        x->state = 3;

        return;
    }

    if (x->state != 3)
        return; /* impossible */

    x->buf[x->pos++] = ch;
    if (x->pos < x->len)
        return;

    if (!packetquery (x->buf, x->len, &q, qtype, qclass, x->id))
    {
        t_close(j);
        return;
    }

    x->active = ++numqueries;

    if (debug_level)
        log_query (x->active, x->ip, x->port, x->id, q, qtype);

    switch (query_start (&x->q, q, qtype, qclass, myipoutgoing))
    {
    case -1:
        t_drop (j);
        return;
    case 1:
        t_respond (j);
        return;
    }
    t_free (j);
    x->state = 0;
}
예제 #26
0
static XtransConnInfo
TRANS(TLIAccept)(XtransConnInfo ciptr, int *status)

{
    struct t_call	*call;
    XtransConnInfo	newciptr;
    int	i;

    prmsg(2,"TLIAccept(%x->%d)\n", ciptr, ciptr->fd);

    if( (call=(struct t_call *)t_alloc(ciptr->fd,T_CALL,T_ALL)) == NULL )
    {
	prmsg(1, "TLIAccept() failed to allocate a t_call\n");
	*status = TRANS_ACCEPT_BAD_MALLOC;
	return NULL;
    }

    if( t_listen(ciptr->fd,call) < 0 )
    {
	extern char *t_errlist[];
	extern int t_errno;
	prmsg(1, "TLIAccept() t_listen() failed\n");
	prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
	t_free((char *)call,T_CALL);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    /*
     * Now we need to set up the new endpoint for the incoming connection.
     */

    i=ciptr->index; /* Makes the next line more readable */

    if( (newciptr=TRANS(TLIOpen)(TLItrans2devtab[i].devcotsname)) == NULL )
    {
	prmsg(1, "TLIAccept() failed to open a new endpoint\n");
	t_free((char *)call,T_CALL);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( TRANS(TLITLIBindLocal)(newciptr->fd,TLItrans2devtab[i].family,"") < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLITLIBindLocal)() failed: %d\n",
	      errno);
	t_free((char *)call,T_CALL);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }


    if( t_accept(ciptr->fd,newciptr->fd,call) < 0 )
    {
	extern char *t_errlist[];
	extern int t_errno;
	prmsg(1, "TLIAccept() t_accept() failed\n");
	prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
	if( t_errno == TLOOK )
	{
	    int evtype = t_look(ciptr->fd);
	    prmsg(1, "TLIAccept() t_look() returned %d\n", evtype);
	    switch( evtype )
	    {
		case T_DISCONNECT:
		    if( t_rcvdis(ciptr->fd, NULL) < 0 )
		    {
			prmsg(1, "TLIAccept() t_rcvdis() failed\n");
			prmsg(1, "TLIAccept: %s\n", t_errlist[t_errno]);
		    }
		    break;
		default:
		    break;
	    }
	}
	t_free((char *)call,T_CALL);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_FAILED;
	return NULL;
    }

    t_free((char *)call,T_CALL);

    if( TRANS(TLIGetAddr)(newciptr) < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLIGetPeerAddr)() failed: %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( TRANS(TLIGetPeerAddr)(newciptr) < 0 )
    {
	prmsg(1,
	      "TLIAccept: TRANS(TLIGetPeerAddr)() failed: %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( ioctl(newciptr->fd, I_POP,"timod") < 0 )
    {
	prmsg(1, "TLIAccept() ioctl(I_POP, \"timod\") failed %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    if( ioctl(newciptr->fd, I_PUSH,"tirdwr") < 0 )
    {
	prmsg(1, "TLIAccept() ioctl(I_PUSH,\"tirdwr\") failed %d\n",
	      errno);
	t_close(newciptr->fd);
	free(newciptr->addr);
	free(newciptr);
	*status = TRANS_ACCEPT_MISC_ERROR;
	return NULL;
    }

    *status = 0;

    return newciptr;
}
예제 #27
0
파일: users.c 프로젝트: Doap/transports
tt_user *users_read(const char *jid) {
	tt_user *user;
	xode cfg, x;
	char *file, *temp;

	if (jid == NULL)
		return NULL;

	if ((file = malloc(strlen(config_dir) + strlen(jid) + 2)) == NULL) {
		perror("malloc()");
		exit(1);
	}
	sprintf(file, "%s/%s", config_dir, jid);

	my_debug(0, "users: Wczytuje plik %s", file);

	if ((cfg = xode_from_file(file)) == NULL) {
		my_debug(0, "users: Nie moge wczytac usera");
		return NULL;
	}

	user = (tt_user*)malloc(sizeof(tt_user));
	memset(user, 0, sizeof(tt_user));

	x = xode_get_tag(cfg, "jid");
	temp = xode_get_data(x);
	my_strcpy(user->jid, temp);

	x = xode_get_tag(cfg, "tid");
	temp = xode_get_data(x);
	my_strcpy(user->tid_short, temp);

	user->tid = malloc(strlen(user->tid_short) + strlen("@tlen.pl") + 1);
	sprintf(user->tid, "*****@*****.**", user->tid_short);

	x = xode_get_tag(cfg, "password");
	temp = xode_get_data(x);
	my_strcpy(user->password, temp);

	user->tlen_sesja = NULL;
	user->tlen_loged = 0;
	user->last_ping_time = 0;

	user->roster = NULL;
	user->search = NULL;
	user->search_id = NULL;

	x = xode_get_tag(cfg, "notify");

	temp = xode_get_attrib(x, "mail");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->mailnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->mailnotify = 0;
		} else {
			user->mailnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->mailnotify = 0;
	}

	temp = xode_get_attrib(x, "typing");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->typingnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->typingnotify = 0;
		} else {
			user->typingnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->typingnotify = 0;
	}

	temp = xode_get_attrib(x, "alarm");
	if (temp != NULL) {
		if (strcmp(temp, "1") == 0) {
			user->alarmnotify = 1;
		} else if (strcmp(temp, "0") == 0) {
			user->alarmnotify = 0;
		} else {
			user->alarmnotify = 0;
			my_debug(0, "Blad w pliku");
		}
	} else {
		user->alarmnotify = 0;
	}

	user->status = NULL;
	user->status_type = 0;

	user->jabber_status = NULL;
	user->jabber_status_type = NULL;

	xode_free(cfg);
	t_free(file);

	return user;
}
예제 #28
0
파일: users.c 프로젝트: Doap/transports
int users_close() {
	tt_user **user, *user0;
	tx_user **tuser, *tuser0;

	for (user = tt_users; *user; user++) {
		user0 = *user;
		t_free(user0->jid);
		t_free(user0->password);
		t_free(user0->tid);
		t_free(user0->tid_short);
		t_free(user0->status);
		t_free(user0->jabber_status);
		t_free(user0->jabber_status_type);
		t_free(user0->search_id);
		t_free(user0->search_jid);
		if (user0->roster) {
			for (tuser = user0->roster; *tuser; tuser++) {
				tuser0 = *tuser;
				t_free(tuser0->tid);
				t_free(tuser0->status);
				t_free(tuser0);
			}
			t_free(user0->roster);
		}
		/* FIXIT: wyniki wyszukiwania, vcard */
		t_free(user0);
	}
	t_free(tt_users);

	return 0;
}
예제 #29
0
파일: prog.c 프로젝트: march1896/snev
p_dfa dfa_compile_from_string( const char* str ) {
	p_dfa pdfa;
	p_regc prc;
	p_stack ps;
	p_stack_ele pse;
	p_nfa pa, pa_x, pa_y, pa_z, pa_w;
	char *buffer, *ch;
	int  v, i, j, k, x, y, z;

	prc = regc_compile( str );

	if ( prc == NULL ) {
		return NULL;
	}

	regc_print( prc );

	ps = stack_new();
	buffer = (char*)t_alloc( sizeof(char) * BUFF_SIZE );
	ch = (char*)t_alloc( sizeof(char) * CHARACTER_SET_END );

	/* TODO: check the str is comfortable */

	for (i = 0;i < prc->size; i++ ) {
		switch ( prc->buffer[i] ) {
			case REG_NUMBER:
				//printf( "%s\n", chset_num );
				pa = nfa_make_from_stringbranch( chset_num );
				stack_push_nfa( ps, pa );
				break;
			case REG_WORDS:
				//printf( "%s\n", chset_word );
				pa = nfa_make_from_stringbranch( chset_word );
				stack_push_nfa( ps, pa );
				break;
			case REG_BLACK:
				//printf( "%s\n", chset_black );
				pa = nfa_make_from_stringbranch( chset_black );
				stack_push_nfa( ps, pa );
				break;
			case REG_LEFTPAR:
				stack_push_op( ps, LEFT_PAR );
				break;
			case REG_RIGHTPAR:
				stack_process( ps, RIGHT_PAR );
				break;
			case REG_LEFTSQUARE:
				v = 1;
				if ( prc->buffer[++i] == REG_NOT ) {
					v = 0;
					i ++;
				}

				for (j = 0; j < CHARACTER_SET_END; j ++ ) ch[j] = 0;
				do {
					if ( prc->buffer[i] == REG_RIGHTSQUARE ) break;
					else if ( prc->buffer[i] == REG_BETWEEN ) {
						i ++;
						x = prc->buffer[i++];
						y = prc->buffer[i++];
						if ( x > y ) {
							z = x;
							x = y;
							y = z;
						}
						while ( x <= y ) ch[x++] = 1;
					}
					else {
						x = prc->buffer[i++];
						ch[x] = 1;
					}
				} while ( i < prc->size );

				for (j = CHARACTER_SET_START, k = 0; j < CHARACTER_SET_END; j ++ ) {
					if ( !( ch[j] ^ v ) ) {
						buffer[k++] = (char)j;
					}
				}
				if ( !( ch['\t'] ^ v ) ) { buffer[k++] = (char)'\t'; }
				if ( !( ch['\n'] ^ v ) ) { buffer[k++] = (char)'\n'; }
				buffer[k] = '\0';
				//printf( "%s\n", buffer );
				pa = nfa_make_from_stringbranch( buffer );
				stack_push_nfa( ps, pa );
				break;
			case REG_RIGHTSQUARE:
				printf( "FATALERROR: right square\n" );
				break;
			case REG_LEFTBRACE:
				pse = stack_top( ps );

				if ( pse->type != e_nfa ) {
					printf( "error!" );
					break;
				}

				i ++;
				x = prc->buffer[i++];
				y = prc->buffer[i++];

				pa = pse->value.pa;

				pa_z = nfa_multiple( pa, x );

				if ( y == REG_INFINITE ) {
					pa_y = nfa_closure( pa );
				}
				else {
					for (j = 0; j <= y-x; j ++ ) {
						pa_x = nfa_multiple( pa, j );

						if ( j == 0 ) {
							pa_y = nfa_copy( pa_x );
						}
						else {
							pa_w = pa_y;
							pa_y = nfa_branch( pa_w, pa_x );
							nfa_del( pa_w );
						}
						nfa_del( pa_x );
					}
				}

				nfa_del( pa );
				pa = nfa_concat( pa_z, pa_y );
				nfa_del( pa_z );
				nfa_del( pa_y );
				
				stack_pop( ps );
				stack_push_nfa( ps, pa );
				break;
			case REG_RIGHTBRACE:
				printf( "FATALERROR: right brace\n" );
				break;
			case REG_BRANCH:
				stack_process( ps, BRANCH );
				stack_push_op( ps, BRANCH );
				break;
			case REG_CONCAT:
				stack_process( ps, CONCAT );
				stack_push_op( ps, CONCAT );
				break;
			case REG_STAR:
				pse = stack_top( ps );

				if ( pse->type != e_nfa ) {
					printf( "error!" );
					break;
				}
				pa = pse->value.pa;
				pa_x = nfa_closure( pa );
				nfa_del( pa );

				stack_pop( ps );
				stack_push_nfa( ps, pa_x );
				break;
			case REG_PLUS:
				pse = stack_top( ps );

				if ( pse->type != e_nfa ) {
					printf( "error!" );
					break;
				}
				pa = pse->value.pa;
				pa_x = nfa_closure( pa );
				pa_y = nfa_copy( pa );
				nfa_del( pa );

				pa = nfa_concat( pa_y, pa_x );
				nfa_del( pa_x );
				nfa_del( pa_y );

				stack_pop( ps );
				stack_push_nfa( ps, pa );
				break;
			case REG_QUESTION:
				pse = stack_top( ps );

				if ( pse->type != e_nfa ) {
					printf( "error!" );
					break;
				}
				pa = pse->value.pa;
				pa_x = nfa_make_from_stringconcat( "" );

				pa_y = nfa_branch( pa, pa_x );
				nfa_del( pa_x );
				nfa_del( pa );

				stack_pop( ps );
				stack_push_nfa( ps, pa_y );
				break;
			case REG_WILDCAST:
				for (j = CHARACTER_SET_START, k = 0; j < CHARACTER_SET_END; j ++ ) {
					buffer[k++] = (char)j;
				}
				buffer[k++] = '\t';
				buffer[k] = '\0';
				//printf( "%s\n", buffer );
				pa = nfa_make_from_stringbranch( buffer );
				stack_push_nfa( ps, pa );
				break;
			case REG_BOL:
				pa = nfa_make_from_stringbranch( "\2\n" );
				stack_push_nfa( ps, pa );
				break;
			case REG_EOL:
				pa = nfa_make_from_stringbranch( "\3\n" );
				stack_push_nfa( ps, pa );
				break;
			case REG_STRFRAGMENT:
				i ++;
				for (j = i; j < prc->size; j ++ ) {
					if ( prc->buffer[j] <= 0 ) {
						buffer[j-i] = '\0';
						break;
					}
					else {
						buffer[j-i] = (char)prc->buffer[j];
					}
				}
				if ( j == prc->size ) buffer[j-i] = '\0';

				//printf( "%d %s\n", j, buffer );
				pa = nfa_make_from_stringconcat( buffer );
				stack_push_nfa( ps, pa );

				i = j-1;
				break;
			default:
				printf( "FATAL ERROR %s\n", regc_str[ -prc->buffer[i] ] );
		}
	}

	stack_process( ps, END );

	if ( ps->top->next != NULL ) printf( "error!\n" );
	pse = stack_top( ps );
	pa = pse->value.pa;

	regc_del( prc );

	pdfa = dfa_convert_from_nfa( pa );
	nfa_del( pa );

	stack_del( ps );
	t_free( ch );
	t_free( buffer );
	return pdfa;
}
예제 #30
0
/*
 * Create the amq service for amd (both TCP and UDP)
 */
int
create_amq_service(int *udp_soAMQp,
		   SVCXPRT **udp_amqpp,
		   struct netconfig **udp_amqncpp,
		   int *tcp_soAMQp,
		   SVCXPRT **tcp_amqpp,
		   struct netconfig **tcp_amqncpp,
		   u_short preferred_amq_port)
{
  /*
   * (partially) create the amq service for amd
   * to be completed further in by caller.
   * XXX: is this "partially" still true?!  See amd/nfs_start.c. -Erez
   */

  /* first create the TCP service */
  if (tcp_amqncpp)
    if ((*tcp_amqncpp = getnetconfigent(NC_TCP)) == NULL) {
      plog(XLOG_ERROR, "cannot getnetconfigent for %s", NC_TCP);
      return 1;
    }

  if (tcp_amqpp) {
    if (preferred_amq_port > 0) {
      struct t_bind *tbp = NULL;
      int sock;

      plog(XLOG_INFO, "requesting preferred amq TCP port %d", preferred_amq_port);
      sock = bind_preferred_amq_port(preferred_amq_port, *tcp_amqncpp, &tbp);
      if (sock < 0) {
	plog(XLOG_ERROR, "bind_preferred_amq_port failed for TCP port %d: %s",
	     preferred_amq_port, t_errlist[t_errno]);
	return 1;
      }
      *tcp_amqpp = svc_tli_create(sock, *tcp_amqncpp, tbp, 0, 0);
      if (*tcp_amqpp != NULL)
	plog(XLOG_INFO, "amq service bound to TCP port %d", preferred_amq_port);
      t_free((char *) tbp, T_BIND);
    } else {
      /* select any port */
      *tcp_amqpp = svc_tli_create(RPC_ANYFD, *tcp_amqncpp, NULL, 0, 0);
    }
    if (*tcp_amqpp == NULL) {
      plog(XLOG_ERROR, "cannot create (tcp) tli service for amq");
      return 1;
    }
  }
  if (tcp_soAMQp && tcp_amqpp)
    *tcp_soAMQp = (*tcp_amqpp)->xp_fd;

  /* next create the UDP service */
  if (udp_amqncpp)
    if ((*udp_amqncpp = getnetconfigent(NC_UDP)) == NULL) {
      plog(XLOG_ERROR, "cannot getnetconfigent for %s", NC_UDP);
      return 1;
    }
  if (udp_amqpp) {
    if (preferred_amq_port > 0) {
      struct t_bind *tbp = NULL;
      int sock;

      plog(XLOG_INFO, "requesting preferred amq UDP port %d", preferred_amq_port);
      sock = bind_preferred_amq_port(preferred_amq_port, *udp_amqncpp, &tbp);
      if (sock < 0) {
	plog(XLOG_ERROR, "bind_preferred_amq_port failed for UDP port %d: %s",
	     preferred_amq_port, t_errlist[t_errno]);
	return 1;
      }
      *udp_amqpp = svc_tli_create(sock, *udp_amqncpp, tbp, 0, 0);
      if (*udp_amqpp != NULL)
	plog(XLOG_INFO, "amq service bound to UDP port %d", preferred_amq_port);
      t_free((char *) tbp, T_BIND);
    } else {
      /* select any port */
      *udp_amqpp = svc_tli_create(RPC_ANYFD, *udp_amqncpp, NULL, 0, 0);
    }
    if (*udp_amqpp == NULL) {
      plog(XLOG_ERROR, "cannot create (udp) tli service for amq");
      return 1;
    }
  }
  if (udp_soAMQp && udp_amqpp)
    *udp_soAMQp = (*udp_amqpp)->xp_fd;

  return 0;			/* all is well */
}