int ClientAuthenticate(const char *protocol, const char *name, const char *pwd, const char *domain, const char *hostname) { tSmbNtlmAuthRequest request; tSmbNtlmAuthChallenge challenge; tSmbNtlmAuthResponse response; short len; buildSmbNtlmAuthRequest(&request,name?(char*)name:(char*)"",domain?(char*)domain:(char*)""); len=htons(SmbLength(&request)); if(tcp_write(&len,sizeof(len))<0) return 0; if(tcp_write(&request,SmbLength(&request))<0) return 0; if(tcp_read(&len,2)!=2) return 0; if(!len) return 0; if(tcp_read(&challenge,ntohs(len))!=ntohs(len)) return 0; buildSmbNtlmAuthResponse(&challenge, &response, name?(char*)name:(char*)"", pwd?(char*)pwd:(char*)""); len=htons(SmbLength(&response)); if(tcp_write(&len,sizeof(len))<0) return 0; if(tcp_write(&response, SmbLength(&response))<0) return 0; return 1; }
static void transport_tcp_rx_cb(void *buf, UINT32 size) { EFI_STATUS ret; switch (tcp_state) { case INITIALIZING: if (size != sizeof(PROTOCOL_VERSION) || strncmp((CHAR8 *)buf, (CHAR8 *)PROTOCOL_VERSION, size)) { error(L"Invalid fastboot TCP protocol version"); tcp_state = ERROR; return; } remaining_data = 0; tcp_state = READY; start_callback(); return; case WAITING_DATA_SIZE: if (size != sizeof(remaining_data) || buf != &remaining_data) { error(L"Waiting data size %d", size); return; } remaining_data = be64toh(remaining_data); tcp_state = WAITING_DATA; ret = tcp_read(rx.buf, min(rx.size, remaining_data)); if (EFI_ERROR(ret)) { efi_perror(ret, L"transport_tcp_rx tcp_read failed"); return; } return; case WAITING_DATA: if (size + rx.used > rx.size || size > remaining_data) { error(L"received too much data"); tcp_state = ERROR; return; } rx.used += size; remaining_data -= size; if (rx.used == rx.size || remaining_data == 0) { tcp_state = READY; rx_callback(rx.buf, rx.used); return; } /* Still more data to read. */ ret = tcp_read(rx.buf + rx.used, min(rx.size - rx.used, remaining_data)); if (EFI_ERROR(ret)) efi_perror(ret, L"Transport_tcp_rx tcp_read failed"); return; default: error(L"Inconsistent TCP state %d at rx", tcp_state); } }
int server_connect(const struct protocol_interface *protocol, int verify_only) { char current_user[256]; char remote_user[256]; char tmp[32]; unsigned char c; int listen_port=0; if(!current_server->current_root->hostname || !current_server->current_root->directory || current_server->current_root->port) return CVSPROTO_BADPARMS; if(tcp_connect_bind(current_server->current_root->hostname,"514",512,1023)<1) return CVSPROTO_FAIL; #ifdef _WIN32 { DWORD dwLen = 256; GetUserNameA(current_user,&dwLen); } #else strncpy(current_user,getpwuid(geteuid())->pw_name,sizeof(current_user)); #endif if(current_server->current_root->username) strncpy(remote_user,current_server->current_root->username,sizeof(remote_user)); else strncpy(remote_user,current_user,sizeof(remote_user)); snprintf(tmp,sizeof(tmp),"%d",listen_port); if(tcp_write(tmp,strlen(tmp)+1)<1) return CVSPROTO_FAIL; if(tcp_write(current_user,strlen(current_user)+1)<1) return CVSPROTO_FAIL; if(tcp_write(remote_user,strlen(remote_user)+1)<1) return CVSPROTO_FAIL; #define CMD "cvs server" if(tcp_write(CMD,sizeof(CMD))<1) return CVSPROTO_FAIL; if(tcp_read(&c,1)<1) return CVSPROTO_FAIL; if(c) { char msg[257]; if((c=tcp_read(msg,256))<1) return CVSPROTO_FAIL; msg[c]='\0'; server_error(0,"rsh server reported: %s",msg); return CVSPROTO_FAIL; } return CVSPROTO_SUCCESS_NOPROTOCOL; /* :server: doesn't need login response */ }
void yunReadProvisioningInfo() { int socketHandle = -1; char buf[128]; char value[64]; memset(value, 0, 64); if ((socketHandle = tcp_connect("127.0.0.1", 5700)) > 0) { // read appId memset(buf, 0, 128); tcp_write(socketHandle, "{\"command\": \"get\", \"key\": \"appId\"}\n"); if (tcp_read(socketHandle, buf, 128, 2)) { getValueFromJSON(buf, "value", value, 64); if(strcmp("null", value) != 0) { strncpy(g_cAppID, value, sizeof(g_cAppID)); } } // read clientKey memset(buf, 0, 128); tcp_write(socketHandle, "{\"command\": \"get\", \"key\": \"clientKey\"}\n"); if (tcp_read(socketHandle, buf, 128, 2)) { getValueFromJSON(buf, "value", value, 64); if(strcmp("null", value) != 0) { strncpy(g_cClientKey, value, sizeof(g_cClientKey)); } } // read installationId memset(buf, 0, 128); tcp_write(socketHandle, "{\"command\": \"get\", \"key\": \"installationId\"}\n"); if (tcp_read(socketHandle, buf, 128, 2)) { getValueFromJSON(buf, "value", value, 64); if(strcmp("null", value) != 0) { strncpy(g_cInstallationID, value, sizeof(g_cInstallationID)); } } // read sessionToken memset(buf, 0, 128); tcp_write(socketHandle, "{\"command\": \"get\", \"key\": \"sessionToken\"}\n"); if (tcp_read(socketHandle, buf, 128, 2)) { getValueFromJSON(buf, "value", value, 64); if(strcmp("null", value) != 0) { strncpy(g_cSessionToken, value, sizeof(g_cSessionToken)); } } tcp_close(socketHandle); } }
int main() { open_pcap(); while(1) tcp_read(); }
int tcp_gets( struct tcp_chan *chan, char *str, size_t ct, unsigned int timeout) { int rc = 0; struct tcpdisbuf *tp; long long data_read = 0; long long data_avail = 0; tp = &chan->readbuf; /* length of usable data in current buffer */ data_avail = tp->tdis_eod - tp->tdis_leadp; while ((size_t)data_avail < ct) { /* not enough data, try to get more */ if ((rc = tcp_read(chan, &data_read, &data_avail, timeout)) != PBSE_NONE) { if (data_read == 0) rc = -2; else rc = -1; return(rc); /* Error or EOF */ } } memcpy((char *)str, tp->tdis_leadp, ct); tp->tdis_leadp += ct; return((int)ct); } /* END tcp_gets() */
int transport_read(rdpTransport* transport, STREAM* s) { int status = -1; while (True) { if (transport->layer == TRANSPORT_LAYER_TLS) status = tls_read(transport->tls, stream_get_tail(s), stream_get_left(s)); else if (transport->layer == TRANSPORT_LAYER_TCP) status = tcp_read(transport->tcp, stream_get_tail(s), stream_get_left(s)); if (status == 0 && transport->blocking) { freerdp_usleep(transport->usleep_interval); continue; } break; } #ifdef WITH_DEBUG_TRANSPORT if (status > 0) { printf("Local < Remote\n"); freerdp_hexdump(s->data, status); } #endif return status; }
/* * TCP server example */ void tcp_server_example (ip_t *ip, int serv_port) { tcp_socket_t *lsock, *sock; char buffer [256]; int n; lsock = tcp_listen (ip, serv_port); if (! lsock) { error ("Error on listen"); return; } for (;;) { sock = tcp_accept (lsock); if (! sock) { error ("Error on accept"); break; } n = tcp_read (sock, buffer, 256); if (n < 0) error ("Error reading from socket"); else { buffer[255] = 0; printf ("Here is the message: %s\n", buffer); n = tcp_write (sock, "I got your message", 19); if (n < 0) error ("Error writing to socket"); } tcp_close (sock); mem_free (sock); } tcp_close (lsock); mem_free (lsock); }
/* * TCP client example */ void tcp_client_example (ip_t *ip, unsigned char *serv_addr, int serv_port, char *message) { tcp_socket_t *sock; char buffer [256]; int n; sock = tcp_connect (ip, serv_addr, serv_port); if (! sock) { error ("Error connecting"); return; } strncpy (buffer, message, 256); buffer[255] = 0; n = tcp_write (sock, buffer, strlen (buffer) + 1); if (n < 0) error ("Error writing to socket"); else { n = tcp_read (sock, buffer, 256); if (n < 0) error ("Error reading from socket"); else { buffer[255] = 0; printf ("%s\n", buffer); } } tcp_close (sock); mem_free (sock); }
static int discard_worker(void *socket) { uint64_t count = 0; uint32_t crc = 0; tcp_socket_t *s = socket; lk_time_t t = current_time(); for (;;) { uint8_t buf[1024]; ssize_t ret = tcp_read(s, buf, sizeof(buf)); if (ret <= 0) break; crc = crc32(crc, buf, ret); count += ret; } t = current_time() - t; TRACEF("discard worker exiting, read %llu bytes in %u msecs (%llu bytes/sec), crc32 0x%x\n", count, (uint32_t)t, count * 1000 / t, crc); tcp_close(s); return 0; }
int zmq::socks_response_decoder_t::input (fd_t fd_) { size_t n = 0; if (bytes_read < 5) n = 5 - bytes_read; else { const uint8_t atyp = buf[3]; zmq_assert (atyp == 0x01 || atyp == 0x03 || atyp == 0x04); if (atyp == 0x01) n = 3 + 2; else if (atyp == 0x03) n = buf[4] + 2; else if (atyp == 0x04) n = 15 + 2; } const int rc = tcp_read (fd_, buf + bytes_read, n); if (rc > 0) { bytes_read += static_cast<size_t> (rc); if (buf[0] != 0x05) return -1; if (bytes_read >= 2) if (buf[1] > 0x08) return -1; if (bytes_read >= 3) if (buf[2] != 0x00) return -1; if (bytes_read >= 4) { const uint8_t atyp = buf[3]; if (atyp != 0x01 && atyp != 0x03 && atyp != 0x04) return -1; } } return rc; }
/* Called on head of multibus group */ SIZE_OR_ERROR COM_read_with_timeout( BYTE * data, size_t length, struct connection_in *connection) { struct port_in * pin ; if ( length == 0 ) { return 0 ; } if ( connection == NO_CONNECTION || data == NULL ) { // bad parameters return -EIO ; } pin = connection->pown ; // unlike write or open, a closed connection isn't automatically opened. // the reason is that reopening won't have the data waiting. We really need // to restart the transaction from the "write" portion if ( FILE_DESCRIPTOR_NOT_VALID( pin->file_descriptor ) ) { return -EBADF ; } else { size_t actual_size ; ZERO_OR_ERROR zoe = tcp_read( pin->file_descriptor, data, length, &(pin->timeout), &actual_size ) ; if ( zoe == -EBADF ) { COM_close(connection) ; return zoe ; } else { return actual_size ; } } }
int handle_fd_can_write (struct event *e, void *d) { struct tcp_map *map = d; uchar tb[65536]; int len; len = tcp_read (map->tcb, tb, sizeof (tb)); if (len < sizeof (tb)) map->e_fd_write = NULL; len = write (map->fd, tb, len); if (map->fp) fprintf (map->fp, "wrote %d to fd %d from buffer\n", len, map->fd); if (len <= 0) { if (len == -1) { if (errno == EAGAIN) return 1; perror ("read"); } map->e_fd_write = NULL; tcp_close (map->tcb, 0); kill_map (map); return 0; } return map->e_fd_write != NULL; }
int transport_read(rdpTransport* transport, STREAM* s) { int status = -1; while (True) { if (transport->layer == TRANSPORT_LAYER_TLS) status = tls_read(transport->tls, s->data, s->size); else if (transport->layer == TRANSPORT_LAYER_TCP) status = tcp_read(transport->tcp, s->data, s->size); if (status == 0 && transport->blocking) { nanosleep(&transport->ts, NULL); continue; } break; } #ifdef WITH_DEBUG_TRANSPORT if (status > 0) { printf("Server > Client\n"); freerdp_hexdump(s->data, status); } #endif return status; }
static bool cli_read_hdr(void *rst_priv, void *priv, unsigned int buflen, bool success) { struct client *cli = rst_priv; uint32_t size; if (!success) return false; if (memcmp(cli->ubbp.magic, PUSHPOOL_UBBP_MAGIC, 4)) return false; cli->ubbp.op_size = le32toh(cli->ubbp.op_size); size = UBBP_SIZE(cli->ubbp.op_size); if (size > CLI_MAX_MSG) return false; if (size == 0) return cli_msg(cli); cli->msg = malloc(size); if (!cli->msg) return false; return tcp_read(&cli->rst, cli->msg, size, cli_read_msg, NULL); }
int main(){ tcp_connect(); tcp_read(nbytestoread); tcp_write(nbytestowrite); tcp_close(); return 0 ; }
int zmq::socks_choice_decoder_t::input (fd_t fd_) { zmq_assert (bytes_read < 2); const int rc = tcp_read (fd_, buf + bytes_read, 2 - bytes_read); if (rc > 0) { bytes_read += static_cast<size_t> (rc); if (buf[0] != 0x05) return -1; } return rc; }
int transport_read(rdpTransport* transport, STREAM* s) { int status = -1; if (transport->layer == TRANSPORT_LAYER_TLS) status = tls_read(transport->tls, s->data, s->size); else if (transport->layer == TRANSPORT_LAYER_TCP) status = tcp_read(transport->tcp, s->data, s->size); return status; }
// receives image stream header information. returns 1 on success, // 1 on error int recv_header(int fd, struct ImageParams * ip) { if(!tcp_read(fd, ip, sizeof *ip)) return 0; ip->nChannels = ntohl(ip->nChannels); ip->depth = ntohl(ip->depth); ///< image depth per channel ip->width = ntohl(ip->width); ///< image width ip->height = ntohl(ip->height); ///< image height return 1; }
/* Called on head of multibus group */ static SIZE_OR_ERROR COM_read_get_size( BYTE * data, size_t length, struct connection_in *connection ) { size_t actual_size ; struct port_in * pin = connection->pown ; // tcp_read seems to work with serial and network ZERO_OR_ERROR zoe = tcp_read( pin->file_descriptor, data, length, &(pin->timeout), &actual_size ) ; if ( zoe < 0 ) { COM_close(connection) ; return zoe ; } else { return actual_size ; } }
static int tcp_getc(int fd) { int x; struct tcpdisbuf *tp; tp = tcp_get_readbuf(fd); if (tp->tdis_lead >= tp->tdis_eod) { /* not enought data, try to get more */ x = tcp_read(fd); if (x <= 0) return ((x == -2) ? -2 : -1); /* Error or EOF */ } return ((int)tp->tdis_thebuf[tp->tdis_lead++]); }
static int _tcp_read(lua_State *L) { int fd = luaL_checkint(L, 1); int count = luaL_optint(L, 2, 1024); char *data = malloc(count); int ret = tcp_read(fd, data, count); lua_pushinteger(L, ret); if(ret <= 0 ) { free(data); return 1; } lua_pushlightuserdata(L, data); return 2; }
int http_download_photo(char *path, char *email) { struct connection *conn; struct connect_options conn_opts = {0}; int error_code; int rc; if (!open_socket_lib()) return 0; rc = 0; if ((conn = tcp_connect("simplemail.sourceforge.net", 80, &conn_opts, &error_code))) { char *line; int download = 0; tcp_write(conn,"GET /gallery_get_image.php?",sizeof("GET /gallery_get_image.php?")-1); tcp_write(conn,email,strlen(email)); tcp_write(conn," HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n",sizeof(" HTTP/1.0\r\nhost: simplemail.sourceforge.net\r\n\r\n")-1); while ((line = tcp_readln(conn))) { if (!mystrnicmp("Content-Type: image/",line,20)) download = 1; if (line[0] == 10 && line[1] == 0) break; } if (download) { FILE *fp = fopen(path,"wb"); if (fp) { int got; char buf[1024]; while ((got = tcp_read(conn,buf,1024))>0) { fwrite(buf,1,got,fp); } rc = 1; fclose(fp); } } tcp_disconnect(conn); } close_socket_lib(); return rc; }
static int tcp_gets(int fd, char *str, size_t ct) { int x; struct tcpdisbuf *tp; tp = tcp_get_readbuf(fd); while (tp->tdis_eod - tp->tdis_lead < ct) { /* not enought data, try to get more */ x = tcp_read(fd); if (x <= 0) return x; /* Error or EOF */ } (void)memcpy(str, &tp->tdis_thebuf[tp->tdis_lead], ct); tp->tdis_lead += ct; return (int)ct; }
int main() { uint8_t ack[1]; initCamera(); tcp_init(); printf("Connected\n"); while(1) { getFrame(); printf("Recieved Frame"); tcp_write(frame, 9840); tcp_read(ack, 1); } closeServer(); return 0; }
static void fastboot_tcp_start_cb(void) { static char version[sizeof(PROTOCOL_VERSION)]; EFI_STATUS ret; tcp_state = INITIALIZING; ret = tcp_write((VOID *)PROTOCOL_VERSION, sizeof(PROTOCOL_VERSION)); if (EFI_ERROR(ret)) { efi_perror(ret, L"tcp_write failed during initialization"); return; } ret = tcp_read(version, sizeof(version)); if (EFI_ERROR(ret)) { efi_perror(ret, L"tcp_read failed during initialization"); return; } }
int transport_read_layer(rdpTransport* transport, BYTE* data, int bytes) { int read = 0; int status = -1; while (read < bytes) { if (transport->layer == TRANSPORT_LAYER_TLS) status = tls_read(transport->TlsIn, data + read, bytes - read); else if (transport->layer == TRANSPORT_LAYER_TCP) status = tcp_read(transport->TcpIn, data + read, bytes - read); else if (transport->layer == TRANSPORT_LAYER_TSG) status = tsg_read(transport->tsg, data + read, bytes - read); else if (transport->layer == TRANSPORT_LAYER_TSG_TLS) { status = tls_read(transport->TsgTls, data + read, bytes - read); } /* blocking means that we can't continue until this is read */ if (!transport->blocking) return status; if (status < 0) { /* A read error indicates that the peer has dropped the connection */ transport->layer = TRANSPORT_LAYER_CLOSED; return status; } read += status; if (status == 0) { /* * instead of sleeping, we should wait timeout on the * socket but this only happens on initial connection */ USleep(transport->SleepInterval); } } return read; }
EFI_STATUS fastboot_tcp_read(void *buf, UINT32 size) { EFI_STATUS ret; if (tcp_state != READY) { error(L"Inconsistent TCP state %d at read", tcp_state); return EFI_INVALID_PARAMETER; } rx.buf = buf; rx.size = size; rx.used = 0; tcp_state = WAITING_DATA_SIZE; ret = tcp_read(&remaining_data, sizeof(remaining_data)); if (EFI_ERROR(ret)) efi_perror(ret, L"fastboot_tcp_read failed"); return ret; }
static int echo_worker(void *socket) { tcp_socket_t *s = socket; for (;;) { uint8_t buf[1024]; ssize_t ret = tcp_read(s, buf, sizeof(buf)); if (ret <= 0) break; tcp_write(s, buf, ret); if (ret <= 0) break; } TRACEF("echo worker exiting\n"); tcp_close(s); return 0; }
// ------------------------------------------------ // Function: smtp_ok() // ------------------------------------------------ // Input: - // Output: TRUE if SMTP sever answers ok // ------------------------------------------------ // Description: Waits for an answer from the // server and returns TRUE if ok // (2xx or 3xx codes) // ------------------------------------------------ BOOL smtp_ok(void) { PPBUF buf; BOOL res; // ------------------- // wait for a response // ------------------- os_set_timeout(TIMEOUT_SMTP); buf = tcp_read(SOCKET_SMTP); if(buf == NULL) return FALSE; // -------------- // analyse answer // -------------- res = FALSE; if(buf->data[0] == '2') res = TRUE; if(buf->data[0] == '3') res = TRUE; release_buffer(buf); return res; }