Exemplo n.º 1
0
void list_group(struct servmsg *user)
{
       struct group_info *group_tmp;
       struct msg buf;
       bzero(&buf,sizeof(buf));
       buf.type = LIST_GROUP;//消息类型为
       strcpy(buf.self_name,"server");
       strcpy(buf.dst_name, "client");

       group_tmp = head_group->next;
       if(group_tmp == NULL) {
              strcpy(buf.data,"暂时没有组!");
	          send_to(buf,user->addr);
       }
       else{
              while(group_tmp != NULL){
                     bzero(&buf.data,sizeof(buf.data));
                    strcpy(buf.data,group_tmp->group_name);
	    	          send_to(buf,user->addr);
                     group_tmp = group_tmp->next;
              }
       }
       bzero(&buf,sizeof(buf));
       buf.type = OVER;//结束发送
       send_to(buf,user->addr);

}
Exemplo n.º 2
0
void delete_group(struct servmsg *user)
{
       struct group_info *group_tmp,*tmp_before;
       struct msg buf;
       bzero(&buf,sizeof(buf));
       buf.type = GROUP_DELETE;//消息类型为
       strcpy(buf.self_name,"server");
       strcpy(buf.dst_name, "client");
		tmp_before=head_group;
       group_tmp = head_group->next;
       if(group_tmp == NULL) {
              strcpy(buf.data,"暂时没有组!");
              send_to(buf,user->addr);
       }
       else
           while(group_tmp != NULL){
         		     if(strcmp(group_tmp->group_name,user->recvmsg.data) == 0){
             	        tmp_before->next = group_tmp->next;
		      	        strcpy(buf.data,group_tmp->group_name);
						  strcat(buf.data,":被删除!");
						 delete_group_and_users(group_tmp);
				         send_to(buf,user->addr); 
				         break;              		      
					 }
				tmp_before=group_tmp;
                group_tmp = group_tmp->next;
            }
         bzero(&buf,sizeof(buf));
         buf.type = OVER;//结束发送
		 send_to(buf,user->addr);
}
Exemplo n.º 3
0
/*发送在线用户列表至客户端*/
void *send_all_online(struct sockaddr_in addr)
{

       int num = -1;
       int len = -1;
       int i = 0;
       struct user_info *tmp;
       struct msg buf;
       bzero(&buf,sizeof(buf));
       buf.type = REFRESH;//消息类型为刷新
 
       tmp = head_user->next;
       if(tmp == NULL) {
           strcpy(buf.data,"暂时没有其他用户在线!");
           send_to(buf,addr);
       }
       else
       while(tmp != NULL){
           strcpy(buf.data,tmp->user_name);
           send_to(buf,addr);
           tmp = tmp->next;
       }

       bzero(&buf,sizeof(buf));
       buf.type = OVER;//结束发送
       send_to(buf,addr);
       printf("结束发送\n");
}
Exemplo n.º 4
0
void *manage_request (void *p){

    void *in_msg; /* incoming request */
    void *out_msg; /* outgoing response */
    size_t size;
    int request;

    pthread_barrier_wait(((struct manage_request_struct *) p)->bar);

    struct conn_node *node = ((struct manage_request_struct *) p)->node;
    struct connection *conn = ((struct manage_request_struct *) p)->conn;

    if (recv_from(node, &in_msg) != 0){
        fprintf(stderr, "Error in receiving data from client\n");
        close_conn_server(conn, node);
        pthread_exit(NULL);
    }
    request = ((struct data*)in_msg)->data_type;

    if(request == LIST)
        printf("\n- New connection - Request LIST \n");
    else if(request == GET)
        printf("\n- New connection - Request GET \n");
    else if(request == PUT)
        printf("\n- New connection - Request PUT \n");

    size = parse_data_rcvd(in_msg, &out_msg); /* It reads the msg received and creates the msg to be sent back*/
    if (send_to(node, out_msg, size) != 0){
        fprintf(stderr,"Error in sending data to client\n");
    }

    free(in_msg);
    free(out_msg);
    close_conn_server(conn, node);
}
Exemplo n.º 5
0
/**
 * Send an acknowledge packet to a player
 * ACK packets consist of just a 16 bytes header
 *
 * @param pl the player
 */
void send_acknowledge(struct player *pl)
{
	char *data, *ptr;
	size_t data_size = 16;
	ssize_t err;

	data = (void *)calloc(data_size, sizeof(char));
	if (data == NULL) {
		printf("(EE) send_acknowledge, packet data allocation failed : %s.\n", strerror(errno));
		return;
	}
	ptr = data;

	*(uint16_t *)ptr = PKT_TYPE_ACK;	ptr+=2;
	*(uint16_t *)ptr = 0x0000;		ptr+=2;
	*(uint32_t *)ptr = pl->private_id;	ptr+=4;
	*(uint32_t *)ptr = pl->public_id;	ptr+=4;
	*(uint32_t *)ptr = pl->f1_s_counter;	ptr+=4;

	err = send_to(pl->in_chan->in_server, data, data_size, 0, (struct sockaddr *)pl->cli_addr, pl->cli_len);
	if (err == -1) {
		printf("(EE) send_acknowledge, sending data failed : %s.\n", strerror(errno));
	}
	pl->f1_s_counter++;
	free(data);
}
Exemplo n.º 6
0
static int rtp_data_bts(struct osmo_fd *fd, unsigned int what)
{
	char buf[4096];
	struct sockaddr_in addr;
	struct mgcp_endpoint *endp;
	int rc, proto;

	endp = (struct mgcp_endpoint *) fd->data;

	rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
	if (rc <= 0)
		return -1;

	proto = fd == &endp->bts_end.rtp ? PROTO_RTP : PROTO_RTCP;

	/* We have no idea who called us, maybe it is the BTS. */
	/* it was the BTS... */
	discover_bts(endp, proto, &addr);

	if (memcmp(&endp->bts_end.addr, &addr.sin_addr, sizeof(addr.sin_addr)) != 0) {
		LOGP(DMGCP, LOGL_ERROR,
			"Data from wrong bts %s on 0x%x\n",
			inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(endp));
		return -1;
	}

	if (endp->bts_end.rtp_port != addr.sin_port &&
	    endp->bts_end.rtcp_port != addr.sin_port) {
		LOGP(DMGCP, LOGL_ERROR,
			"Data from wrong bts source port %d on 0x%x\n",
			ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
		return -1;
	}

	/* throw away the dummy message */
	if (rc == 1 && buf[0] == DUMMY_LOAD) {
		LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from bts on 0x%x\n",
			ENDPOINT_NUMBER(endp));
		return 0;
	}

	/* do this before the loop handling */
	endp->bts_end.packets += 1;
	endp->bts_end.octets += rc;

	forward_data(fd->fd, &endp->taps[MGCP_TAP_BTS_IN], buf, rc);

	switch (endp->type) {
	case MGCP_RTP_DEFAULT:
		return send_to(endp, DEST_NETWORK, proto == PROTO_RTP, &addr,
			       buf, rc);
	case MGCP_RTP_TRANSCODED:
		return send_transcoder(&endp->trans_bts, endp->cfg,
				       proto == PROTO_RTP, buf, rc);
	}

	LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
	     endp->type, ENDPOINT_NUMBER(endp));
	return 0;
}
Exemplo n.º 7
0
void creature_update(void)
{
	CREATURE *crit=0;
	CREATURE *crit_next=0;
	char buf[MAX_BUFFER];
	static int counter=0;

	counter++; // tick counter

	for(crit = creature_list; crit; crit = crit_next)
	{
		crit_next = crit->next;

		// don't process anything on mobs that are in areas w/no players (idle area)
		if(!IsPlayer(crit) && crit->in_room->area->players < 1)
			continue;

/*		if(counter % 4 == 0 && IsPlayer(crit)
		&& flag_isset(crit->flags, CFLAG_ANTIIDLE))
			sendcrit(crit,"Anti-Idle");
*/
		if (counter % 4 == 0 && IsPlayer(crit) && IsPlaying(crit)) // No-Op! 0
		{
			buf[0] = '\0';
			send_to(crit->socket,buf);
		}
	}
}
Exemplo n.º 8
0
void send_to_all(request request1) {
    for (int i = 0; i < MAX_CLIENTS; i++) {
        if (clients[i].state != INACTIVE && strcmp(request1.sender, clients[i].name) != 0) {
            send_to(i, request1);
        }
    }
}
Exemplo n.º 9
0
void group_chat(struct servmsg *user)
{
       struct group_info *group_tmp,*tmp_before;
       struct msg buf;
       bzero(&buf,sizeof(buf));
       buf.type = GROUP_CHAT;//消息类型为
       strcpy(buf.self_name,user->recvmsg.self_name);
       strcpy(buf.dst_name, user->recvmsg.dst_name);
       strcpy(buf.data,user->recvmsg.data);

		tmp_before=head_group;
       group_tmp = head_group->next;

       if(group_tmp == NULL) {
              strcpy(buf.data,"暂时没有组!");
		      send_to(buf,user->addr);
			   return ;
       }
       else
        while(group_tmp != NULL)
        {
					
                  if(strcmp(group_tmp->group_name,user->recvmsg.dst_name) == 0)
                  {

                     bzero(&buf.data,sizeof(buf.data));
                     buf.type = GROUP_CHAT;//消息类型为
                     strcpy(buf.data,user->recvmsg.data);
                     struct user_info  *user_tmp=group_tmp->owner;
			         

					while(user_tmp != NULL)
                    {
		    		      send_to(buf,user_tmp->cli_addr);
                         user_tmp=user_tmp->next;
					}
					return;
			      }
				 tmp_before=group_tmp;
                 group_tmp = group_tmp->next;
       }
       
      bzero(&buf,sizeof(buf));
      buf.type = OVER;//结束发送
	  send_to(buf,user->addr);
}
/*
 * This will handle connection for each client
 * */
void *connection_handler(void *socket_desc)
{
    int sock = *(int*)socket_desc;
    int read_size;
    char *mess;
    message msg;
     
    //Send some messages to the client
    mess = "Greetings! I am your connection handler\n";
    write(sock , mess , strlen(mess));
     
    mess = "Now type something and i shall repeat what you type \n";
    write(sock , mess , strlen(mess));
     
    //Receive a message from client
    while( (read_size = recv(sock , &msg , sizeof(msg) , 0)) > 0 )
    {
        //Send the message back to client
        switch(msg.cmd){
            case CMD_LOGIN:
                login(sock,msg);
                bzero(&msg,sizeof(msg));
                break;
            case CMD_REGISTER:
                client_register(sock,msg);
                break;
            case CMD_SEND_TO:
                send_to(msg);
                break;
            case CMD_LIST_PEOPLE:
                who_is_online(sock);
                break;
            case CMD_EXIT:
                client_exit(sock,msg.user_name);
                //puts("Client disconnected");
                //fflush(stdout);
                //free(socket_desc);
                break;
        }
        //write(sock , client_message , strlen(client_message));
    }
     
    if(read_size == 0)
    {
        puts("Client disconnected");
        fflush(stdout);
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }
         
    //Free the socket pointer
    free(socket_desc);
     
    return 0;
}
Exemplo n.º 11
0
	void write_shape(shape const & s, std::ostream &streamToWriteTo)
    {
        streamToWriteTo << s.size() << "\n";
        for (constShapeIterator shapeIter = s.begin(); shapeIter != s.end(); ++shapeIter)
        {
            send_to (*shapeIter, streamToWriteTo);
            streamToWriteTo << "\n";
        }
    }
Exemplo n.º 12
0
int main(){
    cmh mcmh,encrypted_mcmh;
    struct string mcert,mpri,en_pri;
    struct string signed_data;
    fd = getsocket(MY_PORT);
    if(fd <0 ){
        error();
        return -1;
    }
    getcert_and_key(&mcert,&mpri,&en_pri);
    if( cme_cmh_request(&mcmh) || cme_cmh_request(&encrypted_mcmh)){
        error();
        return-1;
    }
    printf("cmh = %d  encrypted cmh = %d\n",mcmh,encrypted_mcmh);
    if( cme_store_cert_key(mcmh,mcert.buf,mcert.len,mpri.buf,mpri.len) ||
            cme_store_cert_key(encrypted_mcmh,mcert.buf,mcert.len,en_pri.buf,en_pri.len)){
        error();
        return -1;
    }
    string_printf("mcert:",&mcert);
    printf("store cert and key\n");
    if(generated_signed_data(mcmh,&signed_data)){
        error();
        return -1;
    }
    printf("send cert\n");
    send_to(mcert.buf,mcert.len);
    printf("send data\n");
    send_to(signed_data.buf,signed_data.len);
    
    struct string encrypteddata;
    encrypteddata.len = 0;
    encrypteddata.buf = 0;
    encrypteddata.len = 1024;
    encrypteddata.buf = (char*)malloc(encrypteddata.len);
    if(encrypteddata.buf == NULL){
        error();
        return;
    }
    encrypteddata.len = mrecvfrom(fd,encrypteddata.buf,encrypteddata.len,OPP_PORT);
    sec_data_parse(&encrypteddata,encrypted_mcmh);    

}
Exemplo n.º 13
0
static int rtp_data_net(struct osmo_fd *fd, unsigned int what)
{
	char buf[4096];
	struct sockaddr_in addr;
	struct mgcp_endpoint *endp;
	int rc, proto;

	endp = (struct mgcp_endpoint *) fd->data;

	rc = receive_from(endp, fd->fd, &addr, buf, sizeof(buf));
	if (rc <= 0)
		return -1;

	if (memcmp(&addr.sin_addr, &endp->net_end.addr, sizeof(addr.sin_addr)) != 0) {
		LOGP(DMGCP, LOGL_ERROR,
			"Endpoint 0x%x data from wrong address %s vs. ",
			ENDPOINT_NUMBER(endp), inet_ntoa(addr.sin_addr));
		LOGPC(DMGCP, LOGL_ERROR,
			"%s\n", inet_ntoa(endp->net_end.addr));
		return -1;
	}

	if (endp->net_end.rtp_port != addr.sin_port &&
	    endp->net_end.rtcp_port != addr.sin_port) {
		LOGP(DMGCP, LOGL_ERROR,
			"Data from wrong source port %d on 0x%x\n",
			ntohs(addr.sin_port), ENDPOINT_NUMBER(endp));
		return -1;
	}

	/* throw away the dummy message */
	if (rc == 1 && buf[0] == DUMMY_LOAD) {
		LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from network on 0x%x\n",
			ENDPOINT_NUMBER(endp));
		return 0;
	}

	proto = fd == &endp->net_end.rtp ? PROTO_RTP : PROTO_RTCP;
	endp->net_end.packets += 1;
	endp->net_end.octets += rc;

	forward_data(fd->fd, &endp->taps[MGCP_TAP_NET_IN], buf, rc);

	switch (endp->type) {
	case MGCP_RTP_DEFAULT:
		return send_to(endp, DEST_BTS, proto == PROTO_RTP, &addr,
			       buf, rc);
	case MGCP_RTP_TRANSCODED:
		return send_transcoder(&endp->trans_net, endp->cfg,
				       proto == PROTO_RTP, buf, rc);
	}

	LOGP(DMGCP, LOGL_ERROR, "Bad MGCP type %u on endpoint %u\n",
	     endp->type, ENDPOINT_NUMBER(endp));
	return 0;
}
Exemplo n.º 14
0
Arquivo: main.c Projeto: eugen0329/tcp
int main(int argc, char *argv[])
{
    int sock;
    struct sockaddr_in addr;
    int port = get_port(argc, argv);
    char server_address_string[16];
    char buf[BUF_SIZE];
    char repeat = 1;
    int id;
    set_address(argc, argv, server_address_string, LENGTH_OF(server_address_string));

    srand(time(NULL));
    id = rand();

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(WRONG_SOC(sock)) {
        perror("socket");
        exit(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    /* addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); */
    addr.sin_addr.s_addr = inet_addr(server_address_string);
    if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
        perror("connect");
        exit(2);
    }

    printf("%s:%d, ID: %d\n", server_address_string, port, id);

    send(sock, &id, sizeof(id), EMPTY_FLAGS);

    while(repeat) {
        printf("-> ");
        read_stdin(buf, LENGTH_OF(buf));
        if(strlen(buf) == 0) continue;
        if(!strncmp(buf, "close", LENGTH_OF(buf))) repeat = 0;
        send_to(sock, buf);

        // download file if its exists
        if(strstr(buf, "download") && receive_int(sock)) {
            receive_file(sock);

            printf("%s\n", FILE_DOWNLOADED_MSG);
            continue;
        }

        // prints info or error mesages
        receive_and_print_msg(sock);
    }

    close(sock);

    return 0;
}
Exemplo n.º 15
0
/* parse command */
void	lhandler (char *c)
{
  char	*w;
  
  if (c != NULL)
    {
      add_history (c);
      //printf ("[+] -> readline : %s\n", c);
      
      w = strtok (c, " ");
      if (w == NULL)
	return;
      
      if (!strcmp ("connect", w))
	{
	  printf ("[+] connect\n");
	  w = strtok (NULL, " ");
	  if (w == NULL)
	    return ;
	  connect_to (w);
	  return ;
	}
      else if (!strcmp ("exit", w))
	{
	  printf ("\n[+] leaving\n");
	  exit (0);
	}
      else if (!strcmp ("quit", w))
        {
          printf ("\n[+] leaving\n");
          exit (0);
        }
      else if (!strcmp ("send", w))
	{
	  w = strtok (NULL, " ");
          if (w == NULL)
            return ;
          send_to (w, strtok (NULL, " "));
	  return ;
	}
      else if (!strcmp ("disconnect", w))
	{
	  w = strtok (NULL, " ");
          if (w == NULL)
            return ;
	  disconnect_from (w);
	  return ;
	}
    }
  else 
    {
      printf ("\n[+] leaving\n");
      exit (0);
    }
}
Exemplo n.º 16
0
int CUdpSocket::send_to(const void* buffer, size_t buffer_size, uint32_t to_ip, uint16_t to_port) throw (sys::CSyscallException)
{
    struct sockaddr_in to_addr;

    to_addr.sin_family = AF_INET;
    to_addr.sin_port = htons(to_port);
    to_addr.sin_addr.s_addr = to_ip;
    memset(to_addr.sin_zero, 0, sizeof(to_addr.sin_zero));

    return send_to(buffer, buffer_size, to_addr);
}
Exemplo n.º 17
0
void join_group(struct servmsg *user)
{
       struct msg mymsg;
	   mymsg.type = JOIN_GROUP;
	   strcpy(mymsg.self_name,"server");
	   strcpy(mymsg.dst_name, user->recvmsg.self_name);
	   strcpy(mymsg.data, user->recvmsg.data);

       struct user_info *new_user;
       new_user = (struct user_info *)malloc(sizeof(struct user_info));
       strcpy(new_user->user_name,user->recvmsg.self_name);
       new_user->id = i++;
       new_user->cli_addr = user->addr; 
       new_user->next = NULL; 
// printf("join user name:%s\n",user->recvmsg.self_name);
       struct group_info *head=head_group,*tmph;
       while( head->next != NULL){
              head = head->next;
              if(strcmp(head->group_name,user->recvmsg.data) == 0){
               	   struct user_info *gu_head=head->owner;
			         while( gu_head->next != NULL){
		                 gu_head = gu_head->next;
 		                 if(strcmp(gu_head->user_name,user->recvmsg.self_name) == 0){
 	 	                     mymsg.type = ERROR;
 		                     strcpy(mymsg.data," : 该用户已登录 group,不能重复登录!\n"); 
			                  send_to(mymsg,user->addr);
   	 	              		 free(new_user);
							return ;
  			             }
  			         }
  				     gu_head->next = new_user;			  				     
			    	strcat(mymsg.data,": join in this group ok!\n"); 
		            send_to(mymsg,user->addr);
					return;
              }
       }
   		 free(new_user);
    	strcat(mymsg.data,":组不存在!\n"); 
        send_to(mymsg,user->addr);
}
Exemplo n.º 18
0
void message_service::send_subscription(const ID& peer_id, const ID& tag_id, const std::string& tag_name, const std::string& type_name)
{
  payload_header_packet hdr;
  hdr.payload_type = subscribe_packet::payload_id;
  outbound_data<serializer::boost_serializer, payload_header_packet> o_hdr(hdr);

  subscribe_packet sub(tag_id);//, tag_name, type_name);
  outbound_data<serializer::boost_serializer, subscribe_packet> o_sub(sub);

  outbound_pair o_combined(o_hdr, o_sub);

  send_to(peer_id, o_combined);
}
Exemplo n.º 19
0
void message_service::send_publish(const ID& peer_id, const ID& tag_id, const std::string& tag_name, const std::string& type_name)
{
  payload_header_packet hdr;
  hdr.payload_type = publish_packet::payload_id;
  outbound_data<serializer::boost_serializer, payload_header_packet> o_hdr(hdr);

  publish_packet pub(tag_id, tag_name, type_name);
  outbound_data<serializer::boost_serializer, publish_packet> o_pub(pub);

  outbound_pair o_combined(o_hdr, o_pub);

  send_to(peer_id, o_combined);
}
Exemplo n.º 20
0
ssize_t UDPLayer::write(const void *buf, size_t nbyte) const {
	std::list<num_addr_pair_t>::const_iterator gmember_iter;
	int rc = 0;

	for(gmember_iter = groupmember_list.begin(); gmember_iter != groupmember_list.end(); ++gmember_iter) {
		try{
			rc = send_to(buf, nbyte, gmember_iter->first, gmember_iter->second);
		}
		catch(const std::exception& e) {
			Logger::log(e.what(), Logger::LOGLEVEL_ERROR);
		}
	}

	return rc;
}
Exemplo n.º 21
0
/*Send a GET or POST http request to server with port 80*/
char * http_request(
		const char * method,
		const char * host,
		const char * uri,
		const char * http_edition,
		struct field * params, int n,
		struct field * data, int m,
		int echo)
{
	/*Construct the GET request url*/
	char * post_data = data2url(data,m,MAX_URI_SIZE);
	char * tmp_buf = NULL;
	char * cmpt_uri = (char *)malloc(MAX_URI_SIZE * sizeof(char));
	if (!cmpt_uri)	Die("malloc failed!");
	char * str = (char *)malloc(BUFFSIZE * sizeof(char));
	if (!str)	Die("malloc failed!");
	int method_type = strcmp(method,"GET") == 0;

	if (method_type && m > 0){
		bzero(cmpt_uri, MAX_URI_SIZE);
		multicat(MULTICAT_EOF,cmpt_uri,uri,"?",post_data,MULTICAT_EOF);
		uri	= cmpt_uri;
	}
	/*Create string for send*/
	bzero(str, BUFFSIZE);
	multicat(MULTICAT_EOF, str, method , " " , uri , " " , http_edition , "\r\n",
			"HOST: ", host , "\r\n", MULTICAT_EOF);
	/*Construct HTTP request header parameters*/
	for (int i = 0; i < n; ++i)
		multicat(MULTICAT_EOF, str, params[i].key ,": " ,params[i].val ,"\r\n", MULTICAT_EOF);
	strcat(str,"\r\n");
	/*Construct the POST request data*/
	if (!method_type && n > 0) /*Not GET then POST*/
		multicat(MULTICAT_EOF, str, 
				"Content-Type: application/x-www-form-urlencoded\r\nContent-Length: ",
				tmp_buf = int2str(strlen(post_data)), 
				"\r\n", post_data, "\r\n", MULTICAT_EOF);
	free(post_data);
	free(cmpt_uri);
	free(tmp_buf);

	if (echo)
		fprintf(stdout,"Sending:\n%s\n", str);
	char * response = send_to(host, 80, str);
	free(str);
	return response;
}
Exemplo n.º 22
0
void send_message_to_all(struct servmsg *user,char ch)
{
       struct user_info *tmp;
       struct msg buf;
       bzero(&buf,sizeof(buf));
       buf.type = ch;//消息类型为
       strcpy(buf.self_name,user->recvmsg.self_name);
       strcpy(buf.dst_name, user->recvmsg.dst_name);
//		strcpy(buf.data,user->recvmsg.self_name);
		strcpy(buf.data,user->recvmsg.data);
         tmp=head_user->next;
		while(tmp != NULL)
        {
	      send_to(buf,tmp->cli_addr);
          tmp=tmp->next;
        }
}
Exemplo n.º 23
0
void create_group( struct servmsg *user)
{
       struct msg mymsg;
       bzero(&mymsg,sizeof(struct msg));
       struct group_info *new_group;

       new_group = (struct group_info *)malloc(sizeof(struct group_info));
       strcpy(new_group->group_name,user->recvmsg.data);
       new_group->id = i++;
       new_group->next = NULL;

	   struct user_info *owner,*new_user;
       new_user = (struct user_info *)malloc(sizeof(struct user_info));
       new_user->cli_addr = user->addr; //结构体整体赋值,客户端端口为5倍进程
       strcpy(new_user->user_name,user->recvmsg.data);
       new_user->id = i++;
       new_user->next = NULL;

	   new_group->owner=new_user;
       struct group_info *head=head_group,*htmp;
       while( head->next != NULL){
				htmp=head;
			   head=head->next;
              if(strcmp(head->group_name,new_group->group_name) == 0)
					break ;
              head = htmp;
       }
 	  if(head->next == NULL)
		{
             head->next = new_group;
	         mymsg.type = CREATE_GROUP;
	        strcpy(mymsg.self_name,"server");
	        strcpy(mymsg.dst_name, user->recvmsg.self_name);
	        strcpy(mymsg.data, new_group->group_name);
	    	strcat(mymsg.data,": 服务器创建好组!\n"); 
         }
		else 
	    {
            mymsg.type = ERROR;
            strcpy(mymsg.data,"该用户已存在,不能重复!\n"); 
 	    }
        send_to(mymsg,user->addr);
       
}
Exemplo n.º 24
0
/**
 * Send the list of bans to a player
 *
 * @param s the server
 * @param pl the player who asked for the list of bans
 */
static void s_resp_bans(struct player *pl)
{
	char *data, *ptr;
	int data_size, tmp_size;
	struct ban *b;
	struct server *s = pl->in_chan->in_server;
	size_t iter;
	
	data_size = 24;
	data_size += 4;	/* number of bans */

	ar_each(struct ban *, b, iter, s->bans)
		data_size += ban_to_data_size(b);
	ar_end_each;

	data = (char *)calloc(data_size, sizeof(char));
	if (data == NULL) {
		logger(LOG_WARN, "s_resp_ban, packet allocation failed : %s.", strerror(errno));
		return;
	}
	ptr = data;

	wu16(PKT_TYPE_CTL, &ptr);	/* */
	wu16(CTL_BANLIST, &ptr);	/* */
	wu32(pl->private_id, &ptr);	/* private ID */
	wu32(pl->public_id, &ptr);	/* public ID */
	wu32(pl->f0_s_counter, &ptr);	/* packet counter */
	ptr += 4;			/* packet version */
	ptr += 4;			/* checksum */
	wu32(s->bans->used_slots, &ptr);/* number of bans */
	logger(LOG_INFO, "number of bans : %zu", s->bans->used_slots);
	ar_each(struct ban *, b, iter, s->bans)
		tmp_size = ban_to_data(b, ptr);
		ptr += tmp_size;
	ar_end_each;
	
	packet_add_crc_d(data, data_size);
	logger(LOG_INFO, "list of bans : sending %i bytes", data_size);
	send_to(s, data, data_size, 0, pl);

	pl->f0_s_counter++;
	free(data);
}
Exemplo n.º 25
0
/**
 * Send a "player kicked from channel" notification to all players.
 *
 * @param s the server
 * @param kicker the player who kicked
 * @param kicked the player kicked from the server
 * @param reason the reason the player was kicked
 * @param kicked_to the channel the player is moved to
 */
static void s_notify_kick_channel(struct player *kicker, struct player *kicked, 
		char *reason, struct channel *kicked_from)
{
	char *data, *ptr;
	struct player *tmp_pl;
	int data_size = 68;
	struct server *s = kicker->in_chan->in_server;
	size_t iter;

	data = (char *)calloc(data_size, sizeof(char));
	if (data == NULL) {
		logger(LOG_WARN, "s_notify_kick_channel, packet allocation failed : %s.", strerror(errno));
		return;
	}
	ptr = data;

	wu16(PKT_TYPE_CTL, &ptr);	/* */
	wu16(CTL_CHKICK_PL, &ptr);	/* */
	ptr += 4;			/* private ID */
	ptr += 4; 			/* public ID */
	ptr += 4;			/* packet counter */
	ptr += 4;			/* packet version */
	ptr += 4;			/* empty checksum */
	wu32(kicked->public_id, &ptr);	/* ID of player who was kicked */
	wu32(kicked_from->id, &ptr);	/* channel the player was kicked from */
	wu32(kicker->public_id, &ptr);	/* ID of the kicker */
	wu16(0, &ptr);	/* visible notification : kicked */
	wstaticstring(reason, 29, &ptr);/* reason message */

	/* check we filled all the packet */
	assert((ptr - data) == data_size);

	ar_each(struct player *, tmp_pl, iter, s->players)
			ptr = data + 4;
			wu32(tmp_pl->private_id, &ptr);
			wu32(tmp_pl->public_id, &ptr);
			wu32(tmp_pl->f0_s_counter, &ptr);
			packet_add_crc_d(data, data_size);
			send_to(s, data, data_size, 0, tmp_pl);
			tmp_pl->f0_s_counter++;
	ar_end_each;
	free(data);
}
Exemplo n.º 26
0
/**
 * Send a notification to all players that target has been banned.
 *
 * @param s the server
 * @param pl the player who gave the order to ban
 * @param target the player who is banned
 * @param duration the duration of the ban (0 = unlimited)
 * @param reason the reason of the ban
 */
static void s_notify_ban(struct player *pl, struct player *target, uint16_t duration, char *reason)
{
	char *data, *ptr;
	struct player *tmp_pl;
	int data_size = 64;
	struct server *s = pl->in_chan->in_server;
	size_t iter;

	data = (char *)calloc(data_size, sizeof(char));
	if (data == NULL) {
		logger(LOG_WARN, "s_notify_ban, packet allocation failed : %s.", strerror(errno));
		return;
	}
	ptr = data;

	wu16(PKT_TYPE_CTL, &ptr);	/* */
	wu16(CTL_PLAYERLEFT, &ptr);	/* */
	ptr += 4;			/* private ID */
	ptr += 4; 			/* public ID */
	ptr += 4;			/* packet counter */
	ptr += 4;			/* packet version */
	ptr += 4;			/* empty checksum */
	wu32(target->public_id, &ptr);	/* ID of player banned */
	wu16(2, &ptr);	/* kick */
	wu32(pl->public_id, &ptr);	/* banner ID */
	wstaticstring(reason, 29, &ptr);/* reason message */

	/* check we filled all the packet */
	assert((ptr - data) == data_size);

	ar_each(struct player *, tmp_pl, iter, s->players)
			ptr = data + 4;
			wu32(tmp_pl->private_id, &ptr);
			wu32(tmp_pl->public_id, &ptr);
			wu32(tmp_pl->f0_s_counter, &ptr);
			packet_add_crc_d(data, data_size);
			send_to(s, data, data_size, 0, tmp_pl);
			tmp_pl->f0_s_counter++;
	ar_end_each;
	free(data);
}
Exemplo n.º 27
0
static int
send_and_wait_for_response (int target, struct Message *m) {
    int result;
    int wait_index;
    if ((wait_index = get_wait_index()) < 0) {
        // Error
        printf("%d: wait_index error\n", thisid);
        return wait_index;
    }
    m->index = wait_index;
    // Send message over network
    if ((result = send_to(target, m)) < 0) {
        printf("%d: send message error\n", thisid);
        return result;
    }
    // wait for response
    printf("%d: waiting on index %d\n", thisid, wait_index);
    pthread_cond_wait(&waits[wait_index], &locks[m->page_number]);
    return_wait_index(wait_index);
    return 0;
}
Exemplo n.º 28
0
static int rtp_data_transcoder(struct mgcp_rtp_end *end, struct mgcp_endpoint *_endp,
			      int dest, struct osmo_fd *fd)
{
	char buf[4096];
	struct sockaddr_in addr;
	struct mgcp_config *cfg;
	int rc, proto;

	cfg = _endp->cfg;
	rc = receive_from(_endp, fd->fd, &addr, buf, sizeof(buf));
	if (rc <= 0)
		return -1;

	proto = fd == &end->rtp ? PROTO_RTP : PROTO_RTCP;

	if (memcmp(&addr.sin_addr, &cfg->transcoder_in, sizeof(addr.sin_addr)) != 0) {
		LOGP(DMGCP, LOGL_ERROR,
			"Data not coming from transcoder dest: %d %s on 0x%x\n",
			dest, inet_ntoa(addr.sin_addr), ENDPOINT_NUMBER(_endp));
		return -1;
	}

	if (end->rtp_port != addr.sin_port &&
	    end->rtcp_port != addr.sin_port) {
		LOGP(DMGCP, LOGL_ERROR,
			"Data from wrong transcoder dest %d source port %d on 0x%x\n",
			dest, ntohs(addr.sin_port), ENDPOINT_NUMBER(_endp));
		return -1;
	}

	/* throw away the dummy message */
	if (rc == 1 && buf[0] == DUMMY_LOAD) {
		LOGP(DMGCP, LOGL_NOTICE, "Filtered dummy from transcoder dest %d on 0x%x\n",
			dest, ENDPOINT_NUMBER(_endp));
		return 0;
	}

	end->packets += 1;
	return send_to(_endp, dest, proto == PROTO_RTP, &addr, &buf[0], rc);
}
Exemplo n.º 29
0
void myhandle(int signum)
{
      struct msg mymsg;
      mymsg.type = SERVER_EXIT;
       strcpy(mymsg.self_name,"server");
      strcpy(mymsg.data," server exit"); 
      struct user_info *tmp=head_user->next;
		while(tmp != NULL)
        {
         printf("user name:%s\n",tmp->user_name);
	      send_to(mymsg,tmp->cli_addr);
          tmp=tmp->next;

        }

       close(serv_fd);
	 free_all_user(head_user);
	 free_all_group(head_group);
	   free_all_message(H);
      printf("\n谢谢使用!再见!\n");
       exit(1);
}
Exemplo n.º 30
0
int
boss::Socket::send_to(const void *msg, size_t len,
                uint16_t port, const char *host_address /*= NULL*/ ,
                int flags /* = 0 */ )
{
    struct sockaddr toaddr;
    struct sockaddr_in *p = (struct sockaddr_in *) &toaddr;

    bzero(&toaddr, sizeof(struct sockaddr));

    p->sin_family = AF_INET;

    if (host_address == NULL) {
        p->sin_addr.s_addr = htonl(INADDR_BROADCAST);
    }
    else {
        get_in_addr(host_address, p->sin_addr, "send_to");
    }

    p->sin_port = htons(port);

    return send_to(msg, len, toaddr, sizeof(toaddr), flags);
}