xref: /csrg-svn/sys/netinet/in_pcb.c (revision 4907)
1 /* in_pcb.c 4.2 81/11/16 */
2 
3 #include "../h/param.h"
4 #include "../h/mbuf.h"
5 #include "../h/socket.h"
6 #include "../h/socketvar.h"
7 #include "../net/inet.h"
8 #include "../net/inet_systm.h"
9 #include "../net/inet_host.h"
10 #include "../net/inet_pcb.h"
11 
12 struct inpcb *
13 in_pcballoc()
14 {
15 
16 	struct mbuf *m;
17 
18 	m = m_getclr(M_WAIT);
19 	m->m_off = MMINOFF;
20 	return (mtod(m, struct inpcb *));
21 }
22 
23 in_pcbfree(inp)
24 	struct inpcb *inp;
25 {
26 	struct socket *so = inp->inp_socket;
27 
28 	if (so->so_isfilerefd == 0)
29 		sofree(so);
30 	else
31 		so->so_pcb = 0;
32 	if (inp->inp_lhost)
33 		in_hostfree(inp->inp_lhost);
34 	if (inp->inp_fhost)
35 		in_hostfree(inp->inp_fhost);
36 	(void) m_free(dtom(inp));
37 }
38 
39 struct inpcb *
40 in_pcblookup(head, fhost, fport, lhost, lport)
41 	struct inpcb *head;
42 	struct ip_addr *fhost, *lhost;
43 	u_short fport, lport;
44 {
45 	register struct inpcb *inp;
46 
47 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
48 		if (inp->inp_fhost->h_addr.s_addr == fhost->s_addr &&
49 		    inp->inp_fport == fport &&
50 		    inp->inp_lhost->h_addr.s_addr == lhost->s_addr &&
51 		    inp->inp_lport == lport)
52 			return (inp);
53 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
54 		if ((inp->inp_fhost->h_addr.s_addr == fhost->s_addr ||
55 		     inp->inp_fhost == 0) &&
56 		    (inp->inp_fport == fport || inp->inp_fport == 0) &&
57 		     inp->inp_lhost->h_addr.s_addr == lhost->s_addr &&
58 		    (inp->inp_lport == lport || inp->inp_lport == 0))
59 			return (inp);
60 	return (0);
61 }
62 
63 in_pcbgenport(head)
64 	struct inpcb *head;
65 {
66 	register struct inpcb *inp;
67 
68 again:
69 	if (head->inp_lport++ < 1024)
70 		head->inp_lport = 1024;
71 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
72 		if (inp->inp_lport == head->inp_lport)
73 			goto again;
74 	return (head->inp_lport);
75 }
76