int handle_server_data(char *in_buf, char *handle, int in_len, int socket_num) { Header *header = (Header *)in_buf; int code = 0; switch (header->flag) { case 2: code = 3; break; case 3: printf("\nHandle already in use: %s\n", handle); code = 1; break; case 4: handle_incoming_broadcast(in_buf + sizeof(Header)); break; case 5: handle_incoming_message(in_buf + sizeof(Header)); break; case 6: code = 3; break; case 7: handle_send_error(in_buf + sizeof(Header)); break; case 9: code = 2; break; case 11: handle_incoming_list(in_buf + sizeof(Header), in_len, socket_num); break; default: code = 3; break; } return code; }
void* watch_port_for_requests_from_clients(void*sz_watchport) /* Purpose:Watch a port for incoming messages from clients. A 'message' could be a request to login/logout or a ping, or perhaps a request to backup/restore data. Params: sz_watchport - the port to watch for incoming messages from clients Return: result (0=success, nonzero=failure) NB: Function will return nonzero if error occurs during setup but will otherwise run forever, or until killed. */ { int watch_port; struct sockaddr_in sin; char buf[MAX_STR_LEN+1]; int len, s, new_s; struct s_client2server_msg_record rec; watch_port = atoi((char*)sz_watchport); // sprintf(tmp, "watch_port_for_requests_from_clients(%d) - starting", watch_port); log_it(debug, tmp); memset((void*)&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = INADDR_ANY; sin.sin_port = htons(watch_port); if ((s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { log_it(error, "Unable to open socket on port #%d", watch_port); return((void*)-1); } if (bind(s, (struct sockaddr*)&sin, sizeof(sin)) < 0) { log_it(error, "Cannot bind %d - %s\n", watch_port, strerror(errno)); return((void*)-1); } if (listen(s, MAX_PENDING) < 0) { log_it(error, "Cannot setup listen (%d) - %s\n", watch_port, strerror(errno)); return((void*)-1); } /* service incoming connections */ log_it(debug, "Bound port #%d OK", watch_port); while(true) { len = sizeof(sin); if ((new_s = accept(s, (struct sockaddr *)&sin, (unsigned int*)&len)) < 0) { sleep(1); continue; } while ((len = recv(new_s, buf, sizeof(buf), 0)) > 0) { if (len > MAX_STR_LEN) { len = MAX_STR_LEN; } buf[len] = '\0'; memcpy((char*)&rec, buf, sizeof(rec)); handle_incoming_message(new_s, &sin, &rec); } close(new_s); } return(NULL); }
/** Sends a user message to remote node via associated kernel connection. */ int send_user_message(int target_slot_type, int target_slot_index, int data_length, char* data) { struct nl_msg* msg = NULL; struct nl_msg *ans_msg = NULL; struct nlmsghdr *nl_hdr; struct genlmsghdr* genl_hdr; struct nlattr *nla; int ret_val = 0; if ( (ret_val=prepare_request_message(state.handle, DIRECTOR_SEND_GENERIC_USER_MESSAGE, state.gnl_fid, &msg) ) != 0 ) { goto done; } ret_val = nla_put_u32(msg, DIRECTOR_A_SLOT_TYPE, target_slot_type); if (ret_val != 0) goto done; ret_val = nla_put_u32(msg, DIRECTOR_A_SLOT_INDEX, target_slot_index); if (ret_val != 0) goto done; ret_val = nla_put_u32(msg, DIRECTOR_A_LENGTH, data_length); if (ret_val != 0) goto done; ret_val = nla_put(msg, DIRECTOR_A_USER_DATA, data_length, data); if (ret_val != 0) goto done; if ( (ret_val = send_request_message(state.handle, msg, 1) ) != 0 ) goto done; if ( (ret_val = read_message(state.handle, &ans_msg) ) != 0 ) { goto done; } // TODO: We are not checking, the ack is for this request. Check it! while ( !is_ack_message(ans_msg) ) { // We can get different than ack messega here.. in this case we have to process it handle_incoming_message(ans_msg); if ( (ret_val = read_message(state.handle, &ans_msg) ) != 0 ) { goto done; } } done: nlmsg_free(ans_msg); return ret_val; }
/** Runs loop that waits for a new messages and dispatches them */ int run_processing_callback(int allow_block) { struct nl_msg *msg = NULL; int res; //printf("Starting processing loop\n"); while (1) { //struct timeval start, end; //gettimeofday(&start); // printf("New netlink message arrived\n"); if ( !allow_block && read_would_block(state.handle) ) return; if ( (res = read_message(state.handle, &msg) ) != 0 ) { printf("Error in message reading: %d\n", res); if ( res == -EINTR ) // Interrupted => finish the thread break; continue; } handle_incoming_message(msg); } }
static int _chat_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_ctx_t *ctx = connection_get_ctx(); gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); Jid *jid = jid_create(from); // private message from chat room use full jid (room/nick) if (muc_active(jid->barejid)) { // determine if the notifications happened whilst offline GTimeVal tv_stamp; gboolean delayed = stanza_get_delay(stanza, &tv_stamp); // check for and deal with message xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (body != NULL) { char *message = xmpp_stanza_get_text(body); if (message != NULL) { if (delayed) { handle_delayed_private_message(jid->str, message, tv_stamp); } else { handle_incoming_private_message(jid->str, message); } xmpp_free(ctx, message); } } jid_destroy(jid); return 1; // standard chat message, use jid without resource } else { // determine chatstate support of recipient gboolean recipient_supports = FALSE; if (stanza_contains_chat_state(stanza)) { recipient_supports = TRUE; } // create or update chat session if (!chat_session_exists(jid->barejid)) { chat_session_start(jid->barejid, recipient_supports); } else { chat_session_set_recipient_supports(jid->barejid, recipient_supports); } // determine if the notifications happened whilst offline GTimeVal tv_stamp; gboolean delayed = stanza_get_delay(stanza, &tv_stamp); // deal with chat states if recipient supports them if (recipient_supports && (!delayed)) { if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_COMPOSING) != NULL) { handle_typing(jid->barejid); } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_GONE) != NULL) { handle_gone(jid->barejid); } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PAUSED) != NULL) { // do something } else if (xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_INACTIVE) != NULL) { // do something } else { // handle <active/> // do something } } // check for and deal with message xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_BODY); if (body != NULL) { char *message = xmpp_stanza_get_text(body); if (message != NULL) { if (delayed) { handle_delayed_message(jid->barejid, message, tv_stamp); } else { handle_incoming_message(jid->barejid, message); } xmpp_free(ctx, message); } } jid_destroy(jid); return 1; } }