void file_sekop_transact_open(Tfsekop_transact *sekop_transact)
{
	sekop_transact->res = FSEKOP_TRANSACT_OK;
	read_hdr(sekop_transact);
	IF_ERROR_RETURN();
	sekop_transact->opened = 1;
}
int open_analyze(char* hdr_filename, char* img_filename, short* voxelMatrix, struct dsr *hdr) {
		
	int returnCode;
	//struct dsr hdr;
	
	printf("	[OPEN_ANALYZE:] Going to open Header...\n");
	returnCode = read_hdr(hdr_filename, hdr);
	printf("	[OPEN_ANALYZE:] ...done. <%d>\n", returnCode);
	
	//showHdr(hdr);
	//returnCode = getImgFilename(img_filename);

	printf("	[OPEN_ANALYZE:] Going to open Data...\n");
	returnCode = read_img(hdr, img_filename, &loadMatrix);
	printf("	[OPEN_ANALYZE:] ...done. <%d>\n", returnCode);
	
	int xSize = hdr->dime.dim[1];		
	int ySize = hdr->dime.dim[2];
	int zSize = hdr->dime.dim[3];
	int tSize = hdr->dime.dim[4];
	
	long voxelAmount = xSize*ySize*zSize;
	int x, t;

	//Voxel stehen so in der Matrix, dass die Zeitreihe eines Voxels an einem Stueck hintereinander steht
	for(x=0; x<voxelAmount; x++) {
		for (t=0; t<tSize; t++) {
			voxelMatrix[t+x*tSize] = loadMatrix[x+t*voxelAmount];
		}
	}
	return EXIT_SUCCESS;
}
Exemple #3
0
int extract_files(char *aname, char **names, int count)
{
	struct ar_hdr hdr;
	int i, err, fd, bytes, seek;
	char buf[1];

	int a_fd = seek_to_armag(aname);
	if (a_fd < 0)
		return ERR_EXTRACT;

	while ((bytes = read(a_fd, buf, 1)) != -1) {
		if (bytes == 0)
			return 0;
		
		if (lseek(a_fd, -1, SEEK_CUR) == -1)
			return ERR_EXTRACT;

		if (read_hdr(&hdr, a_fd) != 0)
			return ERR_EXTRACT;

		seek = 1;
		for (i = 0; i < count; i++) {	
			if (cmp_hdr_name(&hdr, names[i])) {
				if (extract(a_fd, names[i], &hdr) != 0)
					return ERR_EXTRACT;
				
				names[i] = "";
				seek = 0;
			} 
		}

		if (seek) {
			if (seek_to_next(&hdr, a_fd) != 0)
				return ERR_EXTRACT;
		} else {
			if (stoi(hdr.ar_size, 10, 10)%2 == 1) {
				if (lseek(a_fd, 1, SEEK_CUR) == -1)
					return ERR_EXTRACT;
			}
		}
	}

	return 0;
}
Exemple #4
0
static int restore(T *thiz)
{
    int r;
    hdr_block_t hdr;

    INFO("restoring %s", thiz->file);

    r = prepare_file(thiz);
    if (r != 0) return -1;

    r = diagnosis(thiz);
    if (r != 0) return -1;

    r = read_hdr(thiz, &hdr);
    if (r != 0) return -1;

    r = fit_file(thiz, &hdr);
    if (r != 0) return -1;

    r = update_hdr(thiz, &hdr);
    if (r != 0) return -1;

    return 0;
}
Exemple #5
0
int main(int argc, char **argv) {
    int sock, preference;
    int len_inet, length, place, result;
    int soa_count = 0;
    struct sockaddr_in zone_server; /* AF_INET */
    js_string *send, *zone, *get, *expand, *mxexpand;
    /* ( Sending and getting data from the client ) */
    q_header header;
    q_rr rr;
    rr_soa soa;
    int qclass = 1;

    unsigned char len[2];

    if(argv[1] == 0 || argv[2] == 0) {
        harderror(L_USAGE); /* "Usage: getzone zone_name zone_server_IP" */
        }
    if(argc == 4 && argv[3] == 0) {
        harderror(L_USAGE); /* "Usage: getzone zone_name zone_server_IP" */
        }

    /* Set up an option to change the query class */
    if(argc == 4 && atoi(argv[3]) == 255)
        qclass = 255;

    /* Create a timeout alarm */
    signal(SIGALRM,timeout);
    alarm(300); /* 5 minutes */

    /* Create a socket to the zone server */
    memset(&zone_server,0,sizeof zone_server);
    zone_server.sin_family = AF_INET;
    zone_server.sin_port = htons(53);
    zone_server.sin_addr.s_addr = inet_addr(argv[2]);

    if(zone_server.sin_addr.s_addr == INADDR_NONE)
        harderror(L_VALID_IP); /* "Please use a valid IP for the zone server" */

    len_inet = sizeof zone_server;

    /* Create a TCP/IP socket */
    sock = socket(PF_INET,SOCK_STREAM,0);
    if(sock == -1)
        harderror(L_NO_SOCK); /* "Unable to create TCP socket" */

    /* Connect to the zone server */
    if(connect(sock,(struct sockaddr *)&zone_server,len_inet) == -1)
        harderror(L_NO_CONNECT); /* "Unable to connect to zone server" */

    /* OK, create a query to send over the connection */
    if((send = js_create(256,1)) == 0)
        harderror(L_NO_SEND); /* "Unable to create send string object" */

    /* The 12-byte header to send to the DNS server */
    header.id = 45;
    header.qr = 0;
    header.opcode = 0;
    header.aa = 0;
    header.tc = 0;
    header.rd = 0;
    header.ra = 0;
    header.z = 0;
    header.rcode = 0;
    header.qdcount = 1;
    header.ancount = 0;
    header.nscount = 0;
    header.arcount = 0;

    if(make_hdr(&header,send) == JS_ERROR)
        harderror(L_NO_HEADER); /* "Unable to make DNS header" */

    if((zone = js_create(128,1)) == 0)
        harderror(L_NO_ZSTRING); /* "Unable to create zone string object" */

    if(js_qstr2js(zone,"A") == JS_ERROR)
        harderror(L_QSTR2JS_ERROR); /* "qstr2js" */

    if(js_qappend(argv[1],zone) == JS_ERROR)
        harderror(L_APPEND_Z); /* "Unable to append zone string to zone object" */

    /* Append trailing dot, if needed */
    if(*(zone->string + zone->unit_count - 1) != '.')
        if(js_qappend(".",zone) == JS_ERROR)
            harderror(L_APPEND_D); /* "Unable to append dot at end of zone string object" */

    /* Convert zone in to raw "over-the-wire" UDP data */
    if(hname_2rfc1035(zone) == JS_ERROR)
        harderror(L_INVALID_NAME); /* "Invalid form of zone name" */

    /* Append raw binary zone to data to send to server */
    if(js_append(zone,send) == JS_ERROR)
        harderror(L_APPEND_ZS); /* "Can not append zone string to send string" */

    /* Append query type and query class to data */
    if(js_adduint16(send,252) == JS_ERROR)
        harderror(L_ADD_QT); /* "Could not add query type to send" */
    if(js_adduint16(send,qclass) == JS_ERROR)
        harderror(L_ADD_QC); /* "Could not add query class to send" */

    /* Question formed, now send question to server */
    len[0] = (send->unit_count & 0xff00) >> 8;
    len[1] = send->unit_count & 0xff;
    if(write(sock,len,2) == -1)
        harderror(L_SEND_2BYTE); /* "Could not send 2-byte length header to zone server" */
    if(write(sock,send->string,send->unit_count) == -1)
        harderror(L_SEND_QUERY); /* "Could not send query to zone server" */

    if((rr.name = js_create(257,1)) == 0)
        harderror(L_C_RRNAME); /* "Could not create rr.name" */
    if((soa.mname = js_create(257,1)) == 0)
        harderror(L_MNAME); /* "Could not create soa.mname" */
    if((soa.rname = js_create(257,1)) == 0)
        harderror(L_RNAME); /* "Could not create soa.rname" */

    while(recv(sock,len,2,MSG_WAITALL) == 2) {

        /* Get the length of the reply from the server */
        length = ((len[0] << 8) & 0xff00) | (len[1] & 0xff);

        if(length < 12)
           harderror(L_NOT_MANLY); /* "Response from server is not long enough to hold header" */

        /* Allocate the string "get" */
        if((get = js_create(length + 7,1)) == 0)
            harderror(L_NO_GET); /* "Could not allocate memory for get string" */
        if((expand = js_create((length + 7) * 4,1)) == 0)
            harderror(L_NO_EXPAND); /* "Could not allocate memory for expand string" */

        if((length = recv(sock,get->string,length,MSG_WAITALL)) == -1)
            harderror(L_SERVER); /* "Could not get packet from server" */

        get->unit_count = length;

        /* Decompress that */
        decomp_init(0);
        if(decompress_data(get,expand) == JS_ERROR) {
            harderror(L_DECOMPRESS); /* "Fatal error performing decompression" */
            }

        if(read_hdr(expand,&header) == JS_ERROR)
            harderror(L_RHEADER); /* "Could not read header from server" */

        /* Stop on any error codes */
        if(header.rcode != 0) {
            switch(header.rcode) {
                case 1:
                    harderror(L_FORMAT); /* "Format error" */
                case 2:
                    harderror(L_SERVER_FAIL); /* "Server failure" */
                case 3:
                    harderror(L_NAME); /* "Name error" */
                case 4:
                    harderror(L_NOTIMPL); /* "Not implemented" */
                case 5:
                    harderror(L_REFUSE); /* "Refused" */
                default:
                    harderror(L_RCODE); /* "Rcode > 5" */
                }
            }


        /* Move past any and all questions */

        place = 12;
        if(header.qdcount > 0) {
            result = dlabel_length(expand,place);
            if(result == -1)
                harderror(L_DLABEL); /* "Invalid dlabel in question" */
            place += result;
            place += 4;
            header.qdcount--;
            }

        /* Convert the answers to csv1-compatible lines in a MaraDNS Zone
           file */
        while(header.ancount > 0) {
            result = read_rr_h(expand,&rr,place);
            if(result == -1)
                harderror(L_READ_RR_H); /* "Fatal error running read_rr_h" */
            place += result;
            js_destroy(get);

            /* Make sure that rr.name is in baliwick.  If not, then
               put in an "Out of baliwick" warning and comment out
               the offending line */
            if((get = js_create(rr.name->unit_count + 7,1)) == 0)
                harderror(L_CGET); /* "Can not make get string" */
            if(js_copy(rr.name,get) == JS_ERROR)
                harderror(L_GET_COPY); /* "Fatal error copying name to get" */
            get->encoding = zone->encoding;
            result = 0; /* Out of baliwick */
            if(js_issame(get,zone) == 1)
                result = 1; /* In baliwick */
            while(result == 0 && get->unit_count > zone->unit_count) {
                bobbit_label(get);
                if(js_issame(get,zone) == 1)
                    result = 1; /* In baliwick */
                }
            if(result == 0) /* If out of baliwick */
                printf("%s%s%s",L_BALIWICK,LF,L_HASH); /* "# Disabled out-of-baliwick record follows" */
            if((get = js_create((int)(rr.rdlength) + 7,1)) == 0)
                harderror(L_CGET); /* "Can not make get string" */
            if(js_substr(expand,get,place,rr.rdlength) == -1)
                harderror(L_RDDATA); /* "Problem getting rddata" */
            switch(rr.type) {
                case RR_SOA:
                    soa_count++;
                    if(soa_count > 1) /* Then the zone has ended */
                        exit(0);
                    /* Translate all the fields, going to failover
                       mode (Make this an "Unsupported" data type)
                       if needed */
                    if(read_soa(get,&soa,0) == JS_ERROR)
                        goto failover;
                    if(hname_translate(rr.name,RR_SOA) == JS_ERROR)
                        goto failover;
                    if(hname_translate(soa.mname,RR_A) == JS_ERROR)
                        goto failover;
                    if(soa.mname->unit_count < 1)
                        goto failover;
                    if(email_translate(soa.rname) == JS_ERROR)
                        goto failover;
                    show_esc_stdout(rr.name);
                    printf("|%u",rr.ttl);
                    *(soa.mname->string) = '|';
                    show_esc_stdout(soa.mname);
                    show_esc_stdout(soa.rname);
                    printf("|%u|%d|%d|%d|%u\n",soa.serial,(int)soa.refresh,
                           (int)soa.retry,(int)soa.expire,
                           soa.minimum);
                    break;
                case RR_A:
                    if(get->unit_count != 4)
                        goto failover;
                    if(hname_translate(rr.name,RR_A) == JS_ERROR)
                        harderror(L_TRANS); /* "Problem translating A record name" */
                    show_esc_stdout(rr.name);
                    printf("|%u|%d.%d.%d.%d\n",rr.ttl,*(get->string),
                           *(get->string + 1),*(get->string + 2),
                           *(get->string + 3));
                    break;
                case RR_MX:
                    if(get->unit_count < 3)
                        goto failover;
                    mxexpand = js_create(512,1);
                    if(mxexpand == 0)
                        goto failover;
                    preference = ((*(get->string) & 0xff) << 8) |
                                  (*(get->string + 1) & 0xff);
                    if(js_substr(get,mxexpand,2,get->unit_count - 2)
                       == JS_ERROR)
                        goto failover;
                    if(hname_translate(rr.name,RR_MX) == JS_ERROR)
                        goto failover;
                    if(hname_translate(mxexpand,RR_MX) == JS_ERROR)
                        goto failover;
                    if(expand->unit_count < 1)
                        goto failover;
                    *(expand->string) = '|';
                    *(mxexpand->string) = '|';
                    show_esc_stdout(rr.name);
                    printf("|%u|%d",rr.ttl,preference);
                    show_esc_stdout(mxexpand);
                    printf("%s",LF); /* "\n" */
                    js_destroy(mxexpand);
                    break;
                case RR_TXT:
                    if(get->unit_count < 1)
                        goto failover;
                    if(*(get->string) != get->unit_count - 1)
                        goto failover;
                    *(get->string) = '|';
                    if(hname_translate(rr.name,RR_TXT) == JS_ERROR)
                        goto failover;
                    show_esc_stdout(rr.name);
                    printf("|%u",rr.ttl);
                    show_esc_stdout(get);
                    printf("%s",LF); /* "\n" */
                    break;
                case RR_NS:
                case RR_PTR:
                case RR_CNAME:
                    if(hname_translate(rr.name,rr.type) == JS_ERROR)
                        harderror(L_HNAME); /* "Hname problem" */
                    if(hname_translate(get,RR_A) == JS_ERROR)
                        goto failover;
                    if(get->unit_count < 1)
                        goto failover;
                    *(get->string) = '|';
                    show_esc_stdout(rr.name);
                    printf("|%u",rr.ttl);
                    show_esc_stdout(get);
                    printf("%s",LF); /* "\n" */
                    break;
                default:
                failover:
                    if(hname_translate(rr.name,RR_A) == JS_ERROR)
                        harderror(L_HNAME); /* "Hname problem" */
                    if(rr.name->unit_count < 1)
                        harderror(L_ZERO); /* "No 0-length names!" */
                    *(rr.name->string) = 'U';
                    show_esc_stdout(rr.name);
                    printf("|%u|%u|",rr.ttl,rr.type);
                    show_esc_stdout(get);
                    printf("%s",LF); /* "\n" */
                }
            place += rr.rdlength;
            header.ancount--;
            }
        js_destroy(get);
        js_destroy(expand);

        }
    return 0; /* Success */
    }
Exemple #6
0
int proc_query(js_string *raw, struct sockaddr_in *from, int sock) {

    q_header header; /* Header of the question */
    js_string *lookfor; /* What to look for in the big hash */
    js_string *origq; /* Original query asked by the user */
    js_string *lc; /* Lower-case version of query asked by the user */
    rr *nxstore = 0; /* A pointer to the SOA we return when we hit a
                        NXDOMAIN */
    int length, case_folded, result_code = 0, qtype;
    int has_recursive_authority = 0;
    mhash_e spot_data;
    int have_authority = 0; /* Do we have authority for this record?
                               (must be 1 to return a NXDOMAIN) */
    rr *point;
    uint32 ip;
    int desires_recursion = 0; /* Do they desire recursion? */
    char *num_string; /* The string to put the number of thread running
                         in */
    unsigned int mem_usage; /* The amount of memory a maradns process has
                               allocated */

    /* Sanity checks */
    if(js_has_sanity(raw) == JS_ERROR)
        return JS_SUCCESS;
    if(raw->unit_size != 1)
        return JS_SUCCESS;

    /* Get the header */
    if(read_hdr(raw,&header) == JS_ERROR) { /* Something went wrong,
                                               return error "Format error" */
        udperror(sock,raw,from,FORMAT_ERROR,"Couldn't get header");
        return JS_SUCCESS;
        }

    /* We only answer questions (Thanks to Roy Arends for pointing out this
       security flaw) */
    if(header.qr != 0) {
        return JS_SUCCESS;
        }

    /* We only support a qdcount of 1 */
    if(header.qdcount != 1) {
        udperror(sock,raw,from,NOT_IMPLEMENTED,"Qdcount not 1");
        return JS_SUCCESS;
        }

    /* We only support an opcode of 0 (standard query) */
    if(header.opcode != 0) {
        /* Since TinyDNS also returns NOT_IMPLEMENTED here, no need for
           a fingerprint check.  Note that tinydns, unlike MaraDNS, echos
           the question. */
        udperror(sock,raw,from,NOT_IMPLEMENTED,"non-0 opcode");
        return JS_SUCCESS;
        }

    /* Get the question from the stream */
    if(raw->unit_count < 14) {
        udperror(sock,raw,from,FORMAT_ERROR,"bad question hdr");
        return JS_SUCCESS;
        }

    /* Determine the length of the domain label in the question */
    length = dlabel_length(raw,12);
    if(length < 0 || length > 255) {
        udperror(sock,raw,from,FORMAT_ERROR,"bad question length");
        return JS_SUCCESS;
        }

    if(raw->unit_count < 16 + length) { /* 16 because 12 for the header,
                                           and 4 for the type and class */
        udperror(sock,raw,from,FORMAT_ERROR,"question doesn't fit");
        return JS_SUCCESS;
        }

    /* Return "not implemented" if the class is not 1 (Internet class) */
    if(*(raw->string + length + 14) != 0 &&
       *(raw->string + length + 15) != 1) {
        udperror(sock,raw,from,NOT_IMPLEMENTED,"Class not 1");
        return JS_ERROR;
        }

    /* Create the lookfor string, returning error if appropriate */
    if((lookfor = js_create(256,1)) == 0) {
        udperror(sock,raw,from,SERVER_FAIL,"can't create lookfor string");
        return JS_ERROR;
        }
    if((origq = js_create(256,1)) == 0) {
        js_destroy(lookfor);
        udperror(sock,raw,from,SERVER_FAIL,"can't create origq string");
        return JS_ERROR;
        }
    if((lc = js_create(256,1)) == 0) {
        js_destroy(lookfor); js_destroy(origq);
        udperror(sock,raw,from,SERVER_FAIL,"can't create lc string");
        return JS_ERROR;
        }
    if(js_set_encode(lc,JS_US_ASCII) == JS_ERROR) { /* ASCII because we
                                                     only fold the case of
                                                     A-Z */
        goto serv_fail;
        }

    /* Get the query we will look for from their raw query */
    if(js_substr(raw,lookfor,12,length + 2) == JS_ERROR) {
        goto serv_fail;
        }

    /* And copy it over to the "original query" */
    if(js_copy(lookfor,origq) == JS_ERROR) {
        goto serv_fail;
        }

    /* Get the type of query the client desires */
    qtype = get_rtype(origq);
    if(qtype == JS_ERROR) {
        goto serv_fail;
        }

    if(qtype >= 250 && qtype <= 254) { /* IXFR, AXFR, and 2 more */
        goto not_impl;
        }

    /* Return a timestamp */
    result_code = timestamp(header.id,sock,(struct sockaddr *)from,origq);
    if(result_code == JS_SUCCESS) {
        js_destroy(lookfor); js_destroy(origq); js_destroy(lc);
        return JS_SUCCESS;
        }
    if(result_code == JS_ERROR) {
        goto serv_fail;
        }

    js_destroy(lookfor); js_destroy(origq); js_destroy(lc);

    return JS_SUCCESS;

    /* Work around C's lack of error handling and garbage collection with
       gotos */
    serv_fail:
        js_destroy(lookfor);
        js_destroy(origq);
        js_destroy(lc);
        udperror(sock,raw,from,SERVER_FAIL,"serv_fail in proc_query");
        return JS_ERROR;

    not_impl:
        js_destroy(lookfor);
        js_destroy(origq);
        js_destroy(lc);
        udperror(sock,raw,from,NOT_IMPLEMENTED,"not_impl in proc_query");
        return JS_ERROR;

    }
Exemple #7
0
static void 
dmtl_data( GCD_RCB *rcb )
{
    GCD_CCB	*ccb = rcb->ccb;
    DAM_TL_HDR	hdr;
    STATUS	status = E_GC480A_PROTOCOL_ERROR;
    u_i2	pkt_type;

    /*
    ** Breaking from the switch will shutdown the connection.
    ** If status is not changed, connection will be aborted
    ** with protocol error (set to OK for simple shutdown).
    ** Set the rcb to NULL if not used for new request.
    **
    ** Return directly in the switch for any other result
    ** (but make sure RCB is used or freed).
    */
    switch( (pkt_type = read_hdr( rcb, &hdr )) )
    {
    case DAM_TL_CR :
    case DAM_TL_CC :
    case DAM_TL_DC :
	/*
	** The Connection Request packet is expected only
	** initially and should be passed to dmtl_accept().
	**
	** The Connection Confirm packet is never expected.
	**
	** We only send a Disconnect Request when aborting
	** the connection and we don't wait for a response,
	** so we don't expect a Disconnect Confirmation.
	*/
	if ( GCD_global.gcd_trace_level >= 1 )
	    TRdisplay( "%4d    GCD unexpected DMTL packet: 0x%x\n",
		       ccb->id, pkt_type );
	break;

    case DAM_TL_DT : /* Data */
	if ( ccb->tl.msg_service )  
	{
	    if ( GCD_global.gcd_trace_level >= 3 )
		TRdisplay( "%4d    GCD DMTL received data\n", ccb->id );

	    /*
	    ** Pass to MSG Layer and exit successfully.
	    */
	    (*ccb->tl.msg_service->data)( rcb );
	    return;
	}
	break;

    case DAM_TL_INT: /* Interrupt */
	if ( ccb->tl.msg_service )
	{
	    if ( GCD_global.gcd_trace_level >= 3 )
		TRdisplay( "%4d    GCD DMTL interrupt\n", ccb->id );

	    /*
	    ** Notify MSG Layer and exit successfully.
	    ** Need to free RCB as we are expected to
	    ** consume the RCB.
	    */
	    gcd_del_rcb( rcb );
	    (*ccb->tl.msg_service->intr)( ccb );
	    return;
	}
	break;

    case DAM_TL_DR : /* Disconnect */
	if ( ccb->tl.gcc_service )
	{
	    if ( GCD_global.gcd_trace_level >= 3 )
		TRdisplay( "%4d    GCD DMTL disconnecting\n", ccb->id );

	    gcd_init_rcb( rcb );
	    hdr.msg_id = DAM_TL_DC;
	    build_hdr( rcb, &hdr );
	    (*ccb->tl.gcc_service->send)( rcb );
	    rcb = NULL;		/* Passed to GCC */
	    status = OK;	/* Shutdown, don't abort */
	}
	break;

    default : 
	if ( GCD_global.gcd_trace_level >= 1 )
	    TRdisplay( "%4d    GCD invalid DMTL packet: ID=0x%x Type=0x%x\n",
		       ccb->id, hdr.tl_id, hdr.msg_id );
	break;
    }

    dmtl_shutdown( ccb, rcb, status );
    return;
}
Exemple #8
0
static bool
dmtl_accept( GCC_SRVC *gcc_service, GCD_RCB *rcb )
{
    GCD_CCB	*ccb = rcb->ccb;
    DAM_TL_HDR	hdr;
    STATUS	status = OK;
    u_i1	*rcb_ptr;
    u_i4	rcb_len;
    u_i1	*msg_parms = NULL;
    u_i2	msg_len = 0;
    u_i1	size = DAM_TL_PKT_MIN;

    if ( GCD_global.gcd_trace_level >= 4 )
	TRdisplay( "%4d    GCD DMTL testing new connection\n", ccb->id );

    /*
    ** Save RCB settings in case we don't accept
    ** the connection and need to restore the RCB.
    */
    rcb_ptr = rcb->buf_ptr;
    rcb_len = rcb->buf_len;

    /*
    ** Validate the packet header and request type.
    ** If invalid, refuse GCC request.  We accept
    ** both the JDBC TL and DAM-TL packet ID's for
    ** now.  The protocol level parameter will tell
    ** which TL should handle this request.
    */
    read_hdr( rcb, &hdr );
    if ( (hdr.tl_id != DAM_TL_ID_1  &&  hdr.tl_id != DAM_TL_ID_2)  ||  
	 hdr.msg_id != DAM_TL_CR )
    {
	rcb->buf_ptr = rcb_ptr;
	rcb->buf_len = rcb_len;
	return( FALSE );
    }

    /*
    ** Read the TL Connection Request parameters.
    **
    ** The DAM-TL protocol is a super-set of the JDBC TL
    ** protocol.  We should be able to read JDBC connect
    ** parms, so if there is an error we will accept the
    ** GCC connection and then abort the TL connection.
    **
    ** We refuse the connection if the protocol level and
    ** packet header ID indicate that this is for JDBC TL.
    */
    if ( (status = read_cr_parms( rcb, &size, &msg_parms, &msg_len )) == OK  &&
         hdr.tl_id == DAM_TL_ID_1 && ccb->tl.proto_lvl < DAM_TL_PROTO_2 )
    {
	rcb->buf_ptr = rcb_ptr;
	rcb->buf_len = rcb_len;
	return( FALSE );
    }

    /*
    ** We will service this connection from GCC,
    ** though we may detect an error and abort
    ** the client connection.
    **
    ** We must respond to this request with the
    ** same TL packet ID as the original request.
    ** Subsequent packets must have the packet
    ** ID appropriate for the protocol level.
    */
    if ( GCD_global.gcd_trace_level >= 4 )
	TRdisplay( "%4d    GCD DMTL servicing request\n", ccb->id );

    ccb->tl.gcc_service = gcc_service;
    ccb->tl.packet_id = hdr.tl_id;

    /*
    ** Validate connection parameters.
    */
    for( ; status == OK; )
    {
	if ( ccb->tl.proto_lvl < DAM_TL_PROTO_1  ||  size < DAM_TL_PKT_MIN )
	{
	    if ( GCD_global.gcd_trace_level >= 1 )
		TRdisplay( "%4d    GCD invalid param value: (%d,%d)\n",
			   ccb->id, ccb->tl.proto_lvl, size );
	    status = E_GC480A_PROTOCOL_ERROR;
	    break;
	}

	/*
	** Negotiate connection parameters.
	*/
	if ( ccb->tl.proto_lvl > DAM_TL_DFLT_PROTO )
	    ccb->tl.proto_lvl = DAM_TL_DFLT_PROTO;
	if ( size > DAM_TL_PKT_DEF )  size = DAM_TL_PKT_DEF;
	ccb->max_buff_len = 1 << size;

	if ( GCD_global.gcd_trace_level >= 4 )
	{
	    TRdisplay( "%4d        protocol level: %d\n", 
		       ccb->id, ccb->tl.proto_lvl );
	    TRdisplay( "%4d        buffer size: %d\n", 
		       ccb->id, ccb->max_buff_len );
	}

	/*
	** Initialize our MSG Service Provider.
	*/
	if ( (status = (*gcd_dam_service.init)( &dmtl_msg_service, ccb, 
						&msg_parms, &msg_len )) != OK )
	    break;

	/*
	** OK, we are ready to accept the connection.  Inform the GCC
	** Service provider and verify that the connection should be
	** accepted.
	*/
	ccb->tl.msg_service = &gcd_dam_service;
	status = (*gcc_service->accept)( &gcd_dmtl_service, ccb );
	break;
    }

    if ( status != OK )
    {
	if ( GCD_global.gcd_trace_level >= 3 )
	    TRdisplay( "%4d    GCD DMTL rejecting connection: 0x%x\n", 
	    		ccb->id, status );

	dmtl_shutdown( ccb, rcb, status );
    }
    else
    {
	if ( GCD_global.gcd_trace_level >= 3 )
	    TRdisplay( "%4d    GCD DMTL accepting connection\n", ccb->id );

	/*
	** Build and send TL Connection Confirmation packet.
	** We use the same packet header ID as was passed in.
	*/
	gcd_init_rcb( rcb );
	build_cc_parms( rcb, size, msg_parms, msg_len );
	hdr.tl_id = ccb->tl.packet_id;
	hdr.msg_id = DAM_TL_CC;
	build_hdr( rcb, &hdr );
	(*ccb->tl.gcc_service->send)( rcb );
    }

    /*
    ** Subsequent communications must use the packet ID appropriate
    ** for the negotiated protocol level.
    **
    ** Whether the connection was accepted or rejected, we assumed
    ** responsibility for servicing the current request, so return
    ** the appropriate indication.
    */
    ccb->tl.packet_id = DAM_TL_ID_2;
    return( TRUE );
}
Exemple #9
0
bfd_boolean
rs6000coff_core_file_matches_executable_p (bfd *core_bfd, bfd *exec_bfd)
{
  CoreHdr core;
  bfd_size_type size;
  char *path, *s;
  size_t alloc;
  const char *str1, *str2;
  bfd_boolean ret;
  file_ptr c_loader;

  if (!read_hdr (core_bfd, &core))
    return FALSE;

  if (CORE_NEW (core))
    c_loader = CNEW_LOADER (core.new_dump);
#ifndef BFD64
  else
    c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
#endif

  if (CORE_NEW (core) && CNEW_PROC64 (core.new_dump))
    size = (int) ((LdInfo *) 0)->l64.ldinfo_filename;
  else
    size = (int) ((LdInfo *) 0)->l32.ldinfo_filename;

  if (bfd_seek (core_bfd, c_loader + size, SEEK_SET) != 0)
    return FALSE;

  alloc = 100;
  path = bfd_malloc ((bfd_size_type) alloc);
  if (path == NULL)
    return FALSE;
  s = path;

  while (1)
    {
      if (bfd_bread (s, (bfd_size_type) 1, core_bfd) != 1)
	{
	  free (path);
	  return FALSE;
	}
      if (*s == '\0')
	break;
      ++s;
      if (s == path + alloc)
	{
	  char *n;

	  alloc *= 2;
	  n = bfd_realloc (path, (bfd_size_type) alloc);
	  if (n == NULL)
	    {
	      free (path);
	      return FALSE;
	    }
	  s = n + (path - s);
	  path = n;
	}
    }

  str1 = strrchr (path, '/');
  str2 = strrchr (exec_bfd->filename, '/');

  /* step over character '/' */
  str1 = str1 != NULL ? str1 + 1 : path;
  str2 = str2 != NULL ? str2 + 1 : exec_bfd->filename;

  if (strcmp (str1, str2) == 0)
    ret = TRUE;
  else
    ret = FALSE;

  free (path);

  return ret;
}
Exemple #10
0
const bfd_target *
rs6000coff_core_p (bfd *abfd)
{
  CoreHdr core;
  struct stat statbuf;
  bfd_size_type size;
  char *tmpptr;

  /* Values from new and old core structures.  */
  int c_flag;
  file_ptr c_stack, c_regoff, c_loader;
  bfd_size_type c_size, c_regsize, c_lsize;
  bfd_vma c_stackend;
  void *c_regptr;
  int proc64;

  if (!read_hdr (abfd, &core))
    {
      if (bfd_get_error () != bfd_error_system_call)
	bfd_set_error (bfd_error_wrong_format);
      return NULL;
    }

  /* This isn't the right handler for 64-bit core files on AIX 5.x.  */
  if (CORE_NEW (core) && CNEW_IS_CORE_DUMPXX (core))
    {
      bfd_set_error (bfd_error_wrong_format);
      return NULL;
    }

  /* Copy fields from new or old core structure.  */
  if (CORE_NEW (core))
    {
      c_flag = core.new_dump.c_flag;
      c_stack = (file_ptr) core.new_dump.c_stack;
      c_size = core.new_dump.c_size;
      c_stackend = CNEW_STACKORG (core.new_dump) + c_size;
      c_lsize = CNEW_LSIZE (core.new_dump);
      c_loader = CNEW_LOADER (core.new_dump);
#ifndef BFD64
      proc64 = CNEW_PROC64 (core.new_dump);
    }
  else
    {
      c_flag = core.old.c_flag;
      c_stack = (file_ptr) (ptr_to_uint) core.old.c_stack;
      c_size = core.old.c_size;
      c_stackend = COLD_STACKEND;
      c_lsize = 0x7ffffff;
      c_loader = (file_ptr) (ptr_to_uint) COLD_LOADER (core.old);
#endif
      proc64 = 0;
    }

  if (proc64)
    {
      c_regsize = sizeof (CNEW_CONTEXT64 (core.new_dump));
      c_regptr = &CNEW_CONTEXT64 (core.new_dump);
    }
  else if (CORE_NEW (core))
    {
      c_regsize = sizeof (CNEW_MSTSAVE (core.new_dump));
      c_regptr = &CNEW_MSTSAVE (core.new_dump);
    }
#ifndef BFD64
  else
    {
      c_regsize = sizeof (COLD_MSTSAVE (core.old));
      c_regptr = &COLD_MSTSAVE (core.old);
    }
#endif
  c_regoff = (char *) c_regptr - (char *) &core;

  if (bfd_stat (abfd, &statbuf) < 0)
    {
      bfd_set_error (bfd_error_system_call);
      return NULL;
    }

  /* If the core file ulimit is too small, the system will first
     omit the data segment, then omit the stack, then decline to
     dump core altogether (as far as I know UBLOCK_VALID and LE_VALID
     are always set) (this is based on experimentation on AIX 3.2).
     Now, the thing is that GDB users will be surprised
     if segments just silently don't appear (well, maybe they would
     think to check "info files", I don't know).

     For the data segment, we have no choice but to keep going if it's
     not there, since the default behavior is not to dump it (regardless
     of the ulimit, it's based on SA_FULLDUMP).  But for the stack segment,
     if it's not there, we refuse to have anything to do with this core
     file.  The usefulness of a core dump without a stack segment is pretty
     limited anyway.  */

  if (!(c_flag & UBLOCK_VALID)
      || !(c_flag & LE_VALID))
    {
      bfd_set_error (bfd_error_wrong_format);
      return NULL;
    }

  if (!(c_flag & USTACK_VALID))
    {
      bfd_set_error (bfd_error_file_truncated);
      return NULL;
    }

  /* Don't check the core file size for a full core, AIX 4.1 includes
     additional shared library sections in a full core.  */
  if (!(c_flag & (FULL_CORE | CORE_TRUNC)))
    {
      /* If the size is wrong, it means we're misinterpreting something.  */
      if (c_stack + (file_ptr) c_size != statbuf.st_size)
	{
	  bfd_set_error (bfd_error_wrong_format);
	  return NULL;
	}
    }

  /* Sanity check on the c_tab field.  */
  if (!CORE_NEW (core)
      && (
#ifndef BFD64
	  c_loader < (file_ptr) sizeof core.old
#else
	  c_loader < (file_ptr) sizeof core.new_dump
#endif
	  || c_loader >= statbuf.st_size
	  || c_loader >= c_stack))
    {
      bfd_set_error (bfd_error_wrong_format);
      return NULL;
    }

  /* Issue warning if the core file was truncated during writing.  */
  if (c_flag & CORE_TRUNC)
    _bfd_error_handler (_("%s: warning core file truncated"),
			bfd_get_filename (abfd));

  /* Allocate core file header.  */
#ifndef BFD64
  size = CORE_NEW (core) ? sizeof (core.new_dump) : sizeof (core.old);
#else
  size =  sizeof (core.new_dump);
#endif
  tmpptr = (char *) bfd_zalloc (abfd, (bfd_size_type) size);
  if (!tmpptr)
    return NULL;

  /* Copy core file header.  */
  memcpy (tmpptr, &core, size);
  set_tdata (abfd, tmpptr);

  /* Set architecture.  */
  if (CORE_NEW (core))
    {
      enum bfd_architecture arch;
      unsigned long mach;

      switch (CNEW_IMPL (core.new_dump))
	{
	case POWER_RS1:
	case POWER_RSC:
	case POWER_RS2:
	  arch = bfd_arch_rs6000;
	  mach = bfd_mach_rs6k;
	  break;
	default:
	  arch = bfd_arch_powerpc;
	  mach = bfd_mach_ppc;
	  break;
	}
      bfd_default_set_arch_mach (abfd, arch, mach);
    }

  /* .stack section.  */
  if (!make_bfd_asection (abfd, ".stack",
			  SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
			  c_size, c_stackend - c_size, c_stack))
    goto fail;

  /* .reg section for all registers.  */
  if (!make_bfd_asection (abfd, ".reg",
			  SEC_HAS_CONTENTS,
			  c_regsize, (bfd_vma) 0, c_regoff))
    goto fail;

  /* .ldinfo section.
     To actually find out how long this section is in this particular
     core dump would require going down the whole list of struct ld_info's.
     See if we can just fake it.  */
  if (!make_bfd_asection (abfd, ".ldinfo",
			  SEC_HAS_CONTENTS,
			  c_lsize, (bfd_vma) 0, c_loader))
    goto fail;

#ifndef CORE_VERSION_1
  /* .data section if present.
     AIX 3 dumps the complete data section and sets FULL_CORE if the
     ulimit is large enough, otherwise the data section is omitted.
     AIX 4 sets FULL_CORE even if the core file is truncated, we have
     to examine core.c_datasize below to find out the actual size of
     the .data section.  */
  if (c_flag & FULL_CORE)
    {
      if (!make_bfd_asection (abfd, ".data",
			      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
			      (bfd_size_type) core.old.c_u.u_dsize,
			      (bfd_vma)
				CDATA_ADDR (core.old.c_u.u_dsize),
			      c_stack + c_size))
	goto fail;
    }
#endif

#ifdef CORE_VERSION_1
  /* AIX 4 adds data sections from loaded objects to the core file,
     which can be found by examining ldinfo, and anonymously mmapped
     regions.  */
  {
    LdInfo ldinfo;
    bfd_size_type ldi_datasize;
    file_ptr ldi_core;
    uint ldi_next;
    bfd_vma ldi_dataorg;
    bfd_vma core_dataorg;

    /* Fields from new and old core structures.  */
    bfd_size_type c_datasize, c_vmregions;
    file_ptr c_data, c_vmm;

    if (CORE_NEW (core))
      {
	c_datasize = CNEW_DATASIZE (core.new_dump);
	c_data = (file_ptr) core.new_dump.c_data;
	c_vmregions = core.new_dump.c_vmregions;
	c_vmm = (file_ptr) core.new_dump.c_vmm;
      }
#ifndef BFD64
    else
      {
	c_datasize = core.old.c_datasize;
	c_data = (file_ptr) (ptr_to_uint) core.old.c_data;
	c_vmregions = core.old.c_vmregions;
	c_vmm = (file_ptr) (ptr_to_uint) core.old.c_vmm;
      }
#endif

    /* .data section from executable.  */
    if (c_datasize)
      {
	/* If Large Memory Model is used, then the .data segment should start from
	   BDATAORG which has been defined in the system header files. */

        if (c_flag & CORE_BIGDATA)
          core_dataorg = BDATAORG;
        else
          core_dataorg = CDATA_ADDR (c_datasize);

	if (!make_bfd_asection (abfd, ".data",
				SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
				c_datasize,
				(bfd_vma) core_dataorg,
				c_data))
	  goto fail;
      }

    /* .data sections from loaded objects.  */
    if (proc64)
      size = (unsigned long) ((LdInfo *) 0)->l64.ldinfo_filename;
    else
      size = (unsigned long) ((LdInfo *) 0)->l32.ldinfo_filename;

    while (1)
      {
	if (bfd_seek (abfd, c_loader, SEEK_SET) != 0)
	  goto fail;
	if (bfd_bread (&ldinfo, size, abfd) != size)
	  goto fail;

	if (proc64)
	  {
	    ldi_core = ldinfo.l64.ldinfo_core;
	    ldi_datasize = ldinfo.l64.ldinfo_datasize;
	    ldi_dataorg = (bfd_vma) ldinfo.l64.ldinfo_dataorg;
	    ldi_next = ldinfo.l64.ldinfo_next;
	  }
	else
	  {
	    ldi_core = ldinfo.l32.ldinfo_core;
	    ldi_datasize = ldinfo.l32.ldinfo_datasize;
	    ldi_dataorg = (bfd_vma) (ptr_to_uint) ldinfo.l32.ldinfo_dataorg;
	    ldi_next = ldinfo.l32.ldinfo_next;
	  }

	if (ldi_core)
	  if (!make_bfd_asection (abfd, ".data",
				  SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
				  ldi_datasize, ldi_dataorg, ldi_core))
	    goto fail;

	if (ldi_next == 0)
	  break;
	c_loader += ldi_next;
      }

    /* .vmdata sections from anonymously mmapped regions.  */
    if (c_vmregions)
      {
	bfd_size_type i;

	if (bfd_seek (abfd, c_vmm, SEEK_SET) != 0)
	  goto fail;

	for (i = 0; i < c_vmregions; i++)
	  {
	    VmInfo vminfo;
	    bfd_size_type vminfo_size;
	    file_ptr vminfo_offset;
	    bfd_vma vminfo_addr;

#ifndef BFD64
	    size = CORE_NEW (core) ? sizeof (vminfo.new_dump) : sizeof (vminfo.old);
#else
            size = sizeof (vminfo.new_dump);
#endif
	    if (bfd_bread (&vminfo, size, abfd) != size)
	      goto fail;

	    if (CORE_NEW (core))
	      {
		vminfo_addr = (bfd_vma) vminfo.new_dump.vminfo_addr;
		vminfo_size = vminfo.new_dump.vminfo_size;
		vminfo_offset = vminfo.new_dump.vminfo_offset;
	      }
#ifndef BFD64
	    else
	      {
		vminfo_addr = (bfd_vma) (ptr_to_uint) vminfo.old.vminfo_addr;
		vminfo_size = vminfo.old.vminfo_size;
		vminfo_offset = vminfo.old.vminfo_offset;
	      }
#endif

	    if (vminfo_offset)
	      if (!make_bfd_asection (abfd, ".vmdata",
				      SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS,
				      vminfo_size, vminfo_addr,
				      vminfo_offset))
		goto fail;
	  }
      }
  }
#endif

  return abfd->xvec;		/* This is garbage for now.  */

 fail:
  bfd_release (abfd, abfd->tdata.any);
  abfd->tdata.any = NULL;
  bfd_section_list_clear (abfd);
  return NULL;
}
Exemple #11
0
int delete_files(char *aname, char **files, int count)
{
	struct ar_hdr hdr;
	struct stat info;
	int i, err, fd, bytes, seek;
	long int size;
	char buf[1];

	int a_fd = seek_to_armag(aname);
	if (a_fd == -1)
		return ERR_DELETE;
	
	if (fstat(a_fd, &info) == -1)
		return ERR_STAT;
	
	if (unlink(aname) == -1)
		return ERR_UNLINK;

	fd = open(aname, O_WRONLY | O_CREAT, info.st_mode);
	if (fd == -1)
		return ERR_UNLINK;

	if (write_start(fd) != 0)
		return ERR_DELETE;
	
	while ((bytes = read(a_fd, buf, 1)) != -1) {
		if (bytes == 0)
			return 0;

		if (lseek(a_fd, -1, SEEK_CUR) == -1)
			return ERR_DELETE;

		if (read_hdr(&hdr, a_fd) != 0)
			return ERR_DELETE;

		seek = 1;
		for (i = 0; i < count; i++) {
			if (cmp_hdr_name(&hdr, files[i])) {
				if (seek_to_next(&hdr, a_fd) != 0)
					return ERR_DELETE;
				
				files[i] = "";
				seek = 0;
			}
		}

		size = stoi(hdr.ar_size, 10, 10);
		if (seek) {
			if (write_header(&hdr, fd) != 0)
				return ERR_DELETE;

			if (size%2 == 1)
				size++;

			if (write_file(fd, a_fd, size, 1) != 0)
				return ERR_DELETE;
		}
	}

	return 0;
}