Subversion Repositories HelenOS

Rev

Rev 4726 | Rev 4738 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4726 mejdrech 1
/*
2
 * Copyright (c) 2008 Lukas Mejdrech
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
/** @addtogroup net_tl
30
 *  @{
31
 */
32
 
33
/** @file
34
 *  Transport layer common functions implementation.
35
 *  @see tl_common.h
36
 */
37
 
38
#include <async.h>
39
#include <ipc/services.h>
40
 
41
#include "../err.h"
42
 
43
#include "../structures/packet/packet.h"
44
#include "../structures/packet/packet_client.h"
45
 
46
#include "../include/icmp_interface.h"
47
#include "../include/in.h"
48
#include "../include/in6.h"
49
#include "../include/inet.h"
50
#include "../include/socket_codes.h"
51
#include "../include/socket_errno.h"
52
 
53
#include "tl_common.h"
54
 
55
int tl_get_address_port( const struct sockaddr * addr, int addrlen, uint16_t * port ){
56
    const struct sockaddr_in *  address_in;
57
    const struct sockaddr_in6 * address_in6;
58
 
59
    if( addrlen < sizeof( struct sockaddr )) return EINVAL;
60
    switch( addr->sa_family ){
61
        case AF_INET:
62
            if( addrlen != sizeof( struct sockaddr_in )) return EINVAL;
63
            address_in = ( struct sockaddr_in * ) addr;
64
            * port = address_in->sin_port;
65
            break;
66
        case AF_INET6:
67
            if( addrlen != sizeof( struct sockaddr_in6 )) return EINVAL;
68
            address_in6 = ( struct sockaddr_in6 * ) addr;
69
            * port = address_in6->sin6_port;
70
            break;
71
        default:
72
            return EAFNOSUPPORT;
73
    }
74
    return EOK;
75
}
76
 
77
 
78
int tl_set_address_port( struct sockaddr * addr, int addrlen, uint16_t port ){
79
    struct sockaddr_in *    address_in;
80
    struct sockaddr_in6 *   address_in6;
81
    size_t                  length;
82
 
83
    if( addrlen < 0 ) return EINVAL;
84
    length = ( size_t ) addrlen;
85
    if( length < sizeof( struct sockaddr )) return EINVAL;
86
    switch( addr->sa_family ){
87
        case AF_INET:
88
            if( length != sizeof( struct sockaddr_in )) return EINVAL;
89
            address_in = ( struct sockaddr_in * ) addr;
90
            address_in->sin_port = port;
91
            return EOK;
92
        case AF_INET6:
93
            if( length != sizeof( struct sockaddr_in6 )) return EINVAL;
94
            address_in6 = ( struct sockaddr_in6 * ) addr;
95
            address_in6->sin6_port = port;
96
            return EOK;
97
        default:
98
            return EAFNOSUPPORT;
99
    }
100
}
101
 
4731 mejdrech 102
int tl_prepare_icmp_packet( int packet_phone, int icmp_phone, packet_t packet, services_t error ){
4726 mejdrech 103
    packet_t    next;
104
    uint8_t *   src;
105
    int         length;
106
 
107
    // detach the first packet and release the others
108
    next = pq_detach( packet );
109
    if( next ){
110
        pq_release( packet_phone, packet_get_id( next ));
111
    }
112
    length = packet_get_addr( packet, & src, NULL );
113
    if(( length > 0 )
114
    && ( ! error )
115
    && ( icmp_phone >= 0 )
116
    // set both addresses to the source one (avoids the source address deletion before setting the destination one)
117
    && ( packet_set_addr( packet, src, src, ( size_t ) length ) == EOK )){
4731 mejdrech 118
        return EOK;
4726 mejdrech 119
    }else{
120
        pq_release( packet_phone, packet_get_id( packet ));
121
    }
4731 mejdrech 122
    return ENOENT;
4726 mejdrech 123
}
124
 
125
int tl_socket_read_packet_data( int packet_phone, packet_ref packet, size_t prefix, const packet_dimension_ref dimension, const struct sockaddr * addr, socklen_t addrlen ){
126
    ERROR_DECLARE;
127
 
128
    ipc_callid_t    callid;
129
    size_t          length;
130
    void *          data;
131
 
132
    if( ! dimension ) return EINVAL;
133
    // get the data length
134
    if( ! ipc_data_write_receive( & callid, & length )) return EINVAL;
135
    // get a new packet
136
    * packet = packet_get_4( packet_phone, length, dimension->addr_len, prefix + dimension->prefix, dimension->suffix );
137
    if( ! packet ) return ENOMEM;
138
    // allocate space in the packet
139
    data = packet_suffix( * packet, length );
140
    if( ! data ){
141
        pq_release( packet_phone, packet_get_id( * packet ));
142
        return ENOMEM;
143
    }
144
    // read the data into the packet
145
    if( ERROR_OCCURRED( ipc_data_write_finalize( callid, data, length ))
146
    // set the packet destination address
147
    || ERROR_OCCURRED( packet_set_addr( * packet, NULL, ( uint8_t * ) addr, addrlen ))){
148
        pq_release( packet_phone, packet_get_id( * packet ));
149
        return ERROR_CODE;
150
    }
151
    return ( int ) length;
152
}
153
 
154
/** @}
155
 */