int fserve_client_create(client_t *httpclient, char *path) { fserve_t *client = calloc(1, sizeof(fserve_t)); int bytes; int client_limit; ice_config_t *config = config_get_config(); client_limit = config->client_limit; config_release_config(); client->file = fopen(path, "rb"); if(!client->file) { client_send_404(httpclient, "File not readable"); return -1; } client->client = httpclient; client->offset = 0; client->datasize = 0; client->ready = 0; client->buf = malloc(BUFSIZE); global_lock(); if(global.clients >= client_limit) { httpclient->respcode = 504; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 504 Server Full\r\n" "Content-Type: text/html\r\n\r\n" "<b>Server is full, try again later.</b>\r\n"); if(bytes > 0) httpclient->con->sent_bytes = bytes; fserve_client_destroy(client); global_unlock(); return -1; } global.clients++; global_unlock(); httpclient->respcode = 200; bytes = sock_write(httpclient->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Type: %s\r\n\r\n", fserve_content_type(path)); if(bytes > 0) httpclient->con->sent_bytes = bytes; sock_set_blocking(client->client->con->sock, SOCK_NONBLOCK); sock_set_nodelay(client->client->con->sock); thread_mutex_lock (&pending_lock); client->next = (fserve_t *)pending_list; pending_list = client; thread_mutex_unlock (&pending_lock); return 0; }
uint32_t proto_send(uint16_t header, sock_t* s) { prefix_t prefix; prefix.header = (header | MAGIC); prefix.length = 0; sock_write(s, &prefix, sizeof(prefix.header)); sock_write(s, &prefix.length, sizeof(prefix.length)); return sock_flush(s); }
int GUIAPI ServerSendReply (int clifd, const void* reply, int len) { MSG reply_msg = {HWND_INVALID, 0}; if (!mgIsServer) return SOCKERR_IO; /* send a reply message to indicate this is a reply of request */ if (sock_write (clifd, &reply_msg, sizeof (MSG)) < 0) return SOCKERR_IO; if (sock_write (clifd, reply, len) < 0) return SOCKERR_IO; return SOCKERR_OK; }
void main() { int bytes_read; char buffer[100]; /* Currently (DC 7.30), printf() has max 127 bytes it can output. */ tcp_Socket socket; sock_init(); while(1) { tcp_listen(&socket,PORT,0,0,NULL,0); printf("Waiting for connection...\n"); while(!sock_established(&socket) && sock_bytesready(&socket)==-1) tcp_tick(NULL); printf("Connection received...\n"); do { bytes_read=sock_fastread(&socket,buffer,sizeof(buffer)-1); if(bytes_read>0) { buffer[bytes_read]=0; printf("%s",buffer); sock_write(&socket,buffer,bytes_read); } } while(tcp_tick(&socket)); printf("Connection closed...\n"); } }
int socket_write(Socket_T S, void *b, int size) { int n= 0; void *p= b; ASSERT(S); /* Clear any extra data read from the server */ socket_reset(S); while(size > 0) { if(S->ssl) { n= send_ssl_socket(S->ssl, p, size, S->timeout); } else { if(S->type==SOCK_DGRAM) n= udp_write(S->socket, p, size, S->timeout); else n= sock_write(S->socket, p, size, S->timeout); } if(n <= 0) break; p+= n; size-= n; } if(n < 0) { /* No write or a partial write is an error */ return -1; } return (int)(p - b); }
static void exec_lua_cb( Widget w, void *u, void *c ) { TRACE(1,"arg: %s", (char*) u); int sln = CWNET.sln; int fd = sln_get_fd(sln ); if( fd < 0 ) { WARN("no connections to lua server"); return; } char *filename = (char*) u; FILE *fp = fopen( filename, "r" ); if(!fp) WARN("faild to open %s", filename ); else { int buf = m_create(1000,1); int ch; int crc=0; /* write program header to slop buffer */ crc = slop_encode_str(buf,crc, "PUT:"); /* write program to slop buffer, -1 will finalize the packet */ do { ch=fgetc(fp); crc = slop_encode(buf,crc, ch); } while( ch >= 0); fclose(fp); sock_write(fd,buf); } }
void send_option(int type, int opt) { int ret; int size; unsigned char *obp; obp = &Comobuf[0]; *obp++ = IAC; *obp++ = type; *obp++ = opt; size = 3; obp = &Comobuf[0]; if (Debug > 2) { sysmessage(MSG_DEBUG, "Sock_write, 3 bytes: %02X %02X %02X\n", Comobuf[0], Comobuf[1], Comobuf[2]); } while (size) { if ((ret = sock_write(obp, size)) == -1) { SET_EVENT(EV_RN, EV_RNHANG, 0, 0); break; } else if (ret != size) { sysmessage(MSG_NOTICE, "Partial write in send_option: %d/%d\n", ret, size); sysdelay(ROOM_DELAY); /* Wait for room */ } size -= ret; obp += ret; } }
static void command_buildm3u(client_t *client, source_t *source, int response) { char *username = NULL; char *password = NULL; char *host = NULL; int port = 0; ice_config_t *config; COMMAND_REQUIRE(client, "username", username); COMMAND_REQUIRE(client, "password", password); config = config_get_config(); host = strdup(config->hostname); port = config->port; config_release_config(); client->respcode = 200; sock_write(client->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Type: audio/x-mpegurl\r\n" "Content-Disposition = attachment; filename=listen.m3u\r\n\r\n" "http://%s:%s@%s:%d%s\r\n", username, password, host, port, source->mount ); free(host); client_destroy(client); }
int SocketWrite(SocketRef const socket, uv_buf_t const *const buf) { if(!socket) return UV_EINVAL; assert(socket->wr->len < WRITE_BUFFER); int rc; if(buf->len > WRITE_BUFFER) { rc = SocketFlush(socket, false); if(rc < 0) return rc; rc = sock_write(socket, buf); if(rc < 0) return rc; return 0; } if(!socket->wr->base) { socket->wr->base = malloc(WRITE_BUFFER); if(!socket->wr->base) return UV_ENOMEM; } size_t const used = MIN(WRITE_BUFFER - socket->wr->len, buf->len); size_t const rest = buf->len - used; memcpy(socket->wr->base + socket->wr->len, buf->base, used); socket->wr->len += used; if(WRITE_BUFFER == socket->wr->len) { rc = SocketFlush(socket, true); if(rc < 0) return rc; memcpy(socket->wr->base, buf->base + used, rest); socket->wr->len = rest; assert(rest < WRITE_BUFFER); } return 0; }
int32_t _connector_write(struct handler_t* h) { char* buffer; int32_t nwrite, res; connector_t* con = (connector_t*)h; nwrite = connbuffer_read_len(con->write_buf); if (nwrite <= 0) return 0; buffer = connbuffer_read_buffer(con->write_buf); res = sock_write(con->h.fd, buffer, nwrite); if (res < 0) { // can't write now if (ERR_EAGAIN == ERRNO || ERR_EWOULDBLOCK == ERRNO || ERR_EINTR == ERRNO) { return 0; } else { printf("write %d errno=%d\n", con->h.fd, ERRNO); return -1; } } else if (0 == res) { return -1; } else { connbuffer_read_nocopy(con->write_buf, res); if (res == nwrite) { reactor_modify(con->r, &con->h, EVENT_IN); } } return 0; }
static int netchar_open(struct inode* inodp, struct file* fp) { int ret; struct fop_request req; struct fop_reply rep; memset(&req, 0, sizeof(req)); memset(&rep, 0, sizeof(rep)); _PKI("open...."); req.call = FOP_OPEN; req.flags = fp->f_flags; req.mode = fp->f_mode; ret = sock_write(nc_socket, &req, sizeof(req)); _PKI("sendmsg: %i", ret); ret = sock_read(nc_socket, &rep, sizeof(rep)); _PKI("recvmsg: %i", ret); _PKI("open returning %i", rep.open); return rep.open; }
/* Always in ASCII mode */ int sock_puts( SOCKET s, const char *pszText ) { sock_flushbuffer( s ); int nBufLen = sock_write( s, pszText ); send( s, "\r\n", 2, 0 ); //printf("\n *DEBUG* sock_puts=[%s]", pszText ); return nBufLen + 2; }
/* * Send the response to the remote machine */ void SendMessage(tcp_Socket *tcpSock, unsigned key, char* messages[]) { sock_write(tcpSock, messages[key], strlen(messages[key])+1); sock_close(tcpSock); while (tcp_tick(tcpSock) != 0); return; }
static int sock_puts(BIO *bp, const char *str) { int n, ret; n = strlen(str); ret = sock_write(bp, str, n); return (ret); }
void client_send_403(client_t *client) { int bytes = sock_write(client->con->sock, "HTTP/1.0 403 Forbidden\r\n" "\r\n" "Access restricted.\r\n"); if(bytes > 0) client->con->sent_bytes = bytes; client->respcode = 403; client_destroy(client); }
static int _send_event_to_client(stats_event_t *event, connection_t *con) { int ret; /* send data to the client!!!! */ ret = sock_write(con->sock, "EVENT %s %s %s\n", (event->source != NULL) ? event->source : "global", event->name, event->value ? event->value : "null"); return (ret == -1) ? 0 : 1; }
int SocketFlush(SocketRef const socket, bool const more) { if(!socket) return UV_EINVAL; if(0 == socket->wr->len) return 0; assert(socket->wr->base); int rc = sock_write(socket, socket->wr); if(!more) FREE(&socket->wr->base); socket->wr->len = 0; return rc; }
/* * Write a printf() style and newline terminated message to the socket * Return 1 if all bytes where successfully written, and 0 if not. * Assert Class: 2 * Potential problems: Will truncate the string if longer than BUFSIZE bytes. */ int sock_write_line(SOCKET sockfd, const char *fmt, ...) { char buff[BUFSIZE]; va_list ap; va_start(ap, fmt); vsnprintf(buff, BUFSIZE, fmt, ap); return sock_write(sockfd, "%s\r\n", buff); }
static ssize_t netchar_write(struct file* fp, const char *buffer, size_t length, loff_t* offset) { int ret; struct fop_request req; struct fop_reply rep; void* payload = NULL; memset(&req, 0, sizeof(req)); memset(&rep, 0, sizeof(rep)); _PKI("write..."); req.call = FOP_WRITE; req.count = length; ret = sock_write(nc_socket, &req, sizeof(req)); _PKI("sendmsg: %i", ret); payload = kmalloc(length, GFP_KERNEL); if(payload == NULL){ _PKA("Write Error: kmalloc returned NULL"); return 0; } ret = copy_from_user(payload, buffer, length); _PKI("payload copy returned: %i", ret); ret = sock_write(nc_socket, payload, length); kfree(payload); payload = NULL; _PKI("write data: %i", ret); ret = sock_read(nc_socket, &rep, sizeof(rep)); _PKI("recvmsg: %i", ret); _PKI("`write returning %zi", rep.write); return rep.read; }
int comport_command(int command, int arg) { int size; int ret; unsigned char *obp; if (Nvt.comport->support == FALSE) { CLR_CMD_ACTIVE(command); /* Synchronous operation */ SET_EVENT(EV_RN, EV_RNCMDOK, 0, 0); return (0); } obp = &Comobuf[0]; *obp++ = IAC; *obp++ = SB; *obp++ = NVT_COM_PORT_OPTION; *obp++ = (unsigned char) command; switch (command) { case USR_COM_SET_BAUDRATE: SET_VALUE_4(obp, arg); obp += 4; break; default: SET_VALUE_1(obp, arg); obp += 1; break; } *obp++ = IAC; *obp++ = SE; size = (int) (obp - &Comobuf[0]); obp = &Comobuf[0]; if (Debug > 2) { sysmessage(MSG_DEBUG, "Sock_write, %d bytes: %02X %02X %02X %02X %02X %02X %02X %02X\n", size, Comobuf[0], Comobuf[1], Comobuf[2], Comobuf[3], Comobuf[4], Comobuf[5], Comobuf[6], Comobuf[7]); } while (size) { if ((ret = sock_write(obp, size)) == -1) { SET_EVENT(EV_RN, EV_RNHANG, 0, 0); return (-1); } else if (ret != size) { sysmessage(MSG_NOTICE, "Partial write in send_comport: %d/%d\n", ret, size); sysdelay(ROOM_DELAY); /* Wait for room */ } size -= ret; obp += ret; } return (0); }
void stats_sendxml(client_t *client) { int bytes; stats_event_t *event; stats_event_t *queue; xmlDocPtr doc; xmlNodePtr node, srcnode; int len; xmlChar *buff = NULL; source_xml_t *snd; source_xml_t *src_nodes = NULL; queue = NULL; _dump_stats_to_queue(&queue); doc = xmlNewDoc("1.0"); node = xmlNewDocNode(doc, NULL, "icestats", NULL); xmlDocSetRootElement(doc, node); event = _get_event_from_queue(&queue); while (event) { if (event->source == NULL) { xmlNewChild(node, NULL, event->name, event->value); } else { srcnode = _find_xml_node(event->source, &src_nodes, node); xmlNewChild(srcnode, NULL, event->name, event->value); } _free_event(event); event = _get_event_from_queue(&queue); } xmlDocDumpMemory(doc, &buff, &len); xmlFreeDoc(doc); client->respcode = 200; bytes = sock_write(client->con->sock, "HTTP/1.0 200 OK\r\n" "Content-Length: %d\r\n" "Content-Type: text/xml\r\n" "\r\n", len); if (bytes > 0) client->con->sent_bytes += bytes; else goto send_error; bytes = sock_write_bytes(client->con->sock, buff, len); if (bytes > 0) client->con->sent_bytes += bytes; send_error: while (src_nodes) { snd = src_nodes->next; free(src_nodes->mount); free(src_nodes); src_nodes = snd; } if (buff) xmlFree(buff); }
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); int err = 0; mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err); if (len == MP_STREAM_ERROR) { mp_raise_OSError(err); } return mp_obj_new_int_from_uint(len); }
void output_data(int *in_grid_array, int coord[4], int *out_grid_array, int width, int height) { int i, j, k; k = 0; for (j=coord[2]; j<=coord[3]; j++) { for (i=coord[0]; i<=coord[1]; i++) { out_grid_array[(j * height) + i] = in_grid_array[k]; k++; } } if (!use_stdin) { sock_write(sock, coord, 4 * sizeof(int)); sock_write(sock, in_grid_array, ((coord[1] + 1 - coord[0]) * (coord[3] + 1 - coord[2])) * sizeof(int)); /* send the data to the visualizer */ } }
int sockprintf(int fd, const char *fmt, ...) { va_list ap; char buf[4096]; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); return (sock_write(fd, buf, strlen(buf))); }
/* * "background" process handling echo + discard TCP sockets. */ static void echo_discard_daemon (void) { sock_type *s = (sock_type*) &tcp_echo_sock; if (sock_dataready(s)) { BYTE buf[ETH_MAX_DATA]; int len = sock_read (s, buf, sizeof(buf)); sock_write (s, buf, len); } }
static void html_success(client_t *client, char *message) { int bytes; client->respcode = 200; bytes = sock_write(client->con->sock, "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" "<html><head><title>Admin request successful</title></head>" "<body><p>%s</p></body></html>", message); if(bytes > 0) client->con->sent_bytes = bytes; client_destroy(client); }
/* * Send the response to the remote machine. Close the socket. */ void SendMessage(tcp_Socket *tcpSock, unsigned key, char* messages[]) { auto char * p; auto int len; /* Use sock_fastwrite() here to encourage good programming practices. */ sock_write(tcpSock, messages[key-1], strlen(messages[key-1])+1); sock_close(tcpSock); while (tcp_tick(tcpSock) != 0); dispClear(); dispPrintf("Response sent"); }
static int fd_puts(BIO *bp, const char *str) #endif { int n,ret; n=strlen(str); #ifndef BIO_FD ret=sock_write(bp,str,n); #else ret=fd_write(bp,str,n); #endif return(ret); }
int vsock_oflush( vsock_t *vsock ) { int rc = 0; if ( vsock->ocnt > 0 ) { /* debug("vsock_oflush: output buffer size = %d, val = %s", vsock->ocnt, vsock->obuf ); */ rc = sock_write( vsock->s, vsock->obuf, vsock->ocnt, vsock->otimeout ); vsock->optr = vsock->obuf; vsock->ocnt = 0; debug("vsock_oflush: wirten %d bytes", rc); } return rc; }
unsigned int ZION_CALLBACK TcpThreadFunc(void * point) { char *p = (char*)point; SOCK_HANDLE handle; SOCK_ADDR sock_addr; char read_buf[1024]; sock_init(); if(sock_str2addr(p, &sock_addr)==NULL) { printf("tcp sock_str2addr function error\n"); goto FINISH_STATE; } handle = sock_bind(&sock_addr, 0); if(handle == SOCK_INVALID_HANDLE) { printf("tcp bind function error\n"); goto ERROR_STATE; } printf("tcp %s wait for client connect....\n", p); handle = sock_accept(handle, NULL); if(handle == SOCK_INVALID_HANDLE) { printf("tcp socket_accept function error\n"); goto ERROR_STATE; } printf("tcp client connect access\n"); while(1) { memset(read_buf, 0, sizeof(read_buf)); //sock_readbuf(handle, (void*)read_buf, sizeof(read_buf)); sock_read(handle, (void*)read_buf, sizeof(read_buf)); printf("tcp client send '%s'\n", read_buf); sock_write(handle, (void*)read_buf, (int)strlen(read_buf)); } ERROR_STATE: sock_unbind(handle); FINISH_STATE: sock_final(); return 0; }