示例#1
0
 void test_nodes() {
   const char * test_nodes[] = { "memyselfi:129", 
				 "zebra:321", 
				 "apple:123",
				 "intheory:876"};
   init_network_nodes(0, 4, test_nodes);
   assert(my_id() == 2);
   assert(get_port(0) == 123);
   assert(get_port(1) == 876);
   assert(get_port(2) == 129);
   assert(get_port(3) == 321);
   char *got = get_address(0);
   assert(strcmp(got, "apple") == 0);
   free(got);
   got = get_address(1);
   assert(strcmp(got, "intheory") == 0);
   free(got);
   got = get_address(2);
   assert(strcmp(got, "memyselfi") == 0);
   free(got);
   got = get_address(3);
   assert(strcmp(got, "zebra") == 0);
   free(got);
   destroy_network_nodes();
}
示例#2
0
//when you need to handle a join reply, use the params join_ip and join_port
//otherwise, join_ip=0 and join_port=-1
void peer_list(unsigned int join_ip, short join_port, unsigned int room){
  //create payload for join and update replies
  struct peer *s;
  int num_in_room = 0;
  for(s=peers; s != NULL; s=(peer *)s->hh.next){
    if(s->room==room){
      num_in_room = num_in_room+1;
    }
  }
  struct sockaddr_in list[num_in_room];
  int a = 0;
  for(s=peers; s != NULL; s=(peer *)s->hh.next){
    if(s->room==room){
      unsigned int peer_ip = get_ip(s->ip_and_port);
      short peer_port = get_port(s->ip_and_port);
      struct sockaddr_in peer_info = get_sockaddr_in(peer_ip, peer_port);
      sockaddr_in* peer_info_ptr = &peer_info;
      memcpy((sockaddr_in*)&list[a], peer_info_ptr, sizeof(peer_info));
      a=a+1;
    }
  }

  packet update_pkt;
  update_pkt.header.type = 'u';
  update_pkt.header.error = '\0';
  update_pkt.header.payload_length = num_in_room * sizeof(struct sockaddr_in);
  memcpy(update_pkt.payload, list, num_in_room * sizeof(struct sockaddr_in));
  for(s=peers; s != NULL; s=(peer *)s->hh.next){
    if(s->room==room){
      unsigned int peer_ip = get_ip(s->ip_and_port);
      short peer_port = get_port(s->ip_and_port);
      if(join_port!=-1 and join_ip!=0 and peer_ip == join_ip and peer_port==join_port){
        //send join
        packet join_pkt;
        join_pkt.header.type = 'j';
        join_pkt.header.error = '\0';
        join_pkt.header.room = room;
        join_pkt.header.payload_length = num_in_room * sizeof(struct sockaddr_in);
        memcpy(join_pkt.payload, list, num_in_room * sizeof(struct sockaddr_in));
        struct sockaddr_in peer_addr = get_sockaddr_in(peer_ip, peer_port);
        int status = sendto(sock, &join_pkt, sizeof(join_pkt), 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr));
        if (status == -1) {
          pthread_mutex_lock(&stdout_lock);
          fprintf(stderr, "%s\n", "error - error sending packet to peer");
          pthread_mutex_unlock(&stdout_lock);
        }
      }else{
        struct sockaddr_in peer_addr = get_sockaddr_in(peer_ip, peer_port);
        int status = sendto(sock, &update_pkt, sizeof(update_pkt), 0, (struct sockaddr *)&peer_addr, sizeof(peer_addr));
        if (status == -1) {
          pthread_mutex_lock(&stdout_lock);
          fprintf(stderr, "%s\n", "error - error sending packet to peer");
          pthread_mutex_unlock(&stdout_lock);
        }
      }
    }
  }
}
示例#3
0
int main(int argc, char* argv[])
{
  int s;
  struct sockaddr_in multiAddr;
  struct timeval now;
  char out_buf[8];

  if ((s = socket(AF_INET, SOCK_DGRAM, 0))<0)
  {
    printf("couldn't get socket: %s\n", strerror(errno));
    return -1;
  };
  // Multicast to send to.
  multiAddr.sin_family = AF_INET;
  multiAddr.sin_port = htons(atoi(get_port()));
  multiAddr.sin_addr.s_addr = inet_addr("224.0.1.1");
  	
	while(sleep(1) == 0) {
		(void) gettimeofday(&now,NULL);
		printf("(MS) %x %x\n",now.tv_sec,now.tv_usec);	
		now.tv_sec = htonl(now.tv_sec);
		now.tv_usec = htonl(now.tv_usec);
		memcpy(&out_buf[0],&now.tv_sec,4);
		memcpy(&out_buf[4],&now.tv_usec,4);
  	
		if ( sendto(s, &out_buf, 8, 0, (struct sockaddr *)&multiAddr, sizeof(multiAddr)) < 0) 
    			printf("sendto error: %s\n", strerror(errno));
  	
		
		
	}
}
示例#4
0
int NAT_TCP(firewall_t* fwall, ethernet_hdr_t* ethernet_header, ip_hdr_t* ip_header, tcp_hdr_t* tcp_header, int dir) {
	if (dir == OUT) {
		memcpy(ethernet_header->src_mac, fwall->switch_mac, sizeof(fwall->switch_mac));
		memcpy(ethernet_header->dst_mac, fwall->router_mac, sizeof(fwall->router_mac));
		memcpy(ip_header->src_ip, fwall->switch_ip_bin, sizeof(fwall->switch_ip_bin));
		compute_ip_checksum(ip_header);	
		
	  uint16_t new_src_port = get_port(unpack_2byte(tcp_header->src_port), TCP, dir);
		new_src_port = pack_2byte((uint8_t*)&new_src_port);
	  memcpy(tcp_header->src_port, (uint8_t*)&new_src_port, sizeof(uint16_t));
		
		pseudo_tcp_hdr_t* pseudo_tcp_header = (pseudo_tcp_hdr_t*) malloc(sizeof(pseudo_tcp_hdr_t));
		
		memcpy(pseudo_tcp_header->src_ip, ip_header->src_ip, sizeof(ip_header->src_ip));
		memcpy(pseudo_tcp_header->dst_ip, ip_header->dst_ip, sizeof(ip_header->dst_ip));
		pseudo_tcp_header->reserved = 0;
		pseudo_tcp_header->protocol = ip_header->protocol;
		uint16_t len = unpack_2byte(ip_header->total_len) - ((ip_header->version_ihl & 0x0F)*4);
		pseudo_tcp_header->tcp_len[0] = ((uint8_t)(len>>8)) & 0xFF;
		pseudo_tcp_header->tcp_len[1] = (uint8_t)(len) & 0xFF;
		
		pseudo_tcp_header->tcp_header = (tcp_hdr_t*) malloc(len);
		memcpy(pseudo_tcp_header->tcp_header, tcp_header, len);
		compute_tcp_checksum(pseudo_tcp_header);
		memcpy(tcp_header->checksum, pseudo_tcp_header->tcp_header->checksum, sizeof(tcp_header->checksum));
		free(pseudo_tcp_header->tcp_header);
		free(pseudo_tcp_header);
	} else if (dir == IN) {
示例#5
0
文件: portmap.c 项目: proot-me/PRoot
/**
 * Change the port of the socket address, if it maps with an entry.
 * Return 0 if no relevant entry is found, and 1 if the port has been changed.
 */
int change_inet6_socket_port(Tracee *tracee, Config *config, word_t sockfd, struct sockaddr_in6 *sockaddr, bool bind_mode) {
	uint16_t port_in, port_out;

	port_in = sockaddr->sin6_port;
	port_out = get_port(&config->portmap, port_in);

	if(port_out == PORTMAP_DEFAULT_VALUE) {
		if (bind_mode && config->netcoop_mode && !config->need_to_check_new_port) {
			VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 netcoop mode with: %d", htons(port_in));
			sockaddr->sin6_port = 0; // the system will assign an available port
			config->old_port = port_in; // we keep this one for adding a new entry
			config->need_to_check_new_port = true;
			config->sockfd = sockfd;
			return 1;
		}

		VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 port ignored: %d ", htons(port_in));
		return 0;
	}

	sockaddr->sin6_port = port_out;
	VERBOSE(tracee, PORTMAP_VERBOSITY, "ipv6 port translation: %d -> %d (NOT GUARANTEED: bind might still fail on target port)", htons(port_in), htons(port_out));

	return 1;
}
示例#6
0
//______________________________________________________________________________
//                                                                            []
//` main                                                                      []    
//                                                                            []
int main (int argc,char *argv[])
{
	Sock SoDb(AF_INET,SOCK_STREAM);

// Создаем сокет для открытия канала связи с БД:
	if(!SoDb.good()) 
	{
		perror("STOP: socket [DataBase]");
		return 1;
	}

	if(SoDb.connect("127.0.0.1", get_port())) 
	{
		perror("STOP: connect [DataBase]");
		return 1;
	}

	char	 buf   [1000];
	memset (buf,0,1000);

	printf("\n\nSTOP: Command STOP was sent to daemon\n");
	SoDb.write (buf, 1000);
	printf("\n\n");

	return 0;
}
static mrb_value
mrb_rx_gpio_initialize(mrb_state *mrb, mrb_value self)
{
    mrb_value pin;
    mrb_get_args(mrb, "o",&pin);

    mrb_iv_set(mrb, self, mrb_intern_lit(mrb, "@pin"),pin );

    mrb_int pin_no = mrb_fixnum(pin);

#if MRB_DEBUG1
    printf("rx_gpio_initialize pin = %d\n",pin_no);
#endif
    int bit;
    int ddr;
    volatile struct st_port0   *port = (volatile struct st_port0   *)get_port(pin_no,&bit,&ddr);

    if (ddr == 1)	//OUTPUT
    {
        port->PDR.BYTE |= BITTBL[bit];
    }
    else
    {
        port->PDR.BYTE &= ~BITTBL[bit];
    }
    return self;
}
示例#8
0
文件: url.c 项目: pandagxnu/snarf
Url *
url_init(Url *u, char *string)
{
	char *sp;	/* since we're going to walk through string,
			   use a copy instead. */

        sp = string;

        u->full_url = (char *)strdup(string);

	if( ! (sp = get_service_type(sp, u)) )
		return 0;

        /* only get username/password if they are not null,
           allows us to handle redirects properly */

        if( !u->username ) 
                sp = get_username(sp, u);
        if( !u->password )
                sp = get_password(sp, u);

        sp = get_hostname(sp, u);

        if( ! (u->host && *(u->host)) )
                return NULL;

        sp = get_port(sp, u);

        sp = get_path(sp, u);
        sp = get_file(sp, u);

	return u;
}
示例#9
0
int bmi_port_interface_add(bmi_port_mgr_t *port_mgr,
			   const char *ifname, int port_num,
			   const char *pcap_input_dump,
			   const char* pcap_output_dump)
{
  if(!port_num_valid(port_num)) return -1;

  bmi_port_t *port = get_port(port_mgr, port_num);
  if(port_in_use(port)) return -1;

  port->ifname = strdup(ifname);

  bmi_interface_t *bmi;
  if(bmi_interface_create(&bmi, ifname) != 0) return -1;

  if(pcap_input_dump) bmi_interface_add_dumper(bmi, pcap_input_dump, 1);
  if(pcap_output_dump) bmi_interface_add_dumper(bmi, pcap_output_dump, 0);

  pthread_mutex_lock(&port_mgr->lock);

  port->bmi = bmi;

  int fd = bmi_interface_get_fd(port->bmi);
  port->fd = fd;

  if(fd > port_mgr->max_fd) port_mgr->max_fd = fd;

  FD_SET(fd, &port_mgr->fds);

  pthread_mutex_unlock(&port_mgr->lock);

  return 0;
}
static int
delip_kernel(struct ip_set *set, 
	     const struct sk_buff *skb,
	     ip_set_ip_t *hash_ip,
	     const u_int32_t *flags,
	     unsigned char index)
{
	ip_set_ip_t port;

	if (flags[index+1] == 0)
		return -EINVAL;
		
	port = get_port(skb, flags[index+1]);

	DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
	   flags[index] & IPSET_SRC ? "SRC" : "DST",
	   NIPQUAD(skb->nh.iph->saddr),
	   NIPQUAD(skb->nh.iph->daddr));
	DP("flag %s port %u",
	   flags[index+1] & IPSET_SRC ? "SRC" : "DST", 
	   port);	
	if (port == INVALID_PORT)
		return -EINVAL;	

	return __delip(set,
		       ntohl(flags[index] & IPSET_SRC 
		       		? skb->nh.iph->saddr 
				: skb->nh.iph->daddr),
		       port,
		       hash_ip);
}
示例#11
0
文件: util.c 项目: Phoul/shim
// tokenize a CONNECT host:port request
struct url *
url_connect_tokenize(const char *str)
{
	struct url *url;
	char *p;
	int port;

	url = mem_calloc(1, sizeof(*url));

	p = strrchr(str, ':');
	if (!p || p == str)
		goto fail;
	port = get_port(p + 1);
	if (port < 0)
		goto fail;	

	url->host = mem_strdup_n(str, p - str);
	url->port = port;

	return url;	

fail:
	url_free(url);

	return NULL;
}
示例#12
0
static int
testip_kernel(struct ip_set *set,
	      const struct sk_buff *skb,
	      ip_set_ip_t *hash_ip,
	      const u_int32_t *flags,
	      unsigned char index)
{
	ip_set_ip_t port;
	int res;

	if (flags[index+1] == 0)
		return 0;
		
	port = get_port(skb, flags[index+1]);

	DP("flag: %s src: %u.%u.%u.%u dst: %u.%u.%u.%u",
	   flags[index] & IPSET_SRC ? "SRC" : "DST",
	   NIPQUAD(ip_hdr(skb)->saddr),
	   NIPQUAD(ip_hdr(skb)->daddr));
	DP("flag %s port %u",
	   flags[index+1] & IPSET_SRC ? "SRC" : "DST",
	   port);	
	if (port == INVALID_PORT)
		return 0;	

	res =  __testip(set,
			ntohl(flags[index] & IPSET_SRC
					? ip_hdr(skb)->saddr
					: ip_hdr(skb)->daddr),
			port,
			hash_ip);
	return (res < 0 ? 0 : res);
	
}
示例#13
0
/*proxy UDP reader*/
int proxy_udp_reader(char *buffer, int count) {
	int numbytes;
	struct sockaddr_storage their_addr;
	socklen_t addr_len;
	//char buf[MAXBUFLEN];
	char s[INET6_ADDRSTRLEN];

	printf("\nproxy: waiting to recvfrom....\n");

	addr_len = sizeof their_addr;

	numbytes = recvfrom(proxy_sockfd, buffer, MAXBUFLEN-1, 0, (struct sockaddr *)&their_addr, &addr_len);

	if(numbytes != -1) {
		printf("proxy: got packet from %s\n",
				inet_ntop(their_addr.ss_family,
					get_in_addr((struct sockaddr *)&their_addr),
					s, sizeof s));
		printf("proxy: packet is %d bytes long\n", numbytes);
		buffer[numbytes] = '\0';
		/*get information of router*/
		rec_router_port[count] = get_port((struct sockaddr *)&their_addr);
	}

	if (numbytes == -1) {
		perror("recvfrom");
		exit(1);
	}
	return 0;
}
示例#14
0
int main(int argc, char *argv[]) {
	char	*host = "localhost";
	char	*service = get_port();
	int 	s,n;
	struct timeval now;
	
	char buf[8];
	int inchars;

	switch(argc) {
		case 1: 
			break;
		case 3: 
			service = argv[2];
			//fall through
		case 2:
			host = argv[1];
			break;
		default:
			errexit("ERROR: Too many commmand line options.");
	}
	
	s = connectTCP(host,service);
	for(inchars=0;inchars<sizeof(buf);inchars+=n) {
		n=read(s,&buf[inchars],sizeof(buf)-inchars);
		if(n <= 0) errexit("ERROR: network read failed");
	}
	memcpy(&now.tv_sec,&buf,4);
	memcpy(&now.tv_usec,&buf[4],4);
	printf("%x %x\n",ntohl(now.tv_sec),ntohl(now.tv_usec));
	exit(0);
}
示例#15
0
bool
ip_set_get_ip4_port(const struct sk_buff *skb, bool src,
		    __be16 *port, u8 *proto)
{
	const struct iphdr *iph = ip_hdr(skb);
	unsigned int protooff = skb_network_offset(skb) + ip_hdrlen(skb);
	int protocol = iph->protocol;

	/* See comments at tcp_match in ip_tables.c */
	if (protocol <= 0)
		return false;

	if (ntohs(iph->frag_off) & IP_OFFSET)
		switch (protocol) {
		case IPPROTO_TCP:
		case IPPROTO_SCTP:
		case IPPROTO_UDP:
		case IPPROTO_UDPLITE:
		case IPPROTO_ICMP:
			/* Port info not available for fragment offset > 0 */
			return false;
		default:
			/* Other protocols doesn't have ports,
			   so we can match fragments */
			*proto = protocol;
			return true;
		}

	return get_port(skb, protocol, protooff, src, port, proto);
}
示例#16
0
 httpserver_ska& operator=(struct sockaddr* addr)
 {
     this->addr = addr;
     this->ip = get_ip_str_new(addr);
     this->port = get_port(addr);
     return *this;
 }
示例#17
0
void gpio_config(pin_t pin, pin_dir_t dir) { 
    //Clock the port
    RCC_AHB1PeriphClockCmd(1 << get_port(pin), ENABLE);

    GPIO_InitTypeDef def;
    def.GPIO_Pin = 1 << get_pin(pin);
    def.GPIO_Mode = dir;
    def.GPIO_Speed = GPIO_Speed_50MHz;
    if (dir)
        def.GPIO_OType = GPIO_OType_PP; //output : Push Pull
    else
        def.GPIO_OType = GPIO_OType_OD; //input : Open Drain
    def.GPIO_PuPd = GPIO_PuPd_UP;       //Pull up resistor

    GPIO_Init(ports[get_port(pin)], &def);
}
示例#18
0
/* load up two integers and a string using xdr, then ship them */
int main(int argc, char* argv[])
{
    int sock;
    int test_number_a;
    int test_number_b;
    float test_number_c;
    char *test_string = NULL;
    char buffer[80];
    char *service = get_port();

    XDR xdrobject;
    XDR *xdrstream = &xdrobject;
    /* Get a socket (UDP) */ 
    sock = passiveUDP(service);
    read(sock, buffer, 80);
    close(sock);
    /* XDR a message */
    xdrmem_create(xdrstream, buffer, 80, XDR_DECODE);
    xdr_int(xdrstream, &test_number_a);
    xdr_int(xdrstream, &test_number_b);
    xdr_float(xdrstream, &test_number_c);
    xdr_wrapstring(xdrstream, &test_string);
    printf("%d, %d, %f  %s\n", test_number_a, test_number_b, test_number_c, test_string);
    /* send the message */
    xdr_destroy(xdrstream);
    return 0;
}
示例#19
0
int
main(int argc, char *argv[])
{
        struct  sockaddr_in fsin;       /* the from address of a client */
        char    *service = get_port();   /* service name or port number  */
        int     msock, ssock;           /* master & slave sockets       */
        int     alen;                   /* from-address length          */

        switch (argc) {
        case    1:
                break;
        case    2:
                service = argv[1];
                break;
        default:
                errexit("<klaxon>\n");
        }

        msock = passiveTCP(service, QLEN);

        while (1) {
                alen = sizeof(fsin);
                ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
                if (ssock < 0)
                        errexit("accept failed: %s\n", strerror(errno));
                (void) Ttimed(ssock);
                (void) close(ssock);
        }
}
示例#20
0
/*------------------------------------------------------------------------
 * main - Concurrent TCP server for browse_file_system service
 *------------------------------------------------------------------------
 */
int
main(int argc, char *argv[])
{
    char    *service = get_port();  /* service name or port number  */
    struct  sockaddr_in fsin;       /* the address of a client      */
    int     alen;                   /* length of client's address   */
    int     msock;                  /* master server socket         */
    int     ssock;                  /* slave server socket          */

    switch (argc)
    {
        case 1:
            break;
        case 2:
            service = argv[1];
            break;
        default:
            errexit("too many arguments...\n");
    }

    msock = passiveTCP(service, QLEN);

    // reaper
    (void) signal(SIGCHLD, reaper);


    // fork() creates a new process by duplicating the calling process.
    // The new process, referred to as the child, is an exact duplicate of 
    // the calling process, referred to as the parent...
    // On success, the PID of the child process is returned in the parent, 
    // and 0 is returned in the child. On failure, -1 is returned in the parent,
    switch (fork())
    {
        case 0:    /* child */
            fork();
        default:   /* parent */
            break;
         case -1:
             errexit("fork: %s\n", strerror(errno));
    } // end switch

    while (1) // handle one client request per iteration.
    {
        alen = sizeof(fsin);
        // accept a client, get slave socket connected to that client.
        ssock = accept(msock, (struct sockaddr *)&fsin, &alen);
        if (ssock < 0)
        {
            if (errno == EINTR)
                continue;
            errexit("accept: %s\n", strerror(errno));
        }
	else
	{
	    TCPbrowser(ssock);
	    close(ssock);
	    printf("im dead\n");
	}
    } // end while loop
} // end main
int
main()
{
	port = get_port(11123);
	parent = getpid();

	child = fork();
	if (-1 == child)
	{   
		fprintf(stderr, "can't fork, error %d\n", errno);
		exit(EXIT_FAILURE);
	}

	if (child == 0)
	{
		int ret = childproc();
		_exit(ret);
	}
	else
	{ 
		int ret = parentproc();
		exit(ret);
	}
	return 1;
}
void execGet(char* filename){        
    char data_port[BUFFER_SIZE], buf[BUFFER_SIZE];
    FILE* file = fopen(filename, "rb");
    if (!file) {
        send(recvfd, FAILURE, BUFFER_SIZE, 0);
        return;
    }
    send(recvfd, SUCCESS, BUFFER_SIZE, 0);
    int port = get_port();
    sprintf(data_port, "%d", port);
    send(recvfd, data_port, BUFFER_SIZE, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = serverAddr.sin_addr.s_addr;

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    listen(sockfd, BACKLOG);
    int getfd = accept(sockfd, (struct sockaddr*)NULL, NULL);
    int len;
    while ((len = fread(buf, 1, BUFFER_SIZE, file))>0) {
        if (send(getfd, buf, len, 0) != len) {
            errorInfo("Send data error!");
            return;
        }
    }
    printf("file %s get successfully!\n", filename);
    close(getfd);
    close(sockfd);
    fclose(file);
}
示例#23
0
static mrb_value
mrb_rx_gpio_input(mrb_state *mrb, mrb_value self)
{
    mrb_value pin   = mrb_iv_get(mrb, self, mrb_intern_lit(mrb, "@pin"));
    mrb_int pin_no = mrb_fixnum(pin);

    int bit;
    int ddr;
    int status=0;
    volatile struct st_port0   *port = (volatile struct st_port0   *)get_port(pin_no,&bit,&ddr);

#if MRB_DEBUG1
    printf("rx_gpio_input port = %08x bit = %d\n",(int)port,bit);
#endif
    if (ddr == 0)	// IN
    {
        status = port->PIDR.BYTE & BITTBL[bit];

        if (status != 0)
        {
            return(mrb_true_value());
        }
    }
    return(mrb_false_value());
}
void execPut(char* filename){
    char data_port[BUFFER_SIZE], buf[BUFFER_SIZE];
    int port = get_port();
    FILE *file = fopen(filename, "wb");
    if (!file){
        errorInfo("Can't open file!");
        return ;
    } 
    else{
        send(recvfd, SUCCESS, BUFFER_SIZE, 0);
    }
    sprintf(data_port, "%d", port);
    send(recvfd, data_port, BUFFER_SIZE, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = serverAddr.sin_addr.s_addr;
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    listen(sockfd, BACKLOG);
    int putfd = accept(sockfd, (struct sockaddr*)NULL, NULL);
    int len;
    while ((len = recv(putfd, buf, BUFFER_SIZE, 0)) >0){
        if (fwrite(buf, 1, len, file) != len) {
            errorInfo("File write error!");
            return;
        }
    }
    printf("file %s put successfully!\n", filename);
    close(putfd);
    close(sockfd);
    fclose(file);
    return;
}
示例#25
0
int main()
{
	int port = get_port(12123);
	parent = getpid();
 
   child = fork();
   if (child == -1)
   {   
      fprintf(stderr, "can't fork, error %d\n", errno);
      exit(EXIT_FAILURE);
   }
 
   if (child == 0)
   {
      childproc(port);
      _exit(0);
   }
   else
   { 
	   int ret = parentproc(port);
	   if(1 == session_closed_called && 0 == ret)
      exit(0);
      else
      exit(ret ? ret : 21);
   }
   return 1;
}
void execDir(){
    char buf[BUFFER_SIZE], data_port[BUFFER_SIZE];
    int port = get_port();
    send(recvfd, SUCCESS, BUFFER_SIZE, 0);
    sprintf(data_port, "%d", port);
    send(recvfd, data_port, BUFFER_SIZE, 0);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = serverAddr.sin_addr.s_addr;

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    listen(sockfd, BACKLOG);
    int datarecvfd = accept(sockfd, (struct sockaddr*)NULL, NULL);
    
    system("ls > .temp");
    int file = open("./.temp", O_RDONLY);
    int len;
    while((len = read(file, buf, BUFFER_SIZE))>0) {
        if (send(datarecvfd, buf, len, 0) != len) {
            errorInfo("Send error!");
            close(sockfd);
            close(datarecvfd);
            return;
        }
    }
    close(sockfd);
    close(datarecvfd);
    system("rm .temp");
    printf("COMMAND DIR SUCCEED!\n");
    return;
}
示例#27
0
文件: hd64570.c 项目: 020gzh/linux
static irqreturn_t sca_intr(int irq, void* dev_id)
{
	card_t *card = dev_id;
	int i;
	u8 stat;
	int handled = 0;
	u8 page = sca_get_page(card);

	while((stat = sca_intr_status(card)) != 0) {
		handled = 1;
		for (i = 0; i < 2; i++) {
			port_t *port = get_port(card, i);
			if (port) {
				if (stat & SCA_INTR_MSCI(i))
					sca_msci_intr(port);

				if (stat & SCA_INTR_DMAC_RX(i))
					sca_rx_intr(port);

				if (stat & SCA_INTR_DMAC_TX(i))
					sca_tx_intr(port);
			}
		}
	}

	openwin(card, page);		/* Restore original page */
	return IRQ_RETVAL(handled);
}
示例#28
0
static irqreturn_t sca_intr(int irq, void* dev_id, struct pt_regs *regs)
{
	card_t *card = dev_id;
	int i;
	u8 stat;
	int handled = 0;

#ifndef ALL_PAGES_ALWAYS_MAPPED
	u8 page = sca_get_page(card);
#endif

	while((stat = sca_intr_status(card)) != 0) {
		handled = 1;
		for (i = 0; i < 2; i++) {
			port_t *port = get_port(card, i);
			if (port) {
				if (stat & SCA_INTR_MSCI(i))
					sca_msci_intr(port);

				if (stat & SCA_INTR_DMAC_RX(i))
					sca_rx_intr(port);

				if (stat & SCA_INTR_DMAC_TX(i))
					sca_tx_intr(port);
			}
		}
	}

#ifndef ALL_PAGES_ALWAYS_MAPPED
	openwin(card, page);		/* Restore original page */
#endif
	return IRQ_RETVAL(handled);
}
示例#29
0
文件: main.c 项目: monaka/B-Free
/*
 * 初期化
 *
 * o 要求受けつけ用のメッセージバッファ ID をポートマネージャに登録
 */
W
init_console (void)
{
  int		i;
  ER		error;

  /*
   * 要求受けつけ用のポートを初期化する。
   */
  recvport = get_port (sizeof (DDEV_REQ), sizeof (DDEV_REQ));
  if (recvport <= 0)
    {
      dbg_printf ("CONSOLE: cannot make receive port.\n");
      slp_tsk ();
      /* メッセージバッファ生成に失敗 */
    }

  error = regist_port (CONSOLE_DRIVER, recvport);
  if (error != E_OK)
    {
      dbg_printf ("console: cannot regist port (error = %d)\n", error);
    }
  initialized = 1;
  clear_console ();
}
示例#30
0
文件: ser.c 项目: kangjianbin/netser
int main(int argc, char *argv[])
{
	const char *dev;
	int port;
	int mode;

	if (argc < 3) {
		usage(argv[0]);
		exit(1);
	}

	dev = argv[1];
	port = get_port(argv[2]);
	/* default to telnet mode */
	if (argc == 3)
		mode = 1;
	else
		mode = get_mode(argv[3]);

	signal(SIGPIPE, SIG_IGN);
	signal(SIGTERM, do_stop);
	ser = ser_new(dev, port, mode);
	if (ser == NULL)
		return -1;
	ser_start(ser);

	return 0;
}