#include "internet.h"Include dependency graph for rudp.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Data Structures | |
| struct | rudp |
| struct | rudpHeader |
Defines | |
| #define | udpEthMaxSize 1444 |
| #define | rudpMaxSize (udpEthMaxSize - sizeof(struct rudpHeader) ) |
Typedefs | |
| typedef bits32 | rudpHost |
Enumerations | |
| enum | rudpType { rudpAck = 199, rudpData = 200 } |
Functions | |
| rudp * | rudpNew (int socket) |
| void | rudpFree (struct rudp **pRu) |
| rudp * | rudpOpen () |
| rudp * | rudpMustOpen () |
| rudp * | rudpOpenBound (struct sockaddr_in *sai) |
| rudp * | rudpMustOpenBound (struct sockaddr_in *sai) |
| void | rudpClose (struct rudp **pRu) |
| int | rudpSend (struct rudp *ru, struct sockaddr_in *sai, void *message, int size) |
| int | rudpReceive (struct rudp *ru, void *messageBuf, int bufSize) |
| int | rudpReceiveFrom (struct rudp *ru, void *messageBuf, int bufSize, struct sockaddr_in *retFrom) |
| int | rudpReceiveTimeOut (struct rudp *ru, void *messageBuf, int bufSize, struct sockaddr_in *retFrom, int timeOut) |
| void | rudpPrintStatus (struct rudp *ru) |
| void | rudpTest () |
| #define rudpMaxSize (udpEthMaxSize - sizeof(struct rudpHeader) ) |
| #define udpEthMaxSize 1444 |
| enum rudpType |
| void rudpClose | ( | struct rudp ** | pRu | ) |
Definition at line 165 of file rudp.c.
References freez(), and rudp::socket.
Referenced by rudpOpenBound().
00167 { 00168 struct rudp *ru = *pRu; 00169 if (ru != NULL) 00170 { 00171 close(ru->socket); 00172 freez(pRu); 00173 } 00174 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void rudpFree | ( | struct rudp ** | pRu | ) |
| struct rudp* rudpMustOpen | ( | ) | [read] |
Definition at line 129 of file rudp.c.
References noWarnAbort(), and rudpOpen().
00131 { 00132 struct rudp *ru = rudpOpen(); 00133 if (ru == NULL) 00134 noWarnAbort(); 00135 return ru; 00136 }
Here is the call graph for this function:

| struct rudp* rudpMustOpenBound | ( | struct sockaddr_in * | sai | ) | [read] |
Definition at line 155 of file rudp.c.
References noWarnAbort(), and rudpOpenBound().
00158 { 00159 struct rudp *ru = rudpOpenBound(sai); 00160 if (ru == NULL) 00161 noWarnAbort(); 00162 return ru; 00163 }
Here is the call graph for this function:

| struct rudp* rudpNew | ( | int | socket | ) | [read] |
Definition at line 92 of file rudp.c.
References AllocVar, rudp::maxRetries, rudp::rttVary, rudpCalcTimeOut(), rudp::socket, and rudp::timeOut.
Referenced by rudpOpen(), and rudpTest().
00095 { 00096 struct rudp *ru; 00097 assert(socket >= 0); 00098 AllocVar(ru); 00099 ru->socket = socket; 00100 ru->rttVary = 250; /* Initial variance 250 microseconds. */ 00101 ru->timeOut = rudpCalcTimeOut(ru); 00102 ru->maxRetries = 7; 00103 return ru; 00104 }
Here is the call graph for this function:

Here is the caller graph for this function:

| struct rudp* rudpOpen | ( | ) | [read] |
Definition at line 112 of file rudp.c.
References errno, rudpNew(), rudp::socket, and warn().
Referenced by rudpMustOpen(), and rudpOpenBound().
00119 { 00120 int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 00121 if (sd < 0) 00122 { 00123 warn("Couldn't open socket in rudpOpen %s", strerror(errno)); 00124 return NULL; 00125 } 00126 return rudpNew(sd); 00127 }
Here is the call graph for this function:

Here is the caller graph for this function:

| struct rudp* rudpOpenBound | ( | struct sockaddr_in * | sai | ) | [read] |
Definition at line 138 of file rudp.c.
References errno, rudpClose(), rudpOpen(), rudp::socket, and warn().
Referenced by rudpMustOpenBound().
00142 { 00143 struct rudp *ru = rudpOpen(); 00144 if (ru != NULL) 00145 { 00146 if (bind(ru->socket, (struct sockaddr *)sai, sizeof(*sai)) < 0) 00147 { 00148 warn("Couldn't bind rudp socket: %s", strerror(errno)); 00149 rudpClose(&ru); 00150 } 00151 } 00152 return ru; 00153 }
Here is the call graph for this function:

Here is the caller graph for this function:

| void rudpPrintStatus | ( | struct rudp * | ru | ) |
Definition at line 421 of file rudp.c.
References rudp::failCount, rudp::receiveCount, rudp::resendCount, rudp::rttAve, rudp::rttLast, rudp::rttVary, rudp::sendCount, and rudp::timeOut.
00423 { 00424 printf("rudp status:\n"); 00425 printf(" receiveCount %d\n", ru->receiveCount); 00426 printf(" sendCount %d\n", ru->sendCount); 00427 printf(" resendCount %d\n", ru->resendCount); 00428 printf(" failCount %d\n", ru->failCount); 00429 printf(" timeOut %d\n", ru->timeOut); 00430 printf(" rttVary %d\n", ru->rttVary); 00431 printf(" rttAve %d\n", ru->rttAve); 00432 printf(" rttLast %d\n", ru->rttLast); 00433 }
| int rudpReceive | ( | struct rudp * | ru, | |
| void * | messageBuf, | |||
| int | bufSize | |||
| ) |
Definition at line 414 of file rudp.c.
References rudpReceiveFrom().
00417 { 00418 return rudpReceiveFrom(ru, messageBuf, bufSize, NULL); 00419 }
Here is the call graph for this function:

| int rudpReceiveFrom | ( | struct rudp * | ru, | |
| void * | messageBuf, | |||
| int | bufSize, | |||
| struct sockaddr_in * | retFrom | |||
| ) |
Definition at line 405 of file rudp.c.
References rudpReceiveTimeOut().
Referenced by rudpReceive().
00410 { 00411 return rudpReceiveTimeOut(ru, messageBuf, bufSize, retFrom, 0); 00412 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int rudpReceiveTimeOut | ( | struct rudp * | ru, | |
| void * | messageBuf, | |||
| int | bufSize, | |||
| struct sockaddr_in * | retFrom, | |||
| int | timeOut | |||
| ) |
Definition at line 335 of file rudp.c.
References errno, rudp::failCount, readReadyWait(), rudp::receiveCount, rudpAck, rudpData, rudpMaxSize, rudp::socket, rudpHeader::type, udpEthMaxSize, and warn().
Referenced by rudpReceiveFrom().
00342 { 00343 char inBuf[udpEthMaxSize]; 00344 struct rudpHeader *head = (struct rudpHeader *)inBuf; 00345 struct rudpHeader ackHead; 00346 struct sockaddr_in sai; 00347 socklen_t saiSize = sizeof(sai); 00348 int readSize, err; 00349 assert(bufSize <= rudpMaxSize); 00350 ru->receiveCount += 1; 00351 for (;;) 00352 { 00353 if (timeOut != 0) 00354 { 00355 if (!readReadyWait(ru->socket, timeOut)) 00356 { 00357 warn("rudpReceive timed out\n"); 00358 errno = ETIMEDOUT; 00359 return -1; 00360 } 00361 } 00362 readSize = recvfrom(ru->socket, inBuf, sizeof(inBuf), 0, 00363 (struct sockaddr*)&sai, &saiSize); 00364 if (retFrom != NULL) 00365 *retFrom = sai; 00366 if (readSize < 0) 00367 { 00368 if (errno == EINTR) 00369 continue; 00370 warn("recvfrom error: %s", strerror(errno)); 00371 ru->failCount += 1; 00372 return readSize; 00373 } 00374 if (readSize < sizeof(*head)) 00375 { 00376 warn("rudpRecieve truncated message"); 00377 continue; 00378 } 00379 if (head->type != rudpData) 00380 { 00381 if (head->type != rudpAck) 00382 warn("skipping non-data message %d in rudpReceive", head->type); 00383 continue; 00384 } 00385 ackHead = *head; 00386 ackHead.type = rudpAck; 00387 err = sendto(ru->socket, &ackHead, sizeof(ackHead), 0, 00388 (struct sockaddr *)&sai, sizeof(sai)); 00389 if (err < 0) 00390 { 00391 warn("problem sending ack in rudpRecieve: %s", strerror(errno)); 00392 } 00393 readSize -= sizeof(*head); 00394 if (readSize > bufSize) 00395 { 00396 warn("read more bytes than have room for in rudpReceive"); 00397 readSize = bufSize; 00398 } 00399 memcpy(messageBuf, head+1, readSize); 00400 break; 00401 } 00402 return readSize; 00403 }
Here is the call graph for this function:

Here is the caller graph for this function:

| int rudpSend | ( | struct rudp * | ru, | |
| struct sockaddr_in * | sai, | |||
| void * | message, | |||
| int | size | |||
| ) |
Definition at line 276 of file rudp.c.
References errno, rudp::failCount, getOurAck(), rudp::lastId, rudp::maxRetries, rudp::resendCount, rudpData, rudpMaxSize, rudpTimedOut(), rudp::sendCount, rudp::socket, rudp::timeOut, udpEthMaxSize, and warn().
00279 { 00280 struct timeval sendTv; /* Current time. */ 00281 00282 char outBuf[udpEthMaxSize]; 00283 struct rudpHeader *head; 00284 int fullSize = size + sizeof(*head); 00285 int i, err = 0, maxRetry = ru->maxRetries; 00286 00287 00288 /* Make buffer with header in front of message. 00289 * At some point we might replace this with a scatter/gather 00290 * iovector. */ 00291 ru->sendCount += 1; 00292 assert(size <= rudpMaxSize); 00293 head = (struct rudpHeader *)outBuf; 00294 memcpy(head+1, message, size); 00295 head->id = ++ru->lastId; 00296 head->type = rudpData; 00297 00298 /* Go into send/wait for ack/retry loop. */ 00299 for (i=0; i<maxRetry; ++i) 00300 { 00301 gettimeofday(&sendTv, NULL); 00302 head->sendSec = sendTv.tv_sec; 00303 head->sendMicro = sendTv.tv_usec; 00304 err = sendto(ru->socket, outBuf, fullSize, 0, 00305 (struct sockaddr *)sai, sizeof(*sai)); 00306 if (err < 0) 00307 { 00308 /* Warn, wait, and retry. */ 00309 struct timeval tv; 00310 warn(" sendto problem %s", strerror(errno)); 00311 tv.tv_sec = 0; 00312 tv.tv_usec = ru->timeOut; 00313 select(0, NULL, NULL, NULL, &tv); 00314 ru->resendCount += 1; 00315 rudpTimedOut(ru); 00316 continue; 00317 } 00318 if (getOurAck(ru, &sendTv)) 00319 { 00320 return 0; 00321 } 00322 rudpTimedOut(ru); 00323 ru->resendCount += 1; 00324 } 00325 if (err >= 0) 00326 { 00327 err = ETIMEDOUT; 00328 warn("rudpSend timed out"); 00329 } 00330 ru->failCount += 1; 00331 return err; 00332 }
Here is the call graph for this function:

| void rudpTest | ( | ) |
Definition at line 435 of file rudp.c.
References ArraySize, rudpAddRoundTripTime(), rudpNew(), and rudp::timeOut.
00437 { 00438 static int times[] = {1000, 200, 200, 100, 200, 200, 200, 400, 200, 200, 200, 200, 1000, 00439 200, 200, 200, 200}; 00440 struct rudp *ru = rudpNew(0); 00441 int i; 00442 00443 for (i=0; i<ArraySize(times); ++i) 00444 { 00445 int oldTimeOut = ru->timeOut; 00446 rudpAddRoundTripTime(ru, times[i]); 00447 printf("%d\t%d\t%d\t%d\n", i, oldTimeOut, times[i], ru->timeOut); 00448 } 00449 }
Here is the call graph for this function:

1.5.2