⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.c

📁 g729 coding ipaddressing
💻 C
📖 第 1 页 / 共 5 页
字号:
		if (d->localLoopback || ((!useSendNotSendto || waNetNoConnect) &&
									(!waNetUseSend))) {
			if (d->localLoopback) {
				stat = loop_sendto(d, buf, buflen,
						(LPSOCKADDR) &(d->name), sizeof d->name);
			} else {									
				stat = sendto(d->sReply, buf, buflen, 0,
						(LPSOCKADDR) &(d->name), sizeof d->name);
			}
			if (stat < 0) {
				if (!waNetNoConnect) {
					useSendNotSendto = TRUE;
					if (hDlgPropeller != NULL) {
						SetDlgItemText(hDlgPropeller, IDC_PH_SENDTO, rstring(IDS_T_SEND));
					}
				}
			} else {
				wasSentTo = TRUE;
			}
		}
		/*	Careful!  Don't "optimise" this to "else if"; we have to be
			able to switch-hit when the first sendto() fails above. */
		if (!wasSentTo) {
			stat = send(d->sReply, buf, buflen, 0);
		}
		if (stat == buflen) {
			propeller(IDC_PH_PACKETS_SENT, ++packetsSent);
#ifdef OVERLOAD			
if ((rand() & 0x3F) == 0) { d->outputSocketBusy = TRUE; OutputDebugString("Dooooh!\r\n"); }  
#endif			
		} else {
			propeller(IDC_PH_OUTPUT_LOST, ++outputPacketsLost);

			if (!waNetNoOutOverflow) {

				/* Great leap of faith.  Treat a "WSAEWOULDBLOCK" error as
				   a truncated buffer and do nothing other than mark the
				   socket busy. */
				   
				d->outputSocketBusy = TRUE;
				if (stat == SOCKET_ERROR &&
					WSAGetLastError() == WSAEWOULDBLOCK) {
					stat = 0;
				}
			} 
#ifdef OVERLOAD			
OutputDebugString("Socket write failed.\r\n");
#endif
		}
#ifdef OVERLOAD
	}
#endif				
		return stat;
	}
}

//	SOCKETERRORBOX  --  Show message box for socket error

static void socketerrorbox(HWND hwnd, LPCLIENT_DATA d)
{
	if (d->modemConnection) {
	    MessageBox(hwnd, rstring(IDS_T_MODEM_WRITE_ERR), NULL,
	    	MB_ICONEXCLAMATION | MB_OK);
	} else {
	    MessageBox(hwnd, SockerrToString(WSAGetLastError()), rstring(IDS_T_SOCKET_WRITE_ERR),
	    	MB_ICONEXCLAMATION | MB_OK);
    }
} 

/*  SENDPKT  --  Send a message to the destination.  */

static int sendpkt(HWND hwnd, LPCLIENT_DATA d, struct soundbuf *asb)
{
    LONG lts = asb->buffer.buffer_len;
    LONG pkl = pktlen;
    struct soundbuf *osb = asb;

#ifdef CRYPTO
    if ((d->deskey[0] || d->ideakey[0] || d->opgpkey[0] ||
		d->blowfish_spec || d->aes_spec || d->otpFileName[0])) {
        int i;
        LONG slen;

        memcpy(&ebuf, asb, (int) pktlen);
        slen = lts;
        osb = &ebuf;

        /* DES encryption. */

        if (d->deskey[0]) {
			if (protocolSent == PROTOCOL_RTP || protocolSent == PROTOCOL_VAT) {
				LONG vlen = pktlen;
				des_key_schedule sched;
				des_cblock ivec;

                /* If we're DES encrypting we must round the size of
				   the data to be sent to be a multiple of 8 so that
				   the entire DES frame is sent.  If this is an RTP
				   packet, we may have to set the Pad bit in the
				   header and include a count of pad bytes at the end
				   of the packet. */

				memset(ivec, 0, 8);
				pkl = (pktlen + 7) & (~7);
				if (pkl > pktlen) {
					memset(((char *) &ebuf) + vlen, 0, (int) (pkl - vlen));
				}
				if ((protocolSent == PROTOCOL_RTP) && (pkl > vlen)) {
					char *p = (char *) &ebuf;

					p[0] |= 0x20; /* Set pad bytes present bit */
					p[pkl - 1] = (unsigned char) (pkl - vlen); /* Set pad count at end */
				}
				des_set_key((des_cblock *) (((protocolSent == PROTOCOL_RTP) ? d->rtpdeskey :
								d->vatdeskey) + 1), sched);
				des_ncbc_encrypt((des_cblock *) &ebuf,
					(des_cblock *) &ebuf, pkl, sched,
					(des_cblock *) ivec, DES_ENCRYPT);
				slen = pkl - (sizeof(struct soundbuf) - BUFL); 
			} else {
	        	char twibble[8];
	        	
	        	memcpy(twibble, d->deskey + 1, 8);
	            setkey(twibble);
	
	            /* If we're DES encrypting we must round the size of
	               the data to be sent to be a multiple of 8 so that
	               the entire DES frame is sent. */
	
	            slen = (slen + 7) & (~7);
	            for (i = 0; i < slen; i += 8) {
	
	                /* Apply cipher block chaining within the packet. */
	
	                if (i > 0) {
	                    int j;
	
	                    for (j = 0; j < 8; j++) {
	                        ebuf.buffer.buffer_val[(i + j)] ^=
	                            ebuf.buffer.buffer_val[(i + j) - 8];
	                    }
	                }
	                endes(ebuf.buffer.buffer_val + i);
	            }
	            ebuf.compression |= fEncDES;
	        }
        }

        /* IDEA encryption. */

        if ((protocolSent == PROTOCOL_SPEAKFREE) && d->ideakey[0]) {
            unsigned short iv[4];
	        char twibble[16];
	        
	        memcpy(twibble, d->ideakey + 1, 16);
            memset(iv, 0, sizeof(iv));
            initcfb_idea(iv, twibble, FALSE);

            /* If we're IDEA encrypting we must round the size of
               the data to be sent to be a multiple of 8 so that
               the entire IDEA frame is sent. */

            slen = (slen + 7) & (~7); 
            ideacfb(ebuf.buffer.buffer_val, (int) slen);
            close_idea();
            ebuf.compression |= fEncIDEA;
        }

        /* Blowfish encryption. */

        if ((protocolSent == PROTOCOL_SPEAKFREE) && d->blowfish_spec) {
            unsigned char iv[8];
	        
            memset(iv, 0, sizeof(iv));

            /* If we're Blowfish encrypting we must round the size of
               the data to be sent to be a multiple of 8 so that
               the entire Blowfish frame is sent. */

            slen = (slen + 7) & (~7); 
            BF_cbc_encrypt((unsigned char *) ebuf.buffer.buffer_val,
						   (unsigned char *) ebuf.buffer.buffer_val,
						   slen, &(d->blowfishkey), iv, BF_ENCRYPT);
            ebuf.compression |= fEncBF;
        }
        
        //	AES Encryption
        
		if ((protocolSent == PROTOCOL_SPEAKFREE)&& d->aes_spec) {
		
            /* If we're AES encrypting we must round the size of
				the data to be sent to be a multiple of 16 so that
				the entire AES block is sent. */

			slen = (slen + 15) & (~15);
			AES_cbc_encrypt((unsigned char *) ebuf.buffer.buffer_val,
							(unsigned char *) ebuf.buffer.buffer_val,
							slen, &(d->aesEkey));
			ebuf.compression |= fEncAES;
		}

        /* PGP session key encryption. */

        if ((protocolSent == PROTOCOL_SPEAKFREE) && d->opgpkey[0]) {
            unsigned short iv[4];
	        char twibble[16];
	        
	        memcpy(twibble, d->opgpkey + 1, 16);
            memset(iv, 0, sizeof(iv));
            initcfb_idea(iv, twibble, FALSE);

            /* If we're IDEA encrypting we must round the size of
               the data to be sent to be a multiple of 8 so that
               the entire IDEA frame is sent. */

            slen = (slen + 7) & (~7); 
            ideacfb(ebuf.buffer.buffer_val, (int) slen);
            close_idea();
            ebuf.compression |= fEncPGP;
        }

        /* One-time pad encryption. */

        if ((protocolSent == PROTOCOL_SPEAKFREE) && d->otpFileName[0]) {
            for (i = 0; i < slen; i++) {
                ebuf.buffer.buffer_val[i] ^= d->otp[i];
            }
            ebuf.compression |= fEncOTP;
        }
        pkl = slen + (sizeof(struct soundbuf) - BUFL);
    }
#endif
     
    {
    	int stat, nr;

		if (protocolSent == PROTOCOL_SPEAKFREE) {    	
			pkl = lpc10stuff(osb, pkl);
			pkl = celpstuff(osb, pkl);

            /*  If we're sending in Speak Freely protocol and robust transmission
				if enabled, include a packet sequence number, modulo 256, in the
				most significant 8 bits of the buffer_len field.  This is used
				on the receiving end to discard duplicates and re-order shuffled
				packets. The fCompRobust flag is set in the compression field to
				inform the receiver it should examine the sequence number.	*/
				
			if (robust > 1) {
				osb->compression |= fCompRobust;
				osb->buffer.buffer_len |= xseq << 24;
				xseq = (xseq + 1) & 0xFF;
			}
	        revlong(&osb->compression);
	        revlong(&osb->buffer.buffer_len);
	    }
		for (nr = 0; nr < ((protocolSent == PROTOCOL_SPEAKFREE) ? robust : 1); nr++) {
			stat = writeOutput(d, (LPSTR) osb, (int) pkl);
			if (stat < 0) {
				break;
			}
		}
        if (protocolSent == PROTOCOL_SPEAKFREE) {
	        revlong(&osb->compression);
	        revlong(&osb->buffer.buffer_len);
			if (osb->compression & fCompRobust) {
				osb->compression &= ~fCompRobust;
				osb->buffer.buffer_len &= 0xFFFFFFL;
			}
			celpunstuff(osb);
			lpc10unstuff(osb);
	    }
        if (stat < 0
&& WSAGetLastError() != WSAEINPROGRESS        ) {
            d->state = d->wantsInput ? SendingLiveAudio : Idle;
            d->wantsInput = FALSE;
            if (d->hFile != HFILE_ERROR) {
            	KillTimer(hwnd, 2);
	            _lclose(d->hFile);
	            d->hFile = HFILE_ERROR;
	        }
            socketerrorbox(hwnd, d);
            return FALSE;
        }
    }
    return TRUE;
}

/*	CHANGEAUDIOSTATE  --  Change transmitting / receiving state.  */

static void changeAudioState(HWND hwnd, LPCLIENT_DATA pClientData)
{
	if (pClientData->face_shown) {
		LPSTR hname = ((pClientData->uname != NULL) && (pClientData->uname[0]) &&
					   ((pClientData->uname[0] & 0xFF) != 0xFF)) ?
						pClientData->uname :
						(pClientData->localLoopback ? rstring(IDS_T_LOOPBACK) : 
						 (pClientData->modemConnection ?
		    			  rstring(IDS_T_MODEM_CONNECTION) : pClientData->szHost));
    	if (pClientData->wantsInput) {
    		char s[MAX_HOST + 20];
    		
    		strcpy(s, "==> ");
    		strcat(s, hname);
    		SetWindowText(hwnd, s); 
    	} else {
    		SetWindowText(hwnd, hname);
    	} 
	} else {
		InvalidateRect(hwnd, NULL, FALSE);
		UpdateWindow(hwnd);
	}
	
	/*	If "Beep on Transmit" is requested, beep when transitioning to
		transmit mode.  */
	if (waAudioBeepOnTransmit && (pClientData->wantsInput)) {
		MessageBeep(MB_ICONASTERISK);
	}
}

/*  SHIPSOUNDBUFFER  --  Output sound buffer to connection.  */

void shipSoundBuffer(HWND hwnd, LPCLIENT_DATA pClientData)
{
	if (sqpacket != squelched) {
		RECT wr;
		POINT cp;
	
		squelched = sqpacket;
		GetWindowRect(hwnd, &wr);
		GetCursorPos(&cp);
		if (PtInRect(&wr, cp)) {
	    	SetCursor((pClientData->wantsInput || broadcasting) ? 
	    				(squelched ? boltCursor : earCursor) :
	    				phoneCursor);
			UpdateWindow(hwnd);			// Change cursor to indicate squelch state
		}
	}

	if (squelched) {
		return;
	}
    
    if (protocolSent == PROTOCOL_SPEAKFREE) {
	    sb.compression = fProtocol | (pClientData->ring ? (fSetDest | fDestSpkr) : 0);
	    pClientData->ring = FALSE;
	    sb.compression |= pClientData->debugging ? fDebug : 0;
	    sb.compression |= pClientData->loopback ? fLoopBack : 0;
	    sb.compression |= compression ? fComp2X : 0;
	    sb.compression |= gsmcompress ? fCompGSM : 0;
	    sb.compression |= adpcmcompress ? fCompADPCM : 0;
	    sb.compression |= lpccompress ? fCompLPC : 0;
	    sb.compression |= lpc10compress ? fCompLPC10 : 0;
	    sb.compression |= celpcompress ? fCompCELP : 0;
	    /* Never offer face data to a multicast address.  It might be
	       nice, but the possibility of screw-ups is just too great
	       given the flakiness of multicast implementations. */
	    sb.compression |= ((faceFile != HFILE_ERROR) &&
	    	(!IN_MULTICAST(pClientData->inetSock.sin_addr.s_addr))) ? fFaceOffer : 0;
	    strcpy(sb.sendinghost, ourSendingHost);
	}
    sendpkt(hwnd, pClientData, &sb);
}

/*	SENDSESSIONCTRL  --  Send an RTP or VAT session control packet
						 on the control channel.  */
						 
static void sendSessionCtrl(LPCLIENT_DATA pClientData, char *msg, int msgl) 
{
	char *aux = (char *) &ebuf;
	int stat;

#ifdef CRYPTO			    			
	if ((!(protocolSent == PROTOCOL_SPEAKFREE)) && pClientData->rtpdeskey[0] &&
			!((protocolSent == PROTOCOL_RTP) && waProtNoRTCPCrypt)) {
		int vlen;
		des_key_schedule sched;
		des_cblock ivec;
						
		memset(ivec, 0, 8);
						
		if (protocolSent == PROTOCOL_RTP) {
						
			/* Encrypted RTCP messages are prefixed with 4 random
			   bytes to prevent known plaintext attacks. */
						
			memcpy(aux, &rtpdesrand, 4);
			memcpy(aux + 4, msg, msgl);
			msgl += 4;
		} else {
			memcpy(aux, msg, msgl);
		}
						
        /* If we're DES encrypting we must round the size of
		   the data to be sent to be a multiple of 8 so that
		   the entire DES frame is sent.  This applies only to
		   VAT, as the code that creates RTCP packets guarantees
           they're already padded to a multiple of 8 bytes. */
						
		vlen = msgl;
		msgl = (msgl + 7) & (~7);
		if (msgl > vlen) {
			memset(aux + vlen, 0, msgl - vlen);
		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -