[Help] Working on a C application, need advice.

Discussion in '3DS - Flashcards & Custom Firmwares' started by WhoAmI?, May 4, 2015.

  1. WhoAmI?
    OP

    WhoAmI? PASTA's dirty animal

    Member
    1,273
    1,009
    Mar 15, 2015
    Poké Ball
    So, I've been using a program called "FBI" for a while, which lets a user install .CIA files on to the 3DS. There's an option within the application which enables you to recieve a .CIA through WiFi via a sock connection.

    The dev of the homebrew is using a Java program to send the files. I want a C version so I can port it easily to other devices. I mainly want it so I can run it on Android (compile C as Linux binary, place it in /system/xbin).

    I looked at some code on other websites but non of which seems to work. Here is the code I'm currently working on:

    Code:
    // For both
    #include <sys/socket.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    #include <sys/types.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #define PORT 5000
    #define BUF_SIZE 16384
     
    int main(int argc, char** argv)
    {
        if (argc == 3)
        {
            const char* filename = argv[1];
            const char* consoleip = argv[2];
            FILE *fp = fopen(filename, "ab");
            if(NULL == fp)
            {
                printf("Error opening file");
                return 1;
            }
     
            /* Create a socket first */
            int sockfd = 0;
            if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)
            {
                printf("\n Error : Could not create socket \n");
                return 1;
            }
            /* Initialize sockaddr_in data structure */
            struct sockaddr_in serv_addr;
            serv_addr.sin_family = AF_INET;
            serv_addr.sin_port = htons(PORT); // port
            serv_addr.sin_addr.s_addr = inet_addr(consoleip);
     
            /* Attempt a connection */
            if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
            {
                printf("\nError : Connect Failed\n");
                return 1;
            }
            /* Receive data in chunks of BUF_SIZE bytes */
            int bytesReceived = 0;
            char buff[BUF_SIZE];
            memset(buff, '0', sizeof(buff));
            while((bytesReceived = read(sockfd, buff, BUF_SIZE)) > 0)
            {
                printf("Bytes received %d\n",bytesReceived);
                fwrite(buff, 1,bytesReceived,fp);
            }
            if(bytesReceived < 0)
            {
                printf("\nRead Error\n");
            }
            return 0;
        }
        else
        {
            printf("Usage:\n\n%s [FILENAME] [3DS IP]\n",argv[0]);
        }
        return 1; // Something went wrong
    }
    I'm new to C so I have no idea what to do. FBI freezes on "Reading info".

    This is the homebrew: http://gbatemp.net/t...staller.386433/

    Any help is deeply appreciated. Thanks!