/********************************************************************* * * _Connect * * Function description * This function is called from the FTP server module if the client * uses active FTP to establish the data connection. */ static FTPS_SOCKET _Connect(FTPS_SOCKET CtrlSocket, U16 Port) { int DataSock; struct sockaddr_in Data; struct sockaddr_in PeerAddr; int ConnectStatus; int AddrSize; DataSock = _socket(AF_INET, SOCK_STREAM, 0); // Create a new socket for data connection to the client if (DataSock != SOCKET_ERROR) { // Socket created? Data.sin_family = AF_INET; Data.sin_port = _htons(20); Data.sin_addr.s_addr = INADDR_ANY; _bind(DataSock, (struct sockaddr *)&Data, sizeof(Data)); // // Get IP address of connected client and connect to listening port // AddrSize = sizeof(struct sockaddr_in); _getpeername((long)CtrlSocket, (struct sockaddr *)&PeerAddr, &AddrSize); PeerAddr.sin_port = _htons(Port); ConnectStatus = _connect(DataSock, (struct sockaddr *)&PeerAddr, sizeof(struct sockaddr_in)); if (ConnectStatus == 0) { return (void*)DataSock; } } return NULL; }
unsigned short TLV::whole(char* buf)//returns the whole tlv { *(unsigned short*)buf = _htons(type_); *(unsigned short*)&buf[2] = _htons(length_); memcpy(&buf[4], value_, length_); return length_ + TLV_HEADER_SIZE; }
static int udp_init_pkb(struct sock *sk, struct pkbuf *pkb, void *buf, int size, struct sock_addr *skaddr) { struct ip *iphdr = pkb2ip(pkb); struct udp *udphdr = (struct udp *)iphdr->ip_data; /* fill ip head */ iphdr->ip_hlen = IP_HRD_SZ >> 2; iphdr->ip_ver = IP_VERSION_4; iphdr->ip_tos = 0; iphdr->ip_len = _htons(pkb->pk_len - ETH_HRD_SZ); iphdr->ip_id = _htons(udp_id); iphdr->ip_fragoff = 0; iphdr->ip_ttl = UDP_DEFAULT_TTL; iphdr->ip_pro = sk->protocol; /* IP_P_UDP */ iphdr->ip_dst = skaddr->dst_addr; /* FIXME:use the sk->rt_dst */ if (rt_output(pkb) < 0) /* fill ip src */ return -1; /* fill udp */ udphdr->src = sk->sk_sport; /* bound local address */ udphdr->dst = skaddr->dst_port; udphdr->length = _htons(size + UDP_HRD_SZ); memcpy(udphdr->data, buf, size); udpdbg(IPFMT":%d" "->" IPFMT":%d(proto %d)", ipfmt(iphdr->ip_src), _ntohs(udphdr->src), ipfmt(iphdr->ip_dst), _ntohs(udphdr->dst), iphdr->ip_pro); udp_set_checksum(iphdr, udphdr); return 0; }
static void send_packet(void) { if (!buf) buf = xmalloc(size + ICMP_HRD_SZ); struct icmp *icmphdr = (struct icmp *)buf; static int first = 1; if (first) { printf("PING "IPFMT" %d(%d) bytes of data\n", ipfmt(ipaddr), size, size + ICMP_HRD_SZ + IP_HRD_SZ); first = 0; } /* fill icmp data */ memset(icmphdr->icmp_data, 'x', size); icmphdr->icmp_type = ICMP_T_ECHOREQ; icmphdr->icmp_code = 0; icmphdr->icmp_id = _htons(id); icmphdr->icmp_seq = _htons(seq); icmphdr->icmp_cksum = 0; icmphdr->icmp_cksum = icmp_chksum((unsigned short *)icmphdr, ICMP_HRD_SZ + size); seq++; /* socket apis */ _send(sock, buf, ICMP_HRD_SZ + size, &skaddr); psend++; }
static int tcp_init_pkb(struct tcp_sock *tsk, struct pkbuf *pkb, unsigned int saddr, unsigned int daddr) { struct ip *iphdr = pkb2ip(pkb); /* fill ip head */ iphdr->ip_hlen = IP_HRD_SZ >> 2; iphdr->ip_ver = IP_VERSION_4; iphdr->ip_tos = 0; iphdr->ip_len = _htons(pkb->pk_len - ETH_HRD_SZ); iphdr->ip_id = _htons(tcp_id); iphdr->ip_fragoff = 0; iphdr->ip_ttl = TCP_DEFAULT_TTL; iphdr->ip_pro = IP_P_TCP; iphdr->ip_dst = daddr; /* NOTE: tsk maybe NULL, if connect doesnt exist */ if (tsk && tsk->sk.sk_dst) { pkb->pk_rtdst = tsk->sk.sk_dst; } else { if (rt_output(pkb) < 0) return -1; if (tsk) tsk->sk.sk_dst = pkb->pk_rtdst; } iphdr->ip_src = saddr; return 0; }
SNAC::SNAC(char* buf,unsigned short length) { service_=_htons((*(unsigned short*)&buf[0])); subgroup_=_htons((*(unsigned short*)&buf[2])); flags_=_htons((*(unsigned short*)&buf[4])); idh_=_htons((*(unsigned short*)&buf[6])); id_=_htons((*(unsigned short*)&buf[8])); value_=&buf[SNAC_SIZE]; length_=length; }
TLV::TLV(char* buf) { type_=_htons((*(unsigned short*)&buf[0])); length_=_htons((*(unsigned short*)&buf[2])); if (length_ > 0) { value_=(char*)mir_alloc(length_+1); memcpy(value_,&buf[4],length_); } else value_= NULL; }
void tcp_send_synack(struct tcp_sock *tsk, struct tcp_segment *seg) { /* * LISTEN : * SYN-SENT: * SEG: SYN, no ACK, no RST * <SEQ=ISS><ACK=RCV.NXT><CTL=SYN,ACK> * (ISS == SND.NXT) */ struct tcp *otcp, *tcphdr = seg->tcphdr; struct pkbuf *opkb; if (tcphdr->rst) return; opkb = alloc_pkb(ETH_HRD_SZ + IP_HRD_SZ + TCP_HRD_SZ); /* fill tcp head */ otcp = (struct tcp *)pkb2ip(opkb)->ip_data; otcp->src = tcphdr->dst; otcp->dst = tcphdr->src; otcp->doff = TCP_HRD_DOFF; otcp->seq = _htonl(tsk->iss); otcp->ackn = _htonl(tsk->rcv_nxt); otcp->syn = 1; otcp->ack = 1; otcp->window = _htons(tsk->rcv_wnd); tcpdbg("send SYN(%u)/ACK(%u) [WIN %d] to "IPFMT":%d", _ntohl(otcp->seq), _ntohs(otcp->window), _ntohl(otcp->ackn), ipfmt(seg->iphdr->ip_dst), _ntohs(otcp->dst)); tcp_send_out(tsk, opkb, seg); }
//Function to write tlv header into a msg according to tlv info, the input buffer size should be at least MAX_TLV_HEADER_SIZE static tlv_status_t write_tlv_header(uint8_t *msg, const tlv_info_t *info) { uint8_t type = info->type; if(info->size>UINT16_MAX ||info->header_size == LARGE_TLV_HEADER_SIZE){//6 bytes header uint32_t size = info->size; //4 bytes in size field type |= FOUR_BYTES_SIZE_TYPE; msg[0] = type; msg[1] = info->version; size = _htonl(size); if(memcpy_s(&msg[2], sizeof(uint32_t), &size, sizeof(size))!=0){ AESM_DBG_ERROR("memcpy failed"); return TLV_UNKNOWN_ERROR; } }else{//4 bytes header uint16_t size = (uint16_t)info->size;//2 bytes in size field msg[0] = type; msg[1] = info->version; size = _htons(size); if(memcpy_s(&msg[2], sizeof(uint16_t), &size, sizeof(size))!=0){ AESM_DBG_ERROR("memcpy failed"); return TLV_UNKNOWN_ERROR; } } return TLV_SUCCESS; }
/** * \brief Creates and connects to an unsecure socket to be used for SMTP. * * \param[in] None. * * \return SOCK_ERR_NO_ERROR if success, -1 if socket create error, SOCK_ERR_INVALID if socket connect error. */ static int8_t smtpConnect(void) { struct sockaddr_in addr_in; addr_in.sin_family = AF_INET; addr_in.sin_port = _htons(MAIN_GMAIL_HOST_PORT); addr_in.sin_addr.s_addr = gu32HostIp; /* Create secure socket */ if (tcp_client_socket < 0) { tcp_client_socket = socket(AF_INET, SOCK_STREAM, SOCKET_FLAGS_SSL); } /* Check if socket was created successfully */ if (tcp_client_socket == -1) { printf("socket error.\r\n"); close(tcp_client_socket); return -1; } /* If success, connect to socket */ if (connect(tcp_client_socket, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in)) != SOCK_ERR_NO_ERROR) { printf("connect error.\r\n"); return SOCK_ERR_INVALID; } /* Success */ return SOCK_ERR_NO_ERROR; }
/** * \brief Callback to get the ServerIP from DNS lookup. * * \param[in] pu8DomainName Domain name. * \param[in] u32ServerIP Server IP. */ static void resolve_cb(uint8_t *pu8DomainName, uint32_t u32ServerIP) { struct sockaddr_in addr; int8_t cDataBuf[48]; int16_t ret; memset(cDataBuf, 0, sizeof(cDataBuf)); cDataBuf[0] = '\x1b'; /* time query */ printf("resolve_cb: DomainName %s\r\n", pu8DomainName); if (udp_socket >= 0) { /* Set NTP server socket address structure. */ addr.sin_family = AF_INET; addr.sin_port = _htons(MAIN_SERVER_PORT_FOR_UDP); addr.sin_addr.s_addr = u32ServerIP; /*Send an NTP time query to the NTP server*/ ret = sendto(udp_socket, (int8_t *)&cDataBuf, sizeof(cDataBuf), 0, (struct sockaddr *)&addr, sizeof(addr)); if (ret != M2M_SUCCESS) { printf("resolve_cb: failed to send error!\r\n"); return; } } }
/* return net order port */ static unsigned short udp_get_port(void) { struct hash_slot *best = udp_best_slot(); unsigned short port; port = udp_get_best_port(); if (port == 0) return udp_get_port_slow(); /* update hash table best slot */ if (--udp_table.best_update <= 0) { int i; udp_table.best_update = UDP_BEST_UPDATE; for (i = 0; i < UDP_HASH_SIZE; i++) { /* * Should we find the minimum(best) one? * No. * We find the first, smaller(better) than current best! */ if (udp_table.slot[i].used < best->used) { udp_table.best_slot = i; break; } } } return _htons(port); }
size_t WiFiUDP::write(const uint8_t *buffer, size_t size) { struct sockaddr_in addr; // Network led ON (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 0); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 0); addr.sin_family = AF_INET; addr.sin_port = _htons(_sndPort); addr.sin_addr.s_addr = _sndIP; if (sendto(_socket, (void *)buffer, size, 0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return 0; } // Network led OFF (rev A then rev B). m2m_periph_gpio_set_val(M2M_PERIPH_GPIO16, 1); m2m_periph_gpio_set_val(M2M_PERIPH_GPIO5, 1); return size; }
void tcp_send_fin(struct tcp_sock *tsk) { struct tcp *otcp; struct pkbuf *opkb; opkb = alloc_pkb(ETH_HRD_SZ + IP_HRD_SZ + TCP_HRD_SZ); /* fill tcp head */ otcp = (struct tcp *)pkb2ip(opkb)->ip_data; otcp->src = tsk->sk.sk_sport; otcp->dst = tsk->sk.sk_dport; otcp->doff = TCP_HRD_DOFF; otcp->seq = _htonl(tsk->snd_nxt); otcp->window = _htons(tsk->rcv_wnd); otcp->fin = 1; /* * Should we send an ACK? * Yes, tcp stack will drop packet if it has no ACK bit * according to RFC 793 #SEGMENT RECEIVE */ otcp->ackn = _htonl(tsk->rcv_nxt); otcp->ack = 1; tcpdbg("send FIN(%u)/ACK(%u) [WIN %d] to "IPFMT":%d", _ntohl(otcp->seq), _ntohl(otcp->ackn), _ntohs(otcp->window), ipfmt(tsk->sk.sk_daddr), _ntohs(otcp->dst)); tcp_send_out(tsk, opkb, NULL); }
/* Start Adafruit_WINC1500UDP socket, listening at local port PORT */ uint8_t Adafruit_WINC1500UDP::begin(uint16_t port, uint32_t multicastAddr) { struct sockaddr_in addr; uint32 u32EnableCallbacks = 0; _flag = 0; _head = 0; _tail = 0; _rcvSize = 0; _rcvPort = 0; _rcvIP = 0; _sndSize = 0; // Initialize socket address structure. addr.sin_family = AF_INET; addr.sin_port = _htons(port); addr.sin_addr.s_addr = 0; // Open TCP server socket. if ((_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return 0; } // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_recvBuffer); setsockopt(_socket, SOL_SOCKET, SO_SET_UDP_SEND_CALLBACK, &u32EnableCallbacks, 0); // Set multicast address option if a multicast address was specified. if (multicastAddr != 0) { multicastAddr = _htonl(multicastAddr); if (setsockopt(_socket, SOL_SOCKET, IP_ADD_MEMBERSHIP, &multicastAddr, sizeof(multicastAddr)) < 0) { // Failed to set the multicast address option. close(_socket); _socket = -1; return 0; } } // Bind socket: if (bind(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { close(_socket); _socket = -1; return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (!READY && millis() - start < 2000) { m2m_wifi_handle_events(NULL); } if (!READY) { close(_socket); _socket = -1; return 0; } _flag &= ~SOCKET_BUFFER_FLAG_BIND; return 1; }
static uint16_t get_udptcp_checksum(void *l3_hdr, void *l4_hdr, uint16_t ethertype) { if (ethertype == _htons(ETHER_TYPE_IPv4)) return rte_ipv4_udptcp_cksum(l3_hdr, l4_hdr); else /* assume ethertype == ETHER_TYPE_IPv6 */ return rte_ipv6_udptcp_cksum(l3_hdr, l4_hdr); }
static uint16_t get_psd_sum(void *l3_hdr, uint16_t ethertype, uint64_t ol_flags) { if (ethertype == _htons(ETHER_TYPE_IPv4)) return rte_ipv4_phdr_cksum(l3_hdr, ol_flags); else /* assume ethertype == ETHER_TYPE_IPv6 */ return rte_ipv6_phdr_cksum(l3_hdr, ol_flags); }
static _inline int __port_used(unsigned short port, struct hlist_head *head) { struct hlist_node *node; struct sock *sk; hlist_for_each_sock(sk, node, head) if (sk->sk_sport == _htons(port)) return 1; return 0; }
/* * Need enough RX buffer space to receive a complete name service packet when * used in UDP mode. NS expects MTU of 1500 subtracts UDP, IP and Ethernet * Type II overhead. 1500 - 8 -20 - 18 = 1454. txData buffer size needs to * be big enough to hold a NS WHO-HAS for one name (4 + 2 + 256 = 262) in UDP * mode. TCP buffer size dominates in that case. */ AJ_Status AJ_Net_Connect(AJ_BusAttachment* bus, const AJ_Service* service) { int ret; // IPAddress ip(service->ipv4); if (!(service->addrTypes & AJ_ADDR_TCP4)) { // AJ_ErrPrintf(("AJ_Net_Connect(): only IPV4 TCP supported\n", ret)); return AJ_ERR_CONNECT; } // printf("AJ_Net_Connect(netSock=0x%p, addrType=%d.)\n", netSock, addrType); printf("AJ_Net_Connect()\n"); // ret = g_client.connect(ip, service->ipv4port); // addr.sin_port = _htons(48256); // addr.sin_addr.s_addr = _htonl(0xc0a8141d); addr.sin_port = _htons(service->ipv4port); addr.sin_addr.s_addr = _htonl(service->ipv4); printf("AJ_Net_Connect(): ipv4= %x, port = %d\n",addr.sin_addr.s_addr, addr.sin_port); tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0); ret=connect(tcp_client_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)); printf("AJ_Net_Connect(): connect\n"); //ждем подключения while(tcp_ready_to_send==0) { m2m_wifi_handle_events(NULL); } printf("AJ_Net_Connect(): connect OK\n"); if (ret == -1) { //AJ_ErrPrintf(("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret)); return AJ_ERR_CONNECT; } else { //AJ_IOBufInit(&netSock->rx, rxData, sizeof(rxData), AJ_IO_BUF_RX, (void*)&g_clientUDP); bus->sock.rx.bufStart = AJ_in_data_tcp; bus->sock.rx.bufSize = sizeof(AJ_in_data_tcp); bus->sock.rx.readPtr = AJ_in_data_tcp; bus->sock.rx.writePtr = AJ_in_data_tcp; bus->sock.rx.direction = AJ_IO_BUF_RX; bus->sock.rx.recv = AJ_Net_Recv; // AJ_IOBufInit(&netSock->tx, txData, sizeof(txData), AJ_IO_BUF_TX, (void*)&g_clientUDP); bus->sock.tx.bufStart = tcp_data_tx; bus->sock.tx.bufSize = sizeof(tcp_data_tx); bus->sock.tx.readPtr = tcp_data_tx; bus->sock.tx.writePtr = tcp_data_tx; bus->sock.tx.direction = AJ_IO_BUF_TX; bus->sock.tx.send = AJ_Net_Send; printf("AJ_Net_Connect(): connect() success: status=AJ_OK\n"); return AJ_OK; } printf("AJ_Net_Connect(): connect() failed: %d: status=AJ_ERR_CONNECT\n", ret); return AJ_ERR_CONNECT; }
bool send_init_oft2(file_transfer *ft, char* file) { aimString astr(file); unsigned short len = max(0x100, 0xc0 + astr.getTermSize()); oft2 *oft = (oft2*)alloca(len); memset(oft, 0, len); memcpy(oft->protocol_version, "OFT2", 4); oft->length = _htons(len); oft->type = 0x0101; oft->total_files = _htons(ft->pfts.totalFiles); oft->num_files_left = _htons(ft->pfts.totalFiles - ft->pfts.currentFileNumber); oft->total_parts = _htons(1); oft->parts_left = _htons(1); oft->total_size = _htonl(ft->pfts.totalBytes); oft->size = _htonl(ft->pfts.currentFileSize); oft->mod_time = _htonl(ft->pfts.currentFileTime); oft->checksum = _htonl(aim_oft_checksum_file(ft->pfts.tszCurrentFile)); oft->recv_RFchecksum = 0x0000FFFF; oft->RFchecksum = 0x0000FFFF; oft->recv_checksum = 0x0000FFFF; memcpy(oft->idstring, "Cool FileXfer", 13); oft->flags = 0x20; oft->list_name_offset = 0x1c; oft->list_size_offset = 0x11; oft->encoding = _htons(astr.isUnicode() ? 2 : 0); memcpy(oft->filename, astr.getBuf(), astr.getTermSize()); if (!ft->requester || ft->pfts.currentFileNumber) memcpy(oft->icbm_cookie, ft->icbm_cookie, 8); return Netlib_Send(ft->hConn, (char*)oft, len, 0) > 0; }
/* NOTE: icmp dont drop @ipkb */ void icmp_send(unsigned char type, unsigned char code, unsigned int data, struct pkbuf *pkb_in) { struct pkbuf *pkb; struct ip *iphdr = pkb2ip(pkb_in); struct icmp *icmphdr; int paylen = _ntohs(iphdr->ip_len); /* icmp payload length */ if (paylen < iphlen(iphdr) + 8) return; /* * RFC 1812 Section 4.3.2.7 for sanity check * An ICMP error message MUST NOT be sent as the result of receiving: * 1. A packet sent as a Link Layer broadcast or multicast * 2. A packet destined to an IP broadcast or IP multicast address *[3] A packet whose source address has a network prefix of zero or is an * invalid source address (as defined in Section [5.3.7]) * 4. Any fragment of a datagram other then the first fragment (i.e., a * packet for which the fragment offset in the IP header is nonzero). * 5. An ICMP error message */ if (pkb_in->pk_type != PKT_LOCALHOST) return; if (MULTICAST(iphdr->ip_dst) || BROADCAST(iphdr->ip_dst)) return; if (iphdr->ip_fragoff & _htons(IP_FRAG_OFF)) return; if (icmp_type_error(type) && iphdr->ip_pro == IP_P_ICMP) { icmphdr = ip2icmp(iphdr); if (icmphdr->icmp_type > ICMP_T_MAXNUM || icmp_error(icmphdr)) return; } /* build icmp packet and send */ /* ip packet size must be smaller than 576 bytes */ if (IP_HRD_SZ + ICMP_HRD_SZ + paylen > 576) paylen = 576 - IP_HRD_SZ - ICMP_HRD_SZ; pkb = alloc_pkb(ETH_HRD_SZ + IP_HRD_SZ + ICMP_HRD_SZ + paylen); icmphdr = (struct icmp *)(pkb2ip(pkb)->ip_data); icmphdr->icmp_type = type; icmphdr->icmp_code = code; icmphdr->icmp_cksum = 0; icmphdr->icmp_undata = data; memcpy(icmphdr->icmp_data, (unsigned char *)iphdr, paylen); icmphdr->icmp_cksum = icmp_chksum((unsigned short *)icmphdr, ICMP_HRD_SZ + paylen); icmpdbg("to "IPFMT"(payload %d) [type %d code %d]\n", ipfmt(iphdr->ip_src), paylen, type, code); ip_send_info(pkb, 0, IP_HRD_SZ + ICMP_HRD_SZ + paylen, 0, IP_P_ICMP, iphdr->ip_src); }
int WiFiClient::connect(IPAddress ip, uint16_t port, uint8_t opt, const uint8_t *hostname) { struct sockaddr_in addr; // Initialize socket address structure: addr.sin_family = AF_INET; addr.sin_port = _htons(port); addr.sin_addr.s_addr = ip; // Create TCP socket: _flag = 0; _head = 0; _tail = 0; if ((_socket = socket(AF_INET, SOCK_STREAM, opt)) < 0) { return 0; } if (opt & SOCKET_FLAGS_SSL && hostname) { setsockopt(_socket, SOL_SSL_SOCKET, SO_SSL_SNI, hostname, m2m_strlen((uint8_t *)hostname)); } // Add socket buffer handler: socketBufferRegister(_socket, &_flag, &_head, &_tail, (uint8 *)_buffer); // Connect to remote host: if (connectSocket(_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { close(_socket); _socket = -1; return 0; } // Wait for connection or timeout: unsigned long start = millis(); while (!IS_CONNECTED && millis() - start < 20000) { m2m_wifi_handle_events(NULL); } if (!IS_CONNECTED) { close(_socket); _socket = -1; return 0; } WiFi._client[_socket] = this; return 1; }
static void tcp_init_text(struct tcp_sock *tsk, struct pkbuf *pkb, void *buf, int size) { struct tcp *tcphdr = pkb2tcp(pkb); tcphdr->src = tsk->sk.sk_sport; tcphdr->dst = tsk->sk.sk_dport; tcphdr->doff = TCP_HRD_DOFF; tcphdr->seq = _htonl(tsk->snd_nxt); tcphdr->ackn = _htonl(tsk->rcv_nxt); tcphdr->ack = 1; tcphdr->window = _htons(tsk->rcv_wnd); memcpy(tcphdr->data, buf, size); tsk->snd_nxt += size; tsk->snd_wnd -= size; tcpsdbg("send TEXT(%u:%d) [WIN %d] to "IPFMT":%d", _ntohl(tcphdr->seq), size, _ntohs(tcphdr->window), ipfmt(tsk->sk.sk_daddr), _ntohs(tcphdr->dst)); }
FLAP::FLAP(char* buf,int num_bytes) { if(FLAP_SIZE>num_bytes) { length_=0; } else { length_=_htons((*(unsigned short*)&buf[4])); if(FLAP_SIZE+length_>num_bytes) { length_=0; } else { type_=buf[1]; value_=&buf[FLAP_SIZE]; } } }
void tcp_send_syn(struct tcp_sock *tsk, struct tcp_segment *seg) { /* * SYN-SENT: */ struct tcp *otcp; struct pkbuf *opkb; opkb = alloc_pkb(ETH_HRD_SZ + IP_HRD_SZ + TCP_HRD_SZ); /* fill tcp head */ otcp = (struct tcp *)pkb2ip(opkb)->ip_data; otcp->src = tsk->sk.sk_sport; otcp->dst = tsk->sk.sk_dport; otcp->doff = TCP_HRD_DOFF; otcp->seq = _htonl(tsk->iss); otcp->syn = 1; otcp->window = _htons(tsk->rcv_wnd); tcpdbg("send SYN(%u) [WIN %d] to "IPFMT":%d", _ntohl(otcp->seq), _ntohs(otcp->window), ipfmt(tsk->sk.sk_daddr), _ntohs(otcp->dst)); tcp_send_out(tsk, opkb, seg); }
void http_client_socket_resolve_handler(uint8_t *doamin_name, uint32_t server_ip) { int i; struct http_client_module *module; struct sockaddr_in addr_in; for (i = 0; i < TCP_SOCK_MAX; i++) { if (module_ref_inst[i] != NULL) { module = module_ref_inst[i]; if (!strcmp((const char*)doamin_name, module->host) && module->req.state == STATE_TRY_SOCK_CONNECT) { if (server_ip == 0) { /* Host was not found or was not reachable. */ _http_client_clear_conn(module, -EHOSTUNREACH); return; } addr_in.sin_family = AF_INET; addr_in.sin_port = _htons(module->config.port); addr_in.sin_addr.s_addr = server_ip; connect(module->sock, (struct sockaddr *)&addr_in, sizeof(struct sockaddr_in)); return; } } } }
void connect_cmd_handler(void *pMsg) { char *arg = (char *)pMsg; uint32_t address; struct sockaddr_in addr; if (tcp_client_socket >= 0) { printf("Already connected to a remote device.\r\n"); return; } /* Create TCP client socket. */ if ((tcp_client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf("Failed to create TCP client socket.\r\n"); return; } while (*arg == ' ') { arg++; } if ((address = nmi_inet_addr(arg)) == 0) { printf("Invalid IP address.\r\n"); return; } /* Connect to the server. */ printf("Connecting to [%s] ...\r\n", arg); addr.sin_family = AF_INET; addr.sin_port = _htons(MAIN_WIFI_M2M_SERVER_PORT); addr.sin_addr.s_addr = address; /* _htonl(address); */ if (connect(tcp_client_socket, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { printf("Failed to connect to the server.\r\n"); close(tcp_client_socket); tcp_client_socket = 1; return; } }
/** Handles start of a TCP(HTTP) connection. */ static void handle_start_connect(pubnub_t *pb) { assert(valid_ctx_prt(pb)); assert((pb->state == PS_IDLE) || (pb->state == PS_WAIT_DNS) || (pb->state == PS_WAIT_CONNECT)); if(pb->state == PS_IDLE && pb->tcp_socket <= 0) { if ((pb->tcp_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) { CONF_WINC_PRINTF("failed to create TCP client socket error!\r\n"); return; } if(pubnub_origin_addr.sin_addr.s_addr <= 0) { pubnub_origin_addr.sin_family = AF_INET; pubnub_origin_addr.sin_port = _htons(PUBNUB_ORIGIN_PORT); pb->state = PS_WAIT_DNS; gethostbyname((uint8 *)PUBNUB_ORIGIN); return; } } connect(pb->tcp_socket, (struct sockaddr*)&pubnub_origin_addr, sizeof(struct sockaddr_in)); pb->state = PS_WAIT_CONNECT; }
/* * Acknowledgment algorithm is not stated directly in RFC 793, * but we can conclude it from all acknowledgment situation. */ void tcp_send_ack(struct tcp_sock *tsk, struct tcp_segment *seg) { /* * SYN-SENT : * SEG: SYN, acceptable ACK, no RST (SND.NXT = SEG.SEQ+1) * <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK> * SYN-RECEIVED / ESTABLISHED / FIN-WAIT-1 / FIN-WAIT-2 / * CLOSE-WAIT / CLOSING / LAST-ACK / TIME-WAIT : * SEG: no RST, ??ACK, ??SYN (segment is not acceptable) * <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK> * ESTABLISHED / FIN-WAIT-1 / FIN-WAIT-2 / process the segment text: * SEG: ACK, no RST * <SEQ=SND.NXT><ACK=RCV.NXT><CTL=ACK> * (This acknowledgment should be piggybacked on a segment being * transmitted if possible without incurring undue delay.) */ struct tcp *otcp, *tcphdr = seg->tcphdr; struct pkbuf *opkb; if (tcphdr->rst) return; opkb = alloc_pkb(ETH_HRD_SZ + IP_HRD_SZ + TCP_HRD_SZ); /* fill tcp head */ otcp = (struct tcp *)pkb2ip(opkb)->ip_data; otcp->src = tcphdr->dst; otcp->dst = tcphdr->src; otcp->doff = TCP_HRD_DOFF; otcp->seq = _htonl(tsk->snd_nxt); otcp->ackn = _htonl(tsk->rcv_nxt); otcp->ack = 1; otcp->window = _htons(tsk->rcv_wnd); tcpdbg("send ACK(%u) [WIN %d] to "IPFMT":%d", _ntohl(otcp->ackn), _ntohs(otcp->window), ipfmt(seg->iphdr->ip_src), _ntohs(otcp->dst)); tcp_send_out(tsk, opkb, seg); }
void __cdecl CAimProto::aim_proxy_helper(void* param) { file_transfer *ft = (file_transfer*)param; if (ft->requester) { if (proxy_initialize_send(ft->hConn, username, ft->icbm_cookie)) return;//error } else { if (proxy_initialize_recv(ft->hConn, username, ft->icbm_cookie, ft->port)) return;//error } //start listen for packets stuff NETLIBPACKETRECVER packetRecv = {0}; packetRecv.cbSize = sizeof(packetRecv); packetRecv.dwTimeout = INFINITE; HANDLE hServerPacketRecver = (HANDLE) CallService(MS_NETLIB_CREATEPACKETRECVER, (WPARAM)ft->hConn, 2048 * 4); for (;;) { int recvResult = CallService(MS_NETLIB_GETMOREPACKETS, (WPARAM)hServerPacketRecver, (LPARAM)&packetRecv); if (recvResult == 0) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } if (recvResult == SOCKET_ERROR) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } if (recvResult > 0) { unsigned short length = _htons(*(unsigned short*)&packetRecv.buffer[0]); packetRecv.bytesUsed = length + 2; unsigned short type = _htons(*(unsigned short*)&packetRecv.buffer[4]); if (type == 0x0001) { unsigned short error = _htons(*(unsigned short*)&packetRecv.buffer[12]); switch (error) { case 0x000D: ShowPopup("Proxy Server File Transfer Error: Bad Request.", ERROR_POPUP); break; case 0x0010: ShowPopup("Proxy Server File Transfer Error: Initial Request Timed Out.", ERROR_POPUP); break; case 0x001A: ShowPopup("Proxy Server File Transfer Error: Accept Period Timed Out.", ERROR_POPUP); break; case 0x000e: ShowPopup("Proxy Server File Transfer Error: Incorrect command syntax.", ERROR_POPUP); break; case 0x0016: ShowPopup("Proxy Server File Transfer Error: Unknown command issued.", ERROR_POPUP); break; } } else if (type == 0x0003) { unsigned short port = _htons(*(unsigned short*)&packetRecv.buffer[12]); unsigned long ip = _htonl(*(unsigned long*)&packetRecv.buffer[14]); aim_send_file(hServerConn, seqno, ip, port, true, ft); LOG("Stage %d Proxy ft and we are not the sender.", ft->req_num); } else if (type == 0x0005) { if (!ft->requester) { aim_file_ad(hServerConn, seqno, ft->sn, ft->icbm_cookie, false, ft->max_ver); ft->accepted = true; } sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_CONNECTED, ft, 0); int i; for (i = 21; --i; ) { if (Miranda_Terminated()) return; Sleep(100); if (ft->accepted) break; } if (i == 0) { sendBroadcast(ft->hContact, ACKTYPE_FILE, ACKRESULT_FAILED, ft, 0); break; } packetRecv.dwTimeout = 350000; int result; if (ft->sending)//we are sending result = sending_file(ft, hServerPacketRecver, packetRecv); else result = receiving_file(ft, hServerPacketRecver, packetRecv); sendBroadcast(ft->hContact, ACKTYPE_FILE, result ? ACKRESULT_FAILED : ACKRESULT_SUCCESS, ft, 0); break; } } } Netlib_CloseHandle(hServerPacketRecver); Netlib_CloseHandle(ft->hConn); ft_list.remove_by_ft(ft); }