1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
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
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
13 /*>>>>>>>>>>>>>>>>>>>>>>> DEFINITIONS & DECLARATIONS <<<<<<<<<<<<<<<<<<<<*/
16 #include <sys/socket.h>
28 /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOCAL VARIABLES <<<<<<<<<<<<<<<<<<<<<<<<<*/
33 uint16_t net_tftp_uport;
34 uint16_t net_mtftp_uport;
36 void net_set_tftp_port(uint16_t tftp_port) {
37 net_tftp_uport = tftp_port;
40 void net_set_mtftp_port(uint16_t tftp_port) {
41 net_mtftp_uport = tftp_port;
46 /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMPLEMENTATION <<<<<<<<<<<<<<<<<<<<<<<<<<*/
50 * NET: Handles UDP-packets according to Receive-handle diagram.
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)
60 handle_udp(int fd, uint8_t * udp_packet, int32_t packetsize) {
61 struct udphdr * udph = (struct udphdr *) udp_packet;
63 if (packetsize < sizeof(struct udphdr))
64 return -1; // packet is too small
66 switch (htons(udph -> uh_dport)) {
68 if (udph -> uh_sport == htons(UDPPORT_BOOTPS))
69 return handle_dhcp(fd, udp_packet + sizeof(struct udphdr),
70 packetsize - sizeof(struct udphdr));
74 if (udph -> uh_sport == htons(UDPPORT_DNSS))
75 return handle_dns(udp_packet + sizeof(struct udphdr),
76 packetsize - sizeof(struct udphdr));
80 return handle_dhcpv6(udp_packet+sizeof(struct udphdr),
81 packetsize - sizeof(struct udphdr));
84 return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
85 packetsize - sizeof(struct udphdr));
87 return handle_tftp(fd, udp_packet, packetsize);
91 if (htons(udph -> uh_dport) == net_tftp_uport)
92 return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
93 packetsize - sizeof(struct udphdr));
94 else if (htons(udph -> uh_dport) == net_mtftp_uport)
95 return handle_tftp(fd, udp_packet + sizeof(struct udphdr),
96 packetsize - sizeof(struct udphdr));
103 * NET: This function handles situation when "Destination unreachable"
104 * ICMP-error occurs during sending UDP-packet.
106 * @param err_code Error Code (e.g. "Host unreachable")
107 * @param packet original UDP-packet
108 * @param packetsize length of the packet
112 handle_udp_dun(uint8_t * udp_packet, uint32_t packetsize, uint8_t err_code) {
113 struct udphdr * udph = (struct udphdr *) udp_packet;
115 if (packetsize < sizeof(struct udphdr))
116 return; // packet is too small
118 switch (htons(udph -> uh_sport)) {
120 handle_tftp_dun(err_code);
126 * NET: Creates UDP-packet. Places UDP-header in a packet and fills it
127 * with corresponding information.
129 * Use this function with similar functions for other network layers
130 * (fill_ethhdr, fill_iphdr, fill_dnshdr, fill_btphdr).
132 * @param packet Points to the place where UDP-header must be placed.
133 * @param packetsize Size of the packet in bytes incl. this hdr and data.
134 * @param src_port UDP source port
135 * @param dest_port UDP destination port
143 fill_udphdr(uint8_t * packet, uint16_t packetsize,
144 uint16_t src_port, uint16_t dest_port) {
145 struct udphdr * udph = (struct udphdr *) packet;
147 udph -> uh_sport = htons(src_port);
148 udph -> uh_dport = htons(dest_port);
149 udph -> uh_ulen = htons(packetsize);
150 udph -> uh_sum = htons(0);