📄 uip.lst
字号:
} else {
DEBUG_PRINTF("Unknown icmp6 message type %d\n", ICMPBUF->type);
UIP_STAT(++uip_stat.icmp.drop);
UIP_STAT(++uip_stat.icmp.typeerr);
UIP_LOG("icmp: unknown ICMP message.");
goto drop;
}
/* End of IPv6 ICMP processing. */
#endif /* !UIP_CONF_IPV6 */
1088 1
1089 1 #if UIP_UDP
/* UDP input processing. */
udp_input:
/* UDP processing is really just a hack. We don't do anything to the
UDP/IP headers, but let the UDP application do all the hard
work. If the application sets uip_slen, it has a packet to
send. */
#if UIP_UDP_CHECKSUMS
uip_len = uip_len - UIP_IPUDPH_LEN;
uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
if(UDPBUF->udpchksum != 0 && uip_udpchksum() != 0xffff) {
UIP_STAT(++uip_stat.udp.drop);
UIP_STAT(++uip_stat.udp.chkerr);
UIP_LOG("udp: bad checksum.");
goto drop;
}
#else /* UIP_UDP_CHECKSUMS */
uip_len = uip_len - UIP_IPUDPH_LEN;
#endif /* UIP_UDP_CHECKSUMS */
C51 COMPILER V7.06 UIP 05/02/2009 15:51:22 PAGE 19
/* Demultiplex this UDP packet between the UDP "connections". */
for(uip_udp_conn = &uip_udp_conns[0];
uip_udp_conn < &uip_udp_conns[UIP_UDP_CONNS];
++uip_udp_conn) {
/* If the local UDP port is non-zero, the connection is considered
to be used. If so, the local port number is checked against the
destination port number in the received packet. If the two port
numbers match, the remote port number is checked if the
connection is bound to a remote port. Finally, if the
connection is bound to a remote IP address, the source IP
address of the packet is checked. */
if(uip_udp_conn->lport != 0 &&
UDPBUF->destport == uip_udp_conn->lport &&
(uip_udp_conn->rport == 0 ||
UDPBUF->srcport == uip_udp_conn->rport) &&
(uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_zeroes_addr) ||
uip_ipaddr_cmp(uip_udp_conn->ripaddr, all_ones_addr) ||
uip_ipaddr_cmp(BUF->srcipaddr, uip_udp_conn->ripaddr))) {
goto udp_found;
}
}
UIP_LOG("udp: no matching connection found");
goto drop;
udp_found:
uip_conn = NULL;
uip_flags = UIP_NEWDATA;
uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN];
uip_slen = 0;
UIP_UDP_APPCALL();
udp_send:
if(uip_slen == 0) {
goto drop;
}
uip_len = uip_slen + UIP_IPUDPH_LEN;
#if UIP_CONF_IPV6
/* For IPv6, the IP length field does not include the IPv6 IP header
length. */
BUF->len[0] = ((uip_len - UIP_IPH_LEN) >> 8);
BUF->len[1] = ((uip_len - UIP_IPH_LEN) & 0xff);
#else /* UIP_CONF_IPV6 */
BUF->len[0] = (uip_len >> 8);
BUF->len[1] = (uip_len & 0xff);
#endif /* UIP_CONF_IPV6 */
BUF->ttl = uip_udp_conn->ttl;
BUF->proto = UIP_PROTO_UDP;
UDPBUF->udplen = HTONS(uip_slen + UIP_UDPH_LEN);
UDPBUF->udpchksum = 0;
BUF->srcport = uip_udp_conn->lport;
BUF->destport = uip_udp_conn->rport;
uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
uip_ipaddr_copy(BUF->destipaddr, uip_udp_conn->ripaddr);
uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPTCPH_LEN];
#if UIP_UDP_CHECKSUMS
/* Calculate UDP checksum. */
C51 COMPILER V7.06 UIP 05/02/2009 15:51:22 PAGE 20
UDPBUF->udpchksum = ~(uip_udpchksum());
if(UDPBUF->udpchksum == 0) {
UDPBUF->udpchksum = 0xffff;
}
#endif /* UIP_UDP_CHECKSUMS */
goto ip_send_nolen;
#endif /* UIP_UDP */
1179 1
1180 1 /* TCP input processing. */
1181 1 tcp_input:
1182 1 UIP_STAT(++uip_stat.tcp.recv);
1183 1
1184 1 /* Start of TCP input header processing code. */
1185 1
1186 1 if(uip_tcpchksum() != 0xffff) { /* Compute and check the TCP
1187 2 checksum. */
1188 2 UIP_STAT(++uip_stat.tcp.drop);
1189 2 UIP_STAT(++uip_stat.tcp.chkerr);
1190 2 UIP_LOG("tcp: bad checksum.");
1191 2 goto drop;
1192 2 }
1193 1
1194 1
1195 1 /* Demultiplex this segment. */
1196 1 /* First check any active connections. */
1197 1 for(uip_connr = &uip_conns[0]; uip_connr <= &uip_conns[UIP_CONNS - 1];
1198 1 ++uip_connr) {
1199 2 if(uip_connr->tcpstateflags != UIP_CLOSED &&
1200 2 BUF->destport == uip_connr->lport &&
1201 2 BUF->srcport == uip_connr->rport &&
1202 2 uip_ipaddr_cmp(BUF->srcipaddr, uip_connr->ripaddr)) {
1203 3 goto found;
1204 3 }
1205 2 }
1206 1
1207 1 /* If we didn't find and active connection that expected the packet,
1208 1 either this packet is an old duplicate, or this is a SYN packet
1209 1 destined for a connection in LISTEN. If the SYN flag isn't set,
1210 1 it is an old packet and we send a RST. */
1211 1 if((BUF->flags & TCP_CTL) != TCP_SYN) {
1212 2 goto reset;
1213 2 }
1214 1
1215 1 tmp16 = BUF->destport;
1216 1 /* Next, check listening connections. */
1217 1 for(c = 0; c < UIP_LISTENPORTS; ++c) {
1218 2 if(tmp16 == uip_listenports[c])
1219 2 goto found_listen;
1220 2 }
1221 1
1222 1 /* No matching connection found, so we send a RST packet. */
1223 1 UIP_STAT(++uip_stat.tcp.synrst);
1224 1 reset:
1225 1
1226 1 /* We do not send resets in response to resets. */
1227 1 if(BUF->flags & TCP_RST) {
1228 2 goto drop;
1229 2 }
1230 1
1231 1 UIP_STAT(++uip_stat.tcp.rst);
1232 1
C51 COMPILER V7.06 UIP 05/02/2009 15:51:22 PAGE 21
1233 1 BUF->flags = TCP_RST | TCP_ACK;
1234 1 uip_len = UIP_IPTCPH_LEN;
1235 1 BUF->tcpoffset = 5 << 4;
1236 1
1237 1 /* Flip the seqno and ackno fields in the TCP header. */
1238 1 c = BUF->seqno[3];
1239 1 BUF->seqno[3] = BUF->ackno[3];
1240 1 BUF->ackno[3] = c;
1241 1
1242 1 c = BUF->seqno[2];
1243 1 BUF->seqno[2] = BUF->ackno[2];
1244 1 BUF->ackno[2] = c;
1245 1
1246 1 c = BUF->seqno[1];
1247 1 BUF->seqno[1] = BUF->ackno[1];
1248 1 BUF->ackno[1] = c;
1249 1
1250 1 c = BUF->seqno[0];
1251 1 BUF->seqno[0] = BUF->ackno[0];
1252 1 BUF->ackno[0] = c;
1253 1
1254 1 /* We also have to increase the sequence number we are
1255 1 acknowledging. If the least significant byte overflowed, we need
1256 1 to propagate the carry to the other bytes as well. */
1257 1 if(++BUF->ackno[3] == 0) {
1258 2 if(++BUF->ackno[2] == 0) {
1259 3 if(++BUF->ackno[1] == 0) {
1260 4 ++BUF->ackno[0];
1261 4 }
1262 3 }
1263 2 }
1264 1
1265 1 /* Swap port numbers. */
1266 1 tmp16 = BUF->srcport;
1267 1 BUF->srcport = BUF->destport;
1268 1 BUF->destport = tmp16;
1269 1
1270 1 /* Swap IP addresses. */
1271 1 uip_ipaddr_copy(BUF->destipaddr, BUF->srcipaddr);
1272 1 uip_ipaddr_copy(BUF->srcipaddr, uip_hostaddr);
1273 1
1274 1 /* And send out the RST packet! */
1275 1 goto tcp_send_noconn;
1276 1
1277 1 /* This label will be jumped to if we matched the incoming packet
1278 1 with a connection in LISTEN. In that case, we should create a new
1279 1 connection and send a SYNACK in return. */
1280 1 found_listen:
1281 1 /* First we check if there are any connections avaliable. Unused
1282 1 connections are kept in the same table as used connections, but
1283 1 unused ones have the tcpstate set to CLOSED. Also, connections in
1284 1 TIME_WAIT are kept track of and we'll use the oldest one if no
1285 1 CLOSED connections are found. Thanks to Eddie C. Dost for a very
1286 1 nice algorithm for the TIME_WAIT search. */
1287 1 uip_connr = 0;
1288 1 for(c = 0; c < UIP_CONNS; ++c) {
1289 2 if(uip_conns[c].tcpstateflags == UIP_CLOSED) {
1290 3 uip_connr = &uip_conns[c];
1291 3 break;
1292 3 }
1293 2 if(uip_conns[c].tcpstateflags == UIP_TIME_WAIT) {
1294 3 if(uip_connr == 0 ||
C51 COMPILER V7.06 UIP 05/02/2009 15:51:22 PAGE 22
1295 3 uip_conns[c].timer > uip_connr->timer) {
1296 4 uip_connr = &uip_conns[c];
1297 4 }
1298 3 }
1299 2 }
1300 1
1301 1 if(uip_connr == 0) {
1302 2 /* All connections are used already, we drop packet and hope that
1303 2 the remote end will retransmit the packet at a time when we
1304 2 have more spare connections. */
1305 2 UIP_STAT(++uip_stat.tcp.syndrop);
1306 2 UIP_LOG("tcp: found no unused connections.");
1307 2 goto drop;
1308 2 }
1309 1 uip_conn = uip_connr;
1310 1
1311 1 /* Fill in the necessary fields for the new connection. */
1312 1 uip_connr->rto = uip_connr->timer = UIP_RTO;
1313 1 uip_connr->sa = 0;
1314 1 uip_connr->sv = 4;
1315 1 uip_connr->nrtx = 0;
1316 1 uip_connr->lport = BUF->destport;
1317 1 uip_connr->rport = BUF->srcport;
1318 1 uip_ipaddr_copy(uip_connr->ripaddr, BUF->srcipaddr);
1319 1 uip_connr->tcpstateflags = UIP_SYN_RCVD;
1320 1
1321 1 uip_connr->snd_nxt[0] = iss[0];
1322 1 uip_connr->snd_nxt[1] = iss[1];
1323 1 uip_connr->snd_nxt[2] = iss[2];
1324 1 uip_connr->snd_nxt[3] = iss[3];
1325 1 uip_connr->len = 1;
1326 1
1327 1 /* rcv_nxt should be the seqno from the incoming packet + 1. */
1328 1 uip_connr->rcv_nxt[3] = BUF->seqno[3];
1329 1 uip_connr->rcv_nxt[2] = BUF->seqno[2];
1330 1 uip_connr->rcv_nxt[1] = BUF->seqno[1];
1331 1 uip_connr->rcv_nxt[0] = BUF->seqno[0];
1332 1 uip_add_rcv_nxt(1);
1333 1
1334 1 /* Parse the TCP MSS option, if present. */
1335 1 if((BUF->tcpoffset & 0xf0) > 0x50) {
1336 2 for(c = 0; c < ((BUF->tcpoffset >> 4) - 5) << 2 ;) {
1337 3 opt = uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + c];
1338 3 if(opt == TCP_OPT_END) {
1339 4 /* End of options. */
1340 4 break;
1341 4 } else if(opt == TCP_OPT_NOOP) {
1342 4 ++c;
1343 4 /* NOP option. */
1344 4 } else if(opt == TCP_OPT_MSS &&
1345 3 uip_buf[UIP_TCPIP_HLEN + UIP_LLH_LEN + 1 + c] == TCP_OPT_MSS_LEN) {
1346 4 /* An MSS option with the righ
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -