Ejemplo n.º 1
0
void send_args(char **argv, const size_t *argc, IRCSession *session,
               char *buf) {
  for (int i = 0; i < *argc - 1; i++) {
    write_to_socket(session, buf, "%s ", argv[i]);
  }
  write_to_socket(session, buf, "%s", argv[*argc - 1]);
}
Ejemplo n.º 2
0
struct proc_process *
receive_proc_from_local_server(char* id) {
	struct sockaddr_un them;
	int sender = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sender < 0) {
		printf("Error creating connection to server\n");
	 	return NULL;
 	}
	them.sun_family = AF_UNIX; 
	strcpy(them.sun_path,LOCAL_SERVER_SOCK);
 	if (connect(sender,(struct sockaddr *)&them,sizeof(them)) < 0) {
		printf("Error connecting to server\n");
	 	return NULL;
 	}
 	if (id!=NULL) {
	 	unsigned char type = 'r';
	 	write_to_socket(&sender,&type,sizeof(char));
	 	write_to_socket(&sender,id,SIZE_OF_ID*sizeof(char));
		return read_process(&sender,read_from_socket);
	} else {
	 	unsigned char type = 'w';
	 	write_to_socket(&sender,&type,sizeof(char));
		return read_process(&sender,read_from_socket);
	}
}
Ejemplo n.º 3
0
/*
** include the content of the file as body of the message
** No encoding will be done.
*/
int include_msg_body(void)
{
    Sll
        *l,
        *msg_body_attachment_head;

    FILE
        *fp = NULL;

    Attachment
        *a;

    msg_body_attachment_head = get_msg_body_attachment_list();
    if (msg_body_attachment_head == NULL)
    {
        return(-1);
    }

    l = msg_body_attachment_head;
    a = (Attachment *) l->data;

    (void) snprintf(buf,bufsz,"Mime-version: 1.0\r\n");
    write_to_socket(buf);

    if (strcmp(a->charset,"none") != 0)
    {
        (void) snprintf(buf,bufsz,"Content-Type: %s; charset=%s\r\n\r\n",
                a->mime_type,
                a->charset);
    }
    else
    {
        (void) snprintf(buf,bufsz,"Content-Type: %s\r\n\r\n",a->mime_type);
    }
    write_to_socket(buf);

    fp=fopen(a->file_path,"r");
    if (fp == (FILE *) NULL)
    {
        errorMsg("Could not open message body file: %s",a->file_path);
        return (-1);
    } 

    while (fgets(buf,bufsz,fp))
    {
        write_to_socket(buf);
        if (g_show_attachment_in_log)
        {
            showVerbose("[C] %s",buf); 
        }
    }
    (void) fclose(fp);

    (void) snprintf(buf,bufsz,"\r\n\r\n");
    msock_puts(buf);
    showVerbose(buf);
    return(0);
}
Ejemplo n.º 4
0
static int ois_submit_sm_invoke(SMSCenter *smsc, const Msg *msg)
{
    char body[BUFLEN+1];
    char buffer[BUFLEN+1];
    int len;
    int count;
    int i;
    int ret;

    SAY(2, "ois_submit_sm_invoke");

    /* construct a message */

    ois_increment_counter();                  /* once per invoke */
    len = ois_encode_submit_sm_invoke(body, msg);

    /* the x.25 gear should be capable to fragment large messages, but... */
    /* let's just use an explicit 128 byte blocks */

    count = (len-1) / 121;                    /* 121 = 128 - 6 - 1 */

    /* first part */

    sprintf(buffer, "%c%c%04d%.121s%c",
	    'S',                              /* submit sm invoke */
	    (char)(0x50|count),               /* ia5 encoding, first part */
	    ois_counter,
	    &body[0],
	    EOL);
    IOTRACE("sending", buffer, strlen(buffer));
    ret = write_to_socket(smsc->socket, buffer);
    if (ret < 0) {
	goto error;
    }

    /* additional parts */

    for (i = 1; i <= count; ++i) {
	sprintf(buffer, "%c%c%04d%.121s%c",
		'S',                          /* submit sm invoke */
		(char)(0x60|(count-i)),       /* ia5, additional part */
		ois_counter,
		&body[i*121],
		EOL);
	IOTRACE("sending", buffer, strlen(buffer));
	ret = write_to_socket(smsc->socket, buffer);
	if (ret < 0) {
	    goto error;
	}
    }

    SAY(2, "ois_submit_sm_invoke ok");
    return 0;

 error:
    SAY(2, "ois_submit_sm_invoke error");
    return -1;
}
Ejemplo n.º 5
0
int main(void)
{
	struct data *d = malloc(sizeof(*d));
	struct addrinfo *addrinfo;
	struct io_listener *l;
	int fd, status;

	/* This is how many tests you plan to run */
	plan_tests(22);
	d->state = 0;
	fd = make_listen_fd(PORT, &addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(fd, init_conn, d);
	ok1(l);
	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		write_to_socket("hellothere", addrinfo);
		freeaddrinfo(addrinfo);
		free(d);
		exit(0);
	}
	ok1(io_loop() == d);
	ok1(d->state == 2);
	ok1(d->bytes > 0);
	ok1(d->bytes <= sizeof(d->buf));
	ok1(memcmp(d->buf, "hellothere", d->bytes) == 0);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	fflush(stdout);
	if (!fork()) {
		io_close_listener(l);
		write_to_socket("hi", addrinfo);
		freeaddrinfo(addrinfo);
		free(d);
		exit(0);
	}
	d->state = 0;
	ok1(io_loop() == d);
	ok1(d->state == 2);
	ok1(d->bytes > 0);
	ok1(d->bytes <= strlen("hi"));
	ok1(memcmp(d->buf, "hi", d->bytes) == 0);

	freeaddrinfo(addrinfo);
	free(d);
	io_close_listener(l);

	ok1(wait(&status));
	ok1(WIFEXITED(status));
	ok1(WEXITSTATUS(status) == 0);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
Ejemplo n.º 6
0
void do_stuff(msg *received_message){
    if(received_message->msg_type==1){
        //Broadcast message.
        if(check_history(received_message->message_id))
        	return;
        insert_into_history_table(received_message->message_id);
        int *neighbours=get_neighbours(ROUTER_ID);
        int size=neighbours[0];
        int i;
        printf("received by router id:%d from %d.\nSending to others.\n", ROUTER_ID,received_message->coming_from);
        received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
        for(i=1;i<=size;i++){
        	write_to_socket(ROUTER_BASE_PORT+neighbours[i],code_message(received_message));
        }
    }
		//unicast message
    else if(received_message->msg_type==2)
        if(received_message->receiver_id!=check_node_list(ROUTER_ID,received_message->receiver_id)) {
            int *path=find_shorest_path_between_routers(ROUTER_ID,get_parent_id(received_message->receiver_id));
            int next_node_to_send_to=path[1];
            if(next_node_to_send_to==-1){
                printf("Next node not found.Exiting...\n");
                exit(0);
            }
            received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
            printf("Passing from router %d to router %d\n",ROUTER_ID,next_node_to_send_to);
            write_to_socket(ROUTER_BASE_PORT+next_node_to_send_to,code_message(received_message));
        }
        else{
            received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
            printf("Passing from router %d to end node %d\n",ROUTER_ID,received_message->receiver_id );
            write_to_socket(NODE_BASE_PORT+received_message->receiver_id,code_message(received_message));
        }
    //Control message
    else if(received_message->msg_type==3){
      	//operations supported==> print the count of the messages received and forwarded on each link.
    	if(check_history(received_message->message_id))		//node is already visited.
        	return;
        insert_into_history_table(received_message->message_id);
        int *neighbours=get_neighbours(ROUTER_ID);
        int size=neighbours[0];
        int i;
      // printf("received by router id:%d.\nSending to others.\n", ROUTER_ID);
        received_message->coming_from=ROUTER_BASE_PORT+ROUTER_ID;
        printf("Message received by Router # %d are %llu (including the current one!)\n",ROUTER_ID,message_count+1 );
        for(i=1;i<=size;i++){
        	write_to_socket(ROUTER_BASE_PORT+neighbours[i],code_message(received_message));
        }
    }
    message_count++;


}
Ejemplo n.º 7
0
/******************************************************************************
* Yeah, we got the message!
*/
static int send_acknowledge(SMSCenter *smsc)
{

    char tmpbuff[100];
    int tmpint;

    if (tmpbuff == NULL) {
        error(0, "cimd_send_acknowledge: memory allocation failure");
        goto error;
    }

    memset(tmpbuff, 0, sizeof(tmpbuff));

    sprintf(tmpbuff, "\2\6\t11\3\n");

    tmpint = write_to_socket(smsc->socket, tmpbuff);
    if (tmpint == -1) {
        error(0, "cimd_send_acknowledge: connection failure");
        goto error;
    }

    return 0;

error:
    debug("bb.sms.cimd", 0, "cimd_send_acknowledge: failed");
    return -1;
}
Ejemplo n.º 8
0
/******************************************************************************
* Log out and close the socket
*
*/
int cimd_close(SMSCenter *smsc)
{

    char *cbuff = NULL;
    int sum;
    int ret;

    if (smsc->socket == -1) {
        debug("bb.sms.cimd", 0, "Trying to close cimd while already closed!");
        return 0;
    }
    cbuff = gw_malloc(2 * 1024);

    sprintf(cbuff, "%c%s%c%s%c%c", 0x02, "02", 0x09, "11", 0x03, 0x0A);

    sum = write_to_socket(smsc->socket, cbuff);
    if (sum < 0) goto error;

    /* this time we don't block waiting for acknowledge */
    recv(smsc->socket, cbuff, 2*1024, 0);

    gw_free(cbuff);

    ret = close(smsc->socket);
    smsc->socket = -1;
    return ret;

error:
    gw_free(cbuff);
    return -1;
}
Ejemplo n.º 9
0
static int ois_deliver_sm_result(SMSCenter *smsc, int result, const char *str)
{
    char body[BUFLEN+1];
    char buffer[BUFLEN+1];
    int len;
    int ret;

    SAY(2, "ois_deliver_sm_result");

    /* construct a message */

    len = ois_encode_deliver_sm_result(body, result);

    /* first and only part */

    sprintf(buffer, "%c%c%.4s%.121s%c",
	    'm',                              /* deliver sm result */
	    (char)(0x50),                     /* ia5 encoding, the only part */
	    &str[2],
	    &body[0],
	    EOL);

    IOTRACE("sending", buffer, strlen(buffer));
    ret = write_to_socket(smsc->socket, buffer);
    if (ret < 0) {
	goto error;
    }

    return 0;

 error:
    return -1;
}
Ejemplo n.º 10
0
static void ftp_login(struct connection *c)
{
    unsigned char *login;
    unsigned char *u;
    int logl = 0;
    set_timeout(c);
    login = init_str();
    add_to_str(&login, &logl, cast_uchar "USER ");
    if ((u = get_user_name(c->url)) && *u) add_to_str(&login, &logl, u);
    else add_to_str(&login, &logl, cast_uchar "anonymous");
    if (u) mem_free(u);
    if (ftp_options.fast_ftp) {
        struct ftp_connection_info *fi;
        add_to_str(&login, &logl, cast_uchar "\r\nPASS ");
        if ((u = get_pass(c->url)) && *u) add_to_str(&login, &logl, u);
        else add_to_str(&login, &logl, ftp_options.anon_pass);
        if (u) mem_free(u);
        add_to_str(&login, &logl, cast_uchar "\r\n");
        if (!(fi = add_file_cmd_to_str(c))) {
            mem_free(login);
            return;
        }
        add_to_str(&login, &logl, fi->cmdbuf);
    } else add_to_str(&login, &logl, cast_uchar "\r\n");
    write_to_socket(c, c->sock1, login, strlen(cast_const_char login), ftp_logged);
    mem_free(login);
    setcstate(c, S_SENT);
}
Ejemplo n.º 11
0
int send_request (int sock, struct request *req)
{
	#ifdef DEBUG
	fprintf (stderr, "\n%s\n", req->content);	
	#endif
	return write_to_socket (sock, req->content, req->content_len);		
}
Ejemplo n.º 12
0
int
main(
    int argc,
    char *argv[])
{
    const axis2_char_t *hostname = "localhost";
    const axis2_char_t *port = "9090";
    const axis2_char_t *filename = "echo.xml";
    const axis2_char_t *endpoint = "/axis2/services/echo/echo";
    int c;
    extern char *optarg;

    while ((c = getopt(argc, argv, ":h:p:f:e:")) != -1)
    {
        switch (c)
        {
        case 'h':
            hostname = optarg;
            break;
        case 'p':
            port = optarg;
            break;
        case 'f':
            filename = optarg;
            break;
        case 'e':
            endpoint = optarg;
            break;
        }
    }

    write_to_socket(hostname, port, filename, endpoint);
    return 0;
}
Ejemplo n.º 13
0
void flush_read_bio(Client* c) {
  char buf[1024*16];
  int bytes_read = 0;
  while((bytes_read = BIO_read(c->write_bio, buf, sizeof(buf))) > 0) {
    write_to_socket(c, buf, bytes_read);
  }
}
Ejemplo n.º 14
0
/*
 * function:		send_file_as_body
 * purpose:			send the given file as response body to the given socket
 * IN:				int sd - socket descriptor for writing socket
 *					char * path - path to file to send
 * OUT:				-
 * globals used:	-
 * return value:	zero if okay, anything else if not
*/
int 
send_file_as_body(int sd, char * path, int range_start, int range_end)
{	
	char * buf = malloc(BUFSIZE);
	int fd = open(path, O_RDONLY);
	
	// jump to range_start in file
	lseek(fd, range_start, SEEK_SET);
	
	if (fd >= 0){
		int cc;
		int left = range_end - range_start;
		while(left > 0 && (cc = read(fd, buf, left))){
			if (cc<0){ /* Error on Read */
				return cc;
			}
			int err = write_to_socket(sd, buf, cc, 1);
			if (err < 0){ /* Error on Write */
				return err;
			}
			left -= cc;
		}
	} else { /* Error on Open */
		return fd;
	}
	return 0;
}
Ejemplo n.º 15
0
void ftp_got_user_info(struct connection *c, struct read_buffer *rb)
{
	int g = get_ftp_response(c, rb, 0);
	if (g == -1) { setcstate(c, S_FTP_ERROR); abort_connection(c); return; }
	if (!g) { read_from_socket(c, c->sock1, rb, ftp_got_user_info); return; }
	if (g >= 530 && g < 540) { setcstate(c, S_FTP_LOGIN); retry_connection(c); return; }
	if (g >= 400) { setcstate(c, S_FTP_UNAVAIL); retry_connection(c); return; }
	if (g >= 200 && g < 300) {
		if (ftp_options.fast_ftp) ftp_dummy_info(c, rb);
		else ftp_send_retr_req(c, S_GETH);
	} else {
		if (ftp_options.fast_ftp) ftp_pass_info(c, rb);
		else {
			unsigned char *login;
			unsigned char *u;
			int logl = 0;
			login = init_str();
			add_to_str(&login, &logl, "PASS ");
			if ((u = get_pass(c->url)) && *u) add_to_str(&login, &logl, u);
			else add_to_str(&login, &logl, ftp_options.anon_pass);
			if (u) mem_free(u);
			add_to_str(&login, &logl, "\r\n");
			write_to_socket(c, c->sock1, login, strlen(login), ftp_sent_passwd);
			mem_free(login);
			setcstate(c, S_LOGIN);
		}
	}
}
Ejemplo n.º 16
0
void
delete_from_local_server(char* id) {
	struct sockaddr_un them;
	int sender = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sender < 0) {
		printf("Error creating connection to server\n");
		return;
 	}
	them.sun_family = AF_UNIX; 
	strcpy(them.sun_path,LOCAL_SERVER_SOCK);
 	if (connect(sender,(struct sockaddr *)&them,sizeof(them)) < 0) {
		printf("Error connecting to server\n");
	 	return;
 	}
 	unsigned char type = 'd';
 	write_to_socket(&sender,&type,sizeof(char));
 	write_to_socket(&sender,id,SIZE_OF_ID*sizeof(char));
}
Ejemplo n.º 17
0
void SocketConnection::send_response(std::string urlPath, int fd)
{
    ServerFileSystem server_fs;
   
    std::cout << "Url path : " << urlPath << std::endl;
    if(server_fs.check_if_URL_exists(urlPath)) {
        
        if(!urlPath.compare("/")) {
            write_to_socket("/learnee.html", fd);
        } else {
            write_to_socket(urlPath, fd);
        }
        
    } else {
        
        write_to_socket("/404.html", fd);
    }
}
Ejemplo n.º 18
0
void
request_from_socket(struct socket *socket, unsigned char *data, int datalen,
		    struct connection_state state, enum socket_state sock_state,
		    socket_read_T read_done)
{
	socket->read_done = read_done;
	socket->state = sock_state;
	write_to_socket(socket, data, datalen, state,
			read_response_from_socket);
}
Ejemplo n.º 19
0
/*
** Print multipart/mixed or multipart/related header
** if mixed and embedded images are specified, make
** to use multipart/alternative for the embedded images.
*/
int print_content_type_header(const char *boundary)
{
    Sll
        *oneline_attachment_list,
        *attachment_list,
        *embed_image_list;

    (void) snprintf(buf, bufsz,"MIME-Version: 1.0\r\n");
    write_to_socket(buf);

    if (*g_content_type!='\0')
    {
      (void) snprintf(buf,sizeof(buf)-1,"Content-Type: %s; boundary=\"%s\"\r\n",
              g_content_type,
              boundary);
      write_to_socket(buf);
      return(0);
    }

    oneline_attachment_list = get_oneline_attachment_list();
    attachment_list = get_attachment_list();
    embed_image_list = get_embed_image_attachment_list();
    if (oneline_attachment_list || attachment_list)
    {
        (void) snprintf(buf,sizeof(buf)-1,
                "Content-Type: multipart/mixed; boundary=\"%s\"\r\n", boundary);
        write_to_socket(buf);
        return(0);
    }
    if (embed_image_list)
    {
        (void) snprintf(buf,sizeof(buf)-1,
                "Content-Type: multipart/related; boundary=\"%s\"\r\n", boundary);
        write_to_socket(buf);
        return(0);
    }
    write_to_socket("\r\n");
    return(0);
}
Ejemplo n.º 20
0
int read_data(int connfd, int byte_amount, char* datapointer){
	int status;
	int l = strlen(datapointer);
        char* result = malloc(l);
        memset(result, '\0', sizeof(result));
        status = read(connfd, result, sizeof(result));
        if(status < 0){
                error("Error in reading.");
                exit(0);
        }
        strcpy(datapointer, result);
        datapointer[l-1] = '\0';
	write_to_socket("Data read successfully. \n", connfd);
}
Ejemplo n.º 21
0
Archivo: finger.c Proyecto: Gingar/port
void finger_send_request(struct connection *c)
{
	unsigned char *req = init_str();
	int rl = 0;
	unsigned char *user;
	add_to_str(&req, &rl, "/W");
	if ((user = get_user_name(c->url))) {
		add_to_str(&req, &rl, " ");
		add_to_str(&req, &rl, user);
		mem_free(user);
	}
	add_to_str(&req, &rl, "\r\n");
	write_to_socket(c, c->sock1, req, rl, finger_sent_request);
	mem_free(req);
	setcstate(c, S_SENT);
}
Ejemplo n.º 22
0
void ip_lookup(char *host, char *out, IRCSession *session, IRCPacket *packet) {
  char sbuf[256];
  sprintf(sbuf, "./geoip.rb %s", host);

  printf("[*] Running %s\n", sbuf);
  FILE *fp = popen(sbuf, "r");

  while (fgets(sbuf, 256, fp) != NULL) {
    if (sbuf[0] == 0x00)
      continue;

    write_to_socket(session, out, "\rPRIVMSG %s :%s", packet->sender, sbuf);
  }

  pclose(fp);
}
Ejemplo n.º 23
0
static void *
slave_thread(void *arg)
{
  struct client_data *from_info;   /* identity of connected client */
  int bytes_read;                  /* number of bytes read from socket */
  int bytes_written;               /* number of bytes written to socket */
  char *buffer;                    /* buffer for socket read/write data */

  /* get connection data from argument pointer */
  from_info = (struct client_data *)(arg);

  buffer = (char *)malloc(BUFFER_SIZE);
  if (buffer == NULL) {
    fprintf(stderr, "ERROR: cannot allocate memory for buffer\n");
    exit(1);
  } /* end if */

  printf("Connection from host %s (%s), port %d.\n",
	 from_info->name, from_info->addr, from_info->port);

  do {
    bytes_read = read_from_socket(from_info->sd, buffer, BUFFER_SIZE, 100);
    if (bytes_read > 0) {
      bytes_written = write_to_socket(from_info->sd, buffer, bytes_read, 100);
    } /* end if */
  } while (bytes_read > 0);

  close(from_info->sd);

  printf("Connection closed for host %s (%s), port %d.\n",
	 from_info->name, from_info->addr, from_info->port);

  /*
   * Deallocate memory
   */
  free(buffer);
  free(from_info);

  pthread_mutex_lock(&slave_thread_info.mutex);
  slave_thread_info.num_active--;
  if (slave_thread_info.num_active == (MAX_NUM_THREADS - 1)) {
    pthread_cond_signal(&slave_thread_info.thread_exit_cv);
  } /* end if */
  pthread_mutex_unlock(&slave_thread_info.mutex);

  pthread_exit(NULL);
} /* end of slave_thread */
Ejemplo n.º 24
0
char*
send_proc_to_local_server(struct proc_process p) {
	struct sockaddr_un them;
	int sender = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sender < 0) {
		printf("Error creating connection to server\n");
	 	return NULL;
 	}
	them.sun_family = AF_UNIX; 
	strcpy(them.sun_path,LOCAL_SERVER_SOCK);
 	if (connect(sender,(struct sockaddr *)&them,sizeof(them)) < 0) {
		printf("Error connecting to server\n");
	 	return NULL;
 	}
 	unsigned char type = 's';
 	write_to_socket(&sender,&type,sizeof(char));
 	write_process(p,&sender,write_to_socket);
	char* id = malloc(SIZE_OF_ID*sizeof(char));;
 	read_from_socket(&sender,id,SIZE_OF_ID*sizeof(char));
 	return id;
}
Ejemplo n.º 25
0
void
list_from_local_server() {
	struct sockaddr_un them;
	int sender = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sender < 0) {
		printf("Error creating connection to server\n");
		return;
 	}
	them.sun_family = AF_UNIX; 
	strcpy(them.sun_path,LOCAL_SERVER_SOCK);
 	if (connect(sender,(struct sockaddr *)&them,sizeof(them)) < 0) {
		printf("Error connecting to server\n");
	 	return;
 	}
 	unsigned char type = 'l';
 	write_to_socket(&sender,&type,sizeof(char));
	while (read(sender,&type,sizeof(type))>=0) {
		if (type==PROC_CHUNK_FINAL)
			break;
		printf("%c",type);
	}
}
Ejemplo n.º 26
0
void ftp_send_retr_req(struct connection *c, int state)
{
	struct ftp_connection_info *fi;
	unsigned char *login;
	int logl = 0;
	set_timeout(c);
	login = init_str();
	if (!c->info && !(fi = add_file_cmd_to_str(c))) {
		mem_free(login);
		return;
	} else fi = c->info;
	if (ftp_options.fast_ftp) a:add_to_str(&login, &logl, fi->cmdbuf);
	else {
		unsigned char *nl = strchr(fi->cmdbuf, '\n');
		if (!nl) goto a;
		nl++;
		add_bytes_to_str(&login, &logl, fi->cmdbuf, nl - fi->cmdbuf);
		memmove(fi->cmdbuf, nl, strlen(nl) + 1);
	}
	write_to_socket(c, c->sock1, login, strlen(login), ftp_retr_1);
	mem_free(login);
	setcstate(c, state);
}
Ejemplo n.º 27
0
int process_embeded_images(const char *boundary)
{
    char
        *ib,
        *b,
        related[17],
        cid[17],
        tbuf[24],
        alternative[17];

    Attachment
        *a;

    Sll
        *il,
        *oneline_attachment_list,
        *attachment_list,
        *embed_image_list;

    int
        rc,
        ic = 1;

    oneline_attachment_list = get_oneline_attachment_list();
    attachment_list = get_attachment_list();

    embed_image_list = get_embed_image_attachment_list();
    if (embed_image_list == NULL)
    {
        return(0);
    }
    memset(related, 0, sizeof(related));
    memset(alternative, 0, sizeof(alternative));

    mutilsGenerateMIMEBoundary(related,sizeof(related));
    mutilsGenerateMIMEBoundary(alternative,sizeof(alternative));
    b = boundary;
    ib = boundary;

    (void) snprintf(buf, bufsz, "--%s\r\n",boundary);
    write_to_socket(buf);

    if (attachment_list || oneline_attachment_list)
    {
        b = related;
        ib = b;
        (void) snprintf(buf, bufsz, "Content-Type: multipart/related; boundary=%s\r\n",b);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "Content-Type: multipart/alternative; boundary=%s\r\n\r\n",
                alternative);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "--%s\r\n",b);
        write_to_socket(buf);
    }
    else
    {
        ib = alternative;
        (void) snprintf(buf, bufsz, "Content-Type: multipart/alternative; boundary=%s\r\n\r\n",
                alternative);
        write_to_socket(buf);

        (void) snprintf(buf, bufsz, "--%s\r\n",alternative);
        write_to_socket(buf);
    }

    write_to_socket("Content-Type: text/html; charset=ISO-8859-1\r\n\r\n");

    /* write the img tags with cid */
    embed_image_list = get_embed_image_attachment_list();
    for (il = embed_image_list; il; il = il->next)
    {
        a = (Attachment *) il->data;
        mutilsGenerateMIMEBoundary(cid,sizeof(cid));
        (void) snprintf(tbuf,sizeof(tbuf)-1,"ii%d_%s",ic,cid);
        a->content_id = xStrdup(tbuf);
        (void) snprintf(buf, bufsz, "<img src=\"cid:%s\" alt=\"inline image %d\"><br>\n",
                tbuf,
                ic);
        write_to_socket(buf);
        ic++;
    }
    write_to_socket("\r\n");
    (void) snprintf(buf, bufsz, "--%s--\r\n",ib);
    write_to_socket(buf);

    for (il = embed_image_list; il; il = il->next)
    {
        a = (Attachment *) il->data;
        if (a == NULL)
            continue;
        rc = send_attachment(a,b);
        RETURN_IF_NOT_ZERO(rc);
    }
    (void) snprintf(buf, bufsz, "--%s--\r\n",b);
    write_to_socket(buf);
    return(0);

ExitProcessing:
    return(-1);
}
Ejemplo n.º 28
0
/*
** write MIME headers, encode attachment if needed
** and write to socket
** returns 0 on success -1 on failure
*/
int send_attachment(Attachment *a, const char *boundary)
{
    Attachment
        *encoded_attachment;

    encoded_attachment = get_encoded_attachment(a);
    if (encoded_attachment == NULL)
    {
        errorMsg("%s (%d) - Could not encode attachment %s",
                MFL,
                a->file_path);
        return(-1);
    }

    (void) snprintf(buf, bufsz,"--%s\r\n",boundary);
    write_to_socket(buf);

    if (a->charset && strncmp(a->charset,"none", 4) != 0)
    {
        (void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
                a->mime_type,
                a->charset);
    }
    else
    {
        if (a->attachment_name)
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s; name=\"%s\"\r\n",
                    a->mime_type,
                    a->attachment_name);
        }
        else
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
        }
    }
    write_to_socket(buf);
    
    if (a->content_id == NULL)
    {
        if (strncmp(a->content_disposition, "inline", 6) == 0)
        {
            (void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
        }
        else
        {
            if (a->attachment_name)
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
                    a->content_disposition,
                    a->attachment_name);

            }
            else if (a->file_name)
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s; filename=\"%s\"\r\n",
                    a->content_disposition,
                    a->file_name);
            }
            else
            {
                (void) snprintf(buf, bufsz, "Content-Disposition: %s\r\n",a->content_disposition);
            }
        }
        write_to_socket(buf);
    }

    if (a->content_id)
    {
        (void) snprintf(buf, bufsz, "Content-ID: <%s>\r\n",a->content_id);
        write_to_socket(buf);
        (void) snprintf(buf, bufsz, "X-Attachment-Id: %s\r\n",a->content_id);
        write_to_socket(buf);
    }

    if (strncmp(a->content_transfer_encoding,"none",4) != 0)
    {
        (void) snprintf(buf, bufsz, "Content-Transfer-Encoding: %s\r\n\r\n",
                a->content_transfer_encoding);
        write_to_socket(buf);
    }
    else
    {
        write_to_socket("\r\n");
    }

    /* our FILE pointer is already open and read to read from */
    while (fgets(buf, bufsz, a->fp_read))
    {
        write_to_socket(buf);
    }
    /* close file, remove tmp file if needed */
    cleanup_attachment(a);
    return(0);
}
Ejemplo n.º 29
0
/*
** send one line messages, each one is an inline attachment
** return 0 if mail is sent, -1 otherwise
*/
int process_oneline_messages(const char *boundary)
{
    int
        n = (-1);

    Attachment
        *a = NULL;
    Sll
        *l,
        *oneline_attachment_list;

    oneline_attachment_list = get_oneline_attachment_list();
    if (oneline_attachment_list == NULL)
    {
        return(0);
    }
    print_oneline_attachment_list();

    for (l = oneline_attachment_list; l; l = l->next)
    {
        a = (Attachment *) l->data;
        (void) snprintf(buf, bufsz, "\r\n--%s\r\n",boundary);
        write_to_socket(buf);

        if (strcmp(a->charset,"none") != 0)
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s; charset=%s\r\n",
                a->mime_type,
                a->charset);
        }
        else
        {
            (void) snprintf(buf, bufsz, "Content-Type: %s\r\n",a->mime_type);
        }
        write_to_socket(buf);

        (void) strcpy(buf,"Content-Disposition: inline\r\n");
        write_to_socket(buf);

        /* add encoding type if needed */
        if (strncmp(a->content_transfer_encoding,"none",4) != 0)
        {
            (void) snprintf(buf, bufsz, "Content-Transfer-Encoding: %s\r\n\r\n",
                    a->content_transfer_encoding);
            write_to_socket(buf);
        }

        
        if (strncmp(a->content_transfer_encoding,"base64",6) == 0)
        {
            /* encode the mssage to base 64 and write to socket */
            encode2base64andwrite2socket(a->oneline_msg);
        }
        else
        {
            write_to_socket(a->oneline_msg);
            if (g_show_attachment_in_log)
            {
                showVerbose("[C] %s\n",a->oneline_msg);

            }
        }
        write_to_socket("\r\n");
    }
    return(0);

ExitProcessing:
    return(-1);
}
Ejemplo n.º 30
0
/*
** return 0 on success
*/
int encode2base64andwrite2socket(const char *str)
{
    FILE
        *tfp1 = NULL,
        *tfp2 = NULL;
    char
        mbuf[1000],
        oneline_tempfile1[MUTILS_PATH_MAX],
        oneline_tempfile2[MUTILS_PATH_MAX];

    memset(oneline_tempfile1, 0, sizeof(oneline_tempfile1));
    /* write the text to a tmp file */
    tfp1 = mutils_get_tempfileFP(oneline_tempfile1,
            sizeof(oneline_tempfile1)-1);
    if (tfp1 == NULL)
    {
        errorMsg("%s (%d) - Could not open temp file1 for writing (%s)",
                MFL,
                ERR_STR);
        return (-1);
    }
    (void) fprintf(tfp1,"%s",str);
    (void) fclose(tfp1);

    /* open another tmp file to write the base64 of the first tmp file to */
    memset(oneline_tempfile2, 0, sizeof(oneline_tempfile2));
    tfp2 = mutils_get_tempfileFP(oneline_tempfile2,
        sizeof(oneline_tempfile2)-1);
    showVerbose("Oneline temp file2: * %s\n",oneline_tempfile2);
    if (tfp2 == NULL)
    {
        errorMsg("%s (%d) - Could not open temp file2 for writing (%s)",
            MFL,
            ERR_STR);
        return (-1);
    }

    tfp1 = fopen(oneline_tempfile1,"rb");
    if (tfp1 == NULL)
    {
        errorMsg("%s (%d) - Could not open temp file for reading (%s)",
            MFL,
            ERR_STR);
        return(-1);
    }

    mutilsBase64Encode(tfp1,tfp2);
    (void) fclose(tfp1);
    (void) fclose(tfp2);

    /* open the file with base64 and write the content to socket */
    tfp2 = fopen(oneline_tempfile2,"r");
    if (tfp2 == NULL)
    {
        errorMsg("%s (%d) - Could not open temp file for reading (%s)",
            MFL,
            ERR_STR);
        return(-1);
    }
    while(fgets(mbuf, sizeof(mbuf)-1, tfp2))
    {
        write_to_socket(mbuf);
        if (g_show_attachment_in_log)
        {
            showVerbose("[C] %s",mbuf);
        }
    }
    (void) fclose(tfp2);
    unlink(oneline_tempfile1);
    unlink(oneline_tempfile2);
 
    return(0);
}