xref: /csrg-svn/sys/netinet/in_pcb.c (revision 4923)
1 /* in_pcb.c 4.3 81/11/18 */
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_pcbenter(head, new)
24 	struct inpcb *head, *new;
25 {
26 	register struct inpcb *inp;
27 
28 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
29 		if (inp->inp_fhost->h_addr.s_addr == new->inp_fhost.s_addr &&
30 		    inp->inp_fport == new->inp_fport &&
31 		    inp->inp_lhost->h_addr.s_addr = new->inp_fhost.s_addr &&
32 		    inp->inp_lport == new->inp_lport)
33 			return (EADDRINUSE);
34 	insque(new, head);
35 	return (0);
36 }
37 
38 in_pcbfree(inp)
39 	struct inpcb *inp;
40 {
41 	struct socket *so = inp->inp_socket;
42 
43 	if (so->so_isfilerefd == 0)
44 		sofree(so);
45 	else
46 		so->so_pcb = 0;
47 	if (inp->inp_lhost)
48 		in_hostfree(inp->inp_lhost);
49 	if (inp->inp_fhost)
50 		in_hostfree(inp->inp_fhost);
51 	(void) m_free(dtom(inp));
52 }
53 
54 struct inpcb *
55 in_pcblookup(head, fhost, fport, lhost, lport)
56 	struct inpcb *head;
57 	struct in_addr *fhost, *lhost;
58 	u_short fport, lport;
59 {
60 	register struct inpcb *inp;
61 
62 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
63 		if (inp->inp_fhost->h_addr.s_addr == fhost->s_addr &&
64 		    inp->inp_fport == fport &&
65 		    inp->inp_lhost->h_addr.s_addr == lhost->s_addr &&
66 		    inp->inp_lport == lport)
67 			return (inp);
68 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
69 		if ((inp->inp_fhost->h_addr.s_addr == fhost->s_addr ||
70 		     inp->inp_fhost == 0) &&
71 		    (inp->inp_fport == fport || inp->inp_fport == 0) &&
72 		     inp->inp_lhost->h_addr.s_addr == lhost->s_addr &&
73 		    (inp->inp_lport == lport || inp->inp_lport == 0))
74 			return (inp);
75 	return (0);
76 }
77 
78 in_pcbgenport(head)
79 	struct inpcb *head;
80 {
81 	register struct inpcb *inp;
82 
83 again:
84 	if (head->inp_lport++ < 1024)
85 		head->inp_lport = 1024;
86 	for (inp = head->inp_next; inp != head; inp = inp->inp_next)
87 		if (inp->inp_lport == head->inp_lport)
88 			goto again;
89 	return (head->inp_lport);
90 }
91