static void send_response(wurfld_connection_context *ctx) { char *useragent = ctx->useragent; DEBUG1("Worker %p is looking up useragent : %s", pthread_self(), useragent); // this might be unnecessary if libwurfl is thread-safe // XXX - needs to be checked wurfld_get_capabilities(useragent, ctx->output); if (use_http) { char response_header[1024]; sprintf(response_header, "%s 200 OK\r\n" "Content-Type: application/json\r\n" "Content-length: %d\r\n" "Server: wurfld\r\n" "Connection: Close\r\n\r\n", ctx->is_http10 ? "HTTP/1.0" : "HTTP/1.1", fbuf_used(ctx->output)); int err = write_socket(ctx->fd, response_header, strlen(response_header)); if (err != 0) { ERROR("(%p) Can't write the response header : %s", pthread_self(), strerror(errno)); } } if (write_socket(ctx->fd, fbuf_data(ctx->output), fbuf_used(ctx->output)) != 0) { ERROR("(%p) Can't write the response data : %s", pthread_self(), strerror(errno)); } }
/* Sends the group at group_index to the user on the socket provided */ void send_group(int group_index, int socket){ char message[2048]; memset(message,'\0',sizeof(message)); strcat(message,"S"); int i; struct chat_group *cg = all_groups[group_index]; printf("Number of users in this group, server side: %d\n",cg->num_users); //First, send the number of users that are in the group: int network_num_users = htonl(cg->num_users); write_socket(socket,&network_num_users,sizeof(int)); //Next, send the message with the users: for(i = 0; i < cg->num_users; ++i){ strcat(message,":"); strcat(message,cg->users[i]->username); strcat(message,":"); strcat(message,cg->users[i]->IP); } strcat(message,"::"); //printf("Message to send: %s\n", message); //Send the size of the message int network_message_size = htonl(sizeof(message)); write_socket(socket,&network_message_size,sizeof(int)); //Send the message itself write_socket(socket,message,sizeof(message)); }
void KCrash::startDrKonqi(const char *argv[], int argc) { int socket = openSocket(); if(socket < -1) { startDirectly(argv, argc); return; } klauncher_header header; header.cmd = LAUNCHER_EXEC_NEW; const int BUFSIZE = 8192; // make sure this is big enough char buffer[BUFSIZE + 10]; int pos = 0; long argcl = argc; memcpy(buffer + pos, &argcl, sizeof(argcl)); pos += sizeof(argcl); for(int i = 0; i < argc; ++i) { int len = strlen(argv[i]) + 1; // include terminating \0 if(pos + len > BUFSIZE) { fprintf(stderr, "BUFSIZE in KCrash not big enough!\n"); startDirectly(argv, argc); return; } memcpy(buffer + pos, argv[i], len); pos += len; } long env = 0; memcpy(buffer + pos, &env, sizeof(env)); pos += sizeof(env); long avoid_loops = 0; memcpy(buffer + pos, &avoid_loops, sizeof(avoid_loops)); pos += sizeof(avoid_loops); header.arg_length = pos; write_socket(socket, (char *)&header, sizeof(header)); write_socket(socket, buffer, pos); if(read_socket(socket, (char *)&header, sizeof(header)) < 0 || header.cmd != LAUNCHER_OK) { startDirectly(argv, argc); return; } long pid; read_socket(socket, buffer, header.arg_length); pid = *((long *)buffer); alarm(0); // Seems we made it.... for(;;) { if(kill(pid, 0) < 0) _exit(253); sleep(1); // the debugger should stop this process anyway } }
/* This function accepts a socket as a parameter and sends the list of current groups to that socket. */ void send_list(int socket) { // First, send an int of the total number of groups int network_num_groups = htonl(num_groups); write_socket(socket,&network_num_groups,sizeof(int)); //Next, go through all of the groups and send them to the user. int i; for(i = 0; i < num_groups; i++){ write_socket(socket,all_groups[i]->groupname, NAME_SIZE); } }
int Cmd_operater(int sockd) { char cmd[7],message[256]; int i; int flag=0; char buffer[512]; char send_buffer[512]; while(1) { if(timeout(sockd,200)>0) { memset(buffer,'\0',512); memset(send_buffer,'\0',512); read_socket(sockd,buffer,512); printf("recv:%s\n",buffer); sprintf(send_buffer,"server recv:%s\n",buffer); write_socket(sockd,send_buffer,512); } else { close(sockd); break; } } }
BOOL send_null_session_msg (int fd) { ssize_t ret; uint32 blank = 0; size_t len = 4; size_t nwritten = 0; char *buffer = (char *) ␣ while (nwritten < len) { ret = write_socket (fd, buffer + nwritten, len - nwritten); if (ret <= 0) { DEBUG (0, ("send_null_session_msg: Error writing %d bytes to client. %d. Exiting\n", (int) len, (int) ret)); close_sockets (); exit (1); } nwritten += ret; } DEBUG (10, ("send_null_session_msg: sent 4 null bytes to client.\n")); return True; }
/* * Function name: time_out * Description : Called after some time to close the connection. */ static void time_out() { write_socket("Application time out.\n"); destruct(); }
/* * Function name: catch_tell * Description: This is the text that normal players gets written to * their sockets. */ void catch_tell(string str) { int il; string pattern, func, euid; if (query_interactive(this_object())) // Monster is possessed { write_socket(str); return; } if (!sizeof(trig_patterns)) return; cur_text = str; for (il = 0; il < sizeof(trig_patterns); il++) { if (stringp(trig_patterns[il])) { pattern = process_string(trig_patterns[il], 1); if (trig_check(str, pattern, trig_functions[il])) return; } } }
/* * Function name: quit * Description : Remove the object from memory when the player aborts. */ static void quit() { write_socket("\nAborting application.\n"); destruct(); }
static void telnet_send_naws(struct session *ses) { unsigned char nego[128], *np; #define PUTBYTE(b) if ((b)==255) *np++=255; *np++=(b); np=nego; *np++=IAC; *np++=SB; *np++=NAWS; PUTBYTE(COLS/256); PUTBYTE(COLS%256); PUTBYTE((LINES-1-!!isstatus)/256); PUTBYTE((LINES-1-!!isstatus)%256); *np++=IAC; *np++=SE; write_socket(ses, (char*)nego, np-nego); #ifdef TELNET_DEBUG { char buf[BUFFER_SIZE], *b=buf; int neb=np-nego-2; np=nego+3; b=buf+sprintf(buf, "IAC SB NAWS "); while (np-nego<neb) b+=sprintf(b, "<%u> ", *np++); b+=sprintf(b, "IAC SE"); tintin_printf(ses, "~8~[telnet] sent: %s~-1~", buf); } #endif }
static void telnet_send_ttype(struct session *ses) { char nego[128]; const char *ttype; switch (ses->last_term_type++) { case 0: ttype=TERM; break; case 1: ttype="hardcopy"; break; case 2: ttype="unknown"; break; /* contrary to what zMud does, we cannot claim we're "vt100" or "ansi", */ /* as we obviously lack an addressable cursor */ default: ses->last_term_type=0; case 3: ttype="KBtin-"VERSION; } write_socket(ses, nego, sprintf(nego, "%c%c%c%c%s%c%c", IAC, SB, TERMINAL_TYPE, IS, ttype, IAC, SE)); #ifdef TELNET_DEBUG tintin_printf(ses, "~8~[telnet] sent: IAC SB TERMINAL-TYPE IS \"%s\" IAC SE~-1~", ttype); #endif }
int send_request(Request req) { if (! req->socket ) { req->socket = connect_socket(req->host,req->port,0); if (! req->socket) { error("Failed to connect to %s:%i\n",req->host,req->port); return 0; } add_req_socket(req->socket->fd); return 0; } if (req->length < 0) { str cmd = _("%s %s HTTP/1.1\r\n",req->method,req->path); write_socket(req->socket,cmd); request_headers(req,_("Host"),req->host); send_headers(req->socket,req->headers); req->length = outbound_content_length(req->contents,req->raw_contents); return req->contents != NULL || req->raw_contents != NULL ; } req->written += req->contents ? send_contents(req->socket,req->contents,is_chunked(req->headers)) : req->raw_contents ? send_raw_contents(req->socket,req->raw_contents,req->written,0): 0; if (is_chunked(req->headers) && req->written >= req->length) write_chunk(req->socket,NULL,0); return req->written < req->length; }
int vprintln_socket(NDB_SOCKET_TYPE socket, int timeout_millis, const char * fmt, va_list ap){ char buf[1000]; char *buf2 = buf; size_t size; if (fmt != 0 && fmt[0] != 0) { size = BaseString::vsnprintf(buf, sizeof(buf), fmt, ap)+1;// extra byte for '/n' /* Check if the output was truncated */ if(size > sizeof(buf)) { buf2 = (char *)malloc(size); if(buf2 == NULL) return -1; BaseString::vsnprintf(buf2, size, fmt, ap); } } else { size = 1; } buf2[size-1]='\n'; int ret = write_socket(socket, timeout_millis, buf2, size); if(buf2 != buf) free(buf2); return ret; }
BOOL cli_send_smb(struct cli_state *cli) { size_t len; size_t nwritten=0; ssize_t ret; /* fd == -1 causes segfaults -- Tom ([email protected]) */ if (cli->fd == -1) return False; len = smb_len(cli->outbuf) + 4; while (nwritten < len) { ret = write_socket(cli->fd,cli->outbuf+nwritten,len - nwritten); if (ret <= 0) { close(cli->fd); cli->fd = -1; cli->smb_rw_error = WRITE_ERROR; DEBUG(0,("Error writing %d bytes to client. %d (%s)\n", (int)len,(int)ret, strerror(errno) )); return False; } nwritten += ret; } return True; }
static pid_t startFromKdeinit(int argc, const char *argv[]) { int socket = openSocket(); if( socket < -1 ) return 0; klauncher_header header; header.cmd = LAUNCHER_EXEC_NEW; const int BUFSIZE = 8192; // make sure this is big enough char buffer[ BUFSIZE + 10 ]; int pos = 0; long argcl = argc; memcpy( buffer + pos, &argcl, sizeof( argcl )); pos += sizeof( argcl ); for( int i = 0; i < argc; ++i ) { int len = strlen( argv[ i ] ) + 1; // include terminating \0 if( pos + len >= BUFSIZE ) { fprintf( stderr, "BUFSIZE in KCrash not big enough!\n" ); return 0; } memcpy( buffer + pos, argv[ i ], len ); pos += len; } long env = 0; memcpy( buffer + pos, &env, sizeof( env )); pos += sizeof( env ); long avoid_loops = 0; memcpy( buffer + pos, &avoid_loops, sizeof( avoid_loops )); pos += sizeof( avoid_loops ); header.arg_length = pos; write_socket(socket, (char *) &header, sizeof(header)); write_socket(socket, buffer, pos); if( read_socket( socket, (char *) &header, sizeof(header)) < 0 || header.cmd != LAUNCHER_OK ) { return 0; } long pid; read_socket(socket, buffer, header.arg_length); pid = *((long *) buffer); return static_cast<pid_t>(pid); }
inline int write_socket_len(int fd, const char* buf) { int len; len = strlen(buf); if (write_socket(fd, buf, len) < len) return 0; return 1; }
int sgt(char **av, t_client *cl, t_kernel *kernel) { char buff[BUFFER_SIZE]; (void)av; sprintf(buff, "sgt %d\n", (int)kernel->options.delai); if (write_socket(cl->fd, buff) <= 0) return (1); return (0); }
bool cli_send_smb(struct cli_state *cli) { size_t len; size_t nwritten=0; ssize_t ret; char *buf_out = cli->outbuf; bool enc_on = cli_encryption_on(cli); /* fd == -1 causes segfaults -- Tom ([email protected]) */ if (cli->fd == -1) return false; cli_calculate_sign_mac(cli, cli->outbuf); if (enc_on) { NTSTATUS status = cli_encrypt_message(cli, cli->outbuf, &buf_out); if (!NT_STATUS_IS_OK(status)) { close(cli->fd); cli->fd = -1; cli->smb_rw_error = SMB_WRITE_ERROR; DEBUG(0,("Error in encrypting client message. Error %s\n", nt_errstr(status) )); return false; } } len = smb_len(buf_out) + 4; while (nwritten < len) { ret = write_socket(cli->fd,buf_out+nwritten,len - nwritten); if (ret <= 0) { if (enc_on) { cli_free_enc_buffer(cli, buf_out); } close(cli->fd); cli->fd = -1; cli->smb_rw_error = SMB_WRITE_ERROR; DEBUG(0,("Error writing %d bytes to client. %d (%s)\n", (int)len,(int)ret, strerror(errno) )); return false; } nwritten += ret; } if (enc_on) { cli_free_enc_buffer(cli, buf_out); } /* Increment the mid so we can tell between responses. */ cli->mid++; if (!cli->mid) cli->mid++; return true; }
int connection_write(struct connection *connection, const void *data, int len) { if (len == 0) { /* successful no-op. Sockets and pipes behave differently here... */ return 0; } if (connection->service->type == CONNECTION_TCP) return write_socket(connection->fd_out, data, len); else return write(connection->fd_out, data, len); }
static void listen_sensor_app(short port) { SOCKET guest_sock; SOCKET sock; char *data; printf("opening server on port 22470\n"); while ((guest_sock = open_server("22470")) == INVALID_SOCKET) { printf("unable to listen on port 22470... retyring in few seconds\n"); our_sleep(5); } while (1) { printf("Waiting for connection from VM\n"); SOCKET client_sock = connect_client(guest_sock); if (client_sock == INVALID_SOCKET) { perror("accept()"); continue; } printf("Got sensors connection from VM\n"); while (1) { printf("Connecting to hardware device on port %d...\n", port); sock = open_socket("127.0.0.1", port); if (sock != INVALID_SOCKET) { printf("Connected to hardware device, socket=%d :-)\n", sock); while ((data = read_socket(sock, PACKET_LEN))) { if (write_socket(client_sock, data, PACKET_LEN)<0) { printf("Connection closed with VM - closing connection with hardware device\n"); free(data); } } printf("Closing connection to hardware device\n"); closesocket(sock); our_sleep(2); } else { printf("Unable to connect to hardware device\n"); closesocket(client_sock); closesocket(guest_sock); return; } } } closesocket(guest_sock); }
void not_modified() { // 304 char buffer[8096]; char body[8096]; sprintf(buffer, "HTTP/1.1 304 Not Modified\r\n"); write_socket(client_sockfd, buffer, strlen(buffer)); sprintf(buffer, SERVER_STRING); write_socket(client_sockfd, buffer, strlen(buffer)); time_t raw_time; struct tm *current_time; time(&raw_time); current_time = localtime(&raw_time); char date_buf[512]; strftime(date_buf, 512, "Date: %a, %d %b %Y %T %Z", current_time); write_socket(client_sockfd, date_buf, strlen(date_buf)); // Body isn't sent for this type of error write_socket(client_sockfd, "\r\n", strlen("\r\n")); }
/* * Function name: enter_game * Description : This function is called from the login object and enables * the player to connect. It takes care of the necessary * initialization before the player is allowed to file his * request. */ void enter_game() { string data; int bl; set_screen_width(80); bl = SECURITY->check_newplayer(query_ip_number(this_object())); if (bl == 0) { write_socket("Your site isn't blocked. Log in as usual.\n"); destruct(); return; } else if (bl == 1) { write_socket("Your site has been banned, not just simply blocked.\n" + "No one will be admitted from your site.\n"); destruct(); return; } data = read_file(APPLICATION_INFO); if (!strlen(data)) { write_socket("Panic! Couldn't read: " + APPLICATION_INFO + ".\n"); destruct(); return; } info = explode(data, "##"); enable_commands(); write_info(); }
int msz(char **av, t_client *cl, t_kernel *kernel) { char buff[BUFFER_SIZE]; int x; int y; (void)av; x = kernel->options.width; y = kernel->options.height; sprintf(buff, "msz %d %d\n", x, y); if (write_socket(cl->fd, buff) <= 0) return (1); return (0); }
int flush_socket(int fd, struct client_state *csp) { struct iob *iob = csp->iob; int n = iob->eod - iob->cur; if(n <= 0) return(0); n = write_socket(fd, iob->cur, n); iob->eod = iob->cur = iob->buf; return(n); }
int plv(char **av, t_client *cl, t_kernel *kernel) { int nb_player; t_player *tempory_player; char buff[BUFFER_SIZE]; if (av_length(av) != 2) return (sbp(cl->fd)); nb_player = atoi(av[1]); if (!(tempory_player = get_player_by_id(nb_player, kernel->game.players))) return (sbp(cl->fd)); sprintf(buff, "plv %d %d\n", nb_player, tempory_player->level); if (write_socket(cl->fd, buff) <= 0) return (1); return (0); }
static int launch_elevation(t_case *c) { t_node *node; int ret; ret = 0; node = c->players->head; while (node) { if (write_socket(((t_player *)node->data)->client->fd, "elevation en cours\n") <= 0) ret = 1; node = node->next; } return (ret); }
void update_server(SERVER *svr) { int sock; char *p; char gbuf[BUFSIZE]; SERVER *server; server = svr; int c = connect_groupserver(&uclient, server->Name); if ( c == CONNECT_DOWN || c == CONNECT_ERR ) die("Failed to connect to server %s", server->Name); server = uclient.groupserver; sock = uclient.serversock; info("Connected to server: %s:%d", server->Hostname, server->Port); if ( server->SplitList == 1 ) { strncpy(gbuf, server->Groups, BUFSIZE); p = strtok(gbuf, ","); do { getlist_active(server, sock, p); if ( server->Descriptions ) getlist_newsgroups(server, sock, p); } while( (p = strtok(NULL, ",")) ); } else { getlist_active(server, sock, server->Groups); if ( server->Descriptions ) getlist_newsgroups(server, sock, server->Groups); } if ( server->ActiveTimes ) getlist_activetimes(server, sock); sprintf(buf, "QUIT\r\n"); if ( write_socket(sock, buf, strlen(buf), server->Timeout) == -1 ) die("Cant write to server"); uclient.connected = 0; close(sock); }
void getlist_newsgroups(SERVER *server, int sock, char *glist) { char group[512], descr[512]; int num = 0, nd; struct DESCR *dsc; sprintf(buf, "LIST newsgroups %s\r\n", glist); if ( write_socket(sock, buf, strlen(buf), server->Timeout) == -1 ) die("Cant write to server"); if ( fgetsrn(buf, BUFSIZE, sock) == NULL ) die("Cant read list newsgroups response"); if ( atoi(buf) != 215 ) die("Invalid response from server: %s", buf); nd = numdescrs; while ( 1 ) { if ( fgetsrn(buf, BUFSIZE, sock) == NULL ) die("Cant read newsgroups file"); if ( buf[0] == '.' && buf[1] == '\r' ) break; if ( sscanf(buf, "%510s %510[^\r\n]", group, descr) == 2 ) { /* * check for garbage description like "???" or "No description" or such * this will also match correct descriptions which just happen to have the * word description in their name. */ if ( descr[0] != '?' && strstr(descr, "Description") == NULL && strstr(descr, "description") == NULL ) { if ( (dsc=getdescr(group, nd)) == NULL ) { (descriptions+numdescrs)->newsgroup = strdup(group); (descriptions+numdescrs)->description = strdup(descr); num++; numdescrs++; } } } } printf("Loaded newsgroups for %d groups\n", num); }
DWORD WINAPI http_do_request(void *lPtr) { socket_conn* conn = (socket_conn*)lPtr; if(conn == 0) return 0; printf("[%08x]new connection\n", GetCurrentThreadId()); while(1) { int keepalive = 0; dynamic_object * req = dynamic_create(); if(http_read_request(conn, req) == 0) { dynamic_delete(req); break; } char* path = dynamic_get_string(dynamic_map_find(req, "PATH")); char* method = dynamic_get_string(dynamic_map_find(req, "METHOD")); char* connection = dynamic_get_string(dynamic_map_find(req, HTTP_HEADER_CONNECTION)); if(connection && strcmp(connection, "keep-alive")==0) keepalive = 1; printf("[%08x]%s %s\n", GetCurrentThreadId(), method, path); if(strcmp(path, "/hello") == 0) { char* html = "<html><body>Hello World!</body></html>"; int htmllen = strlen(html); dynamic_object * res = dynamic_create(); dynamic_string_printf(dynamic_map_insert(res, HTTP_HEADER_CONTENT_LENGTH), "%d", htmllen); dynamic_set_string(dynamic_map_insert(res, HTTP_HEADER_CONTENT_TYPE), "text/html"); http_send_response(conn, 200, res); write_socket(conn, html, htmllen, -1); dynamic_delete(res); }else { http_send_file(conn, req); } dynamic_delete(req); if(keepalive == 0) break; } close_socket(conn); printf("[%08x]close connection\n", GetCurrentThreadId()); return 0; }
void add_message(char *fmt, ...) { char buff[10000]; va_list argp; va_start(argp, fmt); (void)vsnprintf(buff, sizeof(buff), fmt, argp); /* LINTED: expression has null effect */ va_end(argp); if (command_giver && !(command_giver->flags & O_DESTRUCTED)) { push_string(buff, STRING_MSTRING); (void)apply("catch_tell", command_giver, 1, 1); } else write_socket(buff, (struct object *)0); }