Changes in uspace/srv/net/inetsrv/pdu.c [a1a101d:a2e3ee6] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/inetsrv/pdu.c
ra1a101d ra2e3ee6 106 106 void **rdata, size_t *rsize, size_t *roffs) 107 107 { 108 void *data; 109 size_t size; 110 ip_header_t *hdr; 111 size_t hdr_size; 112 size_t data_offs; 113 uint16_t chksum; 114 uint16_t ident; 115 uint16_t flags_foff; 116 uint16_t foff; 117 size_t fragoff_limit; 118 size_t xfer_size; 119 size_t spc_avail; 120 size_t rem_offs; 121 108 uint32_t src_addr; 109 int rc = inet_addr_pack(&packet->src, &src_addr); 110 if (rc != EOK) 111 return rc; 112 113 uint32_t dest_addr; 114 rc = inet_addr_pack(&packet->dest, &dest_addr); 115 if (rc != EOK) 116 return rc; 117 122 118 /* Upper bound for fragment offset field */ 123 fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l);124 119 size_t fragoff_limit = 1 << (FF_FRAGOFF_h - FF_FRAGOFF_l); 120 125 121 /* Verify that total size of datagram is within reasonable bounds */ 126 122 if (offs + packet->size > FRAG_OFFS_UNIT * fragoff_limit) 127 123 return ELIMIT; 128 129 hdr_size = sizeof(ip_header_t);130 data_offs = ROUND_UP(hdr_size, 4);131 124 125 size_t hdr_size = sizeof(ip_header_t); 126 size_t data_offs = ROUND_UP(hdr_size, 4); 127 132 128 assert(offs % FRAG_OFFS_UNIT == 0); 133 129 assert(offs / FRAG_OFFS_UNIT < fragoff_limit); 134 130 135 131 /* Value for the fragment offset field */ 136 foff = offs / FRAG_OFFS_UNIT;137 132 uint16_t foff = offs / FRAG_OFFS_UNIT; 133 138 134 if (hdr_size >= mtu) 139 135 return EINVAL; 140 136 141 137 /* Amount of space in the PDU available for payload */ 142 s pc_avail = mtu - hdr_size;138 size_t spc_avail = mtu - hdr_size; 143 139 spc_avail -= (spc_avail % FRAG_OFFS_UNIT); 144 140 145 141 /* Amount of data (payload) to transfer */ 146 xfer_size = min(packet->size - offs, spc_avail);147 142 size_t xfer_size = min(packet->size - offs, spc_avail); 143 148 144 /* Total PDU size */ 149 size = hdr_size + xfer_size;150 145 size_t size = hdr_size + xfer_size; 146 151 147 /* Offset of remaining payload */ 152 rem_offs = offs + xfer_size;153 148 size_t rem_offs = offs + xfer_size; 149 154 150 /* Flags */ 155 flags_foff =151 uint16_t flags_foff = 156 152 (packet->df ? BIT_V(uint16_t, FF_FLAG_DF) : 0) + 157 153 (rem_offs < packet->size ? BIT_V(uint16_t, FF_FLAG_MF) : 0) + 158 154 (foff << FF_FRAGOFF_l); 159 160 data = calloc(size, 1);155 156 void *data = calloc(size, 1); 161 157 if (data == NULL) 162 158 return ENOMEM; 163 159 164 160 /* Allocate identifier */ 165 161 fibril_mutex_lock(&ip_ident_lock); 166 ident = ++ip_ident;162 uint16_t ident = ++ip_ident; 167 163 fibril_mutex_unlock(&ip_ident_lock); 168 164 169 165 /* Encode header fields */ 170 hdr = (ip_header_t *)data; 166 ip_header_t *hdr = (ip_header_t *) data; 167 171 168 hdr->ver_ihl = (4 << VI_VERSION_l) | (hdr_size / sizeof(uint32_t)); 172 169 hdr->tos = packet->tos; … … 177 174 hdr->proto = packet->proto; 178 175 hdr->chksum = 0; 179 hdr->src_addr = host2uint32_t_be( packet->src.ipv4);180 hdr->dest_addr = host2uint32_t_be( packet->dest.ipv4);181 176 hdr->src_addr = host2uint32_t_be(src_addr); 177 hdr->dest_addr = host2uint32_t_be(dest_addr); 178 182 179 /* Compute checksum */ 183 chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *)hdr, hdr_size); 180 uint16_t chksum = inet_checksum_calc(INET_CHECKSUM_INIT, (void *) hdr, 181 hdr_size); 184 182 hdr->chksum = host2uint16_t_be(chksum); 185 183 186 184 /* Copy payload */ 187 memcpy((uint8_t *) data + data_offs, packet->data + offs, xfer_size);188 185 memcpy((uint8_t *) data + data_offs, packet->data + offs, xfer_size); 186 189 187 *rdata = data; 190 188 *rsize = size; 191 189 *roffs = rem_offs; 192 190 193 191 return EOK; 194 192 } … … 238 236 /* XXX Checksum */ 239 237 240 packet->src.ipv4 = uint32_t_be2host(hdr->src_addr);241 packet->dest.ipv4 = uint32_t_be2host(hdr->dest_addr);238 inet_addr_unpack(uint32_t_be2host(hdr->src_addr), &packet->src); 239 inet_addr_unpack(uint32_t_be2host(hdr->dest_addr), &packet->dest); 242 240 packet->tos = hdr->tos; 243 241 packet->proto = hdr->proto;
Note:
See TracChangeset
for help on using the changeset viewer.