- struct inpcb *pcb;
- struct sockaddr_in sin;
- struct proc *p = current_proc();
- int stat;
-
-
- pcbinfo->nat_dummy_socket.so_pcb = 0;
- pcbinfo->nat_dummy_socket.so_options = 0;
- if (*lport) {
- /* The grabber wants a particular port */
-
- if (faddr.s_addr || fport) {
- /*
- * This is either the second half of an active connect, or
- * it's from the acceptance of an incoming connection.
- */
- if (laddr.s_addr == 0) {
- return EINVAL;
- }
-
- if (in_pcblookup_hash(pcbinfo, faddr, fport,
- laddr, *lport, 0, NULL) != NULL) {
- if (!(IN_MULTICAST(ntohl(laddr.s_addr)))) {
- return (EADDRINUSE);
- }
- }
-
- stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
- if (stat)
- return stat;
- pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
- pcb->inp_vflag |= INP_IPV4;
-
- pcb->inp_lport = *lport;
- pcb->inp_laddr.s_addr = laddr.s_addr;
-
- pcb->inp_faddr = faddr;
- pcb->inp_fport = fport;
- in_pcbinshash(pcb);
- }
- else {
- /*
- * This is either a bind for a passive socket, or it's the
- * first part of bind-connect sequence (not likely since an
- * ephemeral port is usually used in this case). Or, it's
- * the result of a connection acceptance when the foreign
- * address/port cannot be provided (which requires the SO_REUSEADDR
- * flag if laddr is not multicast).
- */
-
- stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
- if (stat)
- return stat;
- pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
- pcb->inp_vflag |= INP_IPV4;
-
- pcbinfo->nat_dummy_socket.so_options = options;
- bzero(&sin, sizeof(struct sockaddr_in));
- sin.sin_len = sizeof(struct sockaddr_in);
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = laddr.s_addr;
- sin.sin_port = *lport;
-
- stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
- (struct sockaddr *) &sin, p);
- if (stat) {
- in_pcbdetach(pcb);
- return stat;
- }
- }
- }
- else {
- /* The grabber wants an ephemeral port */
-
- stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
- if (stat)
- return stat;
- pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
- pcb->inp_vflag |= INP_IPV4;
-
- bzero(&sin, sizeof(struct sockaddr_in));
- sin.sin_len = sizeof(struct sockaddr_in);
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = laddr.s_addr;
- sin.sin_port = 0;
-
- if (faddr.s_addr || fport) {
- /*
- * Not sure if this case will be used - could occur when connect
- * is called, skipping the bind.
- */
-
- if (laddr.s_addr == 0) {
- in_pcbdetach(pcb);
- return EINVAL;
- }
-
- stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
- (struct sockaddr *) &sin, p);
- if (stat) {
- in_pcbdetach(pcb);
- return stat;
- }
-
- if (in_pcblookup_hash(pcbinfo, faddr, fport,
- pcb->inp_laddr, pcb->inp_lport, 0, NULL) != NULL) {
- in_pcbdetach(pcb);
- return (EADDRINUSE);
- }
-
- pcb->inp_faddr = faddr;
- pcb->inp_fport = fport;
- in_pcbrehash(pcb);
- }
- else {
- /*
- * This is a simple bind of an ephemeral port. The local addr
- * may or may not be defined.
- */
-
- stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
- (struct sockaddr *) &sin, p);
- if (stat) {
- in_pcbdetach(pcb);
- return stat;
- }
- }
- *lport = pcb->inp_lport;
- }
-
-
- pcb->nat_owner = owner_id;
- pcb->nat_cookie = cookie;
- pcb->inp_ppcb = (caddr_t) pcbinfo->dummy_cb;
- return 0;
-}