void linemode_suboption(struct WindRec *tw) { switch(tw->parsedat[1]) { char s[80]; case L_MODE: /* change mode */ #ifdef OPTS_DEBUG strcpy(s, "RECV: SB LINEMODE MODE => "); DemangleLineModeShort(s, tw->parsedat[2]); opts_debug_print(s); #endif if (tw->lineAllow) { // First make sure we allowed linemode in the first place. // RFC 1184 says client should ignore MODE negotiations with the MODE_ACK bit set, and should not // generate a response if the negotiated MODE matches the current MODE if (!((tw->parsedat[2] & L_MODE_ACK) || ((tw->parsedat[2] & L_MODE_MASK) == tw->lmodeBits))) { tw->lmodeBits = tw->parsedat[2]; // Accept the mode tw->parsedat[2] |= L_MODE_ACK; // Set the MODE_ACK bit sprintf(s,"%c%c%c%c",IAC,TEL_SB,N_LINEMODE,L_MODE); netwrite(tw->port,s,4); sprintf(s,"%c%c%c",tw->parsedat[2],IAC,TEL_SE); netwrite(tw->port,s,3); #ifdef OPTS_DEBUG opts_debug_print("SENT: IAC SB"); strcpy(s, "SENT: LM MODE = "); DemangleLineModeShort(s, tw->parsedat[2]); opts_debug_print(s); opts_debug_print("SENT: IAC SE"); #endif } #ifdef OPTS_DEBUG else { strcpy(s, "LINEMODE MODE = "); DemangleLineModeShort(s, tw->parsedat[2]); opts_debug_print(s); if (tw->parsedat[2] & L_MODE_ACK) opts_debug_print("\tignored because MODE_ACK was set."); else opts_debug_print("\tIgnored because we are already at that mode."); strcpy(s, "Curr Linemode = "); DemangleLineModeShort(s, tw->lmodeBits); opts_debug_print(s); } #endif } break; case TEL_DOTEL: #ifdef OPTS_DEBUG sprintf(munger,"RECV: SB LINEMODE DO %c", tw->parsedat[2]); opts_debug_print(munger); #endif tw->forwardMask = TRUE; setForwardMask(tw); str_lm[3] = TEL_WILLTEL; str_lm[4] = L_FORWARDMASK; netpush(tw->port); netwrite(tw->port, str_lm, sizeof(str_lm)); #ifdef OPTS_DEBUG sprintf(munger,"SENT: IAC SB LINEMODE WILL %c IAC SE", tw->parsedat[2]); opts_debug_print(munger); #endif break; case TEL_DONTTEL: #ifdef OPTS_DEBUG sprintf(munger,"RECV: SB LINEMODE DO %c", tw->parsedat[2]); opts_debug_print(munger); #endif tw->forwardMask = FALSE; str_lm[3] = TEL_WONTTEL; str_lm[4] = L_FORWARDMASK; netpush(tw->port); netwrite(tw->port, str_lm, sizeof(str_lm)); #ifdef OPTS_DEBUG sprintf(munger,"SENT: IAC SB LINEMODE WONT %c IAC SE", tw->parsedat[2]); opts_debug_print(munger); #endif break; case L_SLC: /* set local chars */ negotiateSLC(tw); break; default: #ifdef OPTS_DEBUG sprintf(munger, "RECV: SB LINEMODE ?? (?? = %c)", tw->parsedat[1]); opts_debug_print(munger); #endif break; }//switch parsedat }
void negotiateSLC(struct WindRec *tw) { short lmslcflag = 0; unsigned char *ourValues = tw->slc; unsigned char *ourLevels = tw->slcLevel; short i; #ifdef OPTS_DEBUG sprintf(munger,"RECV: IAC SB LINEMODE SLC"); opts_debug_print(munger); for(i=2;i <= tw->parseIndex - 3; i+=3) { if (tw->parsedat[i] > 30) break; // RAB BetterTelnet 2.0b3 // we shouldn't panic on bad data // if (tw->parsedat[i] > 30) // DebugStr("\pAck! Bad option number"); if(tw->parsedat[i+1] & SLC_AWK) sprintf(munger," %s %s|AWK %d",LMoptions[tw->parsedat[i]],LMflags[tw->parsedat[i+1] & SLC_LEVELBITS],tw->parsedat[i+2]); else sprintf(munger," %s %s %d",LMoptions[tw->parsedat[i]],LMflags[tw->parsedat[i+1] & SLC_LEVELBITS],tw->parsedat[i+2]); opts_debug_print(munger); } sprintf(munger,"RECV: IAC SE"); opts_debug_print(munger); #endif for (i=2, lmslcflag=0; i <= tw->parseIndex - 3; i+=3) { short optionNumber = tw->parsedat[i]; Boolean awked = (tw->parsedat[i+1] & SLC_AWK); unsigned char requestedValue = tw->parsedat[i+2]; unsigned char requestedLevel = tw->parsedat[i+1] & SLC_LEVELBITS; Boolean flushin = tw->parsedat[i+1] & SLC_FLUSHIN; Boolean flushout = tw->parsedat[i+1] & SLC_FLUSHOUT; if ((ourValues[optionNumber] != requestedValue)||(ourLevels[optionNumber] != tw->parsedat[i+1])) //if we are sent what we already have, ignore it { if (requestedLevel == SLC_NOSUPPORT) { if (ourLevels[optionNumber] & SLC_LEVELBITS != SLC_NOSUPPORT) { ourValues[optionNumber] = 255; ourLevels[optionNumber] = SLC_NOSUPPORT;//ok, we wont support this if (!awked) respondSLC(optionNumber,&lmslcflag, tw, TRUE);//awk only if he didnt awk #ifdef OPTS_DEBUG else { sprintf(munger,"\t %s %s %d accepted; no response sent because it was AWKED", LMoptions[optionNumber],LMflags[requestedLevel],requestedValue); opts_debug_print(munger); } #endif } #ifdef OPTS_DEBUG else { sprintf(munger, "\t %s %s %d Ignored because we are already at nosupport", LMoptions[optionNumber],LMflags[requestedLevel],ourValues[optionNumber]); opts_debug_print(munger); } #endif } else if (requestedLevel == SLC_DEFAULT) { ourValues[optionNumber] = default_slc[optionNumber]; //get our default if (ourValues[optionNumber] == 255) ourLevels[optionNumber] = SLC_NOSUPPORT; else ourLevels[optionNumber] = SLC_VALUE; respondSLC(optionNumber, &lmslcflag, tw, FALSE); //tell him about our choice } else if (requestedLevel > ourLevels[optionNumber]) respondSLC(optionNumber, &lmslcflag, tw, FALSE); //keep lower setting else { ourValues[optionNumber] = requestedValue; ourLevels[optionNumber] = requestedLevel; if (flushin) ourLevels[optionNumber] |= SLC_FLUSHIN; if (flushout) ourLevels[optionNumber] |= SLC_FLUSHOUT; if (!awked) respondSLC(optionNumber, &lmslcflag, tw, TRUE); //agree to his new setting } } #ifdef OPTS_DEBUG else { sprintf(munger, "\t %s %s Ignored because %d is already the current value", LMoptions[optionNumber],LMflags[requestedLevel],ourValues[optionNumber]); opts_debug_print(munger); } #endif }//end for on parseindex if (lmslcflag) { unsigned char toSend[2] = {IAC,TEL_SE}; netpush(tw->port); netwrite(tw->port,toSend,2); opts_debug_print("SENT: IAC SE"); } }
static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get) { if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing netwrite(ftp->ctrl_fd, cmd, strlen(cmd)); return is_get? kftp_get_response(ftp) : 0; }
void simple_tcpserver(cyg_addrword_t pnetdata) { int s, new_s, i, len; char *msg; struct sockaddr_in sa, r_sa; int r_sa_l = sizeof(r_sa); struct hostent *hp; struct sockaddr_in peername; int socklen; int threadid; int localcount = 0; int port = ((NET_DATA_T*)pnetdata)->iport; char *pbuf = ((NET_DATA_T*)pnetdata)->pbuf; //s = ((NET_DATA_T*)pnetdata)->fd; threadid = cyg_thread_self(); msg = malloc(BUFSIZE); if(msg == NULL) { printf("simple_tcpserver(%d): malloc error\n", threadid); cyg_thread_exit(); } printf("simple_tcpserver(%d): to gethostbyname\n", threadid); if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL) { printf("simple_tcpserver(%d): gethostbyname error!!!!!!\n", threadid); cyg_thread_exit(); } printf("simple_tcpserver(%d): gethostbyname ok\n", threadid); //memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length); r_sa.sin_addr.s_addr = inet_addr("10.132.11.10", pbuf, RNT_BUFFER_LEN); printf("simple_tcpserver(%d): final addr=%s\n", threadid, inet_ntoa(r_sa.sin_addr, pbuf, RNT_BUFFER_LEN)); r_sa.sin_family = AF_INET; r_sa.sin_port = htons(IPPORT_USERRESERVED + port); printf("simple_tcpserver(%d): receive from port %d\n", threadid, ntohs(r_sa.sin_port)); if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): socket error!!!!!!\n", threadid); cyg_thread_exit(); } printf("simple_tcpserver(%d): create socket success...\n", threadid); printf("simple_tcpserver(%d): socket=%d\n", threadid, s); /* Test for netfcntl & setsockopt */ /* { int sock_opt = 1; // server socket is nonblocking if (netfcntl(s, F_SETFL, O_NONBLOCK, pbuf, RNT_BUFFER_LEN) == -1) { printf("netfcntl error\n"); netclose(s, pbuf, RNT_BUFFER_LEN); return -1; } if ((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &sock_opt, sizeof(sock_opt), pbuf, RNT_BUFFER_LEN)) == -1) { printf("setsockopt error\n"); netclose(s, pbuf, RNT_BUFFER_LEN); return -1; } } */ if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1) { printf("simple_tcpserver(%d): bind error!!!!!!\n", threadid); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } printf("simple_tcpserver(%d): bind success...\n", threadid); /* { struct sockaddr_in sockname; int socklen; if(getsockname(s, (struct sockaddr*)&sockname, &socklen, pbuf, RNT_BUFFER_LEN) != -1) { printf("simple_tcpserver(%d): getsockname:sin_family=%d sin_port=%d sin_addr=%s socklen=%d\n", threadid, sockname.sin_family, sockname.sin_port, inet_ntoa(sockname.sin_addr, pbuf, RNT_BUFFER_LEN), socklen); } else { printf("simple_tcpserver(%d): getsockname error\n", threadid); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } } */ if(listen(s, 10, pbuf, RNT_BUFFER_LEN) == -1) { printf("simple_tcpserver(%d): listen error\n", threadid); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } printf("simple_tcpserver(%d): listen success...\n", threadid); while(1) { if((new_s = accept(s, (struct sockaddr*)&sa, (size_t*)&r_sa_l, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): accept error\n", threadid); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } printf("simple_tcpserver(%d): accept success...\n", threadid); { int val, vallen; vallen = sizeof(val); if(getsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("get rcvbuf size error!!!!!!\n"); } printf("recv window size = %d\n", val); vallen = sizeof(val); if(getsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("get sndbuf size error!!!!!!\n"); } printf("send window size = %d\n", val); vallen = sizeof(val); val = 50000; if(setsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("set rcvbuf size error!!!!!!\n"); } if(setsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("set sndbuf size error!!!!!!\n"); } vallen = sizeof(val); if(getsockopt(new_s, SOL_SOCKET, SO_RCVBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("get rcvbuf size error!!!!!!\n"); } printf("new recv window size = %d\n", val); vallen = sizeof(val); if(getsockopt(new_s, SOL_SOCKET, SO_SNDBUF, &val, &vallen, pbuf, RNT_BUFFER_LEN) < 0) { printf("get sndbuf size error!!!!!!\n"); } printf("new send window size = %d\n", val); } /* { if(getpeername(new_s, (struct sockaddr*)&peername, &socklen, pbuf, RNT_BUFFER_LEN) != -1) { printf("simple_tcpserver(%d): getpeername:sin_family=%d sin_port=%d sin_addr=%s socklen=%d\n", threadid, peername.sin_family, peername.sin_port, inet_ntoa(peername.sin_addr, pbuf, RNT_BUFFER_LEN), socklen); } else { printf("simple_tcpserver(%d): getpeername error\n", threadid); netclose(new_s, pbuf, RNT_BUFFER_LEN); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } } */ i = 0; while(1) { /* Test recvmsg & sendmsg method */ /* { struct msghdr msghdr_msg; struct iovec *piov; int j, n, copylen; int perIov_len; perIov_len = BUFSIZE/4; piov = malloc(4*sizeof(struct iovec)); for(j = 0; j < 4; j++) { piov[j].iov_base = malloc(perIov_len); piov[j].iov_len = perIov_len; } msghdr_msg.msg_name = NULL; msghdr_msg.msg_namelen = 0; msghdr_msg.msg_iov = piov; msghdr_msg.msg_iovlen = 4; msghdr_msg.msg_control = NULL; msghdr_msg.msg_controllen = 0; msghdr_msg.msg_flags = 0; if((len = recvmsg(new_s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): recvmsg error\n", threadid); break; } if(len == 0) { printf("simple_tcpserver(%d): recvmsg 0, connection broken!!!!!\n", threadid); break; } n = 0; for(j = 0; j < 4; j++) { //printf("simple_tcpserver(%d): recvmsg piov[%d].iovlen=%d\n", threadid, j, piov[j].iov_len); copylen = piov[j].iov_len < (len - n) ? piov[j].iov_len : (len - n); memcpy(&msg[n], piov[j].iov_base, copylen); n += copylen; if(n >= len) break; } msg[len] = '\0'; //printf("simple_tcpserver(%d): recvmsg: %s\n", threadid, msg); len = 17408; memcpy(msg, g_LargeData, len); //len = sprintf(msg, "ack%d", i); n = 0; for(j = 0; j < 4; j++) { copylen = piov[j].iov_len < (len - n) ? piov[j].iov_len : (len - n); memcpy(piov[j].iov_base, &msg[n], copylen); piov[j].iov_len = copylen; //printf("simple_tcpserver(%d): sendmsg piov[%d].iov_len=%d\n", threadid, j, piov[j].iov_len); n += copylen; if(n >= len) break; } j++; for(; j < 4; j++) piov[j].iov_len = 0; if((len = sendmsg(new_s, &msghdr_msg, 0, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): sendmsg error\n", threadid); break; } if(len == 0) { printf("simple_tcpserver(%d): sendmsg 0, connection broken!!!!!\n", threadid); break; } for(j = 0; j < 4; j++) { free(piov[j].iov_base); } free(piov); i++; } */ /* Test recv & send method and fdset operation */ /* { fd_set readset, exceptset; int maxfd; int written_bytes; int bytes_left; int reconnect; FD_ZERO(&readset); FD_SET(new_s, &readset); FD_ZERO(&exceptset); FD_SET(new_s, &exceptset); maxfd = new_s + 1; if(netselect(maxfd, &readset, NULL, &exceptset, NULL, pbuf, RNT_BUFFER_LEN) == -1) { printf("simple_tcpserver(%d): netselect error\n", threadid); continue; } if(FD_ISSET(new_s, &readset)) { //printf("simple_tcpserver(%d): readset get\n", threadid); } else if(FD_ISSET(new_s, &exceptset)) { printf("simple_tcpserver(%d): exceptset get\n", threadid); break; } if((len = recv(new_s, msg, BUFSIZE, 0, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): recv error\n", threadid); break; } if(len == 0) { printf("simple_tcpserver(%d): recv 0, connection broken!!!!!\n", threadid); break; } //printf("simple_tcpserver(%d): recv: %s\n", threadid, msg); //len = sprintf(msg, "ack%d", i); len = 40000; memcpy(msg, g_LargeData, len); bytes_left = len; reconnect = 0; while(bytes_left > 0) { written_bytes = send(new_s, msg+(len-bytes_left), bytes_left, 0, pbuf, RNT_BUFFER_LEN); if(written_bytes <= 0) { if(errno == EINTR) { written_bytes = 0; } else { printf("simple_tcpserver(%d): netwrite error, reconnect!!!!!\n", threadid); reconnect = 1; break; } } bytes_left -= written_bytes; } if(reconnect == 1) break; i++; } */ /* Test for netread & netwrite method */ { int written_bytes; int bytes_left; int reconnect; if((len = netread(new_s, msg, BUFSIZE, pbuf, RNT_BUFFER_LEN)) == -1) { printf("simple_tcpserver(%d): netread error %d\n", threadid, errno); break; } if(len == 0) { printf("simple_tcpserver(%d): netread 0, connection broken, errno=%d!!!!!\n", threadid, errno); break; } msg[len] = '\0'; //printf("simple_tcpserver(%d): len %d, netread %s\n", threadid, len, msg); //printf("simple_tcpserver(%d): len %d\n", threadid, len); //len = sprintf(msg, "ack%d", i); len = 40000; memcpy(msg, g_LargeData, len); bytes_left = len; reconnect = 0; while(bytes_left > 0) { written_bytes = netwrite(new_s, msg+(len-bytes_left), bytes_left, pbuf, RNT_BUFFER_LEN); if(written_bytes <= 0) { printf("errno=%d\n", errno); if(errno == EINTR) { written_bytes = 0; } else { printf("simple_tcpserver(%d): netwrite error, reconnect!!!!!\n", threadid); reconnect = 1; break; } } //printf("simple_tcpserver(%d): write %d\n", threadid, written_bytes); bytes_left -= written_bytes; if(bytes_left > 0) { printf("write %d < %d left %d\n", written_bytes, bytes_left+written_bytes, bytes_left); } } if(reconnect == 1) break; i++; } totallen += len; icount++; if((icount>=2000) && (icount%2000==0)) { tend = cyg_current_time(); printf("rate=%d rate1=%03fk\n", (int)(icount/((tend-tbegin)/100)), (float)(totallen/((tend-tbegin)/100)/1024)); } localcount++; if(localcount > 100 && localcount % 100 == 0) { printf("simple_tcpserver: %d\n", port); } } printf("simple_tcpserver(%d): to close %d with 0x%x\n", threadid, new_s, pbuf); netclose(new_s, pbuf, RNT_BUFFER_LEN); printf("simple_tcpserver(%d): close connection ok\n", threadid); } netclose(s, pbuf, RNT_BUFFER_LEN); free(msg); cyg_thread_exit(); }
sendrep(int rpfd, int rep_type, ...) { va_list args; char func[16]; char *msg; int n; char *p; char prtbuf[PRTBUFSZ]; char *q; char *rbp; int rc; char repbuf[REPBUFSZ+12]; int repsize; strcpy (func, "sendrep"); rbp = repbuf; marshall_LONG (rbp, CNS_MAGIC2); va_start (args, rep_type); marshall_LONG (rbp, rep_type); switch (rep_type) { case MSG_ERR: msg = va_arg (args, char *); vsprintf (prtbuf, msg, args); marshall_LONG (rbp, strlen (prtbuf) + 1); marshall_STRING (rbp, prtbuf); nslogit (func, "%s", prtbuf); break; case MSG_DATA: case MSG_LINKS: case MSG_REPLIC: case MSG_REPLICP: case MSG_REPLICX: case MSG_REPLICS: case MSG_GROUPS: case MSG_STATUSES: case MSG_FILEST: case MSG_GRPINFO: case MSG_USRINFO: n = va_arg (args, int); marshall_LONG (rbp, n); msg = va_arg (args, char *); memcpy (rbp, msg, n); /* marshalling already done */ rbp += n; break; case CNS_IRC: case CNS_RC: rc = va_arg (args, int); marshall_LONG (rbp, rc); break; } va_end (args); repsize = rbp - repbuf; if (netwrite (rpfd, repbuf, repsize) != repsize) { nslogit (func, NS002, "send", neterror()); if (rep_type == CNS_RC) netclose (rpfd); return (-1); } if (rep_type == CNS_RC) netclose (rpfd); return (0); }
/* ** deliver_it ** Attempt to send a sequence of bytes to the connection. ** Returns ** ** < 0 Some fatal error occurred, (but not EWOULDBLOCK). ** This return is a request to close the socket and ** clean up the link. ** ** >= 0 No real error occurred, returns the number of ** bytes actually transferred. EWOULDBLOCK and other ** possibly similar conditions should be mapped to ** zero return. Upper level routine will have to ** decide what to do with those unwritten bytes... ** ** *NOTE* alarm calls have been preserved, so this should ** work equally well whether blocking or non-blocking ** mode is used... ** ** *NOTE* I nuked 'em. At the load of current ircd servers ** you can't run with stuff that blocks. And we don't. */ int deliver_it(aClient *cptr, char *str, int len) { int retval; aClient *acpt = cptr->listener; #ifdef DEBUGMODE writecalls++; #endif #ifdef VMS retval = netwrite(cptr->fd, str, len); #else if (IsDead(cptr) || (!IsServer(cptr) && !IsPerson(cptr) && !IsHandshake(cptr) #ifdef USE_SSL && !IsSSLHandshake(cptr) #endif && !IsUnknown(cptr))) { str[len] = '\0'; sendto_ops ("* * * DEBUG ERROR * * * !!! Calling deliver_it() for %s, status %d %s, with message: %s", cptr->name, cptr->status, IsDead(cptr) ? "DEAD" : "", str); return -1; } #ifdef USE_SSL if (cptr->flags & FLAGS_SSL) retval = ircd_SSL_write(cptr, str, len); else #endif retval = send(cptr->fd, str, len, 0); /* ** Convert WOULDBLOCK to a return of "0 bytes moved". This ** should occur only if socket was non-blocking. Note, that ** all is Ok, if the 'write' just returns '0' instead of an ** error and errno=EWOULDBLOCK. ** ** ...now, would this work on VMS too? --msa */ # ifndef _WIN32 if (retval < 0 && (errno == EWOULDBLOCK || errno == EAGAIN || errno == ENOBUFS)) # else if (retval < 0 && (WSAGetLastError() == WSAEWOULDBLOCK || WSAGetLastError() == WSAENOBUFS)) # endif { retval = 0; SetBlocked(cptr); } else if (retval > 0) { ClearBlocked(cptr); } #endif #ifdef DEBUGMODE if (retval < 0) { writeb[0]++; Debug((DEBUG_ERROR, "write error (%s) to %s", STRERROR(ERRNO), cptr->name)); } else if (retval == 0) writeb[1]++; else if (retval < 16) writeb[2]++; else if (retval < 32) writeb[3]++; else if (retval < 64) writeb[4]++; else if (retval < 128) writeb[5]++; else if (retval < 256) writeb[6]++; else if (retval < 512) writeb[7]++; else if (retval < 1024) writeb[8]++; else writeb[9]++; #endif if (retval > 0) { cptr->sendB += retval; me.sendB += retval; if (cptr->sendB > 1023) { cptr->sendK += (cptr->sendB >> 10); cptr->sendB &= 0x03ff; /* 2^10 = 1024, 3ff = 1023 */ }
void test_netwrite_server(cyg_addrword_t pnetdata) { int s, i, len, writelen; struct sockaddr_in sa, r_sa; struct hostent *hp; int threadid; int port = ((TEST_NETWRITE_DATA_T*)pnetdata)->iport; char *pbuf = ((TEST_NETWRITE_DATA_T*)pnetdata)->pbuf; char *pwritebuf = ((TEST_NETWRITE_DATA_T*)pnetdata)->pwritebuf; threadid = port; if(inet_aton(TEST_NETWRITE_SERVER_ADDR, &sa.sin_addr, pbuf, RNT_BUFFER_LEN) == 0) { test_printf_error("test_netwrite_server"); cyg_thread_exit(); } sa.sin_family = AF_INET; sa.sin_port = htons(IPPORT_USERRESERVED + port); if((hp = gethostbyname(TEST_REMOTEFUNC_HOSTNAME, pbuf, RNT_BUFFER_LEN)) == NULL) { test_printf_error("test_netwrite_server"); cyg_thread_exit(); } memcpy(&(r_sa.sin_addr), hp->h_addr_list0, hp->h_length); r_sa.sin_family = AF_INET; r_sa.sin_port = htons(IPPORT_USERRESERVED + port); if((s = socket(AF_INET, SOCK_STREAM, PF_UNSPEC, pbuf, RNT_BUFFER_LEN)) == -1) { test_printf_error("test_netwrite_server"); cyg_thread_exit(); } if(set_reuseaddr(s, pbuf, RNT_BUFFER_LEN) == -1) { test_printf_error("test_netwrite_server"); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } if(bind(s, (struct sockaddr*)&r_sa, sizeof(r_sa), pbuf, RNT_BUFFER_LEN) == -1) { test_printf_error("test_netwrite_server"); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); } if(connect(s, (struct sockaddr*)&sa, sizeof(sa), pbuf, RNT_BUFFER_LEN) == -1) { test_printf_error("test_netwrite_server"); cyg_thread_exit(); } for(i = 0; i < TEST_NETWRITE_WRITE_TIMES; i++) { len = sprintf(pwritebuf, "%s", TEST_NETWRITE_MSG); len++; writelen = netwrite(s, pwritebuf, len, pbuf, RNT_BUFFER_LEN); if(writelen < 0) { test_printf_error("test_netwrite_server"); break; } } if(i == TEST_NETWRITE_WRITE_TIMES) test_printf_success("test_netwrite_server"); netclose(s, pbuf, RNT_BUFFER_LEN); cyg_thread_exit(); }