|
|
1.1 root 1: /******************************************************************************
2: * Copyright (c) 2004, 2008 IBM Corporation
3: * All rights reserved.
4: * This program and the accompanying materials
5: * are made available under the terms of the BSD License
6: * which accompanies this distribution, and is available at
7: * http://www.opensource.org/licenses/bsd-license.php
8: *
9: * Contributors:
10: * IBM Corporation - initial implementation
11: *****************************************************************************/
12:
13: /*>>>>>>>>>>>>>>>>>>>>>>> DEFINITIONS & DECLARATIONS <<<<<<<<<<<<<<<<<<<<*/
14:
15: #include <udp.h>
16: #include <sys/socket.h>
17: #include <dhcp.h>
18: //#include <dhcpv6.h>
19: #include <dns.h>
20: #ifdef USE_MTFTP
21: #include <mtftp.h>
22: #else
23: #include <tftp.h>
24: #endif
25:
26:
27:
28: /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOCAL VARIABLES <<<<<<<<<<<<<<<<<<<<<<<<<*/
29:
30:
31: #ifdef USE_MTFTP
32:
33: uint16_t net_tftp_uport;
34: uint16_t net_mtftp_uport;
35:
36: void net_set_tftp_port(uint16_t tftp_port) {
37: net_tftp_uport = tftp_port;
38: }
39:
40: void net_set_mtftp_port(uint16_t tftp_port) {
41: net_mtftp_uport = tftp_port;
42: }
43:
44: #endif
45:
46: /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMPLEMENTATION <<<<<<<<<<<<<<<<<<<<<<<<<<*/
47:
48:
49: /**
50: * NET: Handles UDP-packets according to Receive-handle diagram.
51: *
52: * @param udp_packet UDP-packet to be handled
53: * @param packetsize Length of the packet
54: * @return ZERO - packet handled successfully;
55: * NON ZERO - packet was not handled (e.g. bad format)
56: * @see receive_ether
57: * @see udphdr
58: */
59: int8_t
60: handle_udp(uint8_t * udp_packet, int32_t packetsize) {
61: struct udphdr * udph = (struct udphdr *) udp_packet;
62:
63: if (packetsize < sizeof(struct udphdr))
64: return -1; // packet is too small
65:
66: switch (htons(udph -> uh_dport)) {
67: case UDPPORT_BOOTPC:
68: if (udph -> uh_sport == htons(UDPPORT_BOOTPS))
69: return handle_dhcp(udp_packet + sizeof(struct udphdr),
70: packetsize - sizeof(struct udphdr));
71: else
72: return -1;
73: case UDPPORT_DNSC:
74: if (udph -> uh_sport == htons(UDPPORT_DNSS))
75: return handle_dns(udp_packet + sizeof(struct udphdr),
76: packetsize - sizeof(struct udphdr));
77: else
78: return -1;
79: /*
80: case UDPPORT_DHCPV6C:
81: return handle_dhcpv6(udp_packet+sizeof(struct udphdr),
82: packetsize - sizeof(struct udphdr));
83: */
84: case UDPPORT_TFTPC:
85: #ifdef USE_MTFTP
86: return handle_tftp(udp_packet + sizeof(struct udphdr),
87: packetsize - sizeof(struct udphdr));
88: #else
89: return handle_tftp(udp_packet, packetsize);
90: #endif
91: default:
92: #ifdef USE_MTFTP
93: if (htons(udph -> uh_dport) == net_tftp_uport)
94: return handle_tftp(udp_packet + sizeof(struct udphdr),
95: packetsize - sizeof(struct udphdr));
96: else if (htons(udph -> uh_dport) == net_mtftp_uport)
97: return handle_tftp(udp_packet + sizeof(struct udphdr),
98: packetsize - sizeof(struct udphdr));
99: #endif
100: return -1;
101: }
102: }
103:
104: /**
105: * NET: This function handles situation when "Destination unreachable"
106: * ICMP-error occurs during sending UDP-packet.
107: *
108: * @param err_code Error Code (e.g. "Host unreachable")
109: * @param packet original UDP-packet
110: * @param packetsize length of the packet
111: * @see handle_icmp
112: */
113: void
114: handle_udp_dun(uint8_t * udp_packet, uint32_t packetsize, uint8_t err_code) {
115: struct udphdr * udph = (struct udphdr *) udp_packet;
116:
117: if (packetsize < sizeof(struct udphdr))
118: return; // packet is too small
119:
120: switch (htons(udph -> uh_sport)) {
121: case UDPPORT_TFTPC:
122: handle_tftp_dun(err_code);
123: break;
124: }
125: }
126:
127: /**
128: * NET: Creates UDP-packet. Places UDP-header in a packet and fills it
129: * with corresponding information.
130: * <p>
131: * Use this function with similar functions for other network layers
132: * (fill_ethhdr, fill_iphdr, fill_dnshdr, fill_btphdr).
133: *
134: * @param packet Points to the place where UDP-header must be placed.
135: * @param packetsize Size of the packet in bytes incl. this hdr and data.
136: * @param src_port UDP source port
137: * @param dest_port UDP destination port
138: * @see udphdr
139: * @see fill_ethhdr
140: * @see fill_iphdr
141: * @see fill_dnshdr
142: * @see fill_btphdr
143: */
144: void
145: fill_udphdr(uint8_t * packet, uint16_t packetsize,
146: uint16_t src_port, uint16_t dest_port) {
147: struct udphdr * udph = (struct udphdr *) packet;
148:
149: udph -> uh_sport = htons(src_port);
150: udph -> uh_dport = htons(dest_port);
151: udph -> uh_ulen = htons(packetsize);
152: udph -> uh_sum = htons(0);
153: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.