Exemplo n.º 1
0
void send_format(const char *fmt, ...) {
	static char buf[513];
	va_list ap;

	va_start(ap, fmt);
	if (vsnprintf(buf, sizeof(buf)-1, fmt, ap) == sizeof(buf)-1)
		buf[sizeof(buf)-1] = '\0';
	va_end(ap);
	send_raw(buf);
}
Exemplo n.º 2
0
/**
 * Sends a frame on the network. 
 * Returns false if the socket has closed or the write failed for some other reason
 */
bool send_frame(int sock, const addr_t &target, nw_cmd_t cmd, nw_var_t * vars) {
	char * nw = (char*)malloc(FRAME_SIZE+1);
	int pos = HASH_SIZE;	
	uint16_t nwi16;
	uint32_t nwi32;

	/**
	 * Select protocol cmd and write cmd to first pos
	 */
	if(protocol[cmd].cmd != cmd)
		fprintf(stderr, "WARNING: Protocol cmd #%d does not match value in frame_t struct\n",cmd);

	const int num_vars = protocol[cmd].num_vars;
	const nw_var_type_t * var_types = protocol[cmd].var_types;
	nw[pos] = cmd;
	pos+=sizeof(char);

	for(int i=0; i < num_vars; ++i) {
		switch(var_types[i]) {
			case NW_VAR_FLOAT:
				pos += ftnw(vars[i].f,nw+pos);
				break;
			case NW_VAR_UINT16:
				nwi16 = htons((uint16_t)vars[i].i);
				memcpy(nw+pos,&nwi16,sizeof(uint16_t));
				pos += sizeof(uint16_t);
				break;
			case NW_VAR_UINT32:
				nwi32 = htonl(vars[i].i);
				memcpy(nw+pos,&nwi32,sizeof(uint32_t));
				pos += sizeof(uint32_t);
				break;
			case NW_VAR_CHAR:
				nw[pos] = vars[i].c;
				pos += sizeof(char);
				break;
			case NW_VAR_STR:
				pos += strtnw(vars[i].str, nw+pos);
				break;
		}
		if(pos >= FRAME_SIZE) {
			fprintf(stderr, "Network frames are to small!\n");
			exit(2);
		}
	}

	char hash[41];
	get_hash(hash,nw+HASH_SIZE,PAYLOAD_SIZE);
	memcpy(nw,hash,HASH_SIZE);
	bool ret = send_raw(sock, nw, target);
	free(nw);
	return ret;
}
Exemplo n.º 3
0
void send_words(const char *first, ...) {
	char buf[513];
	struct manyargs arg;
	char *word;
	va_list ap;

	va_start(ap, first);
	arg.c = 0;
	arg.v[arg.c++] = (char *)first;
	while ((word = va_arg(ap, char *)))
		arg.v[arg.c++] = word;
	va_end(ap);
	rfc_join(buf, sizeof(buf), arg.c, arg.v);
	send_raw(buf);
}
Exemplo n.º 4
0
void *thread (void *arg)
{
    int i;
    char *buf = NULL;

    if (bufsize > 0)
        buf = xzmalloc (bufsize);
    for (i = 0; i < iter; i++) {
        if (vopt)
            log_msg ("sending %d of %d", i + 1, iter);
        if (raw)
            send_raw (buf, bufsize);
        else
            send_czmq (buf, bufsize);
    }
    if (buf)
        free (buf);
    return NULL;
}
Exemplo n.º 5
0
int main (void)
{
    /* temperature 26 ^C */
    char data[6] = {0xb2,0x4d,0x1F,0xE0,0xD8,0x27};


    if (wiringPiSetupGpio() == -1)
        return 1;

    if (timer_init())
        return 1;

    printf ("Raspberry Pi IR encode program....\n") ;

    pinMode (26, OUTPUT) ;

    while (1) {


        output_init();


#if 1
        send_raw(rawCodes_ac_open, sizeof(rawCodes_ac_open)/sizeof(int));
#else
        send_data(data, 6);
        send_data(data, 6);
#endif



        pinMode(PWM_PIN,OUTPUT);
        delay_us(100000);


    }
    printf("end of program \n");
    output_38k_off();
    //pinMode(PWM_PIN,OUTPUT);
}
Exemplo n.º 6
0
/* connect: connect to IRC */
bool TwitchBot::connect()
{
	char buf[MAX_MSG];

	if (!(bot_connected = !cconnect(&client, TWITCH_SERV, TWITCH_PORT)))
		return false;

	/* send required IRC data: PASS, NICK, USER */
	snprintf(buf, MAX_MSG, "PASS %s", bot_password);
	send_raw(&client, buf);
	snprintf(buf, MAX_MSG, "NICK %s", bot_name);
	send_raw(&client, buf);
	snprintf(buf, MAX_MSG, "USER %s", bot_name);
	send_raw(&client, buf);

	/* enable tags in PRIVMSGs */
	snprintf(buf, MAX_MSG, "CAP REQ :twitch.tv/tags");
	send_raw(&client, buf);

	/* receive join and part information */
	snprintf(buf, MAX_MSG, "CAP REQ :twitch.tv/membership");
	send_raw(&client, buf);

	/* allow access to additional twitch commands */
	snprintf(buf, MAX_MSG, "CAP REQ :twitch.tv/commands");
	send_raw(&client, buf);

	if (strlen(bot_channel) > 32) {
		fprintf(stderr, "error: channel name too long\n");
		disconnect();
		return false;
	}

	/* join channel */
	snprintf(buf, MAX_MSG, "JOIN %s", bot_channel);
	send_raw(&client, buf);

	tick_thread = std::thread(&TwitchBot::tick, this);
	init_timers(bot_channel + 1, bot_token, CID);

	return true;
}
Exemplo n.º 7
0
void usb_midi_class::sendNoteOn(uint8_t note, uint8_t velocity, uint8_t channel)
{
	send_raw(0x09, 0x90 | ((channel - 1) & 0x0F), note & 0x7F, velocity & 0x7F);
}
Exemplo n.º 8
0
static void lwp_write_int64(int afdt_fd, int64_t d) {
  send_raw(afdt_fd, &d, sizeof(d));
}
Exemplo n.º 9
0
static void lwp_write(int afdt_fd, const std::string &buf) {
  size_t len = buf.length();
  send_raw(afdt_fd, &len, sizeof(len));
  send_raw(afdt_fd, buf.c_str(), len);
}
Exemplo n.º 10
0
int main(int argc, char** argv){
  int ret  = send_raw("hola", 4, "127.0.0.1", 54269);  
  printf("sent %d bytes\n", ret);
}
Exemplo n.º 11
0
Arquivo: smtp.cpp Projeto: ossmon/nscp
		void smtp_client::connection::send_line(std::string line) {
			send_raw(line + "\r\n");
		}
Exemplo n.º 12
0
Arquivo: smtp.cpp Projeto: ossmon/nscp
		void smtp_client::connection::got_response(std::string resp, boost::system::error_code ec, size_t bytes) {
			if (ec) {
				NSC_LOG_ERROR("smtp failure in reading: " + ec.message());
				boost::lock_guard<boost::mutex> lg(sc->m);
				if (cur) 
					sc->ready.push_back(cur);
				sc->active_connection.reset();
				return;
			}

			std::string line;
			line.reserve(bytes);
			for (size_t i = 0; i < bytes; i++)
				line += char(readbuf.sbumpc());

			resp += line;

			if (line.length() >= 4 && line[3] == '-') {
				boost::asio::async_read_until(serv, readbuf, "\r\n", boost::bind(&connection::got_response,  shared_from_this(), resp, _1, _2));
				return;
			}
			NSC_DEBUG_MSG("smtp read " + resp);

			bool broken_resp = resp.empty() || !('2' <= resp[0] && resp[0] <= '5') || (resp[0] == '3' && (state != DATA || resp.substr(0,3) != "354"));

			// FIXME deferral / drop-on-the-floor notifications

			if (broken_resp || resp[0] == '4')
			{
				boost::lock_guard<boost::mutex> lg(sc->m);
				if (cur) 
					sc->deferred.push_back(cur);
			}

			if (broken_resp || state == QUIT) {
				NSC_LOG_ERROR("smtp terminating");

				boost::lock_guard<boost::mutex> lg(sc->m);
				sc->active_connection.reset();
				return;
			}

			if ((resp[0] == '4' || resp[0] == '5' || state == DATA_354) && (resp.substr(0,3) != "502" || state != EHLO)) {
				cur.reset();
				if (sc->ready.empty() || state <= RSET) {
					state = QUIT;
					send_line("QUIT");
				} else {
					state = RSET;
					send_line("RSET");
				}
				return;
			}

			assert(!resp.empty());

			switch (state) {
			case BANNER:
				assert(resp[0] == '2');
				state = EHLO;
				send_line("EHLO " + config["canonical-name"]);
				break;
			case EHLO:
				if (resp.substr(0,3) == "502")
				{
					state = HELO;
					send_line("HELO" + config["canonical-name"]);
					break;
				}
				assert(resp[0] == '2');
				/* passthrough */
			case HELO:
			case RSET:
				assert(resp[0] == '2');
				assert(!cur);
				{
					boost::lock_guard<boost::mutex> lg(sc->m);
					if (sc->ready.empty())
					{
						state = QUIT;
						send_line("QUIT");
						break;
					}
					cur = sc->ready.front();
					sc->ready.pop_front();
				}
				assert(cur);
				state = MAIL_FROM;
				send_line("MAIL FROM: <" + cur->sender + ">");
				break;
			case MAIL_FROM:
				assert(resp[0] == '2');
				assert(cur);
				state = RCPT_TO;
				send_line("RCPT TO: <" + cur->recipient + ">");
				break;
			case RCPT_TO:
				assert(resp[0] == '2');
				assert(cur);
				state = DATA;
				send_line("DATA");
				break;
			case DATA:
				assert(resp.substr(0,3) == "354");
				assert(cur);
				state = DATA_354;
				send_raw(cur->data + ".\r\n");
				break;
			case DATA_354: 
			case QUIT:
				assert(0); // handled above
				break;
			}
		}
Exemplo n.º 13
0
void send_string(char* ip, int port, char* string, int size, char flags){
	char buffer[size+1];
	buffer[0] = flags;
	strncpy(buffer+1, string, size);
	send_raw(ip, port, buffer, size+1);
}
Exemplo n.º 14
0
void usb_midi_class::sendControlChange(uint8_t control, uint8_t value, uint8_t channel)
{
	send_raw(0x0B, 0xB0 | ((channel - 1) & 0x0F), control & 0x7F, value & 0x7F);
}
Exemplo n.º 15
0
void usb_midi_class::sendPolyPressure(uint8_t note, uint8_t pressure, uint8_t channel)
{
	send_raw(0x0A, 0xA0 | ((channel - 1) & 0x0F), note & 0x7F, pressure & 0x7F);
}
Exemplo n.º 16
0
void usb_midi_class::sendProgramChange(uint8_t program, uint8_t channel)
{
	send_raw(0x0C, 0xC0 | ((channel - 1) & 0x0F), program & 0x7F, 0);
}
Exemplo n.º 17
0
void usb_midi_class::sendAfterTouch(uint8_t pressure, uint8_t channel)
{
	send_raw(0x0D, 0xD0 | ((channel - 1) & 0x0F), pressure & 0x7F, 0);
}