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