|
|
1.1 root 1: /* udp_output.c 6.1 83/07/29 */
2: #include "udp.h"
3: #if NUDP > 0
4:
5: #include "../h/param.h"
6: #include "../h/systm.h"
7: #include "../h/stream.h"
8: #include "../h/inet/mbuf.h"
9:
10: #include "../h/inet/in.h"
11: #include "../h/inet/ip.h"
12: #include "../h/inet/ip_var.h"
13: #include "../h/inet/udp.h"
14: #include "../h/inet/udp_var.h"
15:
16: #define IPPROTO_UDP 17
17: #define UDP_DATA_LEN (1500-sizeof(struct udpiphdr))
18:
19: udp_output(bp, udp)
20: register struct block *bp;
21: register struct udp *udp;
22: {
23: register struct block *head;
24: register struct udpiphdr *ui;
25: register int len = 0, tlen;
26: int flags;
27:
28: /*
29: * Calculate data length and get a mbuf
30: * for UDP and IP headers.
31: */
32:
33: head = bp;
34: while(bp) {
35: len += BLEN(bp);
36: bp = bp->next;
37: }
38:
39: if (NULL == (bp = allocb(sizeof(struct udpiphdr)))) {
40: printf("Can't allocate block for udpiphdr\n");
41: bp_free(head);
42: return;
43: }
44: bp->wptr += sizeof(struct udpiphdr);
45: /*
46: * point "next" of allocated block containing udpiphdr to data
47: * point head to this block
48: */
49: bp->next = head;
50:
51: /*
52: * Fill in mbuf with extended UDP header
53: * and addresses and length put into network format.
54: */
55: ui = mtod(bp, struct udpiphdr *);
56: ui->ui_next = ui->ui_prev = 0;
57: ui->ui_x1 = 0;
58: ui->ui_pr = IPPROTO_UDP;
59: ui->ui_len = (u_short)len + sizeof (struct udphdr);
60: ui->ui_len = htons(ui->ui_len);
61: ui->ui_src = htonl(udp->src);
62: ui->ui_dst = htonl(udp->dst);
63: ui->ui_sport = htons(udp->sport);
64: ui->ui_dport = htons(udp->dport);
65: ui->ui_ulen = ui->ui_len;
66:
67: /* Stuff checksum and output datagram. */
68: ui->ui_sum = 0;
69: if ((ui->ui_sum = in_cksum(bp, sizeof(struct udpiphdr)+len)) == 0)
70: ui->ui_sum = -1;
71:
72:
73: /* Put length and timeout time into the `real' ip header. */
74: ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr)+len;
75: ((struct ip *)ui)->ip_ttl = MAXTTL;
76:
77: /* Ip expects internet addresses in host order. */
78: ui->ui_src = ntohl(ui->ui_src);
79: ui->ui_dst = ntohl(ui->ui_dst);
80: udp_ldout(bp);
81: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.