diff options
author | Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> | 2014-07-16 13:54:47 +0530 |
---|---|---|
committer | Nikunj A Dadhania <nikunj@linux.vnet.ibm.com> | 2014-10-29 16:17:10 +0530 |
commit | 8e6f2bba50c0a443dae592784cd6e12d106348bb (patch) | |
tree | 785d046f2217ec564e1d627e2299c359bdedcfdb /clients | |
parent | 4ca13ddf7f3fb12cf6ba22255ea07ee3e4c7e9b2 (diff) | |
download | SLOF-8e6f2bba50c0a443dae592784cd6e12d106348bb.zip SLOF-8e6f2bba50c0a443dae592784cd6e12d106348bb.tar.gz SLOF-8e6f2bba50c0a443dae592784cd6e12d106348bb.tar.bz2 |
net-snk: use socket descriptor in the network stack
With module layer cleanup, and a fixed socket() call, start using the
fd across the network stack.
Signed-off-by: Nikunj A Dadhania <nikunj@linux.vnet.ibm.com>
Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
Diffstat (limited to 'clients')
-rw-r--r-- | clients/net-snk/app/netapps/netboot.c | 16 | ||||
-rw-r--r-- | clients/net-snk/app/netapps/ping.c | 6 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/bootp.c | 4 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/dhcp.c | 35 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/dhcp.h | 4 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/dhcpv6.c | 17 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/dns.c | 16 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/dns.h | 2 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ethernet.c | 15 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ethernet.h | 4 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/icmpv6.c | 28 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/icmpv6.h | 6 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ipv4.c | 51 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ipv4.h | 10 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ipv6.c | 31 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/ipv6.h | 8 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/tftp.c | 36 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/tftp.h | 5 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/udp.c | 12 | ||||
-rw-r--r-- | clients/net-snk/app/netlib/udp.h | 2 |
20 files changed, 172 insertions, 136 deletions
diff --git a/clients/net-snk/app/netapps/netboot.c b/clients/net-snk/app/netapps/netboot.c index aa009bf..cf20b59 100644 --- a/clients/net-snk/app/netapps/netboot.c +++ b/clients/net-snk/app/netapps/netboot.c @@ -350,7 +350,7 @@ int dhcp(char *ret_buffer, filename_ip_t * fn_ip, unsigned int retries, int flag } if ((!flags && (rc == -1)) || (flags == F_IPV6)) { ip_version = 6; - set_ipv6_address(0); + set_ipv6_address(fn_ip->fd, 0); rc = dhcpv6(ret_buffer, fn_ip); if (rc == 0) { printf("\n"); @@ -428,6 +428,8 @@ netboot(int argc, char *argv[]) return -101; } + fn_ip.fd = fd_device; + printf(" Reading MAC address from device: " "%02x:%02x:%02x:%02x:%02x:%02x\n", own_mac[0], own_mac[1], own_mac[2], @@ -506,7 +508,7 @@ netboot(int argc, char *argv[]) obp_tftp_args.bootp_retries, F_IPV6); break; case IP_INIT_IPV6_MANUAL: - set_ipv6_address(&obp_tftp_args.ci6addr); + set_ipv6_address(fn_ip.fd, &obp_tftp_args.ci6addr); break; case IP_INIT_DEFAULT: printf(" Requesting IP address via DHCP: "); @@ -605,7 +607,7 @@ netboot(int argc, char *argv[]) &tftp_err, huge_load, block_size, ip_version); if(obp_tftp_args.ip_init == IP_INIT_DHCP) - dhcp_send_release(); + dhcp_send_release(fn_ip.fd); if (rc > 0) { printf(" TFTP: Received %s (%d KBytes)\n", fn_ip.filename, @@ -732,10 +734,12 @@ netboot(int argc, char *argv[]) * @param buffer string with arguments, * @param server_ip server ip as result * @param filename default filename - * @param len len of the buffer, + * @param fd Socket descriptor + * @param len len of the buffer, * @return 0 on SUCCESS and -1 on failure */ -int parse_tftp_args(char buffer[], char *server_ip, char filename[], int len) +int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, + int len) { char *raw; char *tmp, *tmp1; @@ -814,7 +818,7 @@ int parse_tftp_args(char buffer[], char *server_ip, char filename[], int len) tmp = raw + 7; tmp[j] = '\0'; strcpy(domainname, tmp); - if (dns_get_ip((int8_t *)domainname, server_ip6, 6) == 0) { + if (dns_get_ip(fd, (int8_t *)domainname, server_ip6, 6) == 0) { printf("\n DNS failed for IPV6\n"); return -1; } diff --git a/clients/net-snk/app/netapps/ping.c b/clients/net-snk/app/netapps/ping.c index 8376f48..2c7dadb 100644 --- a/clients/net-snk/app/netapps/ping.c +++ b/clients/net-snk/app/netapps/ping.c @@ -138,6 +138,8 @@ ping(int argc, char *argv[]) return -101; } + fn_ip.fd = fd_device; + printf("%02x:%02x:%02x:%02x:%02x:%02x\n", own_mac[0], own_mac[1], own_mac[2], own_mac[3], own_mac[4], own_mac[5]); @@ -178,11 +180,11 @@ ping(int argc, char *argv[]) ((fn_ip.server_ip >> 8) & 0xFF), (fn_ip.server_ip & 0xFF)); - ping_ipv4(fn_ip.server_ip); + ping_ipv4(fd_device, fn_ip.server_ip); set_timer(TICKS_SEC / 10 * ping_args.timeout); while(get_timer() > 0) { - receive_ether(); + receive_ether(fd_device); if(pong_ipv4() == 0) { printf("success\n"); return 0; diff --git a/clients/net-snk/app/netlib/bootp.c b/clients/net-snk/app/netlib/bootp.c index 7adf3f4..1bc6efe 100644 --- a/clients/net-snk/app/netlib/bootp.c +++ b/clients/net-snk/app/netlib/bootp.c @@ -85,7 +85,7 @@ send_bootp(filename_ip_t * fn_ip) printf(".\n"); #endif - send_ipv4(packet, iph->ip_len); + send_ipv4(fn_ip->fd, packet, iph->ip_len); #if DEBUG printf("%d bytes transmitted over socket.\n", i); #endif @@ -121,7 +121,7 @@ receive_bootp(filename_ip_t * fn_ip) do { /* let's receive a packet */ - len = recv(0, packet, packetsize, 0); + len = recv(fn_ip->fd, packet, packetsize, 0); #if DEBUG int j; diff --git a/clients/net-snk/app/netlib/dhcp.c b/clients/net-snk/app/netlib/dhcp.c index 7a92e28..5f26f3a 100644 --- a/clients/net-snk/app/netlib/dhcp.c +++ b/clients/net-snk/app/netlib/dhcp.c @@ -135,7 +135,7 @@ static uint8_t dhcp_state; /*>>>>>>>>>>>>>>>>>>>>>>>>>>>> PROTOTYPES <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ static int32_t -dhcp_attempt(void); +dhcp_attempt(int fd); static int32_t dhcp_encode_options(uint8_t * opt_field, dhcp_options_t * opt_struct); @@ -161,10 +161,10 @@ dhcp_combine_option(uint8_t dst_options[], uint32_t * dst_len, uint32_t dst_offset, uint8_t * new_option); static void -dhcp_send_discover(void); +dhcp_send_discover(int fd); static void -dhcp_send_request(void); +dhcp_send_request(int fd); static uint8_t strtoip(int8_t * str, uint32_t * ip); @@ -187,12 +187,14 @@ int32_t dhcpv4(char *ret_buffer, filename_ip_t * fn_ip) { uint32_t dhcp_tftp_ip = 0; + int fd = fn_ip->fd; + strcpy((char *) dhcp_filename, ""); strcpy((char *) dhcp_tftp_name, ""); response_buffer = ret_buffer; - if (dhcp_attempt() == 0) + if (dhcp_attempt(fd) == 0) return -1; if (fn_ip->own_ip) { @@ -218,7 +220,7 @@ dhcpv4(char *ret_buffer, filename_ip_t * fn_ip) { else { // TFTP server defined by its name if (!strtoip(dhcp_tftp_name, &(dhcp_tftp_ip))) { - if (!dns_get_ip(dhcp_tftp_name, (uint8_t *)&(dhcp_tftp_ip), 4)) { + if (!dns_get_ip(fd, dhcp_tftp_name, (uint8_t *)&(dhcp_tftp_ip), 4)) { // DNS error - can't obtain TFTP-server name // Use TFTP-ip from siaddr field, if presented if (dhcp_siaddr_ip) { @@ -244,11 +246,11 @@ dhcpv4(char *ret_buffer, filename_ip_t * fn_ip) { * DHCP: Tries o obtain DHCP parameters, refer to state-transition diagram */ static int32_t -dhcp_attempt(void) { +dhcp_attempt(int fd) { int sec; // Send DISCOVER message and switch DHCP-client to SELECT state - dhcp_send_discover(); + dhcp_send_discover(fd); dhcp_state = DHCP_STATE_SELECT; @@ -256,7 +258,7 @@ dhcp_attempt(void) { for (sec = 0; sec < 2; sec++) { set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); // Wait until client will switch to Final state or Timeout occurs switch (dhcp_state) { @@ -611,7 +613,7 @@ dhcp_combine_option(uint8_t dst_options[], uint32_t * dst_len, * DHCP: Sends DHCP-Discover message. Looks for DHCP servers. */ static void -dhcp_send_discover(void) { +dhcp_send_discover(int fd) { uint32_t packetsize = sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct btphdr); struct btphdr *btph; @@ -647,14 +649,14 @@ dhcp_send_discover(void) { sizeof(struct udphdr) + sizeof(struct iphdr), IPTYPE_UDP, dhcp_own_ip, 0xFFFFFFFF); - send_ipv4(ether_packet, packetsize); + send_ipv4(fd, ether_packet, packetsize); } /** * DHCP: Sends DHCP-Request message. Asks for acknowledgment to occupy IP. */ static void -dhcp_send_request(void) { +dhcp_send_request(int fd) { uint32_t packetsize = sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct btphdr); struct btphdr *btph; @@ -695,14 +697,14 @@ dhcp_send_request(void) { sizeof(struct udphdr) + sizeof(struct iphdr), IPTYPE_UDP, 0, 0xFFFFFFFF); - send_ipv4(ether_packet, packetsize); + send_ipv4(fd, ether_packet, packetsize); } /** * DHCP: Sends DHCP-Release message. Releases occupied IP. */ -void dhcp_send_release(void) { +void dhcp_send_release(int fd) { uint32_t packetsize = sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct btphdr); struct btphdr *btph; @@ -735,13 +737,14 @@ void dhcp_send_release(void) { sizeof(struct udphdr) + sizeof(struct iphdr), IPTYPE_UDP, dhcp_own_ip, dhcp_server_ip); - send_ipv4(ether_packet, packetsize); + send_ipv4(fd, ether_packet, packetsize); } /** * DHCP: Handles DHCP-messages according to Receive-handle diagram. * Changes the state of DHCP-client. * + * @param fd socket descriptor * @param packet BootP/DHCP-packet to be handled * @param packetsize length of the packet * @return ZERO - packet handled successfully; @@ -751,7 +754,7 @@ void dhcp_send_release(void) { */ int8_t -handle_dhcp(uint8_t * packet, int32_t packetsize) { +handle_dhcp(int fd, uint8_t * packet, int32_t packetsize) { struct btphdr * btph; struct iphdr * iph; dhcp_options_t opt; @@ -863,7 +866,7 @@ handle_dhcp(uint8_t * packet, int32_t packetsize) { if (opt.msg_type == DHCPOFFER) { dhcp_own_ip = htonl(btph -> yiaddr); dhcp_server_ip = opt.server_ID; - dhcp_send_request(); + dhcp_send_request(fd); dhcp_state = DHCP_STATE_REQUEST; } return 0; diff --git a/clients/net-snk/app/netlib/dhcp.h b/clients/net-snk/app/netlib/dhcp.h index 3aab4ef..69dd49d 100644 --- a/clients/net-snk/app/netlib/dhcp.h +++ b/clients/net-snk/app/netlib/dhcp.h @@ -45,9 +45,9 @@ struct btphdr { int bootp(char *ret_buffer, filename_ip_t *, unsigned int); int dhcpv4(char *ret_buffer, filename_ip_t *); -void dhcp_send_release(void); +void dhcp_send_release(int fd); /* Handles DHCP-packets, which are detected by receive_ether. */ -extern int8_t handle_dhcp(uint8_t * packet, int32_t packetsize); +extern int8_t handle_dhcp(int fd, uint8_t * packet, int32_t packetsize); #endif diff --git a/clients/net-snk/app/netlib/dhcpv6.c b/clients/net-snk/app/netlib/dhcpv6.c index 53cc7d3..4deef30 100644 --- a/clients/net-snk/app/netlib/dhcpv6.c +++ b/clients/net-snk/app/netlib/dhcpv6.c @@ -37,7 +37,7 @@ generate_transaction_id(void) } static void -send_info_request(void) +send_info_request(int fd) { uint8_t ether_packet[ETH_MTU_SIZE]; uint32_t payload_length; @@ -78,19 +78,19 @@ send_info_request(void) dhcph->option.option_request_option.option_code[2] = DHCPV6_OPTION_BOOT_URL; - send_ipv6( ether_packet + sizeof(struct ethhdr), + send_ipv6(fd, ether_packet + sizeof(struct ethhdr), sizeof(struct ethhdr)+ sizeof(struct ip6hdr) + sizeof(struct udphdr) + sizeof( struct dhcp_message_header) ); } static int32_t -dhcpv6_attempt(void) +dhcpv6_attempt(int fd) { int sec; // Send information request - send_info_request(); + send_info_request(fd); dhcpv6_state = DHCPV6_STATE_SELECT; @@ -98,7 +98,7 @@ dhcpv6_attempt(void) for (sec = 0; sec < 2; sec++) { set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); // Wait until client will switch to Final state or Timeout occurs switch (dhcpv6_state) { @@ -117,8 +117,12 @@ dhcpv6_attempt(void) int32_t dhcpv6 ( char *ret_buffer, void *fn_ip) { + int fd; + my_fn_ip = (filename_ip_t *) fn_ip; - if( !dhcpv6_attempt()) { + fd = my_fn_ip->fd; + + if( !dhcpv6_attempt(fd)) { return -1; } @@ -178,6 +182,7 @@ dhcp6_process_options (uint8_t *option, int32_t option_length) if (parse_tftp_args(buffer, (char *)my_fn_ip->server_ip6.addr, (char *)my_fn_ip->filename, + (int)my_fn_ip->fd, option_boot_url->length) == -1) return NULL; break; diff --git a/clients/net-snk/app/netlib/dns.c b/clients/net-snk/app/netlib/dns.c index 831d22d..0ab1346 100644 --- a/clients/net-snk/app/netlib/dns.c +++ b/clients/net-snk/app/netlib/dns.c @@ -66,7 +66,7 @@ struct dnshdr { /***************************** PROTOTYPES ********************************/ static void -dns_send_query(int8_t * domain_name, uint8_t ip_version); +dns_send_query(int fd, int8_t * domain_name, uint8_t ip_version); static void fill_dnshdr(uint8_t * packet, int8_t * domain_name, uint8_t ip_version); @@ -125,6 +125,7 @@ dns_init(uint32_t _dns_server_ip, uint8_t _dns_server_ipv6[16], uint8_t ip_versi * <br>(e.g. "www.host.org"); * </ul> * + * @param fd socket descriptor * @param url the URL to be resolved * @param domain_ip In case of SUCCESS stores extracted IP. * In case of FAULT stores zeros (0.0.0.0). @@ -132,7 +133,7 @@ dns_init(uint32_t _dns_server_ip, uint8_t _dns_server_ipv6[16], uint8_t ip_versi * FALSE - error condition occurs. */ int8_t -dns_get_ip(int8_t * url, uint8_t * domain_ip, uint8_t ip_version) +dns_get_ip(int fd, int8_t * url, uint8_t * domain_ip, uint8_t ip_version) { /* this counter is used so that we abort after 30 DNS request */ int32_t i; @@ -171,14 +172,14 @@ dns_get_ip(int8_t * url, uint8_t * domain_ip, uint8_t ip_version) for(i = 0; i < 30; ++i) { // Use canonical name in case we obtained it if (strlen((char *) dns_domain_cname)) - dns_send_query(dns_domain_cname, ip_version); + dns_send_query(fd, dns_domain_cname, ip_version); else - dns_send_query(dns_domain_name, ip_version); + dns_send_query(fd, dns_domain_name, ip_version); // setting up a timer with a timeout of one seconds set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); if (dns_error) return 0; // FALSE - error if ((dns_result_ip != 0) && (ip_version == 4)) { @@ -295,13 +296,14 @@ handle_dns(uint8_t * packet, int32_t packetsize) * DNS-server respones with host IP or signals some error condition. * Responses from the server are handled by handle_dns function. * + * @param fd socket descriptor * @param domain_name the domain name given as series of labels preceded * with length(label) and terminated with 0 * <br>(e.g. "\3,w,w,w,\4,h,o,s,t,\3,o,r,g,\0") * @see handle_dns */ static void -dns_send_query(int8_t * domain_name, uint8_t ip_version) +dns_send_query(int fd, int8_t * domain_name, uint8_t ip_version) { int qry_len = strlen((char *) domain_name) + 5; int iphdr_len = (ip_version == 4) ? sizeof(struct iphdr) : sizeof(struct ip6hdr); @@ -333,7 +335,7 @@ dns_send_query(int8_t * domain_name, uint8_t ip_version) &server_ipv6); } - send_ip(ether_packet, packetsize); + send_ip(fd, ether_packet, packetsize); } /** diff --git a/clients/net-snk/app/netlib/dns.h b/clients/net-snk/app/netlib/dns.h index 6277418..82eea4e 100644 --- a/clients/net-snk/app/netlib/dns.h +++ b/clients/net-snk/app/netlib/dns.h @@ -20,7 +20,7 @@ extern int8_t dns_init(uint32_t _dns_server_ip, uint8_t _dns_server_ipv6[16], uint8_t ip_version); /* For given URL retrieves IPv4 from DNS-server. */ -extern int8_t dns_get_ip(int8_t * url, uint8_t * domain_ip, uint8_t ip_version); +extern int8_t dns_get_ip(int fd, int8_t * url, uint8_t * domain_ip, uint8_t ip_version); /* Handles DNS-packets, which are detected by receive_ether. */ extern int32_t handle_dns(uint8_t * packet, int32_t packetsize); diff --git a/clients/net-snk/app/netlib/ethernet.c b/clients/net-snk/app/netlib/ethernet.c index c52583c..bbfd6d1 100644 --- a/clients/net-snk/app/netlib/ethernet.c +++ b/clients/net-snk/app/netlib/ethernet.c @@ -103,16 +103,17 @@ is_multicast_mac(uint8_t * mac) { * Ethernet: Receives an ethernet-packet and handles it according to * Receive-handle diagram. * + * @param fd socket fd * @return ZERO - packet was handled or no packets received; * NON ZERO - error condition occurs. */ int32_t -receive_ether(void) { +receive_ether(int fd) { int32_t bytes_received; struct ethhdr * ethh; memset(ether_packet, 0, ETH_MTU_SIZE); - bytes_received = recv(0, ether_packet, ETH_MTU_SIZE, 0); + bytes_received = recv(fd, ether_packet, ETH_MTU_SIZE, 0); if (!bytes_received) // No messages return 0; @@ -130,15 +131,15 @@ receive_ether(void) { switch (htons(ethh -> type)) { case ETHERTYPE_IP: - return handle_ipv4((uint8_t*) (ethh + 1), + return handle_ipv4(fd, (uint8_t*) (ethh + 1), bytes_received - sizeof(struct ethhdr)); case ETHERTYPE_IPv6: - return handle_ipv6(ether_packet + sizeof(struct ethhdr), + return handle_ipv6(fd, ether_packet + sizeof(struct ethhdr), bytes_received - sizeof(struct ethhdr)); case ETHERTYPE_ARP: - return handle_arp((uint8_t*) (ethh + 1), + return handle_arp(fd, (uint8_t*) (ethh + 1), bytes_received - sizeof(struct ethhdr)); default: break; @@ -152,9 +153,9 @@ receive_ether(void) { * @return number of transmitted bytes */ int -send_ether(void* buffer, int len) +send_ether(int fd, void* buffer, int len) { - return send(0, buffer, len, 0); + return send(fd, buffer, len, 0); } /** diff --git a/clients/net-snk/app/netlib/ethernet.h b/clients/net-snk/app/netlib/ethernet.h index ce44906..e541c8f 100644 --- a/clients/net-snk/app/netlib/ethernet.h +++ b/clients/net-snk/app/netlib/ethernet.h @@ -35,10 +35,10 @@ extern void set_mac_address(const uint8_t * own_mac); extern const uint8_t * get_mac_address(void); /* Receives and handles packets, according to Receive-handle diagram */ -extern int32_t receive_ether(void); +extern int32_t receive_ether(int fd); /* Sends an ethernet frame. */ -extern int send_ether(void* buffer, int len); +extern int send_ether(int fd, void* buffer, int len); /* fills ethernet header */ extern void fill_ethhdr(uint8_t * packet, uint16_t eth_type, diff --git a/clients/net-snk/app/netlib/icmpv6.c b/clients/net-snk/app/netlib/icmpv6.c index 8cf0e2d..be6cc11 100644 --- a/clients/net-snk/app/netlib/icmpv6.c +++ b/clients/net-snk/app/netlib/icmpv6.c @@ -25,10 +25,10 @@ static int ra_received = 0; /** * NET: - * + * @param fd socket fd */ void -send_router_solicitation () +send_router_solicitation (int fd) { ip6_addr_t dest_addr; uint8_t ether_packet[ETH_MTU_SIZE]; @@ -58,7 +58,7 @@ send_router_solicitation () memcpy( &(headers.icmp6h->icmp6body.router_solicit.lladdr.mac), get_mac_address(), 6); - send_ip (headers.ip6h, sizeof(struct ip6hdr) + + send_ip (fd, headers.ip6h, sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE + sizeof(struct router_solicitation)); } @@ -194,10 +194,11 @@ int is_ra_received(void) /** * NET: * + * @param fd socket fd * @param ip6_addr_t *dest_ip6 */ void -send_neighbour_solicitation (ip6_addr_t *dest_ip6) +send_neighbour_solicitation (int fd, ip6_addr_t *dest_ip6) { ip6_addr_t snma; @@ -233,7 +234,7 @@ send_neighbour_solicitation (ip6_addr_t *dest_ip6) memcpy( &(headers.icmp6h->icmp6body.nghb_solicit.lladdr.mac), get_mac_address(), 6); - send_ip (ether_packet + sizeof(struct ethhdr), + send_ip (fd, ether_packet + sizeof(struct ethhdr), sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE + sizeof(struct neighbour_solicitation)); } @@ -241,12 +242,13 @@ send_neighbour_solicitation (ip6_addr_t *dest_ip6) /** * NET: * + * @param fd socket fd * @param ip6_packet pointer to an IPv6 packet * @param icmp6hdr pointer to the icmp6 header in ip6_packet * @param na_flags Neighbour advertisment flags */ static void -send_neighbour_advertisement (struct neighbor *target) +send_neighbour_advertisement (int fd, struct neighbor *target) { struct na_flags na_adv_flags; uint8_t ether_packet[ETH_MTU_SIZE]; @@ -297,7 +299,7 @@ send_neighbour_advertisement (struct neighbor *target) memcpy( &(headers.icmp6h->icmp6body.nghb_adv.lladdr.mac), get_mac_address(), 6); - send_ip (ether_packet + sizeof(struct ethhdr), + send_ip (fd, ether_packet + sizeof(struct ethhdr), sizeof(struct ip6hdr) + ICMPv6_HEADER_SIZE + sizeof(struct neighbour_advertisement)); } @@ -305,10 +307,11 @@ send_neighbour_advertisement (struct neighbor *target) /** * NET: * + * @param fd socket fd * @param ip6_packet pointer to an IPv6 packet */ static int8_t -handle_na (uint8_t *packet) +handle_na (int fd, uint8_t *packet) { struct neighbor *n = NULL; struct packeth headers; @@ -338,7 +341,7 @@ handle_na (uint8_t *packet) if (n->eth_len > 0) { struct ethhdr * ethh = (struct ethhdr *) &(n->eth_frame); memcpy(ethh->dest_mac, &(n->mac), 6); - send_ether (&(n->eth_frame), n->eth_len + sizeof(struct ethhdr)); + send_ether (fd, &(n->eth_frame), n->eth_len + sizeof(struct ethhdr)); n->eth_len = 0; } } @@ -349,11 +352,12 @@ handle_na (uint8_t *packet) /** * NET: Handles ICMPv6 messages * + * @param fd socket fd * @param ip6_packet pointer to an IPv6 packet * @param packetsize size of ipv6_packet */ int8_t -handle_icmpv6 (struct ethhdr *etherhdr, +handle_icmpv6 (int fd, struct ethhdr *etherhdr, uint8_t *ip6_packet) { @@ -371,10 +375,10 @@ handle_icmpv6 (struct ethhdr *etherhdr, /* process ICMPv6 types */ switch(received_icmp6->type) { case ICMPV6_NEIGHBOUR_SOLICITATION: - send_neighbour_advertisement( &target ); + send_neighbour_advertisement(fd, &target); break; case ICMPV6_NEIGHBOUR_ADVERTISEMENT: - handle_na((uint8_t *) ip6_packet - sizeof(struct ethhdr)); + handle_na(fd, (uint8_t *) ip6_packet - sizeof(struct ethhdr)); break; case ICMPV6_ROUTER_ADVERTISEMENT: handle_ra(received_icmp6, (uint8_t *) received_ip6); diff --git a/clients/net-snk/app/netlib/icmpv6.h b/clients/net-snk/app/netlib/icmpv6.h index 280df1f..3279797 100644 --- a/clients/net-snk/app/netlib/icmpv6.h +++ b/clients/net-snk/app/netlib/icmpv6.h @@ -47,9 +47,9 @@ #define ICMPV6_REDIRECT_MSG 137 /* Redirect message */ /******** Functions *******************/ -int8_t handle_icmpv6 (struct ethhdr *etherhdr, uint8_t *ip6_packet); -void send_neighbour_solicitation( ip6_addr_t *target_ip6); -void send_router_solicitation(void); +int8_t handle_icmpv6 (int fd, struct ethhdr *etherhdr, uint8_t *ip6_packet); +void send_neighbour_solicitation(int fd, ip6_addr_t *target_ip6); +void send_router_solicitation(int fd); int is_ra_received(void); /* Prefix information */ diff --git a/clients/net-snk/app/netlib/ipv4.c b/clients/net-snk/app/netlib/ipv4.c index 83293e8..1d5dd73 100644 --- a/clients/net-snk/app/netlib/ipv4.c +++ b/clients/net-snk/app/netlib/ipv4.c @@ -87,10 +87,10 @@ static unsigned short checksum(unsigned short *packet, int words); static void -arp_send_request(uint32_t dest_ip); +arp_send_request(int fd, uint32_t dest_ip); static void -arp_send_reply(uint32_t src_ip, uint8_t * src_mac); +arp_send_reply(int fd, uint32_t src_ip, uint8_t * src_mac); static void fill_arphdr(uint8_t * packet, uint8_t opcode, @@ -104,7 +104,7 @@ static void fill_udp_checksum(struct iphdr *ipv4_hdr); static int8_t -handle_icmp(struct iphdr * iph, uint8_t * packet, int32_t packetsize); +handle_icmp(int fd, struct iphdr * iph, uint8_t * packet, int32_t packetsize); /*>>>>>>>>>>>>>>>>>>>>>>>>>>>>> LOCAL VARIABLES <<<<<<<<<<<<<<<<<<<<<<<<<*/ @@ -129,7 +129,7 @@ static arp_entry_t arp_table[ARP_ENTRIES]; static arp_entry_t pending_pkt; /* Function pointer send_ip. Points either to send_ipv4() or send_ipv6() */ -int (*send_ip) (void *, int); +int (*send_ip) (int fd, void *, int); /*>>>>>>>>>>>>>>>>>>>>>>>>>>>> IMPLEMENTATION <<<<<<<<<<<<<<<<<<<<<<<<<<<*/ @@ -300,6 +300,7 @@ fill_iphdr(uint8_t * packet, uint16_t packetsize, /** * IPv4: Handles IPv4-packets according to Receive-handle diagram. * + * @param fd socket fd * @param ip_packet IP-packet to be handled * @param packetsize Length of the packet * @return ZERO - packet handled successfully; @@ -308,7 +309,7 @@ fill_iphdr(uint8_t * packet, uint16_t packetsize, * @see iphdr */ int8_t -handle_ipv4(uint8_t * ip_packet, int32_t packetsize) +handle_ipv4(int fd, uint8_t * ip_packet, int32_t packetsize) { struct iphdr * iph; int32_t old_sum; @@ -377,10 +378,10 @@ handle_ipv4(uint8_t * ip_packet, int32_t packetsize) switch (iph -> ip_p) { case IPTYPE_ICMP: - return handle_icmp(iph, ip_packet + sizeof(struct iphdr), + return handle_icmp(fd, iph, ip_packet + sizeof(struct iphdr), iph -> ip_len - sizeof(struct iphdr)); case IPTYPE_UDP: - return handle_udp(ip_packet + sizeof(struct iphdr), + return handle_udp(fd, ip_packet + sizeof(struct iphdr), iph -> ip_len - sizeof(struct iphdr)); case IPTYPE_TCP: return handle_tcp(ip_packet + sizeof(struct iphdr), @@ -409,6 +410,7 @@ handle_ipv4(uint8_t * ip_packet, int32_t packetsize) * If there is already an ARP request pending, then we drop this packet * and send again an ARP request. * + * @param fd socket fd * @param ip_packet IP-packet to be handled * @param packetsize Length of the packet * @return -2 - packet dropped (MAC address not resolved - ARP request pending) @@ -421,7 +423,7 @@ handle_ipv4(uint8_t * ip_packet, int32_t packetsize) * @see iphdr */ int -send_ipv4(void* buffer, int len) +send_ipv4(int fd, void* buffer, int len) { arp_entry_t *arp_entry = 0; struct iphdr *ip; @@ -482,7 +484,7 @@ send_ipv4(void* buffer, int len) // If we could not resolv the MAC address by our own... if(!mac_addr) { // send the ARP request - arp_send_request(ip->ip_dst); + arp_send_request(fd, ip->ip_dst); // drop the current packet if there is already a ARP request pending if(arp_entry) @@ -510,7 +512,7 @@ send_ipv4(void* buffer, int len) set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); if (!arp_entry->eth_len) break; } while (get_timer() > 0); @@ -522,7 +524,7 @@ send_ipv4(void* buffer, int len) fill_ethhdr(arp_entry->eth_frame, htons(ETHERTYPE_IP), get_mac_address(), mac_addr); memcpy(&arp_entry->eth_frame[sizeof(struct ethhdr)], buffer, len); - return send_ether(arp_entry->eth_frame, len + sizeof(struct ethhdr)); + return send_ether(fd, arp_entry->eth_frame, len + sizeof(struct ethhdr)); } /** @@ -609,10 +611,11 @@ lookup_mac_addr(uint32_t ipv4_addr) * ARP: Sends an ARP-request package. * For given IPv4 retrieves MAC via ARP (makes several attempts) * + * @param fd socket fd * @param dest_ip IP of the host which MAC should be obtained */ static void -arp_send_request(uint32_t dest_ip) +arp_send_request(int fd, uint32_t dest_ip) { arp_entry_t *arp_entry = &arp_table[arp_producer]; @@ -622,7 +625,7 @@ arp_send_request(uint32_t dest_ip) fill_ethhdr(arp_entry->eth_frame, ETHERTYPE_ARP, get_mac_address(), broadcast_mac); - send_ether(arp_entry->eth_frame, + send_ether(fd, arp_entry->eth_frame, sizeof(struct ethhdr) + sizeof(struct arphdr)); } @@ -631,11 +634,12 @@ arp_send_request(uint32_t dest_ip) * This package is used to serve foreign requests (in case IP in * foreign request matches our host IP). * + * @param fd socket fd * @param src_ip requester IP address (foreign IP) * @param src_mac requester MAC address (foreign MAC) */ static void -arp_send_reply(uint32_t src_ip, uint8_t * src_mac) +arp_send_reply(int fd, uint32_t src_ip, uint8_t * src_mac) { arp_entry_t *arp_entry = &arp_table[arp_producer]; @@ -645,7 +649,7 @@ arp_send_reply(uint32_t src_ip, uint8_t * src_mac) fill_arphdr(&arp_entry->eth_frame[sizeof(struct ethhdr)], ARP_REPLY, get_mac_address(), own_ip, src_mac, src_ip); - send_ether(arp_entry->eth_frame, + send_ether(fd, arp_entry->eth_frame, sizeof(struct ethhdr) + sizeof(struct arphdr)); } @@ -689,6 +693,7 @@ fill_arphdr(uint8_t * packet, uint8_t opcode, * ARP: Handles ARP-messages according to Receive-handle diagram. * Updates arp_table for outstanding ARP requests (see arp_getmac). * + * @param fd socket fd * @param packet ARP-packet to be handled * @param packetsize length of the packet * @return ZERO - packet handled successfully; @@ -698,7 +703,7 @@ fill_arphdr(uint8_t * packet, uint8_t opcode, * @see arphdr */ int8_t -handle_arp(uint8_t * packet, int32_t packetsize) +handle_arp(int fd, uint8_t * packet, int32_t packetsize) { struct arphdr * arph = (struct arphdr *) packet; @@ -715,7 +720,7 @@ handle_arp(uint8_t * packet, int32_t packetsize) case ARP_REQUEST: // foreign request if(own_ip != 0) - arp_send_reply(htonl(arph->src_ip), arph -> src_mac); + arp_send_reply(fd, htonl(arph->src_ip), arph -> src_mac); return 0; // no error case ARP_REPLY: { unsigned int i; @@ -748,7 +753,7 @@ handle_arp(uint8_t * packet, int32_t packetsize) struct ethhdr * ethh = (struct ethhdr *) pending_pkt.eth_frame; memcpy(ethh -> dest_mac, arp_table[i].mac_addr, 6); - send_ether(pending_pkt.eth_frame, pending_pkt.eth_len); + send_ether(fd, pending_pkt.eth_frame, pending_pkt.eth_len); pending_pkt.pkt_pending = 0; arp_table[i].eth_len = 0; } @@ -768,10 +773,11 @@ handle_arp(uint8_t * packet, int32_t packetsize) * In other words, reading a value of 0 form this variable * means that an answer to the request has been arrived. * + * @param fd socket descriptor * @param _ping_dst_ip destination IPv4 address */ void -ping_ipv4(uint32_t _ping_dst_ip) +ping_ipv4(int fd, uint32_t _ping_dst_ip) { unsigned char packet[sizeof(struct iphdr) + sizeof(struct icmphdr)]; struct icmphdr *icmp; @@ -794,7 +800,7 @@ ping_ipv4(uint32_t _ping_dst_ip) icmp->checksum = checksum((unsigned short *) icmp, sizeof(struct icmphdr) >> 1); - send_ipv4(packet, sizeof(struct iphdr) + sizeof(struct icmphdr)); + send_ipv4(fd, packet, sizeof(struct iphdr) + sizeof(struct icmphdr)); } /** @@ -813,6 +819,7 @@ pong_ipv4(void) /** * ICMP: Handles ICMP-packets according to Receive-handle diagram. * + * @param fd socket fd * @param icmp_packet ICMP-packet to be handled * @param packetsize Length of the packet * @return ZERO - packet handled successfully; @@ -820,7 +827,7 @@ pong_ipv4(void) * @see handle_ipv4 */ static int8_t -handle_icmp(struct iphdr * iph, uint8_t * packet, int32_t packetsize) +handle_icmp(int fd, struct iphdr * iph, uint8_t * packet, int32_t packetsize) { struct icmphdr *icmp = (struct icmphdr *) packet; @@ -873,7 +880,7 @@ handle_icmp(struct iphdr * iph, uint8_t * packet, int32_t packetsize) reply_icmph->checksum = checksum((unsigned short *) reply_icmph, sizeof(struct icmphdr) >> 1); - send_ipv4(reply_packet, sizeof(struct iphdr) + packetsize); + send_ipv4(fd, reply_packet, sizeof(struct iphdr) + packetsize); break; } case ICMP_TIME_EXCEEDED: diff --git a/clients/net-snk/app/netlib/ipv4.h b/clients/net-snk/app/netlib/ipv4.h index fd104cf..eb719f8 100644 --- a/clients/net-snk/app/netlib/ipv4.h +++ b/clients/net-snk/app/netlib/ipv4.h @@ -70,7 +70,7 @@ extern uint32_t get_ipv4_router(void); extern void set_ipv4_netmask(uint32_t subnet_mask); extern uint32_t get_ipv4_netmask(void); -extern int (*send_ip) (void *, int); +extern int (*send_ip) (int fd, void *, int); /* fills ip header */ extern void fill_iphdr(uint8_t * packet, uint16_t packetsize, @@ -79,18 +79,18 @@ extern void fill_iphdr(uint8_t * packet, uint16_t packetsize, /* Send a IPv4 packet. Adding the Ethernet-Header and resolving the * MAC address is done transparent in the background if necessary. */ -extern int send_ipv4(void* buffer, int len); +extern int send_ipv4(int fd, void* buffer, int len); /* Sends an ICMP Echo request to destination IPv4 address */ -extern void ping_ipv4(uint32_t _ping_dst_ip); +extern void ping_ipv4(int fd, uint32_t _ping_dst_ip); /* Returns host IPv4 address that we are waiting for a response */ extern uint32_t pong_ipv4(void); /* Handles IPv4-packets that are detected by receive_ether. */ -extern int8_t handle_ipv4(uint8_t * packet, int32_t packetsize); +extern int8_t handle_ipv4(int fd, uint8_t * packet, int32_t packetsize); /* Handles ARP-packets that are detected by receive_ether. */ -extern int8_t handle_arp(uint8_t * packet, int32_t packetsize); +extern int8_t handle_arp(int fd, uint8_t * packet, int32_t packetsize); #endif diff --git a/clients/net-snk/app/netlib/ipv6.c b/clients/net-snk/app/netlib/ipv6.c index 109598b..0cb0a2e 100644 --- a/clients/net-snk/app/netlib/ipv6.c +++ b/clients/net-snk/app/netlib/ipv6.c @@ -32,7 +32,7 @@ /****************************** PROTOTYPES *******************************/ int8_t ip6addr_add (struct ip6addr_list_entry *new_address); -static void ipv6_init(void); +static void ipv6_init(int fd); static int ip6_is_multicast (ip6_addr_t * ip); /****************************** LOCAL VARIABLES **************************/ @@ -52,12 +52,12 @@ static uint8_t null_mac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /** * IPv6: Set the own IPv6 address. * + * @param fd Socket descriptor * @param _own_ip client IPv6 address (e.g. ::1) */ void -set_ipv6_address (ip6_addr_t *_own_ip6) +set_ipv6_address (int fd, ip6_addr_t *_own_ip6) { - own_ip6 = malloc (sizeof(struct ip6addr_list_entry)); /* If no address was passed as a parameter generate a link-local @@ -72,7 +72,7 @@ set_ipv6_address (ip6_addr_t *_own_ip6) /* Add to our list of IPv6 addresses */ ip6addr_add (own_ip6); - ipv6_init(); + ipv6_init(fd); } /** @@ -110,6 +110,7 @@ find_ip6addr (ip6_addr_t *ip) /** * NET: Handles IPv6-packets * + * @param fd - Socket descriptor * @param ip6_packet - Pointer to IPv6 header * @param packetsize - Size of Ipv6 packet * @return ERROR - -1 if packet is too small or unknown protocol @@ -119,7 +120,7 @@ find_ip6addr (ip6_addr_t *ip) * @see ip6hdr */ int8_t -handle_ipv6 (uint8_t * ip6_packet, int32_t packetsize) +handle_ipv6 (int fd, uint8_t * ip6_packet, int32_t packetsize) { struct ip6hdr *ip6 = NULL; @@ -134,10 +135,10 @@ handle_ipv6 (uint8_t * ip6_packet, int32_t packetsize) switch (ip6->nh) { case IPTYPE_UDP: - return handle_udp (ip6_packet + sizeof (struct ip6hdr), + return handle_udp (fd, ip6_packet + sizeof (struct ip6hdr), ip6->pl); case IPTYPE_ICMPV6: - return handle_icmpv6 ((struct ethhdr *) ip6_packet - sizeof(struct ethhdr), + return handle_icmpv6 (fd, (struct ethhdr *) ip6_packet - sizeof(struct ethhdr), ip6_packet); } @@ -329,9 +330,10 @@ ip6addr_add (struct ip6addr_list_entry *new_address) /** * NET: Initialize IPv6 * + * @param fd socket fd */ static void -ipv6_init () +ipv6_init (int fd) { int i = 0; @@ -363,11 +365,11 @@ ipv6_init () first_neighbor = NULL; last_neighbor = first_neighbor; - send_router_solicitation (); + send_router_solicitation (fd); for(i=0; i < 4 && !is_ra_received(); i++) { set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); if (is_ra_received()) break; } while (get_timer() > 0); @@ -467,6 +469,7 @@ ip6_checksum (struct ip6hdr *ip6h, unsigned short *packet, int words) /** * NET: Handles IPv6-packets * + * @param fd socket fd * @param ip6_packet Pointer to IPv6 header in packet * @param packetsize Size of IPv6 packet * @return -1 == ERRROR @@ -476,7 +479,7 @@ ip6_checksum (struct ip6hdr *ip6h, unsigned short *packet, int words) * @see ip6hdr */ int -send_ipv6 (void* buffer, int len) +send_ipv6 (int fd, void* buffer, int len) { struct neighbor *n; struct ip6hdr *ip6h; @@ -544,7 +547,7 @@ send_ipv6 (void* buffer, int len) if (! memcmp (mac_addr, &null_mac, 6)) { if (n->eth_len == 0) { - send_neighbour_solicitation (&ip_dst); + send_neighbour_solicitation (fd, &ip_dst); // Store the packet until we know the MAC address memset(n->eth_frame, 0, 1500); @@ -557,7 +560,7 @@ send_ipv6 (void* buffer, int len) n->eth_len = len; set_timer(TICKS_SEC); do { - receive_ether(); + receive_ether(fd); } while (get_timer() > 0); } } @@ -566,7 +569,7 @@ send_ipv6 (void* buffer, int len) fill_ethhdr (n->eth_frame, htons(ETHERTYPE_IPv6), get_mac_address(), mac_addr); memcpy (&(n->eth_frame[sizeof(struct ethhdr)]), buffer, len); - return send_ether (n->eth_frame, len + sizeof(struct ethhdr)); + return send_ether (fd, n->eth_frame, len + sizeof(struct ethhdr)); } static int diff --git a/clients/net-snk/app/netlib/ipv6.h b/clients/net-snk/app/netlib/ipv6.h index 82f2cb0..b496364 100644 --- a/clients/net-snk/app/netlib/ipv6.h +++ b/clients/net-snk/app/netlib/ipv6.h @@ -130,7 +130,7 @@ struct ip6_config { /******************** VARIABLES **********************************************/ /* Function pointer send_ip. Points either to send_ipv4() or send_ipv6() */ -extern int (*send_ip) (void *, int); +extern int (*send_ip) (int fd, void *, int); /* IPv6 link-local multicast addresses */ struct ip6addr_list_entry all_routers_ll; // Routers @@ -151,14 +151,14 @@ struct router *last_router; /******************** FUNCTIONS *********************************************/ /* Handles IPv6-packets that are detected by receive_ether. */ -int8_t handle_ipv6(uint8_t * ip6_packet, int32_t packetsize); +int8_t handle_ipv6(int fd, uint8_t * ip6_packet, int32_t packetsize); /* Fill IPv6 header */ void fill_ip6hdr(uint8_t * packet, uint16_t packetsize, uint8_t ip_proto, ip6_addr_t *ip6_src, ip6_addr_t *ip6_dst); /* Set own IPv6 address */ -void set_ipv6_address(ip6_addr_t *own_ip6); +void set_ipv6_address(int fd, ip6_addr_t *own_ip6); /* Get own IPv6 address */ ip6_addr_t *get_ipv6_address(void); @@ -183,7 +183,7 @@ int8_t ip6_cmp( ip6_addr_t *ip_1, ip6_addr_t *ip_2 ); int8_t unknown_prefix (ip6_addr_t *ip); /* Send IPv6 packet */ -int send_ipv6 (void* buffer, int len); +int send_ipv6 (int fd, void* buffer, int len); /* Add IPv6 address to list */ int8_t ip6addr_add (struct ip6addr_list_entry *new_address); diff --git a/clients/net-snk/app/netlib/tftp.c b/clients/net-snk/app/netlib/tftp.c index 8a39a15..0a7c0ec 100644 --- a/clients/net-snk/app/netlib/tftp.c +++ b/clients/net-snk/app/netlib/tftp.c @@ -86,9 +86,11 @@ dump_package(unsigned char *buffer, unsigned int len) /** * send_rrq - Sends a read request package. + * + * @fd: Socket Descriptor */ static void -send_rrq(void) +send_rrq(int fd) { int ip_len = 0; int ip6_payload_len = 0; @@ -142,7 +144,7 @@ send_rrq(void) ptr += strlen("blksize") + 1; memcpy(ptr, blocksize_str, strlen(blocksize_str) + 1); - send_ip (packet, ip_len); + send_ip (fd, packet, ip_len); #ifdef __DEBUG__ printf("tftp RRQ with %d bytes transmitted.\n", ip_len); @@ -157,7 +159,7 @@ send_rrq(void) * @dport: UDP destination port */ static void -send_ack(int blckno, unsigned short dport) +send_ack(int fd, int blckno, unsigned short dport) { int ip_len = 0; int ip6_payload_len = 0; @@ -192,7 +194,7 @@ send_ack(int blckno, unsigned short dport) tftp->th_opcode = htons(ACK); tftp->th_data = htons(blckno); - send_ip(packet, ip_len); + send_ip(fd, packet, ip_len); #ifdef __DEBUG__ printf("tftp ACK %d bytes transmitted.\n", ip_len); @@ -204,11 +206,12 @@ send_ack(int blckno, unsigned short dport) /** * send_error - Sends an error package. * + * @fd: Socket Descriptor * @error_code: Used sub code for error packet * @dport: UDP destination port */ static void -send_error(int error_code, unsigned short dport) +send_error(int fd, int error_code, unsigned short dport) { int ip_len = 0; int ip6_payload_len = 0; @@ -244,7 +247,7 @@ send_error(int error_code, unsigned short dport) tftp->th_data = htons(error_code); ((char *) &tftp->th_data)[2] = 0; - send_ip(packet, ip_len); + send_ip(fd, packet, ip_len); #ifdef __DEBUG__ printf("tftp ERROR %d bytes transmitted.\n", ip_len); @@ -330,13 +333,14 @@ get_blksize(unsigned char *buffer, unsigned int len) * I for an ICMP packet * #+* for different unexpected TFTP packets (not very good) * + * @param fd socket descriptor * @param packet points to the UDP header of the packet * @param len the length of the network packet * @return ZERO if packet was handled successfully * ERRORCODE if error occurred */ int32_t -handle_tftp(uint8_t *pkt, int32_t packetsize) +handle_tftp(int fd, uint8_t *pkt, int32_t packetsize) { struct udphdr *udph; struct tftphdr *tftp; @@ -361,17 +365,17 @@ handle_tftp(uint8_t *pkt, int32_t packetsize) /* an OACK means that the server answers our blocksize request */ blocksize = get_blksize(pkt, packetsize); if (!blocksize || blocksize > MAX_BLOCKSIZE) { - send_error(8, port_number); + send_error(fd, 8, port_number); tftp_errno = -8; goto error; } - send_ack(0, port_number); + send_ack(fd, 0, port_number); } else if (tftp->th_opcode == htons(ACK)) { /* an ACK means that the server did not answers * our blocksize request, therefore we will set the blocksize * to the default value of 512 */ blocksize = 512; - send_ack(0, port_number); + send_ack(fd, 0, port_number); } else if ((unsigned char) tftp->th_opcode == ERROR) { #ifdef __DEBUG__ printf("tftp->th_opcode : %x\n", tftp->th_opcode); @@ -413,7 +417,7 @@ handle_tftp(uint8_t *pkt, int32_t packetsize) tftp->th_data, block + 1); printf("\b+ "); #endif - send_ack(tftp->th_data, port_number); + send_ack(fd, tftp->th_data, port_number); lost_packets++; tftp_err->bad_tftp_packets++; return 0; @@ -444,7 +448,7 @@ handle_tftp(uint8_t *pkt, int32_t packetsize) } memcpy(buffer + received_len, &tftp->th_data + 1, udph->uh_ulen - 12); - send_ack(tftp->th_data, port_number); + send_ack(fd, tftp->th_data, port_number); received_len += udph->uh_ulen - 12; /* Last packet reached if the payload of the UDP packet * is smaller than blocksize + 12 @@ -539,7 +543,7 @@ tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len, buffer = _buffer; set_timer(TICKS_SEC); - send_rrq(); + send_rrq(fn_ip->fd); while (! tftp_finished) { /* if timeout (no packet received) */ @@ -547,19 +551,19 @@ tftp(filename_ip_t * _fn_ip, unsigned char *_buffer, int _len, /* the server doesn't seem to retry let's help out a bit */ if (tftp_err->no_packets > 4 && port_number != -1 && block > 1) { - send_ack(block, port_number); + send_ack(fn_ip->fd, block, port_number); } else if (port_number == -1 && block == 0 && (tftp_err->no_packets&3) == 3) { printf("\nRepeating TFTP read request...\n"); - send_rrq(); + send_rrq(fn_ip->fd); } tftp_err->no_packets++; set_timer(TICKS_SEC); } /* handle received packets */ - receive_ether(); + receive_ether(fn_ip->fd); /* bad_tftp_packets are counted whenever we receive a TFTP packet * which was not expected; if this gets larger than 'retries' diff --git a/clients/net-snk/app/netlib/tftp.h b/clients/net-snk/app/netlib/tftp.h index a5ae948..1cf1266 100644 --- a/clients/net-snk/app/netlib/tftp.h +++ b/clients/net-snk/app/netlib/tftp.h @@ -29,6 +29,7 @@ typedef struct { ip6_addr_t server_ip6; ip6_addr_t dns_ip6; int8_t filename[256]; + int fd; } __attribute__ ((packed)) filename_ip_t ; typedef struct { @@ -43,8 +44,8 @@ int tftp(filename_ip_t *, unsigned char *, int, unsigned int, int tftp_netsave(filename_ip_t *, uint8_t * buffer, int len, int use_ci, unsigned int retries, tftp_err_t * tftp_err); -int32_t handle_tftp(uint8_t *, int32_t); +int32_t handle_tftp(int fd, uint8_t *, int32_t); void handle_tftp_dun(uint8_t err_code); -int parse_tftp_args(char buffer[], char *server_ip, char filename[], int len); +int parse_tftp_args(char buffer[], char *server_ip, char filename[], int fd, int len); #endif diff --git a/clients/net-snk/app/netlib/udp.c b/clients/net-snk/app/netlib/udp.c index 134a29e..db29bc9 100644 --- a/clients/net-snk/app/netlib/udp.c +++ b/clients/net-snk/app/netlib/udp.c @@ -57,7 +57,7 @@ void net_set_mtftp_port(uint16_t tftp_port) { * @see udphdr */ int8_t -handle_udp(uint8_t * udp_packet, int32_t packetsize) { +handle_udp(int fd, uint8_t * udp_packet, int32_t packetsize) { struct udphdr * udph = (struct udphdr *) udp_packet; if (packetsize < sizeof(struct udphdr)) @@ -66,7 +66,7 @@ handle_udp(uint8_t * udp_packet, int32_t packetsize) { switch (htons(udph -> uh_dport)) { case UDPPORT_BOOTPC: if (udph -> uh_sport == htons(UDPPORT_BOOTPS)) - return handle_dhcp(udp_packet + sizeof(struct udphdr), + return handle_dhcp(fd, udp_packet + sizeof(struct udphdr), packetsize - sizeof(struct udphdr)); else return -1; @@ -81,18 +81,18 @@ handle_udp(uint8_t * udp_packet, int32_t packetsize) { packetsize - sizeof(struct udphdr)); case UDPPORT_TFTPC: #ifdef USE_MTFTP - return handle_tftp(udp_packet + sizeof(struct udphdr), + return handle_tftp(fd, udp_packet + sizeof(struct udphdr), packetsize - sizeof(struct udphdr)); #else - return handle_tftp(udp_packet, packetsize); + return handle_tftp(fd, udp_packet, packetsize); #endif default: #ifdef USE_MTFTP if (htons(udph -> uh_dport) == net_tftp_uport) - return handle_tftp(udp_packet + sizeof(struct udphdr), + return handle_tftp(fd, udp_packet + sizeof(struct udphdr), packetsize - sizeof(struct udphdr)); else if (htons(udph -> uh_dport) == net_mtftp_uport) - return handle_tftp(udp_packet + sizeof(struct udphdr), + return handle_tftp(fd, udp_packet + sizeof(struct udphdr), packetsize - sizeof(struct udphdr)); #endif return -1; diff --git a/clients/net-snk/app/netlib/udp.h b/clients/net-snk/app/netlib/udp.h index 8acc0bc..1ba9332 100644 --- a/clients/net-snk/app/netlib/udp.h +++ b/clients/net-snk/app/netlib/udp.h @@ -40,7 +40,7 @@ typedef int32_t *(*handle_upper_udp_t)(uint8_t *, int32_t); typedef void *(*handle_upper_udp_dun_t)(uint8_t); /* Handles UDP-packets that are detected by any network layer. */ -extern int8_t handle_udp(uint8_t * udp_packet, int32_t packetsize); +extern int8_t handle_udp(int fd, uint8_t * udp_packet, int32_t packetsize); /* Handles UDP related ICMP-Dest.Unreachable packets that are detected by * the network layers. */ |